163 lines
3.4 KiB
JavaScript
163 lines
3.4 KiB
JavaScript
|
|
function msg(title, duration = 1000) {
|
|
uni.showToast({
|
|
title: title,
|
|
duration: duration,
|
|
icon: 'none',
|
|
mask: true,
|
|
});
|
|
}
|
|
|
|
function show(title, msg, showCancel = true) {
|
|
let res = new Promise((resolve, reject) => {
|
|
uni.showModal({
|
|
title: title,
|
|
content: msg,
|
|
showCancel: showCancel,
|
|
success: function(res) {
|
|
resolve(res.confirm)
|
|
},
|
|
fail: function(res) {
|
|
reject(false)
|
|
},
|
|
});
|
|
})
|
|
return res;
|
|
}
|
|
|
|
const loading = (title = '加载中', mask = true) => uni.showLoading({
|
|
title: title,
|
|
mask: mask
|
|
})
|
|
const hideLoading = () => uni.hideLoading()
|
|
|
|
// 保留当前页面,跳转到应用内的某个页面,使用uni.navigateBack可以返回到原页面。
|
|
const navigateTo = url => uni.navigateTo({
|
|
url: url
|
|
});
|
|
// 关闭所有页面,打开到应用内的某个页面。
|
|
const reLaunch = url => {
|
|
uni.reLaunch({
|
|
url: url
|
|
});
|
|
}
|
|
// 跳转到 tabBar 页面,并关闭其他所有非 tabBar 页面。
|
|
const switchTab = url => {
|
|
uni.switchTab({
|
|
url: url
|
|
});
|
|
}
|
|
// 关闭当前页面,返回上一页面或多级页面。可通过 getCurrentPages() 获取当前的页面栈,决定需要返回几层。
|
|
const navigateBack = (delta = 1, fun = () => {}) => {
|
|
uni.navigateBack({
|
|
delta: delta,
|
|
success() {
|
|
fun()
|
|
}
|
|
})
|
|
}
|
|
|
|
//关闭当前页面,跳转到应用内的某个页面。
|
|
const redirectTo = url => uni.redirectTo({
|
|
url: url
|
|
});
|
|
|
|
// 时间转换时间戳
|
|
const dateToTime = (dateString) => {
|
|
let date = new Date(dateString);
|
|
let timestamp = date.getTime();
|
|
return timestamp
|
|
}
|
|
|
|
// 数字格式化
|
|
const formatDate2 = (timestamp, format = 'yyyy-MM-dd HH:mm:ss') => {
|
|
if (!timestamp)
|
|
return ''
|
|
let date
|
|
try {
|
|
if (timestamp) {
|
|
if (typeof timestamp == 'number' && timestamp <= 9999999999) {
|
|
timestamp *= 1000
|
|
}
|
|
date = new Date(timestamp)
|
|
} else {
|
|
date = new Date()
|
|
}
|
|
} catch (e) {
|
|
return ''
|
|
}
|
|
let day = date.getDate()
|
|
let month = date.getMonth() + 1
|
|
let year = date.getFullYear()
|
|
let hours = date.getHours()
|
|
let minutes = date.getMinutes()
|
|
let seconds = date.getSeconds()
|
|
|
|
// 替换日期格式中的占位符
|
|
return format
|
|
.replace('yyyy', year)
|
|
.replace('MM', month.toString().padStart(2, '0'))
|
|
.replace('dd', day.toString().padStart(2, '0'))
|
|
.replace('HH', hours.toString().padStart(2, '0'))
|
|
.replace('mm', minutes.toString().padStart(2, '0'))
|
|
.replace('ss', seconds.toString().padStart(2, '0'))
|
|
}
|
|
|
|
// 获取token
|
|
export const getToken = () => uni.getStorageSync('token')
|
|
|
|
export const getUserInfo = (key = '') => {
|
|
const userInfo = uni.getStorageSync('userInfo')
|
|
return '' === key ? userInfo : userInfo[key];
|
|
}
|
|
|
|
|
|
|
|
// 将对象和字符串相互转换
|
|
export const objToJson = obj => encodeURLComponent('page_data', obj)
|
|
export const jsonToObj = () => uni.getStorageSync('page_data')
|
|
|
|
// 将对象存入缓存
|
|
export const setPageCache = (name, obj) => {
|
|
uni.setStorageSync(name, obj)
|
|
}
|
|
|
|
// 将对象取出并且删除缓存
|
|
export const getPageCache = name => {
|
|
const cache = uni.getStorageSync(name)
|
|
uni.removeStorageSync(name)
|
|
return cache
|
|
}
|
|
|
|
function setValue(key, value) {
|
|
uni.setStorageSync(key, value);
|
|
}
|
|
|
|
function getValue(key) {
|
|
return uni.getStorageSync(key)
|
|
}
|
|
|
|
function isLogin() {
|
|
return !!getToken();
|
|
}
|
|
|
|
export default {
|
|
getPageCache,
|
|
setPageCache,
|
|
objToJson,
|
|
jsonToObj,
|
|
hideLoading,
|
|
loading,
|
|
msg,
|
|
show,
|
|
redirectTo,
|
|
navigateTo,
|
|
navigateBack,
|
|
formatDate2,
|
|
setValue,
|
|
getValue,
|
|
isLogin,
|
|
reLaunch,
|
|
switchTab,
|
|
dateToTime,
|
|
} |