微信最新登录按钮更新

2022-09-27 10:06:57

uniapp开发小程序对于新的获取用户名和手机号方式

                       <!-- 旧版本方式 -->
                        <!-- #ifdef MP-WEIXIN -->
                        <block v-if="canIUseProfile==false">
                            <button class="cu-btn bg-green margin-left bottom" type="primary" @getuserinfo='getuserinfo'
                                open-type="getUserInfo" v-show="!uname"
                                style="position: absolute; top: -10rpx;left: 50%;">立即登录</button>
                        </block>

                        <!-- 新版本方式 -->
                        <block v-else>
                            <button class="cu-btn bg-green margin-left bottom" type="primary" @tap='getuserinfo'
                                v-show="!uname" style="position: absolute; top: -10rpx;left: 50%;">立即登录</button>
                        </block>
                        <!-- #endif -->

            export default {
                  data() {
                        return {
                // 用户小程序获取用户信息新旧修改
                canIUseProfile: false;

          },

methods:{

//#ifdef MP-WEIXIN
            getuserinfo(e) {
                if (this.canIUseProfile == false) {
                    console.log(e)
                    this.modalName = null;
                    let _this = this;
                    _this.uname = e.detail.userInfo.nickName;
                    _this.avatarUrl = e.detail.userInfo.avatarUrl
                    uni.setStorageSync('nickname', e.detail.userInfo.nickName);
                    uni.setStorageSync('avatarUrl', e.detail.userInfo.avatarUrl);
                } else {
                    var that = this;
                    wx.getUserProfile({
                        desc: '用于完善用户资料',
                        lang: 'zh_CN',
                        success: function(res) {
                            console.log(res.userInfo)
                            that.uname = res.userInfo.nickName;
                            that.avatarUrl = res.userInfo.avatarUrl
                            uni.setStorageSync('nickname', res.userInfo.nickName);
                            uni.setStorageSync('avatarUrl', res.userInfo.avatarUrl);
                            /* 以下是网友说的,我还没来得及测试:
                            按新方案的意思,encryptedData,iv,rawData,signature这几个属性用不上了。
                            小程序端wx.getUserProfile()获取到userInfo,wx.login()获取到code,
                            这两个传给后端,code通过code2session接口获取到unionid,openid,session_key等;
                            原有的拿session_key解密encryptedData的逻辑不需要了,毕竟encryptedData也只是比userInfo多了openid和unionid。
                            */

                        },
                        fail: function(res) {
                            console.log('wx.getUserProfile=>用户拒绝了授权');
                            console.log(res);

                        },
                    });

                }
            },
            // #endif

}

  • 作者:文仔酱酱酱
  • 原文链接:https://blog.csdn.net/baidu_41899377/article/details/116005264
    更新时间:2022-09-27 10:06:57