uniapp实现小程序登录,微信一键登录,获取token,iv,code,ncryptedData
-
首先布局登录按钮(触发登录的一定要是button,button才有获取用户的方法)
<buttonopen-type="getPhoneNumber"@click.stop="loginfn"@getphonenumber.stop="onGetPhoneNumber">点击微信一键登录</button>
2.写点击button之后的逻辑,,获取iv,code,ncryptedData,调用后端接口进行登录
// 用户授权登录//首先点击登录按钮的时候获取一下code,保存到data里loginfn(){wx.login({success(res) {that.code = res.code;return;},});}//调用button自带的弹窗获取用户信息方法async onGetPhoneNumber(val) {//此时的val是用户点击了允许还是拒绝let that = this;if (val.detail.errMsg === "getPhoneNumber:ok") {//保存需要的** iv, encryptedData **const { iv, encryptedData } = val.detail;//调用后台登录的接口,传递参数const result = await that.api.getLoginApi({loginType: "1",iv,code: that.code,encryptedData,});// 登陆成功if (result.data.token) {//保存tokenuni.setStorageSync("token", result.data.token);that.token = uni.getStorageSync("token");uni.showToast({title: 登录成功",duration: 800,icon: "success",});// 刷新回到用户页页面uni.reLaunch({url: "/pages/myself/myself",});return;}}} else {uni.showToast({title: "已取消登录",duration: 500,icon: "success",});}}
发布者:admin,转转请注明出处:http://www.yc00.com/news/1692860176a651729.html
评论列表(0条)