小程序登录流程
html部分
<button class="login-btn" type="primary" @click="getUserInfo">
微信用户一键登录
</button>
<view class="user">
用户名+头像
<view>
{{userName}}
</view>
<img :src="pic" alt="" style="width: 100px; height:100px;">
</view>
登录请求
export default {
name: 'Index',
data() {
return {
userName: '',
pic: ''
}
},
onLoad() {
},
methods: {
getUserInfo() {
var that = this
uni.showModal({
title: '温馨提示',
content: '亲,授权微信登录后才能正常使用小程序功能',
success(res) {
if (res.confirm) {
uni.getUserProfile({
desc: "注册用户信息使用",
lang: "zh_CN",
success: (res) => {
console.log('res', res)
that.userName = res.userInfo.nickName,
that.pic = res.userInfo.avatarUrl,
uni.login({
provider: 'weixin',
success: function(loginRes) {
console.log('loginRes.authResult', loginRes);
uni.request({
url: 'https://api.weixin.qq.com/sns/jscode2session',
method:'GET',
data: {
appid: "wxxxxxxxxxxxx",
secret: "xxxxxxxxxxxx",
js_code: loginRes.code
},
success: (cts) => {
console.log(cts);
}
});
}
});
}
})
} else {
uni.showToast({
title: '您取消了授权',
duration: 2000
});
}
}
})
},
}
}