对微信小程序自身特性的功能进行封装使用
/**
* 查询授权
* @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
}