62 lines
1.6 KiB
JavaScript
62 lines
1.6 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 formatNumber = n => {
|
||
n = n.toString()
|
||
return n[1] ? n : '0' + n
|
||
}
|
||
|
||
// 获取当前页url
|
||
const getCurrentPageUrl = () => {
|
||
let pages = getCurrentPages();
|
||
let currentPage = pages[pages.length - 1];
|
||
let url = '/' + currentPage.route;
|
||
let options = currentPage.options;
|
||
for (let key in options) {
|
||
url += (url.indexOf('?') >= 0 ? '&' : '?') + `${key}=${options[key]}`;
|
||
}
|
||
return url;
|
||
}
|
||
|
||
const setTimeOutStorage = (key,value,timeout) => {
|
||
let timestamp = Date.parse(new Date());
|
||
wx.setStorageSync(key,
|
||
{
|
||
'value': value,
|
||
'timeout': timestamp + timeout
|
||
})
|
||
}
|
||
|
||
const getTimeoutStorage = (key) => {
|
||
// 当前时间
|
||
let timestamp = Date.parse(new Date());
|
||
// 缓存中的过期时间
|
||
let data = wx.getStorageSync(key);
|
||
let data_expiration = data.timeout;
|
||
let value = data.value;
|
||
// 如果缓存中没有data_expiration,说明也没有token,还未登录
|
||
if (data_expiration) {
|
||
// 如果超时了,清除缓存,重新登录
|
||
if (timestamp > data_expiration) {
|
||
wx.removeStorageSync(key);
|
||
return null;
|
||
}
|
||
}
|
||
return value;
|
||
}
|
||
|
||
module.exports = {
|
||
formatTime: formatTime,
|
||
getCurrentPageUrl: getCurrentPageUrl,
|
||
setTimeOutStorage: setTimeOutStorage,
|
||
getTimeoutStorage: getTimeoutStorage
|
||
}
|