小程序工具方法

不断更新中.....

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)
}
/**
 * 授权请求
 * @export
 * @param {*} authorizeScope 更多scope参考
 * @param {*} modal modal弹窗参数信息
 */

function setScope (authorizeScope, modal) {
  return new Promise((resolve, reject) => {
    if (!modal) {
      modal = {
        title: '授权',
        content: '需要您设置授权已使用相应功能',
        confirmText: '设置'
      }
    }
    wx.getSetting({
      success(res) {
        // hasAuthor === undefined  表示 初始化进入,从未授权
        // hasAuthor === true       表示 已授权
        // hasAuthor === false      表示 授权拒绝
        const hasAuthor = res.authSetting[authorizeScope]
        switch (hasAuthor) {
          case undefined:
            wx.authorize({
              scope: authorizeScope,
              success: res => {
                resolve(res)
              },
              fail: err => {
                wx.showToast({
                  title: '授权失败',
                  icon: 'none',
                  duration: 3000
                })
                reject(err)
              }
            })
            break
          case true:
            resolve()
            break
          case false:
           //bug 在电脑模拟器会报错,手机不会
            wx.showModal({
              ...modal,
              success: res => {
                if (res.confirm) {
                  wx.openSetting({
                    success: res => {
                      if (res.authSetting[authorizeScope]) {
                        resolve(res)
                      } else {
                        reject(res)
                        wx.showToast({
                          title: '授权失败',
                          icon: 'none',
                          duration: 3000
                        })
                      }
                    },
                    fail: err => {
                      console.log(err)
                      reject(err)
                      wx.showToast({
                        title: '打开设置异常',
                        icon: 'none',
                        duration: 3000
                      })
                    }
                  })
                } else {
                  reject(res)
                  wx.showToast({
                    title: '授权失败',
                    icon: 'none',
                    duration: 3000
                  })
                }
              },
              fail: err => {
                reject(err)
                wx.showToast({
                  title: '弹窗异常',
                  icon: 'none',
                  duration: 3000
                })
              }
            })
            break
        }
      },
      fail: err => {
        reject(err)
        wx.showToast({
          title: '获取当前设置异常',
          icon: 'none',
          duration: 3000
        })
      }
    })
  })
}

module.exports={
    errorToShow,
    successToShow
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。