微信授权:这一步获取到code的
获取用户信息:获取微信用户的信息userinfo(头像,昵称等)
发送请求:将第一步获取到的code和第二步的用户信息发送请求
//在wxLogin.js文件写三个微信授权 获取用户信息 发送请求三个函数//1.微信授权[uni.login()]
function wxMiniLogin(onSuccess, onFail, onComplete) {
uni.login({
provider: 'weixin',
scopes: '', //支付宝需要
timeout: 5000,
success: (res) => {
if (onSuccess) {
onSuccess(res)
}
},
fail: () => {
if (onFail) {
onFail()
}
},
complete: () => {
if (onComplete) {
onComplete()
}
}
})
}
function getUserInfo(onSuccess, onFail, onComplete) {
uni.getUserInfo({
provider: true,
withCredentials: true,
lang: 'zh_CN',
timeout: 5000,
success: (res) => {
if (onSuccess) {
onSuccess(res)
}
},
fail: () => {
if (onFail) {
onFail()
}
},
complete: () => {
if (onComplete) {
onComplete()
}
}
})
}
function pveLogin(obj,onSuccess) {
let url = $memberAPI.WX_LOGIN;
HTTP.request(url, HTTP.Method.POST, obj, (res) => {
STORE.setUserInfo(res.data)
if(onSuccess){
onSuccess(res)
}
}, () => {
}, () => {
})
}
在vue页面中
WxLogin.wxMiniLogin((res) => {
this.loginCode = res.code
WxLogin.getUserInfo((info) => {
this.pveLogin(info)
}, () => {
uni.hideLoading()
})
}, () => {
uni.hideLoading()
uni.showToast({
title: '微信登录失败',
icon: 'none'
})
})
在this.pveLogin(info) 方法中
pveLogin(info) {
if (info.encryptedData) {
let obj = {}
obj.code = this.loginCode
obj.encryptedData = info.encryptedData
obj.iv = info.iv
obj.rawData = info.rawData
obj.signature = info.signature
WxLogin.pveLogin(obj, (res) => {
uni.showToast({
title: '微信登录成功',
icon: 'none'
})
this.checkPass()
uni.hideLoading();
}, () => {
console.log("拒绝授权")
uni.hideLoading();
}, () => {
uni.hideLoading()
})
} else {
uni.hideLoading()
uni.showToast({
title: '微信登录失败',
icon: 'none'
})
}
}