不斷更新中.....
util.js
//操作失敗的提示信息
function errorToShow(msg = '操作失敗', callback = function () {}) {
wx.showToast({
title: msg,
icon: 'none',
duration: 1500
})
setTimeout(function () {
callback()
}, 1500)
}
//操作成功后宝惰,的提示信息
function successToShow(msg = '保存成功', callback = function () {}) {
wx.showToast({
title: msg,
icon: 'success',
duration: 1500
})
setTimeout(function () {
callback()
}, 1500)
}
/**
* 授權(quán)請(qǐng)求
* @export
* @param {*} authorizeScope 更多scope參考
* @param {*} modal modal彈窗參數(shù)信息
*/
function setScope (authorizeScope, modal) {
return new Promise((resolve, reject) => {
if (!modal) {
modal = {
title: '授權(quán)',
content: '需要您設(shè)置授權(quán)已使用相應(yīng)功能',
confirmText: '設(shè)置'
}
}
wx.getSetting({
success(res) {
// hasAuthor === undefined 表示 初始化進(jìn)入冗尤,從未授權(quán)
// hasAuthor === true 表示 已授權(quán)
// hasAuthor === false 表示 授權(quán)拒絕
const hasAuthor = res.authSetting[authorizeScope]
switch (hasAuthor) {
case undefined:
wx.authorize({
scope: authorizeScope,
success: res => {
resolve(res)
},
fail: err => {
wx.showToast({
title: '授權(quán)失敗',
icon: 'none',
duration: 3000
})
reject(err)
}
})
break
case true:
resolve()
break
case false:
//bug 在電腦模擬器會(huì)報(bào)錯(cuò)稳诚,手機(jī)不會(huì)
wx.showModal({
...modal,
success: res => {
if (res.confirm) {
wx.openSetting({
success: res => {
if (res.authSetting[authorizeScope]) {
resolve(res)
} else {
reject(res)
wx.showToast({
title: '授權(quán)失敗',
icon: 'none',
duration: 3000
})
}
},
fail: err => {
console.log(err)
reject(err)
wx.showToast({
title: '打開(kāi)設(shè)置異常',
icon: 'none',
duration: 3000
})
}
})
} else {
reject(res)
wx.showToast({
title: '授權(quán)失敗',
icon: 'none',
duration: 3000
})
}
},
fail: err => {
reject(err)
wx.showToast({
title: '彈窗異常',
icon: 'none',
duration: 3000
})
}
})
break
}
},
fail: err => {
reject(err)
wx.showToast({
title: '獲取當(dāng)前設(shè)置異常',
icon: 'none',
duration: 3000
})
}
})
})
}
module.exports={
errorToShow,
successToShow
}