JM-MiniApp/utils/index.js
2024-03-20 14:44:01 +08:00

115 lines
3.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import config from '../config';
export default {
// 控制字符长度
controlLength: (str, len = 10) => {
if (!str) return "";
if (str.length > len) return str.slice(0, len) + "..."
return str;
},
uploadFile:(chooseImageRes) => {
const tempFilePaths = chooseImageRes.tempFilePaths;
uni.uploadFile({
url:`${config.baseUrl}/user/upload`,
filePath: tempFilePaths[0],
name: 'file',
header:{
'Authorization':token,
'x-token':token
},
success: (res) => {
var f_res = JSON.parse(res.data)
// if(f_res.code === 0){
// userInfo.value.avatar = f_res.data
// }
}
});
},
uploadImage:(img,callfunc) => {
const token = uni.getStorageSync('access_token')
// console.log('uploadImage:',img)
const tempFilePaths = img;
uni.uploadFile({
url:`${config.baseUrl}/user/upload`,
filePath: tempFilePaths,
name: 'file',
header:{
'Authorization':token,
'x-token':token,
'token':token,
// 'content-type': 'multipart/form-data',
},
success: (res) => {
var f_res = JSON.parse(res.data)
// console.log(f_res)
callfunc(f_res)
// if(f_res.code === 0){
// userInfo.value.avatar = f_res.data
// }
}
});
},
timestampToDate(stamp) { // 时间戳转日期
var time = new Date(stamp);
var y = time.getFullYear();
var m = time.getMonth()+1;
var d = time.getDate();
const date = `${y}-${m}-${d}`
return date
},
datatime(time){ // 获取近五天的日期数组
var myDate = new Date(); //获取当天日期
myDate.setDate(myDate.getDate());
// console.log(myDate.getFullYear())//年
// console.log(myDate.getMonth())//月0-11
// console.log(myDate.getDate())//日
var dateArray = [];
var dateTemp;
var flag = 1;
// 日期格式化
for (var i = 0; i < time; i++) {
let month = 0;
// 月小于10在前面加0因为月从0开始故需+1
if (myDate.getMonth() + 1 < 10) {
month = '0' + (myDate.getMonth() + 1);
} else {
month = myDate.getMonth() + 1;
}
let day = 0;
// 日小于10在前加0
if (myDate.getDate() < 10) {
day = '0' + myDate.getDate();
} else {
day = myDate.getDate();
}
// YY-MM-DD
dateTemp = myDate.getFullYear() + '-' + month + '-' + day;
dateArray.push(dateTemp);
// console.log(dateArray)
//若不加flag则结果都是一个日期当前日期减time天
//日期 +1 天此日期为减time天后的日期
myDate.setDate(myDate.getDate() + flag);
}
return dateArray
},
getDistances(lat1, lng1, lat2, lng2) { // 根据经纬度计算距离,参数分别为第一点的纬度,经度;第二点的纬度,经度
let EARTH_RADIUS = 6378.137;// 地球半径
let radLat1 = lat1 * Math.PI / 180.0; //lat1 * Math.PI / 180.0=>弧度计算
let radLat2 = lat2 * Math.PI / 180.0;
let a = radLat1 - radLat2;
let b = lng1 * Math.PI / 180.0 - lng2 * Math.PI / 180.0;
let s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
s = s * EARTH_RADIUS;
s = Math.round(s * 10000) / 10000;// 输出为公里
return { m: s * 1000, km: Number(s.toFixed(2)) }
},
checkLogin(){
// let token = uni.getStorageSync('access_token')
let u_info = uni.getStorageSync('userInfo')
if(u_info){
return true
}
else{
return false
}
}
}