135 lines
3.2 KiB
TypeScript
135 lines
3.2 KiB
TypeScript
// 小程序更新检测
|
|
|
|
import { isLogin } from '@/utils';
|
|
|
|
export function mpUpdate() {
|
|
const updateManager = uni.getUpdateManager();
|
|
updateManager.onCheckForUpdate((res) => {
|
|
// 请求完新版本信息的回调
|
|
console.log(res.hasUpdate);
|
|
});
|
|
updateManager.onUpdateReady(() => {
|
|
uni.showModal({
|
|
title: '更新提示',
|
|
content: '检测到新版本,是否下载新版本并重启小程序?',
|
|
success(res) {
|
|
if(res.confirm) {
|
|
// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
|
|
updateManager.applyUpdate();
|
|
}
|
|
}
|
|
});
|
|
});
|
|
updateManager.onUpdateFailed(() => {
|
|
// 新的版本下载失败
|
|
uni.showModal({
|
|
title: '已经有新版本了哟~',
|
|
content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
|
|
showCancel: false
|
|
});
|
|
});
|
|
}
|
|
|
|
interface ToastOption {
|
|
icon?: 'none' | 'success' | 'loading' | 'error' | 'fail' | 'exception' | undefined,
|
|
duration?: number,
|
|
complete?: Function
|
|
}
|
|
|
|
export function showToast(title: string, { icon, duration, complete }: ToastOption = {}) {
|
|
const defaultDuration = 1500;
|
|
uni.showToast({
|
|
title: title,
|
|
icon: icon || 'none',
|
|
duration: duration || defaultDuration,
|
|
complete(result) {
|
|
setTimeout(() => {
|
|
if(complete != undefined) {
|
|
complete();
|
|
}
|
|
}, duration || defaultDuration);
|
|
}
|
|
});
|
|
}
|
|
|
|
export function goPath(path: string, needAuth: boolean = false) {
|
|
if(needAuth && !isLogin()) {
|
|
uni.showModal({
|
|
title: '提示',
|
|
content: '您还未登录,请先登录',
|
|
showCancel: true,
|
|
success: (res) => {
|
|
if(res.confirm) {
|
|
goLogin();
|
|
}
|
|
}
|
|
});
|
|
return;
|
|
}
|
|
if(path.includes('home/index') || path.includes('mall/index') || path.includes('qrcode/index') || path.includes('mine/index')) {
|
|
uni.switchTab({
|
|
url: path
|
|
}).then(r => {
|
|
});
|
|
} else {
|
|
uni.navigateTo({
|
|
url: path
|
|
}).then(r => {
|
|
});
|
|
}
|
|
}
|
|
|
|
export function goLogin() {
|
|
uni.navigateTo({
|
|
url: '/pages/common/login/index'
|
|
}).then(r => {
|
|
});
|
|
}
|
|
|
|
export function formatTimeWithZeroPad(num: number): string {
|
|
return num < 10 ? '0' + num : num + '';
|
|
}
|
|
|
|
export function sortASCII(obj: any, isSort = true) {
|
|
let arr: any[] = [];
|
|
Object.keys(obj).forEach(item => arr.push(item));
|
|
let sortArr = isSort ? arr.sort() : arr.sort().reverse();
|
|
let sortObj: any = {};
|
|
for (let i in sortArr) {
|
|
sortObj[sortArr[i]] = obj[sortArr[i]];
|
|
}
|
|
return sortObj;
|
|
}
|
|
|
|
// Array.prototype.contains = function(obj: any) {
|
|
// let i = this.length;
|
|
// while (i--) {
|
|
// if(this[i] === obj) {
|
|
// return true;
|
|
// }
|
|
// }
|
|
// return false;
|
|
// };
|
|
|
|
export function parseParameter(obj: any) {
|
|
if(obj === null || obj === undefined) return '';
|
|
const arr = [];
|
|
const keys: string[] = Object.keys(obj);
|
|
const entries: any[] = Object.entries(obj);
|
|
for (const [key, value] of entries) {
|
|
if(keys.includes(key) && !key.startsWith('function')) {
|
|
arr.push(key + '=' + value);
|
|
}
|
|
}
|
|
return arr.join('&');
|
|
}
|
|
|
|
export function copy(content: string) {
|
|
uni.setClipboardData({
|
|
data: content,
|
|
success: function(res) {
|
|
showToast('复制成功');
|
|
}
|
|
});
|
|
}
|