• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    微信小程序如何獲取用戶信息

    來源:懂視網 責編:小采 時間:2020-11-27 22:20:45
    文檔

    微信小程序如何獲取用戶信息

    微信小程序如何獲取用戶信息:最近在研究微信小程序怎么玩的。接觸后發現好多的坑。 比如在瀏覽器中我們可以通過document.getElementById 獲取到頁面的DOM對象。而在微信小程序中是獲取不到DOM對象的。document.getElementById() 直接報錯 getElementById not f
    推薦度:
    導讀微信小程序如何獲取用戶信息:最近在研究微信小程序怎么玩的。接觸后發現好多的坑。 比如在瀏覽器中我們可以通過document.getElementById 獲取到頁面的DOM對象。而在微信小程序中是獲取不到DOM對象的。document.getElementById() 直接報錯 getElementById not f

    最近在研究微信小程序怎么玩的。接觸后發現好多的坑。

    比如在瀏覽器中我們可以通過document.getElementById 獲取到頁面的DOM對象。而在微信小程序中是獲取不到DOM對象的。document.getElementById() 直接報錯 getElementById not function 我也是醉了。不支持這個好多有趣的功能不能實現了。
    言歸正傳,我談下獲取用戶信息的感想。

    有兩種獲取用戶信息的方案。
    1、不包含敏感信息openId 的json對象(包含:nickname、avatarUrl等基本信息)
    2、包含敏感信息openId的基本信息。

    第一種獲取方案

    1、首先調用wx.login()接口 讓用戶授權驗證,也就是我們肉眼觀察到的,你是否對xxxxx授權這種信息。
    2、用戶成功授權后,調用wx.getUserInfo() 接口獲取用戶信息。

    完整代碼如下

    wx.login({
     success:function(){
     wx.getUserInfo({
     success:function(res){
     var simpleUser = res.userInfo;
     console.log(simpleUser.nickName);
     }
     });
     }
    });
    
    

    第二種比較復雜了,需要與后臺進行交互才能獲得userInfo,但是這種方案獲得的數據是完整的(包含openId)。

    1、調用wx.login()接口 授權 在success 成功函數的參數中包含code。
    2、調用wx.getUserInfo()接口success 函數中包含encryptedData、iv
    3、將上述參數傳給后臺解析,生成userInfo

    代碼如下
    js

    var request = require("../../utils/request.js");
    
    wx.login({
     success:function(res_login){
     if(res_login.code)
     {
     wx.getUserInfo({
     withCredentials:true,
     success:function(res_user){
     var requestUrl = "/getUserApi/xxx.php";
     var jsonData = {
     code:res_login.code,
     encryptedData:res_user.encryptedData,
     iv:res_user.iv
     };
     request.httpsPostRequest(requestUrl,jsonData,function(res){
     console.log(res.openId);
     });
     }
     })
     }
     }
     })
    
    

    后臺解析

    /**
     * 獲取粉絲信息
     * 其中的參數就是前端傳遞過來的
     */
    public function wxUserInfo($code,$encryptedData,$iv)
    {
     $apiUrl = "https://api.weixin.qq.com/sns/jscode2session?appid={$this->wxConfig['appid']}&secret={$this->wxConfig['appsecret']}&js_code={$code}&grant_type=authorization_code";
    
     $apiData = json_decode(curlHttp($apiUrl,true),true);
    
     if(!isset($apiData['session_key']))
     {
     echoJson(array(
     "code" => 102,
     "msg" => "curl error"
     ),true);
     }
    
     $userInfo = getUserInfo($this->wxConfig['appid'],$apiData['session_key'],$encryptedData,$iv);
    
     if(!$userInfo)
     {
     echoJson(array(
     "code" => 105,
     "msg" => "userInfo not"
     ));
     }
    
     //$userInfo = json_decode($userInfo,true);
    
     //載入用戶服務
     //$userService = load_service("User");
    
     //$userService->checkUser($this->projectId,$userInfo);
    
     echo $userInfo; //微信響應的就是一個json數據
    }
    
    

    getUserInfo function 其中wxBizDataCrypt.php 就是微信官方提供的素材包

    curlHttp 函數是一個自定函數 該函數的源碼查看我的這篇文章curlHttp

    //獲取粉絲信息
    function getUserInfo($appid,$sessionKey,$encryptedData,$iv){
     require_once ROOTPATH . "/extends/wxUser/wxBizDataCrypt.php";
     $data = array();
     $pc = new WXBizDataCrypt($appid, $sessionKey);
     $errCode = $pc->decryptData($encryptedData, $iv, $data );
    
     if ($errCode == 0) {
     return $data;
     } else {
     return false;
     }
    }
    
    

    自己寫的小工具 request.js

    var app = getApp();
    
    //遠程請求
    var __httpsRequest = {
    
     //http 請求
     https_request : function(obj){
     wx.request(obj);
     },
    
     //文件上傳
     upload_request : function(dataSource){
     wx.uploadFile(dataSource);
     }
    };
    
    module.exports = {
     //執行異步請求get
     httpsRequest:function(obj){
     var jsonUrl = {};
     jsonUrl.url = obj.url;
     if(obj.header)jsonUrl.header=obj.header;
     if(obj.type)
     jsonUrl.method = obj.type;
     else
     jsonUrl.method="GET";
     if(obj.data)jsonUrl.data = obj.data;
     obj.dataType?(jsonUrl.dataType=obj.dataType):(jsonUrl.dataType="json");
    
     jsonUrl.success = obj.success;
    
     jsonUrl.data.projectId = app.globalData.projectId;
    
     __httpsRequest.https_request(jsonUrl);
     },
    
     //get 請求
     httpsGetRequest:function(req_url,req_obj,res_func)
     {
     var jsonUrl = {
     url:app.globalData.host + req_url,
     header:{"Content-Type":"application/json"},
     dataType:"json",
     method:"get",
     success:function(res)
     {
     typeof res_func == "function" && res_func(res.data);
     }
     }
    
     if(req_obj)
     {
     jsonUrl.data = req_obj;
     }
    
     jsonUrl.data.projectId = app.globalData.projectId;
    
     __httpRequest.https_request(jsonUrl);
     },
    
     //post 請求
     httpsPostRequest:function(req_url,req_obj,res_func)
     {
     var jsonUrl = {
     url:app.globalData.host + req_url,
     header:{"Content-Type":"application/x-www-form-urlencoded"},
     dataType:"json",
     method:"post",
     success:function(res)
     {
     typeof res_func == "function" && res_func(res.data);
     }
     }
    
     if(req_obj)
     {
     jsonUrl.data = req_obj;
     }
    
     jsonUrl.data.projectId = app.globalData.projectId;
    
     __httpsRequest.https_request(jsonUrl);
     },
    
     //文件上傳
     httpsUpload:function(uid,fileDataSource,res_func)
     {
     dataSource = {
     url:app.globalData.host + req_url,
     header:{
     "Content-Type":"multipart/form-data"
     },
     dataType:"json",
     formData : {
     "uid" : uid
     },
     filePath : fileDataSource,
     name : "fileObj",
     success:function(res){
     typeof res_func == "function" && res_func(res);
     }
     }
    
     __httpsRequest.upload_request(dataSource);
     }
    };
    
    

    app.globalData.host 就是域名地址如 https://xxxxx.com;

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    微信小程序如何獲取用戶信息

    微信小程序如何獲取用戶信息:最近在研究微信小程序怎么玩的。接觸后發現好多的坑。 比如在瀏覽器中我們可以通過document.getElementById 獲取到頁面的DOM對象。而在微信小程序中是獲取不到DOM對象的。document.getElementById() 直接報錯 getElementById not f
    推薦度:
    標簽: 微信 信息 小程序
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 久久99精品国产麻豆婷婷| 欧美国产成人久久精品| 久久久久久久亚洲精品| 国产欧美亚洲精品A| 久久se精品一区精品二区国产| 精品国产一区二区三区不卡| 无码精品人妻一区| 无码人妻精品一区二区三区久久| 国产精品欧美日韩| www.精品| 国产成人精品久久一区二区三区| 中日韩产精品1卡二卡三卡| Xx性欧美肥妇精品久久久久久 | 蜜芽亚洲av无码精品色午夜| 精品久久人人妻人人做精品 | 国产精品免费久久久久电影网| 99re热这里只有精品视频中文字幕| 无码日韩精品一区二区免费暖暖| 欧美成人精品欧美一级乱黄一区二区精品在线 | 尤物yw午夜国产精品视频 | 国产精品综合专区中文字幕免费播放 | 一级成人精品h| 久久精品国产精品青草app| 成人精品视频99在线观看免费| 无码精品视频一区二区三区| 伊人久久精品影院| 无码AⅤ精品一区二区三区| 麻豆国产精品VA在线观看不卡| 国产亚洲精品线观看动态图| 国产91精品黄网在线观看| 2023国产精品自拍| 国产观看精品一区二区三区| 2021国产精品视频网站| segui久久国产精品| 国产精品国产三级在线高清观看| 99热成人精品免费久久| 99爱在线视频这里只有精品| 国产A∨免费精品视频| 国产精品爱搞视频网站 | 国产精品 91 第一页| 国产欧美日本精品|