JM-MiniApp/unpackage/dist/dev/mp-weixin/utils/index.js

102 lines
2.8 KiB
JavaScript
Raw Normal View History

2023-10-18 21:00:42 +08:00
"use strict";
const common_vendor = require("../common/vendor.js");
const config_index = require("../config/index.js");
const custom = {
// 控制字符长度
controlLength: (str, len = 10) => {
if (!str)
return "";
if (str.length > len)
return str.slice(0, len) + "...";
return str;
},
uploadFile: (chooseImageRes) => {
const tempFilePaths = chooseImageRes.tempFilePaths;
common_vendor.index.uploadFile({
url: `${config_index.config.baseUrl}/user/upload`,
filePath: tempFilePaths[0],
name: "file",
header: {
"Authorization": token,
"x-token": token
},
success: (res) => {
JSON.parse(res.data);
}
});
},
uploadImage: (img, callfunc) => {
const token2 = common_vendor.index.getStorageSync("access_token");
const tempFilePaths = img;
common_vendor.index.uploadFile({
url: `${config_index.config.baseUrl}/user/upload`,
filePath: tempFilePaths,
name: "file",
header: {
"Authorization": token2,
"x-token": token2,
"token": token2
// 'content-type': 'multipart/form-data',
},
success: (res) => {
var f_res = JSON.parse(res.data);
callfunc(f_res);
}
});
},
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 = /* @__PURE__ */ new Date();
myDate.setDate(myDate.getDate());
var dateArray = [];
var dateTemp;
var flag = 1;
for (var i = 0; i < time; i++) {
let month = 0;
if (myDate.getMonth() + 1 < 10) {
month = "0" + (myDate.getMonth() + 1);
} else {
month = myDate.getMonth() + 1;
}
let day = 0;
if (myDate.getDate() < 10) {
day = "0" + myDate.getDate();
} else {
day = myDate.getDate();
}
dateTemp = myDate.getFullYear() + "-" + month + "-" + day;
dateArray.push(dateTemp);
myDate.setDate(myDate.getDate() + flag);
}
return dateArray;
},
getDistances(lat1, lng1, lat2, lng2) {
let EARTH_RADIUS = 6378.137;
let radLat1 = lat1 * Math.PI / 180;
let radLat2 = lat2 * Math.PI / 180;
let a = radLat1 - radLat2;
let b = lng1 * Math.PI / 180 - lng2 * Math.PI / 180;
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 * 1e4) / 1e4;
return { m: s * 1e3, km: Number(s.toFixed(2)) };
},
checkLogin() {
let token2 = common_vendor.index.getStorageSync("access_token");
console.log(token2);
if (token2) {
return true;
} else {
return false;
}
}
};
exports.custom = custom;