微信中openId的获取和使用
通常是在注册的时候使用。
const type = 'customer';
wx.login({
success ({ code }) {
js2code({ code, type }).then((res) => {
value.openid = res.data.openid
value.mobile = mobile
addUser(value).then(res => {
if (res.status === 201) {
console.log(res);
showToast({ title: '注册成功' }).catch();
setTimeout(() => {
navigateBack({ detla: 1 }).catch();
}, 1000);
}
})
解释说明:
1.先通过微信的原生接口,可以获取code
2.根据后台的接口:传值:{code,type}。就可以获取到openid
3.把openid存起来,在注册时,一起返回给后台。
代码解释:
js2code:‘是封装的请求方法’
addUser:‘也是请求方法’