sqb-util
Version:
sqb-util,是送气宝系列的共用工具套
126 lines (120 loc) • 4.72 kB
JavaScript
import { parseParam } from '../libs/function/util.js'
export default {
data() {
return {
CustomBar: this.CustomBar || 0,
StatusBar: this.StatusBar || 0,
commonRedirect: ''
}
},
onLoad(options) {
const param = this.getParam(options)
// 重定向链接,如果有传入保存后将重定向到此链接,如果没传将返回上一页
if (param.redirect) {
this.commonRedirect = decodeURIComponent(param.redirect)
}
},
methods: {
qsParse(str = '') {
if (!str) {
return {}
}
const result = {}
const objs = str.split('&')
objs.forEach(item => {
const [key, value] = item.split('=')
if (value) {
result[key] = value
}
})
return result
},
qsStringify(objs) {
if (!objs) {
return ''
}
let result = ''
Object.keys(objs).forEach((key) => {
if (result) {
result += '&'
}
result += `${key}=${objs[key]}`
})
return result
},
// 前往重定向链接地址
toRedirect(obj = {}) {
let url = this.commonRedirect
// 没重定向链接返回上一页
if (!url) {
uni.navigateBack()
return
}
const param = this.qsParse(url.split('?')[1])
Object.keys(obj).forEach(key => {
param[key] = obj[key]
})
uni.redirectTo({
url: url.split('?')[0] + '?' + this.qsStringify(param)
})
},
/*
* 获取参数,可接收小程序码参数与url参数,并以统一格式返回
* @param option onLoad的回调参数
*/
getParam(option) {
return parseParam(option)
},
// 版本更新
updateApp() {
return new Promise((resolve, reject) => {
if (uni.canIUse('getUpdateManager')) {
const updateManager = uni.getUpdateManager()
updateManager.onCheckForUpdate((res) => {
if (res.hasUpdate) { // 请求完新版本信息的回调
updateManager.onUpdateReady(() => {
resolve()
uni.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success: (res) => {
if (res.confirm) {// 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
}
}
})
})
updateManager.onUpdateFailed(() => {
uni.showModal({// 新的版本下载失败
title: '已经有新版本了哟~',
content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~',
showCancel: false,
success: () => {
uni.exitMiniProgram()
}
})
reject()
})
} else {
reject('当前已是最新版本!')
}
})
} else {
uni.showModal({// 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。',
showCancel: false
})
}
})
},
/**
* 获取历史页面实例
*/
getHistoryPage(delta = 1) {
const pages = getCurrentPages()
const prevPage = pages[pages.length - (delta + 1)]
return prevPage && prevPage.$vm || {}
}
}
}