322 lines
8.5 KiB
JavaScript
322 lines
8.5 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)
|
||
}
|
||
})
|
||
})
|
||
}
|
||
import amap from '../js_sdk/AMapWX_SDK_V1.3.0/amap-wx.130.js'
|
||
//获取定位
|
||
export function getLocationFn() {
|
||
const amapObject = new amap.AMapWX({
|
||
key: 'e93c598cf009f1ce92e79a438cc91ee4'
|
||
})
|
||
amapObject.getRegeo({
|
||
success: (res) => {
|
||
console.log('当前位置', res[0].regeocodeData.addressComponent.city);
|
||
let city = res[0].regeocodeData.addressComponent.city.replace('市', '');
|
||
uni.setStorageSync('current_city', city)
|
||
store.dispatch('SelectCity', city)
|
||
},
|
||
fail: (err) => {
|
||
console.log(err)
|
||
}
|
||
})
|
||
// const QQMapWX = require('@/js_sdk/qqmap-wx-jssdk/qqmap-wx-jssdk.js')
|
||
// var qqmapsdk = new QQMapWX({
|
||
// key: 'SH4BZ-BN36N-UYSFP-SJMPC-LZMGK-EWF3P'
|
||
// })
|
||
// uni.getLocation({
|
||
// type: 'gcj02',
|
||
// success: res => {
|
||
// qqmapsdk.reverseGeocoder({
|
||
// location: {
|
||
// latitude: res.latitude,
|
||
// longitude: res.longitude
|
||
// },
|
||
// success: res1 => {
|
||
// console.log('当前位置',res1.result.address_component.city);
|
||
// uni.setStorageSync('current_city',res1.result.address_component.city)
|
||
// store.dispatch('SelectCity', res1.result.address_component.city)
|
||
// },
|
||
// fail: res => {},
|
||
// })
|
||
// },
|
||
// })
|
||
}
|
||
// 日期格式化
|
||
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
|
||
}
|
||
|
||
//金额格式化 传入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;
|
||
}
|
||
|
||
//分享维修/朋友圈
|
||
export function shareApi(type, data) {
|
||
// type:分享类型 聊天:WXSceneSession 朋友圈:WXSceneTimeline
|
||
return new Promise((resolve, reject) => {
|
||
// uni.showLoading({
|
||
// title:'分享中',
|
||
// })
|
||
uni.share({
|
||
provider: "weixin",
|
||
scene: type,
|
||
type: data.type,
|
||
summary: data.summary,
|
||
href: data.href,
|
||
imageUrl: data.imageUrl,
|
||
success: (res) => {
|
||
// uni.hideLoading();
|
||
resolve(res)
|
||
},
|
||
fail: (err) => {
|
||
// uni.hideLoading();
|
||
reject(err)
|
||
}
|
||
});
|
||
})
|
||
}
|
||
//微信小程序支付
|
||
export function wxpayPaymentFn(jsConfig) {
|
||
return new Promise((resolve, reject) => {
|
||
uni.requestPayment({
|
||
provider: 'wxpay',
|
||
orderInfo: 'orderInfo',
|
||
timeStamp: jsConfig.timeStamp,
|
||
nonceStr: jsConfig.nonceStr,
|
||
package: jsConfig.packages,
|
||
signType: jsConfig.signType,
|
||
paySign: jsConfig.paySign,
|
||
success: (res) => {
|
||
resolve(res)
|
||
},
|
||
fail: (err) => {
|
||
reject(err)
|
||
}
|
||
})
|
||
})
|
||
}
|
||
|
||
//输入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();
|
||
}
|
||
}
|
||
|
||
//支付 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 const getTimeType = (time, type) => {
|
||
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}`;
|
||
}
|
||
}
|
||
|
||
//算经纬度
|
||
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 + "秒前";
|
||
}
|
||
}
|
||
|
||
//传入url 返回base64格式
|
||
export const urlToBase64 = (str) => {
|
||
let CHARS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
|
||
let out = '', i = 0, len = str.length, c1, c2, c3, enc1, enc2, enc3, enc4;
|
||
while (i < len) {
|
||
c1 = str.charCodeAt(i++);
|
||
c2 = str.charCodeAt(i++);
|
||
c3 = str.charCodeAt(i++);
|
||
enc1 = c1 >> 2;
|
||
enc2 = ((c1 & 3) << 4) | (c2 >> 4);
|
||
enc3 = ((c2 & 15) << 2) | (c3 >> 6);
|
||
enc4 = c3 & 63;
|
||
if (isNaN(c2)) {
|
||
enc3 = enc4 = 64;
|
||
} else if (isNaN(c3)) {
|
||
enc4 = 64;
|
||
}
|
||
out += CHARS.charAt(enc1) + CHARS.charAt(enc2) + CHARS.charAt(enc3) + CHARS.charAt(enc4);
|
||
}
|
||
return out;
|
||
}
|