微信小程序(授权登录)(不建议,点击主页观看更新版授权登录)

HTML

<view>

    <view class='headView'>

        <view class='headImageView'>

          </view>

          <button bindtap="getUserProfile" wx:if="{{ showInfo}}"> 获取头像昵称 </button>

          <image bindtap="bindViewTap" class="userinfo-avatar" src="/image/8482661.jpg" mode="cover" wx:if="{{ pic }}" style="width: 200rpx;height: 200rpx;padding-left:40% ;"></image>

        <!-- <button class='authBtn' type='primary' open-type='getUserInfo' bindgetuserinfo='auth' wx:if="{{shoeAuth}}">授权登录</button> -->

        <view class='titleText' style="text-align: center;">{{ userInfo.nick_name }}</view>

      </view>

</view>

JS

 getUserProfile(e) {

        wx.getUserProfile({

          success: (res) => {

            // console.log(res);

            let nick_name = res.userInfo.nickName;

            let avatar_url = res.userInfo.avatarUrl;

            let that =this;

            wx.login({

               //获取code 来与服务器交换 获取openid

              success(ret){

                // console.log(ret)

                let code = ret.code;

                wx.request({

                  url: 'http://www.week3.com/login',

                  data:{

                    code:code,

                    nick_name:nick_name,

                  },

                  success(e){

                    // console.log(e.data.data)

                    let uid=e.data.data;

                    //将用户信息保存到缓存里面

                    wx.setStorage({

                      key:"uid",

                      data:uid

                    })

                    wx.showToast({

                        title: '登录成功!',

                      })

                    that.setData({

                        userInfo:e.data.data,

                        showInfo:false,

                        pic:true

                    })

                  },

                  fail(res){

                    wx.showToast({

                      title: '请求失败',

                      icon:'error'

                    })

                  }

                })

              }

            })

          },

          fail(res){

            wx.showToast({

              title: '授权失败',

              icon:"error"

            })

          }

        })

      }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值