tangyuxian
文章65
标签33
分类5
微信小程序-常用原生功能封装

微信小程序-常用原生功能封装

对微信小程序自身特性的功能进行封装使用

/**
 * 查询授权
 * @param {*} scope 
 */
const wxGetSetting = (scope) => {
  return new Promise((resolve, reject) => {
    wx.getSetting({
      success(res) {
        if (!res.authSetting['scope.' + scope]) {
          wx.authorize({
            scope: 'scope.' + scope,
            success(res) {
              resolve(res)
            },
            fail(err) {
              wx.showToast({
                title: '请您到设置页面允许授权',
                icon: 'none'
              })
              wx.openSetting()
              reject(err)
            }
          })
        } else {
          resolve({})
        }
      },
      fail(err) {
        reject(err)
      }
    })
  })
}
/**
 * 微信登录功能
 */
const wxLogin = () => {
  return new Promise((resolve, reject) => {
    wx.login({
      success: function (res) {
        resolve(res)
      }
    })
  })
}
/**
 * 获取个人信息
 */
const wxGetUserInfo = () => {
  return new Promise((resolve, reject) => {
    wxGetSetting('userInfo').then(res => {
      wx.getUserInfo({
        success: function (res) {
          resolve(res)
        }
      })
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 * 保存图片
 * @param {*} filePath 
 */
const wxSaveImageToPhotosAlbum = (src,from="web") => {
  return new Promise((resolve, reject) => {
    wxGetSetting('writePhotosAlbum').then(res => {
      if(from == "local"){
        wx.saveImageToPhotosAlbum({
          filePath:src,
          success: function (res) {
            resolve(res)
          },fail(e){
            reject(e)
          }
        })
      }else if(from == "web"){
        let fileName = new Date().valueOf();
        wx.downloadFile({
          url:src,
          filePath:wx.env.USER_DATA_PATH + '/' + fileName + '.jpg',
          success(image){
            wx.saveImageToPhotosAlbum({
              filePath:image.filePath,
              success: function (res) {
                resolve(res)
              },fail(e){
                reject(e)
              }
            })
          },fail(err){
            reject(err)
          }
        })
      }else{
        reject("wxSaveImageToPhotosAlbum:from is unknown")
      }
    }).catch(err => {
      reject(err)
    })
  })
}

/**
 * 复制文字到剪切板
 * @param {*} data 
 */
const wxClipboardData = data => {
  return new Promise((resolve, reject) => {
    wx.setClipboardData({
      data: data,
      success(res) {
        wx.getClipboardData({
          success(res) {
            resolve(res)
          },
          fail(err) {
            reject(err)
          }
        })
      },
      fail(err) {
        reject(err)
      }
    })
  })
}

/**
 * 打开文档
 * @param {*} path 
 */
const wxOpenDocument = path =>{
  return new Promise((resolve, reject) => {
    wx.downloadFile({
      url: path,
      success: function (res) {
        const filePath = res.tempFilePath
        wx.openDocument({
          filePath: filePath,
          success: function (res) {
            resolve(res)
          },fail(err){
            reject(err)
          }
        })
      },fail(err){
        reject(err)
      }
    })
  })
}

const wxMakePhoneCall = phone =>{
  return new Promise((resolve, reject) => {
  let type = Object.prototype.toString.call(phone);
  let phoneArray = []
  if(type == "[object String]"){
      phoneArray = [phone]
  }else if(type == "[object Number]"){
    phoneArray = [phone.toString]
  }else if(type == "[object Array]"){
    phoneArray = phone;
  }else{
    reject("wxMakePhoneCall:parameter type is error");
    return;
  }
  wx.showActionSheet({
    itemList: phoneArray,
    success: function (res) {
      wx.makePhoneCall({
        phoneNumber: phone, //此号码并非真实电话号码,仅用于测试  
        success: function () {
          resolve()
        },
        fail: function (e) {
          reject(e)
        }
      })
      if (!res.cancel) {
        reject(res.tapIndex)
      }
    }
  });



})
}

/**
 * 微信拉起支付
 * @param {*} data 
 */
const wxPayment = data => {
  return new Promise((resolve, reject) => {
    let {
      timeStamp,
      nonceStr,
      paySign
    } = data;
    wx.requestPayment({
      timeStamp,
      nonceStr,
      package:data.package,
      signType: 'MD5',
      paySign,
      success(res) {
        resolve(res)
      },
      fail(res) {
        reject(res)
      }
    })
  })
}


export {
  wxGetUserInfo,
  wxLogin,
  wxSaveImageToPhotosAlbum,
  wxClipboardData,
  wxOpenDocument,
  wxMakePhoneCall,
  wxPayment
}
本文作者:tangyuxian
本文链接:https://www.tangyuxian.com/2021/02/20/%E5%89%8D%E7%AB%AF/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F/%E5%BE%AE%E4%BF%A1%E5%B0%8F%E7%A8%8B%E5%BA%8F-%E5%B8%B8%E7%94%A8%E5%8E%9F%E7%94%9F%E5%8A%9F%E8%83%BD%E5%B0%81%E8%A3%85/
版权声明:本文采用 CC BY-NC-SA 3.0 CN 协议进行许可