uniapp 微信授权登录

2022年6月21日 255点热度 0人点赞 0条评论
// 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 返回登录授权operId  getUserSessionKey: 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 data                    var 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 获取登录用户 code        uni.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);                        // 隐藏loading                        uni.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      }    

注意:本人作品,请勿转载

37160uniapp 微信授权登录

这个人很懒,什么都没留下

文章评论