小程序工具方法

不斷更新中.....

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)請求
 * @export
 * @param {*} authorizeScope 更多scope參考
 * @param {*} modal modal彈窗參數(shù)信息
 */

function setScope (authorizeScope, modal) {
  return new Promise((resolve, reject) => {
    if (!modal) {
      modal = {
        title: '授權(quán)',
        content: '需要您設置授權(quán)已使用相應功能',
        confirmText: '設置'
      }
    }
    wx.getSetting({
      success(res) {
        // hasAuthor === undefined  表示 初始化進入,從未授權(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 在電腦模擬器會報錯,手機不會
            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: '打開設置異常',
                        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: '獲取當前設置異常',
          icon: 'none',
          duration: 3000
        })
      }
    })
  })
}

module.exports={
    errorToShow,
    successToShow
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容