getUserProfile 获取用户信息

2022-10-01 12:17:16
<viewclass="denglu"data-url="{{Successurl}}"bindtap="getUserInfo">登 录</view>

Successurl登录成功后跳转的url

    getUserInfo:function(e){

        var url = decodeURIComponent(e.currentTarget.dataset.url)
        // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
        // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
            success: (res) => {
                var userinfo = res.rawData
                wx.showLoading({
                    title: '登录中...',
                })
                wx.login({
                    success: function(res){
                      loginModel.login(res.code)
                      .then(loginres => {
                        loginModel.newaddUserInfo(res.rawData , loginres.data.openid , userinfo)
                        .then(result=>{
                                wx.hideLoading()
                                wx.setStorageSync("uid" ,result.data[0].userId);
                                loginModel.Success("授权成功")
                                wx.reLaunch({
                                    url:url
                                })
                    
                            })
    
                      })
                    },
                    fail:(res) => {
                        wx.hideLoading();
                    }
                  })
            },
            fail:(res) => {
                console.log(res)
                wx.showModal({
                    title: '警告',
                    content: '您点击了拒绝授权,将无法进入小程序,请授权之后再进入!!!',
                    showCancel: false,
                    confirmText: '返回授权',
                    success: function(res) {
                    }
                });
            }
        })
    }

点击之后会从底部弹起授权按钮 确认授权后 还走以前的流程 进行登录userinfo 是用户的微信名 和头像信息 因为以前的openid 获取不变 还跟以前的一样

  • 作者:梦到韩大厨
  • 原文链接:https://blog.csdn.net/x007008009/article/details/117362358
    更新时间:2022-10-01 12:17:16