小程序utils方法

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',
                //   }
                // }
            }
        })
    })
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值