Files
CompanyProject/衣莎项目/源码/YiShaXiYi/utils/util.js
2024-09-27 01:32:49 +08:00

235 lines
5.4 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.

const app = getApp();
const config = app.loadConfig();
const regeneratorRuntime = app.loadAsync()
const { StorageSync } = app.loadModel("getSetting")
const storage = new StorageSync()
const QQMapWX = require("./qqmap-wx-jssdk")
var qqmapsdk
/**
*
* @param {Number} number 传入时间戳
*/
const formatTime = (number, format) => {
let time = new Date(parseInt(number) * 1000)
let newArr = []
let formatArr = ['Y', 'M', 'D', 'h', 'm', 's']
newArr.push(time.getFullYear())
newArr.push(formatNumber(time.getMonth() + 1))
newArr.push(formatNumber(time.getDate()))
newArr.push(formatNumber(time.getHours()))
newArr.push(formatNumber(time.getMinutes()))
newArr.push(formatNumber(time.getSeconds()))
for (let i in newArr) {
format = format.replace(formatArr[i], newArr[i])
}
return format;
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
/**
* 获取本地token
*/
const token = async () => {
let token = await storage.getSetting("token")
return token || ''
}
/**
* 判断本地是否登录的跳转
*/
const isLoginJump = async () => {
let t = await token()
let isTel = app.globalData.userInfo.telnum
if (!t || isTel == "点击授权手机号" || isTel == '') {
wx.navigateTo({
url: '/pages/login/login'
})
return 1
}
return 2
}
/**
* 重载页面,在需要刷新当前页时使用
*/
const heavyLoad = () => {
setTimeout(() => {
const pages = getCurrentPages()
const perpage = pages[pages.length - 1]
perpage.onLoad()
wx.stopPullDownRefresh()
}, 2000)
}
/**
* 调用微信api打开用户微信的收货地址
*/
const selectAdd = () => {
return new Promise(function (resolve, reject) {
wx.chooseLocation({
success(res) {
// 获取省市区
conAdd({
latitude: res.latitude,
longitude: res.longitude
}, (rel) => {
resolve(rel)
})
},
fail(err) {
// 用户首页拒绝位置授权处理
if (err.errMsg != 'chooseLocation:fail cancel') {
wx.showModal({
title: '授权地址信息',
content: '你拒绝了地址授权,我获取不到你的地理位置,哦!',
success(res) {
if (res.confirm) {
wx.openSetting({
success(res) {
// console.log(res.authSetting)
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
}
})
}
}
})
}
}
})
})
}
/**
* 根据经纬度转换成真实地址
* @param {Object} data 经纬度
* latitude 精度 longitude 纬度
*/
const conAdd = (data, success) => {
qqmapsdk = new QQMapWX({
key: 'QLGBZ-DNIRS-ITLOU-64W7Q-4YDN3-QDBS6' // 必填
})
qqmapsdk.reverseGeocoder({
location: {
latitude: data.latitude,
longitude: data.longitude
},
success: function (res) {
success(res)
}
})
}
/**
*
* @param {*} img base64加密的图片
*/
const base64Img = (img) => {
let base64 = res.data.data
base64 = base64.replace(/[\r\n]/g, "")
return 'data:image/jpeg;base64,' + base64
}
/**
* 判断入参是否是对象
* @param { any } object
*/
const isObj = (object) => {
return object && typeof (object) == 'object' && Object.prototype.toString.call(object).toLowerCase() == "[object object]";
}
/**
* 判断是否是数组
* @param { any } object
*/
const isArray = (object) => {
return object && typeof (object) == 'object' && object.constructor == Array;
}
/**
* 获取对象长度
* @param { object } object
*/
const getLength = (object) => {
var count = 0;
for (var i in object) {
count++
};
return count;
}
/**
* 判断2个无序数组是否相同
*/
Array.prototype.equals = function (arr) {
return this.sort().join() === arr.sort().join()
}
/**
* 只能检测一层json下面的子级
* 判断2个json数组是否相同
* @param { JSON } objA
* @param { JSON } objB
* @param { boolean } flag 锁,防止死循环
*/
const CompareObj = (objA, objB, flag, ignore) => {
for (var key in objA) {
if (!flag) //跳出整个循环
break;
if (!objB.hasOwnProperty(key)) {
flag = false;
break;
}
if (!isArray(objA[key])) { //子级不是数组时,比较属性值
if (objB[key] != objA[key] && key != ignore) { //需要忽略检查的字段暂时只需要忽略1个字段
flag = false;
break;
}
} else {
if (!isArray(objB[key])) {
flag = false;
break;
}
var oA = objA[key],
oB = objB[key];
if (!oA.equals(oB)) {
flag = false;
break;
}
for (var k in oA) {
if (!flag) //这里跳出循环是为了不让递归继续
break;
flag = CompareObj(oA[k], oB[k], flag);
}
}
}
return flag;
}
/**
* 判断json是否完全相同
* @param { JSON } objA
* @param { JSON } objB
*/
const Compare = (objA, objB, ignore) => {
if (!isObj(objA) || !isObj(objB)) return false; //判断类型是否正确
// if (getLength(objA) != getLength(objB)) return false; //判断长度是否一致
return CompareObj(objA, objB, true, ignore); //默认为true
}
module.exports = {
formatTime: formatTime,
token: token,
heavyLoad: heavyLoad,
selectAdd: selectAdd,
conAdd: conAdd,
Compare: Compare,
isLoginJump: isLoginJump
}