332 lines
8.4 KiB
JavaScript
332 lines
8.4 KiB
JavaScript
import {
|
||
getToken
|
||
} from '@/utils/auth'
|
||
import config from '@/config'
|
||
import store from '@/store'
|
||
export function uploadImg(url) {
|
||
// 默认请求地址
|
||
let baseURL = config.fileUploadUrl;
|
||
let token = getToken();
|
||
return new Promise((resolve, reject) => {
|
||
uni.uploadFile({
|
||
url: baseURL, // 仅为示例,非真实的接口地址
|
||
filePath: url,
|
||
name: 'file',
|
||
header: {
|
||
'Authorization': token ? 'Bearer' + ' ' + token : ''
|
||
},
|
||
success: (res) => {
|
||
uni.hideLoading();
|
||
let url = JSON.parse(res.data).data.url;
|
||
resolve(url)
|
||
},
|
||
fail: (error) => {
|
||
uni.hideLoading();
|
||
uni.showToast({
|
||
title: error.msg,
|
||
icon: 'none'
|
||
})
|
||
reject(error)
|
||
}
|
||
})
|
||
})
|
||
}
|
||
//获取定位
|
||
export function getLocationFn() {
|
||
uni.getLocation({
|
||
type: 'gcj02',
|
||
geocode: true,
|
||
success: res => {
|
||
// console.log('定位', res);
|
||
let city = res.address.city.replace('市', '');
|
||
uni.setStorageSync('current_city', city)
|
||
store.dispatch('SetCity', city)
|
||
},
|
||
fail: err => {
|
||
// console.log('报错', err);
|
||
}
|
||
})
|
||
}
|
||
// 日期格式化
|
||
export function parseTime(time, pattern) {
|
||
if (arguments.length === 0 || !time) {
|
||
return null
|
||
}
|
||
const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}'
|
||
let date
|
||
if (typeof time === 'object') {
|
||
date = time
|
||
} else {
|
||
if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) {
|
||
time = parseInt(time)
|
||
} else if (typeof time === 'string') {
|
||
time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), '');
|
||
}
|
||
if ((typeof time === 'number') && (time.toString().length === 10)) {
|
||
time = time * 1000
|
||
}
|
||
date = new Date(time)
|
||
}
|
||
const formatObj = {
|
||
y: date.getFullYear(),
|
||
m: date.getMonth() + 1,
|
||
d: date.getDate(),
|
||
h: date.getHours(),
|
||
i: date.getMinutes(),
|
||
s: date.getSeconds(),
|
||
a: date.getDay()
|
||
}
|
||
const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => {
|
||
let value = formatObj[key]
|
||
// Note: getDay() returns 0 on Sunday
|
||
if (key === 'a') {
|
||
return ['日', '一', '二', '三', '四', '五', '六'][value]
|
||
}
|
||
if (result.length > 0 && value < 10) {
|
||
value = '0' + value
|
||
}
|
||
return value || 0
|
||
})
|
||
return time_str
|
||
}
|
||
//获取固定格式时间
|
||
export const getTimeType = (time, type) => {
|
||
console.log(time);
|
||
let date = new Date(parseInt(time));
|
||
let y = date.getFullYear();
|
||
let m = (date.getMonth() + 1 + '').padStart(2, '0');
|
||
let tom = (date.getMonth() + 1 + '');
|
||
let d = (date.getDate() + '').padStart(2, '0');
|
||
let tod = (date.getDate() + '');
|
||
let hh = (date.getHours() + '').padStart(2, '0');
|
||
let mm = (date.getMinutes() + '').padStart(2, '0');
|
||
let ss = (date.getSeconds() + '').padStart(2, '0');
|
||
if (type === 0) {
|
||
return `${y}-${m}-${d} ${hh}:${mm}:${ss}`;
|
||
} else if (type === 1) {
|
||
return `${y}-${m}-${d} ${hh}:${mm}`;
|
||
} else if (type === 2) {
|
||
return `${y}.${m} ${hh}:${mm}`;
|
||
} else if (type === 3) {
|
||
return `${y}-${m}-${d}`;
|
||
} else if (type === 4) {
|
||
return `${y}-${m}`;
|
||
} else if (type === 5) {
|
||
return `${hh}:${mm}:${ss}`;
|
||
}
|
||
}
|
||
//金额格式化2 传入30000 输出3万
|
||
export function convertToChineseNumber(number) {
|
||
const units = ['万', '亿'];
|
||
const digits = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
|
||
const decimals = ['角', '分'];
|
||
|
||
let result = '';
|
||
let unitIndex = 0;
|
||
|
||
// 处理小数部分
|
||
const decimalPart = String(number).split('.')[1];
|
||
if (decimalPart) {
|
||
for (let i = 0; i < Math.min(decimalPart.length, 2); i++) {
|
||
const digit = Number(decimalPart[i]);
|
||
if (digit !== 0) {
|
||
result += digits[digit] + decimals[i];
|
||
}
|
||
}
|
||
}
|
||
|
||
// 处理整数部分
|
||
let integerPart = Math.floor(number);
|
||
while (integerPart > 0) {
|
||
const digit = integerPart % 10;
|
||
if (digit !== 0) {
|
||
result = digits[digit] + units[unitIndex % 2] + result;
|
||
} else if (result !== '' && result[0] !== digits[0]) {
|
||
result = digits[0] + result;
|
||
}
|
||
integerPart = Math.floor(integerPart / 10);
|
||
unitIndex++;
|
||
}
|
||
|
||
return result;
|
||
}
|
||
//金额格式化 传入2201 输出二千二佰零一
|
||
export function convertNumberToChinese(number) {
|
||
const cnNums = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"];
|
||
const units = ["", "拾", "佰", "仟"];
|
||
const bigUnits = ["", "万", "亿", "万亿"];
|
||
let result = "";
|
||
const intPart = Math.floor(number);
|
||
const decPart = Math.round((number - intPart) * 100);
|
||
const intStr = intPart.toString();
|
||
const intLen = intStr.length;
|
||
|
||
for (let i = 0; i < intLen; i++) {
|
||
const num = intStr.charAt(i);
|
||
const unit = intLen - i - 1;
|
||
const bigUnit = Math.floor(unit / 4);
|
||
const smallUnit = unit % 4;
|
||
|
||
if (num !== "0") {
|
||
result += cnNums[num] + units[smallUnit] + bigUnits[bigUnit];
|
||
} else {
|
||
if (result[result.length - 1] !== "零") {
|
||
result += cnNums[num];
|
||
}
|
||
}
|
||
}
|
||
|
||
if (decPart > 0) {
|
||
result += "点";
|
||
const decStr = decPart.toString();
|
||
result += cnNums[decStr.charAt(0)] + cnNums[decStr.charAt(1)];
|
||
}
|
||
return result;
|
||
}
|
||
|
||
//输入100000展示10.0万,输入1000000展示100万
|
||
export function formatNumber(num) {
|
||
if (num >= 10000 && num < 100000000) {
|
||
if (num % 10000 === 0) {
|
||
return (num / 10000) + '万';
|
||
} else {
|
||
return (num / 10000).toFixed(1) + '万';
|
||
}
|
||
} else if (num >= 100000000) {
|
||
if (num % 1000000 === 0) {
|
||
return (num / 1000000) + '百万';
|
||
} else {
|
||
return (num / 1000000).toFixed(1) + '百万';
|
||
}
|
||
} else {
|
||
return num.toString();
|
||
}
|
||
}
|
||
//微信支付
|
||
export function wxpayPaymentFn(jsConfig) {
|
||
return new Promise((resolve, reject) => {
|
||
let orderInfo = {
|
||
"appid": jsConfig.appId,
|
||
"noncestr": jsConfig.nonceStr,
|
||
"package": 'Sign=WXPay',
|
||
"partnerid": jsConfig.partnerid,
|
||
"prepayid": jsConfig.prepayId,
|
||
"timestamp": jsConfig.timeStamp,
|
||
"sign": jsConfig.paySign
|
||
}
|
||
uni.requestPayment({
|
||
provider: 'wxpay',
|
||
orderInfo: orderInfo, // 支付宝支付参数
|
||
success: (res) => {
|
||
resolve(res)
|
||
},
|
||
fail: (err) => {
|
||
reject(err)
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
//支付 api type:支付类型 data:参数
|
||
export function paymentFn(type, data) {
|
||
return new Promise((resolve, reject) => {
|
||
uni.requestPayment({
|
||
provider: type,
|
||
orderInfo: data, // 支付宝支付参数
|
||
success: (res) => {
|
||
resolve(res)
|
||
},
|
||
fail: (err) => {
|
||
reject(err)
|
||
}
|
||
})
|
||
})
|
||
}
|
||
//分享维修/朋友圈
|
||
export function shareApi(type, data, path) {
|
||
// type:分享类型 聊天:WXSceneSession 朋友圈:WXSceneTimeline
|
||
config.miniProgram.path = path ? path : 'pages/index/index'
|
||
console.log(type, data, path);
|
||
console.log(config.miniProgram);
|
||
return new Promise((resolve, reject) => {
|
||
uni.share({
|
||
provider: "weixin",
|
||
scene: type,
|
||
type: data.type == 2 ? 2 : 5,
|
||
imageUrl: data.imageUrl,
|
||
title: data.content,
|
||
miniProgram: config.miniProgram,
|
||
success: (res) => {
|
||
uni.hideLoading();
|
||
resolve(res)
|
||
},
|
||
fail: (err) => {
|
||
uni.hideLoading();
|
||
reject(err)
|
||
},
|
||
complete: (isok) => {
|
||
uni.hideLoading();
|
||
reject(isok)
|
||
},
|
||
});
|
||
})
|
||
}
|
||
//拨打电话
|
||
export function dialFn(call) {
|
||
return new Promise((resolve, reject) => {
|
||
uni.makePhoneCall({
|
||
phoneNumber: call,
|
||
success: (res) => {
|
||
resolve(res)
|
||
},
|
||
fail: (err) => {
|
||
reject(err)
|
||
}
|
||
});
|
||
})
|
||
}
|
||
|
||
//算经纬度
|
||
export const getdeltaLat = (lat1, lon1, lat2, lon2) => {
|
||
const earthRadius = 6371; // 地球半径,单位为公里
|
||
|
||
const toRadians = (degrees) => {
|
||
return degrees * (Math.PI / 180);
|
||
};
|
||
|
||
const deltaLat = toRadians(lat2 - lat1);
|
||
const deltaLon = toRadians(lon2 - lon1);
|
||
|
||
const a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2) +
|
||
Math.cos(toRadians(lat1)) * Math.cos(toRadians(lat2)) *
|
||
Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
|
||
|
||
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
||
|
||
const distance = earthRadius * c;
|
||
return distance;
|
||
}
|
||
|
||
//传入时间,获取距离当前时间差
|
||
export const getTimeDifference = (time) => {
|
||
const currentTime = new Date();
|
||
const givenTime = new Date(time);
|
||
|
||
const diffInSeconds = Math.floor((currentTime - givenTime) / 1000);
|
||
const diffInMinutes = Math.floor(diffInSeconds / 60);
|
||
const diffInHours = Math.floor(diffInMinutes / 60);
|
||
const diffInDays = Math.floor(diffInHours / 24);
|
||
const diffInMonths = Math.floor(diffInDays / 30);
|
||
|
||
if (diffInMonths > 0) {
|
||
return diffInMonths + "个月前";
|
||
} else if (diffInDays > 0) {
|
||
return diffInDays + "天前";
|
||
} else if (diffInHours > 0) {
|
||
return diffInHours + "小时前";
|
||
} else if (diffInMinutes > 0) {
|
||
return diffInMinutes + "分钟前";
|
||
} else if (diffInSeconds > 0) {
|
||
return diffInSeconds + "秒前";
|
||
}
|
||
} |