uni APP 微信小程序獲取授權的微信信息


根據微信小程序開發文檔,獲取用戶信息的接口有3個UserInfo,wx.getUserInfo,wx.getUserProfile。

UserInfo比較老就不用了。wx.getUserInfo基礎庫版本 2.3.1 以后支持,但是必須用戶授權 scope.userInfo。

wx.getUserProfile 基礎庫 2.10.4 開始支持,獲取用戶信息。頁面產生點擊事件(例如 button 上 bindtap 的回調中)后才可調用,每次請求都會彈出授權窗口,用戶同意后返回 userInfo。該接口用於替換 wx.getUserInfo。

                        //用戶授權
                        wx.authorize({
                            scope: 'scope.userInfo',
                            success(res) {
                                wx.startRecord();
                            }
                        });

//查看微信小程序接口是否可用

console.log('wx.getUserProfile', wx.canIUse('getUserProfile'))

wx.getUserInfo獲取用戶信息

                       // 獲取微信用戶信息
                        wx.getUserInfo({
                            success: function(res) {
                                //用戶信息
                                const userInfo = res.userInfo;
                                //性別 0:未知、1:男、2:女
                                const {
                                    nickName,
                                    avatarUrl,
                                    gender,
                                    province,
                                    city,
                                    country
                                } = userInfo;
                                console.log(res)
                                let userUrl = userInfo.avatarUrl;//微信頭像
                                let wxname = userInfo.nickName;//微信昵稱
                            },
                            fail: res => {
                                // 獲取失敗的去引導用戶授權
                                uni.showToast({
                                    title: '您需要授權,才能獲取您的信息!'
                                });
                            }
                        });

wx.getUserProfile 獲取用戶信息,獲取openid,思路如下wx.login獲取code,然后調微信服務端接口

'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + APP_SECRET + '&js_code=' +
                                code + '&grant_type=authorization_code'
就能得到openid和sessionKey。
WXBizDataCrypt是官網js提供的解密用戶信息的。通過上述接口就能獲取sessionKey,然后可以解密微信用戶加密的信息。
                

        wx.login({
          success: function(data) {

          let code=data.code;
         //登錄微信授權
                wx.getUserProfile({
                    desc: '用於完善用戶資料',
                    lang: 'zh_CN',
                    success: function(res) {
                        console.log('wx.getUserProfile=>用戶允許了授權');
                        console.log(res.userInfo);
                        // console.log( res.rawData );  
                        console.log(res.signature);
                        console.log(res.encryptedData);
                        console.log(res.iv);
                        that.userUrl = res.userInfo.avatarUrl;
                        that.wxname = res.userInfo.nickName;
                                                
                        
                        var sessionKey = '';
                        var ed = res.encryptedData;
                        var eiv = res.iv;
                        var appId = ''; //AppID(小程序ID)
                        var APP_SECRET = ''; //AppSecret(小程序密鑰)
                        wx.request({
                            //獲取openid接口
                            url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appId + '&secret=' + APP_SECRET + '&js_code=' +
                                code + '&grant_type=authorization_code',
                            data: {},
                            method: 'GET',
                            success: function(res) {
                                console.log(res.data)
                                that.userId = res.data.openid; //獲取到的openid 
                                uni.setStorage({
                                    key: "lsopenid",
                                    data: res.data.openid
                                })
                                sessionKey = res.data.session_key; //獲取到session_key
                                //that.getPhoneNumber(res);//得到res.encryptedData后,需要解碼才能得到openId
                                var pc = new WXBizDataCrypt(appId, res.data.session_key);
                                console.log(pc)
                                var data = pc.decryptData(ed,eiv);
                                console.log(data)
                            }
                        });
                          
                        小程序端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);

                    },
                });
        }});
 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM