封装原生微信小程序API
与page平级新建文件夹
utils/asyncWx.js
在需要的页面按需引入:
import {getSetting,chooseAddress} from "../../utils/asyncWx.js"
// 获取用户的唯一标识opendid
export function getStorageOpenid() {
const openId = wx.getStorageSync('openId')
if (openId) {
return openId
} else {
return ''
}
}
export default {
getStorageOpenid
}
/**
* promise 形式 getSetting 获取权限状态
*/
export const getSetting = () => {
return new Promise((resolve, reject) => {
wx.getSetting({
success: (result) => {
resolve(result);
},
fail: (err) => {
reject(err);
}
});
})
}
/**
* promise 形式 chooseAddress获取收货地址
*/
export const chooseAddress = () => {
return new Promise((resolve, reject) => {
wx.chooseAddress({
success: (result) => {
resolve(result);
},
fail: (err) => {
reject(err);
}
});
})
}
/**
* promise 形式 openSetting 打开授权页面
*/
export const openSetting = () => {
return new Promise((resolve, reject) => {
wx.openSetting({
success: (result) => {
resolve(result);
},
fail: (err) => {
reject(err);
}
});
})
}
/**
* promise 形式 showModal
* @param {object} param0
*/
export const showModal = ({ content }) => {
return new Promise((resolve, reject) => {
wx.showModal({
title: '提示',
content: content,
success: (res) => {
resolve(res);
},
fail: (err) => {
reject(err);
}
});
})
}
/**
* promise 形式 showToast
* @param {object} param0
*/
export const showToast = ({ title }) => {
return new Promise((resolve, reject) => {
wx.showToast({
title: title,
icon: 'none',
success: (res) => {
resolve(res);
},
fail: (err) => {
reject(err);
}
});
})
}
/**
* promise 形式 login
*
*/
export const login = () => {
return new Promise((resolve, reject) => {
wx.login({
timeout: 10000,
success: (res) => {
resolve(res);
},
fail: (err) => {
reject(err);
}
});
})
}
/**
* promise 形式的小程序的微信支付
* @param {object} pay 支付所必要的参数
*/
export const requestPayment = (pay) => {
return new Promise((resolve, reject) => {
wx.requestPayment({
...pay,
success: (res) => {
resolve(res);
},
fail: (err) => {
reject(err);
}
});
})
}