showToast: 提示窗
showLoading:加载中提示
showModal:模态对话框,支持async await
getSetting:获取用户设置,返回promise,支持async await
// message length max 7
function showToast(message, icon = 'none', duration = 3000,) {
wx.showToast({
icon,
title: message,
duration,
mask: true
})
}
// loading加载窗
function showLoading(title = '加载中...', mask = true) {
wx.showLoading({title, mask})
}
// 支持使用async await
function showModal(
title = "提示",
content = "这是一个模态弹窗",
successFun = () => {
console.log("用户点击确定");
},
showCancel = false,
cancelFun = () => {
console.log("用户点击取消");
},
) {
return new Promise((resolve, reject) => {
wx.showModal({
title,
content,
showCancel,
success(res) {
if (res.confirm) {
successFun();
resolve()
} else if (res.cancel) {
cancelFun();
reject()
}
},
});
})
}
// 获取用户的当前设置。返回值中只会出现小程序已经向用户请求过的权限。
/**
*
* @param settingName 设置名称 ( 不需要scope )
* @param isWithSubscriptions 是否是订阅消息设置
* @returns {Promise<boolean>} 返回boolean值
*/
async function getSetting(settingName, isWithSubscriptions = false) {
return new Promise((resolve, reject) => {
wx.getSetting({
withSubscriptions: isWithSubscriptions,
success(res) {
console.log(res.authSetting)
console.log(res.subscriptionsSetting)
if (isWithSubscriptions) {
resolve(res.subscriptionsSetting.itemSettings[settingName] === 'accept' ? true : false)
} else {
resolve(res.authSetting[`scope.${settingName}`])
}
// res.authSetting = {
// "scope.userInfo": true,
// "scope.userLocation": true
// }
// res.subscriptionsSetting = {
// mainSwitch: true, // 订阅消息总开关
// itemSettings: { // 每一项开关
// SYS_MSG_TYPE_INTERACTIVE: 'accept', // 小游戏系统订阅消息
// SYS_MSG_TYPE_RANK: 'accept'
// zun-LzcQyW-edafCVvzPkK4de2Rllr1fFpw2A_x0oXE: 'reject', // 普通一次性订阅消息
// ke_OZC_66gZxALLcsuI7ilCJSP2OJ2vWo2ooUPpkWrw: 'ban',
// }
// }
}
})
})
}