随手记:uniapp 小程序 微信授权登录

<view class="tfcs-content-menu">
                <u-button type="primary" shape="circle" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber" lang="zh_CN">
                    <view class="flex-center-top">
                        <u-image width="30" height="25" :src="baseImgUrl + 'icons/weixin@2x.png'"></u-image>
                        <text>微信登录</text>
                    </view>
                </u-button>

需求:点击微信登录按钮,弹出小程序微信授权,用户同意授权可以获取到用户code,传给后端,调用后端接口获取到手机号码完成授权并且跳到首页

button设置open-type=getPhoneNumber 可以获取用户手机号,可以从@getphonenumber回调中获取到用户信息 

详见:button | uni-app官网
  

getPhoneNumber(e) {
            console.log('微信授权登录', e)
            if (!e.detail.hasOwnProperty("encryptedData")) { // 没有encryptedData代表用户拒绝授权
                return
            }
            // 授权登录获取用户code
            uni.login({
                provider: 'weixin',
                success: loginRes => {
                    console.log(loginRes);
                    this.miniProgramCode = loginRes.code;
                    console.log('this.miniProgramCode', this.miniProgramCode);
                    console.log(e)
                    uni.showLoading({
                        mask: true,
                        title: '授权登录中'
                    })
                    // 调接口传递给后端参数获取手机号码-完成授权并做页面跳转
                    wxMiniProgramLogin({
                        jsCode: this.miniProgramCode,
                        code: e.detail.code,
                    }).then(res => {
                        console.log(res);
                        uni.hideLoading();
                        // uni.setStorageSync('_USER_OPENID', res.data.openid);
                        this.settingToken(res.data.token);
                        this.getUserInfo().then(response => {
                            let url = '';
                            uni.showToast({ title: '登录成功', icon: 'none'});
                            url = '/pages/home/index'
                            setTimeout(() => { uni.switchTab({ url }) }, 500);
                        })
                    })
                }
            });
        },

             
                
                
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

peachSoda7

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值