NONGJINGQUANXIAOCHENGXU/utils/util.js

144 lines
2.9 KiB
JavaScript

const formatTime = date => {
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
}
const wxlogin = (fun, wxinfo) => {
var that = this;
// 设置手机是否允许获取权限
wx.getSetting({
success: function(res) {
console.log('00',res)
if (res.authSetting['scope.userInfo']) {
// 微信系统登录
wx.login({
success: function(res) {
// 获取code
var code = res.code;
getApp().globalData.userInfo.code = code
console.log('123321',res.code)
console.log('进行第三方登录')
let wxinfo = getApp().globalData.userInfo
}
})
} else {
// 没有授权
hideloading();
confirm("请先授权", function() {
wx.navigateTo({
url: '/pages/authorize/authorize',
})
}, false)
}
}
})
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : `0${n}`
}
function getLocalStorage(key){
try {
var value = wx.getStorageSync(key)
if (value) {
return value;
}
} catch (e) { }
}
function setLocalStorage(key, value){
try {
wx.setStorageSync(key, value)
} catch (e) { }
}
function removeLocalStorage(e, t){
wx.removeStorage({
key: e,
success: t
})
}
function loading(e, t, m, n) {
if (wx.showLoading) {
wx.showLoading({
title: e || "加载中...",
mask: m || !0,
success: n,
})
} else {
wx.showToast({
icon: "loading",
mask: m || !0,
duration: t || 1000,
title: e || "加载中...",
success: n
})
}
}
function hideloading(t) {
if (wx.hideLoading) {
wx.hideLoading();
}
clearTimeout(e);
t = t || 1500;
var e = setTimeout(function () {
wx.hideToast()
}, t)
}
// 错误提示信息函数
function showerror(info, url, time) {
var index = getCurrentPages().length - 1;
var time = time || 1000;
if (url) {
getCurrentPages()[index].setData({
errorurl: url,
})
}
getCurrentPages()[index].setData({
errorflag: true,
errorinfo: info,
})
setTimeout(function () {
getCurrentPages()[index].setData({
errorflag: false,
errorinfo: '提示信息',
})
}, time)
}
function confirm(e, t, n, title) {
wx.showModal({
title: title || "提示",
content: e,
showCancel: n || !1,
success: t
})
}
// 返回上一页
function backpage(e, t) {
wx.navigateBack({
delta: e || 1,
success: t
})
}
module.exports = {
formatTime,
getLocalStorage,
setLocalStorage,
removeLocalStorage,
loading,
hideloading,
showerror,
confirm,
backpage,
wxlogin
}