// view<view class="unllBox" @tap="login"><view class="unll"><view class="me"><image src="../../../static/d.png" mode="widthFix"></image></view><view class="p">登录</view></view></view>// jsonLoad() {this.getUserInfo();},//页面初次加载 调用登录接口uni.login并且给getUserSessionKey传参getUserInfo: function() {var that = this//调用登录接口uni.login({success: function(res) {var code = res.code;that.getUserSessionKey(code);}});},onShow(){//用户USER_ID 存在的话执行getinfo()var that = this;if (uni.getStorageSync('user_id')) {this.uid = uni.getStorageSync('user_id');this.uname = uni.getStorageSync('user_name');this.getinfo();}}//传code 返回登录授权operIdgetUserSessionKey: function(code) {var that = this;uni.request({//通过url获取url: that.apiServer + 'xxxxx/getsessionkey',method: 'post',data: {code: code},header: {'Content-Type': 'application/x-www-form-urlencoded'},success: function(res) {//--init datavar data = res.data;console.log(data);if (data.errcode) {ui.showToast({title: data.errmsg,duration: 2000});return false;} else {that.sessionKey = data.session_key;console.log(data.session_key);}},fail: function(e) {wx.showToast({title: '网络异常!',duration: 2000});},});},// login()登录login() {var that = this;// 1. wx 获取登录用户 codeuni.getUserProfile({desc: 'weixin',success: (infoRes) => {console.log(infoRes)that.data1 = infoRes.userInfo;uni.login({provider: 'weixin',success: loginRes => {that.code = loginRes.code;console.log(loginRes);// 2. 将用户登录code传递到后台置换用户SessionKey、OpenId等信息uni.request({url: that.apiServer + 'xxxxxx/getsessionkey',data: {code: that.code,},method: 'post',header: {'Content-Type': 'application/x-www-form-urlencoded'},success: codeRes => {console.log(codeRes);that.openId = codeRes.data.openid;that.sessionKey = codeRes.data.session_key;// 3.通过 openId 判断用户是否授权uni.request({url: that.apiServer +'memberAjax/authlogin',data: {openid: that.openId,nickname: that.data1.nickName,gender: that.data1.gender,headimgurl: that.data1.avatarUrl,province: that.data1.province,city: that.data1.city},method: 'post',header: {'Content-Type': 'application/x-www-form-urlencoded'},success: openIdRes => {console.log(openIdRes);// 隐藏loadinguni.hideLoading();// 还没授权登录、请先授权然后登录if (openIdRes.data.status == 0) {// 提示消息、让用户授权uni.showToast({title: openIdRes.data.err,icon: 'none'});}// 已经授权了、查询到用户的数据了if (openIdRes.data.status == 1) {// 用户信息写入缓存uni.showToast({title: '登录授权成功',icon: 'success',duration: 1500,complete: function() {console.log(openIdRes.data.arr);setTimeout(function() {that.data2=openIdRes.data.arr;that.bindmobile =true;},1500);} //接口调用结束的回调函数});}},fail: () => {uni.showToast({title: '获取授权信息失败',icon: 'none'});return false;}});},fail: () => {uni.showToast({title: '获取 SesssionKey OpenId 失败',icon: 'none'});return false;}});},fail: () => {uni.showToast({title: '获取 code 失败',icon: 'none'});return false;}});return false;},fail: () => {uni.showToast({title: '获取用户信息失败',icon: 'none'});}});return false}
注意:本人作品,请勿转载
文章评论