init version kelfy-mini for new gitea
This commit is contained in:
196
utils/util.js
Normal file
196
utils/util.js
Normal file
@@ -0,0 +1,196 @@
|
||||
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 formatNumber = n => {
|
||||
n = n.toString()
|
||||
return n[1] ? n : `0${n}`
|
||||
}
|
||||
|
||||
var toHide = function(array) {
|
||||
var phone = array.substring(0, 3) + '********' + array.substring(13);
|
||||
return phone;
|
||||
}
|
||||
|
||||
|
||||
function formatDate(date) {
|
||||
var year = date.getFullYear()
|
||||
var month = date.getMonth() + 1
|
||||
var day = date.getDate()
|
||||
|
||||
|
||||
return [year, month, day].map(formatNumber)
|
||||
}
|
||||
|
||||
// function formatNumber(n) {
|
||||
// n = n.toString()
|
||||
// return n[1] ? n : '0' + n
|
||||
// }
|
||||
|
||||
function requestPost(url, data = {}, method = "POST") {
|
||||
return new Promise(function(resolve, reject) {
|
||||
wx.request({
|
||||
url: url,
|
||||
data: data,
|
||||
method: method,
|
||||
dataType: 'json',
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
'tenant-id':'1',
|
||||
'X-Litemall-Token': wx.getStorageSync('token')
|
||||
},
|
||||
success: function(res) {
|
||||
|
||||
if (res.statusCode == 200) {
|
||||
|
||||
if (res.data.errno == 501) {
|
||||
// 清除登录相关内容
|
||||
try {
|
||||
wx.removeStorageSync('userInfo');
|
||||
wx.removeStorageSync('token');
|
||||
} catch (e) {
|
||||
// Do something when catch error
|
||||
}
|
||||
// 切换到登录页面
|
||||
wx.navigateTo({
|
||||
url: '/pages/auth/login/login'
|
||||
});
|
||||
} else {
|
||||
resolve(res.data);
|
||||
}
|
||||
} else {
|
||||
reject(res.errMsg);
|
||||
}
|
||||
|
||||
},
|
||||
fail: function(err) {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
function requestForm(url, data = {}, method) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
wx.request({
|
||||
url: url,
|
||||
data: data,
|
||||
method: method,
|
||||
header: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'tenant-id':'1',
|
||||
'X-Litemall-Token': wx.getStorageSync('token')
|
||||
},
|
||||
success: function(res) {
|
||||
|
||||
if (res.statusCode == 200) {
|
||||
|
||||
if (res.data.errno == 501) {
|
||||
// 清除登录相关内容
|
||||
try {
|
||||
wx.removeStorageSync('userInfo');
|
||||
wx.removeStorageSync('token');
|
||||
} catch (e) {
|
||||
// Do something when catch error
|
||||
}
|
||||
// 切换到登录页面
|
||||
wx.navigateTo({
|
||||
url: '/pages/auth/login/login'
|
||||
});
|
||||
} else {
|
||||
resolve(res.data);
|
||||
}
|
||||
} else {
|
||||
reject(res.errMsg);
|
||||
}
|
||||
|
||||
},
|
||||
fail: function(err) {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
/**
|
||||
* 封封微信的的request
|
||||
*/
|
||||
function request(url, data = {}, method = "GET") {
|
||||
return new Promise(function(resolve, reject) {
|
||||
wx.request({
|
||||
url: url,
|
||||
data: data,
|
||||
method: method,
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
'tenant-id':'1',
|
||||
'X-Litemall-Token': wx.getStorageSync('token')
|
||||
},
|
||||
success: function(res) {
|
||||
|
||||
if (res.statusCode == 200) {
|
||||
|
||||
if (res.data.errno == 501) {
|
||||
// 清除登录相关内容
|
||||
try {
|
||||
wx.removeStorageSync('userInfo');
|
||||
wx.removeStorageSync('token');
|
||||
} catch (e) {
|
||||
// Do something when catch error
|
||||
}
|
||||
// 切换到登录页面
|
||||
wx.navigateTo({
|
||||
url: '/pages/auth/login/login'
|
||||
});
|
||||
} else {
|
||||
resolve(res.data);
|
||||
}
|
||||
} else {
|
||||
reject(res.errMsg);
|
||||
}
|
||||
|
||||
},
|
||||
fail: function(err) {
|
||||
reject(err)
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
function redirect(url) {
|
||||
|
||||
//判断页面是否需要登录
|
||||
if (false) {
|
||||
wx.redirectTo({
|
||||
url: '/pages/auth/login/login'
|
||||
});
|
||||
return false;
|
||||
} else {
|
||||
wx.redirectTo({
|
||||
url: url
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function showErrorToast(msg) {
|
||||
wx.showToast({
|
||||
title: msg,
|
||||
image: '/static/images/icon_error.png'
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
formatTime,
|
||||
toHide,
|
||||
request,
|
||||
requestPost,
|
||||
requestForm,
|
||||
redirect,
|
||||
showErrorToast
|
||||
}
|
||||
Reference in New Issue
Block a user