293 lines
10 KiB
JavaScript
293 lines
10 KiB
JavaScript
import {getLocation, getUserInfo, setLocation} from "@/util/user";
|
|
import {API, MAIN_API} from "@/config";
|
|
import {get} from "@/api/request";
|
|
import {areaInfoByNameReq, areaInfoReq} from "@/api/common";
|
|
import dayjs from "dayjs";
|
|
import md5 from "md5";
|
|
|
|
export const ORDER_STATUS_LIST = ['已取消', '进行中', '已完成', '申请退款中', '退款完成', '退款失败']
|
|
|
|
export const CHECK_STATUS_LIST = [
|
|
{
|
|
label: '审核中',
|
|
color: 'info'
|
|
},
|
|
{
|
|
label: '审核通过',
|
|
color: 'success'
|
|
},
|
|
{
|
|
label: '审核拒绝',
|
|
color: 'error'
|
|
},
|
|
{
|
|
label: '已到账',
|
|
color: 'success'
|
|
},
|
|
|
|
]
|
|
|
|
export const chooseImg = (limit = 1, sourceType = ['album', 'camera']) => {
|
|
return new Promise(resolveMain => {
|
|
uni.chooseImage({
|
|
count: limit,
|
|
sourceType,
|
|
extension: ['png', 'jpg', 'jpeg'],
|
|
sizeType: ['original'],
|
|
success: async ({tempFiles}) => {
|
|
const result = await Promise.all(tempFiles.map(async file => {
|
|
const uploadRes = await uploadFileToAliOss(file.path)
|
|
return {
|
|
path: uploadRes,
|
|
}
|
|
}))
|
|
resolveMain(result)
|
|
},
|
|
complete: () => {
|
|
uni.hideLoading()
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
let ossUploadSeed = 0
|
|
|
|
const createOssKey = (filePath) => {
|
|
ossUploadSeed += 1
|
|
const suffix = filePath.substring(filePath.lastIndexOf("."));
|
|
const hash = md5(`${filePath}_${Date.now()}_${ossUploadSeed}_${Math.random()}`)
|
|
return `${dayjs().format('YYYYMMDD')}/${hash}${suffix}`
|
|
}
|
|
|
|
export const uploadImg = filePath => {
|
|
const {token} = getUserInfo()
|
|
uni.showLoading({title: '上传中'})
|
|
return new Promise(async resolve => {
|
|
// uni.uploadFile({
|
|
// url: `${MAIN_API}/api/file/upload`,
|
|
// name: 'file',
|
|
// filePath,
|
|
// header: {
|
|
// Authorization: token,
|
|
// TenantId: 10325
|
|
// },
|
|
// formData: {
|
|
// type: 'header'
|
|
// },
|
|
// success: uploadRes => {
|
|
// uni.hideLoading()
|
|
// const {data: {url}} = JSON.parse(uploadRes.data)
|
|
// resolve({
|
|
// path: url,
|
|
// })
|
|
// }
|
|
// })
|
|
const uploadRes = await uploadFileToAliOss(filePath)
|
|
uni.hideLoading()
|
|
resolve({
|
|
path: uploadRes,
|
|
})
|
|
})
|
|
}
|
|
|
|
const getSts = () => get('/oss/getSTSToken')
|
|
|
|
export const uploadFileToAliOss = async (filePath) => {
|
|
const stsRes = await getSts()
|
|
const sts = stsRes.data
|
|
const key = createOssKey(filePath)
|
|
const url = 'https://yaodongxiao.oss-cn-guangzhou.aliyuncs.com';
|
|
return new Promise(resolve => {
|
|
uni.uploadFile({
|
|
url,
|
|
name: 'file',
|
|
filePath,
|
|
formData: {
|
|
key,
|
|
OSSAccessKeyId: sts.accessKeyId,
|
|
signature: sts.signature,
|
|
security_token: sts.token,
|
|
policy: sts.policy,
|
|
},
|
|
success: () => {
|
|
resolve(`${url}/${key}`)
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
|
|
export const getLocationAction = (save = true) => {
|
|
return new Promise((resolve, reject) => {
|
|
const res = getLocation()
|
|
if (res.area_id) resolve(res)
|
|
else {
|
|
uni.authorize({
|
|
scope: 'scope.userLocation',
|
|
success: () => {
|
|
uni.getLocation({
|
|
isHighAccuracy: true,
|
|
success: async ({longitude, latitude}) => {
|
|
const {result} = await get(`https://api.tianditu.gov.cn/geocoder?postStr={'lon':${longitude},'lat':${latitude},'ver':1}&type=geocode&tk=1099ec280e86f4e9b632173326cde90b`)
|
|
const areaRes = await areaInfoByNameReq({name: result.addressComponent.county}).catch(() => {
|
|
if (!this.areaId) reject(false)
|
|
})
|
|
if (save) setLocation({
|
|
longitude,
|
|
latitude,
|
|
province_str: result.addressComponent.province,
|
|
city_str: result.addressComponent.city,
|
|
area_str: result.addressComponent.county,
|
|
area_id: areaRes.data.id,
|
|
city_id: areaRes.data.fid,
|
|
province_id: areaRes.data.city.fid
|
|
})
|
|
resolve({
|
|
longitude,
|
|
latitude,
|
|
area_id: areaRes.data.id,
|
|
city_id: areaRes.data.fid,
|
|
province_id: areaRes.data.city.fid,
|
|
province_str: result.addressComponent.province,
|
|
city_str: result.addressComponent.city,
|
|
area_str: result.addressComponent.county,
|
|
})
|
|
},
|
|
fail: () => {
|
|
reject(false)
|
|
}
|
|
})
|
|
},
|
|
fail: () => {
|
|
uni.showModal({
|
|
title: '提示',
|
|
content: '为了更好的提供服务,请开启定位服务',
|
|
success: ({confirm}) => {
|
|
if (confirm) uni.openSetting()
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
export const chooseVideo = (sourceType = ['album', 'camera']) => {
|
|
return new Promise((resolveMain, reject) => {
|
|
uni.chooseVideo({
|
|
sourceType,
|
|
compressed: true,
|
|
maxDuration: 60,
|
|
success: async ({tempFilePath, duration, size}) => {
|
|
if (duration > 180) {
|
|
uni.showToast({
|
|
title: '暂时不支持3分钟以上的视频',
|
|
icon: 'none'
|
|
})
|
|
return reject(false)
|
|
}
|
|
// 50M限制
|
|
if (size > 50 * 1024 * 1024) {
|
|
uni.showToast({
|
|
title: '暂时不支持50M以上的视频',
|
|
icon: 'none'
|
|
})
|
|
return reject(false)
|
|
}
|
|
const {token} = getUserInfo()
|
|
uni.showLoading({title: '上传中...'})
|
|
// uni.uploadFile({
|
|
// url: `${MAIN_API}/api/file/upload`,
|
|
// name: 'file',
|
|
// filePath: tempFilePath,
|
|
// header: {
|
|
// Authorization: token,
|
|
// TenantId: 10325
|
|
// },
|
|
// success: uploadRes => {
|
|
// const {data} = JSON.parse(uploadRes.data)
|
|
// uni.hideLoading()
|
|
// resolveMain({
|
|
// path: data.url,
|
|
// })
|
|
// }
|
|
// })
|
|
const uploadRes = await uploadFileToAliOss(tempFilePath)
|
|
resolveMain({
|
|
path: uploadRes,
|
|
})
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
export const getLocationOnly = () => {
|
|
return new Promise((resolve, reject) => {
|
|
uni.authorize({
|
|
scope: 'scope.userLocation',
|
|
success: () => {
|
|
uni.getLocation({
|
|
isHighAccuracy: true,
|
|
success: async ({longitude, latitude}) => {
|
|
const {result} = await get(`https://api.tianditu.gov.cn/geocoder?postStr={'lon':${longitude},'lat':${latitude},'ver':1}&type=geocode&tk=1099ec280e86f4e9b632173326cde90b`)
|
|
const {data} = await areaInfoByNameReq({name: result.addressComponent.county}).catch(() => {
|
|
if (!this.areaId) reject(false)
|
|
})
|
|
resolve({
|
|
longitude,
|
|
latitude,
|
|
area_id: data.id,
|
|
city_id: data.fid,
|
|
province_id: data.city.fid,
|
|
province_str: result.addressComponent.province,
|
|
city_str: result.addressComponent.city,
|
|
area_str: result.addressComponent.county,
|
|
full: result.formatted_address
|
|
})
|
|
},
|
|
fail: () => {
|
|
uni.setStorageSync("no_more_realtime_location", 1)
|
|
reject(false)
|
|
}
|
|
})
|
|
},
|
|
fail: err => {
|
|
uni.showModal({
|
|
title: '提示',
|
|
content: '为了更好的提供服务,请开启定位服务',
|
|
success: ({confirm}) => {
|
|
if (confirm) uni.openSetting()
|
|
}
|
|
})
|
|
}
|
|
})
|
|
})
|
|
}
|
|
|
|
|
|
export const getExpressMode = () => parseInt(uni.getStorageSync("express_mode"))
|
|
|
|
export const calcDistanceMeters = (lat1, lng1, lat2, lng2) => {
|
|
const nLat1 = Number(lat1)
|
|
const nLng1 = Number(lng1)
|
|
const nLat2 = Number(lat2)
|
|
const nLng2 = Number(lng2)
|
|
if (![nLat1, nLng1, nLat2, nLng2].every(Number.isFinite)) return NaN
|
|
const rad = value => value * Math.PI / 180
|
|
const r = 6371000
|
|
const dLat = rad(nLat2 - nLat1)
|
|
const dLng = rad(nLng2 - nLng1)
|
|
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
|
|
+ Math.cos(rad(nLat1)) * Math.cos(rad(nLat2))
|
|
* Math.sin(dLng / 2) * Math.sin(dLng / 2)
|
|
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
|
|
return r * c
|
|
}
|
|
|
|
export const formatDistance = meters => {
|
|
if (!Number.isFinite(meters)) return ''
|
|
if (meters < 1000) return `${Math.round(meters)}m`
|
|
const km = (meters / 1000).toFixed(1).replace(/\.0$/, '')
|
|
return `${km}km`
|
|
}
|