【小程序】使用uni-app搭建小程序環境--登錄流程


微信小程序授權一般可以獲取用戶的openid、昵稱、頭像、用戶所在省和市、性別

 

①、首先引導用戶點擊授權按鈕

1
<button open-type= "getUserInfo"  bindgetuserinfo= "bindGetUserInfo" >點擊授權</button>

 

②、然后編寫bindGetUserInfo函數:

1
2
3
4
5
6
7
8
   bindGetUserInfo(res) {
     console.log(res);
     if  (res.detail.userInfo) {
       console.log( "點擊了同意授權" );
     else  {
       console.log( "點擊了拒絕授權" );
     }
}

 

③、此時會出現一個彈出框,

  

 

④、點擊允許會返回用戶除了openid以外的基本信息:

 

⑤、接着是獲取用戶openid,需要用到微信的wx.login函數

1
2
3
4
5
6
7
8
9
10
11
12
bindGetUserInfo(res) {
   let info = res;
   if  (info.detail.userInfo) {
     console.log( "點擊了同意授權" );
     wx.login({
       success:  function  (res) {
         console.log(res);
       }
     })
   else  {
     console.log( "點擊了拒絕授權" );
   }  

此時為微信會返回一個臨時登錄憑證code,只有獲取了這個code才能進一步獲取openid 和session_key

 

⑦、請求服務端后台,讓后台請求微信再返回openid和session_key,再用微信的本地緩存保存用戶的基本信息

官方文檔建議:會話密鑰session_key 是對用戶數據進行加密簽名的密鑰。為了應用自身的數據安全,開發者服務器不應該把會話密鑰下發到小程序,也不應該對外提供這個密鑰

請求前端代碼:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
bindGetUserInfo(res) {
     let info = res;
     console.log(info);
     if  (info.detail.userInfo) {
       console.log( "點擊了同意授權" );
       wx.login({
         success:  function  (res) {
           if  (res.code) {
             wx.request({
               url:  'http://www.test.com/test' ,
               data: {
                 code: res.code,
                 nickName: info.detail.userInfo.nickName,
                 city: info.detail.userInfo.city,
                 province: info.detail.userInfo.province,
                 avatarUrl: info.detail.userInfo.avatarUrl
               },
               header: {
                 'content-type' 'application/json'  // 默認值
               },
               success:  function  (res) {
                   var  userinfo = {};
                   userinfo[ 'id' ]=res.data.id;
                   userinfo[ 'nickName' ] = info.detail.userInfo.nickName;
                   userinfo[ 'avatarUrl' ] = info.detail.userInfo.avatarUrl;
                   wx.setStorageSync( 'userinfo' , userinfo);                    
               }
             })
           else  {
             console.log( "授權失敗" );
           }
         },
       })
 
     else  {
       console.log( "點擊了拒絕授權" );
     }
   }           

 

請求后台代碼 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public  static  $appid = '你的appid' ;
public  static  $secret = '你的密鑰' ;
 
public  function  test()
{
     $params =Request::instance()->param();
     $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" .self:: $appid . "&secret=" .self:: $secret . "&js_code=" . $params [ 'code' ]. "&grant_type=authorization_code" ;
     $data = $this ->doCurl( $url );
     $info [ 'openid' ]= $data ->openid;   //獲取到用戶的openid
     $info [ 'avatar' ]= $params [ 'avatarUrl' ];
     $info [ 'province' ]= $params [ 'province' ];
     $info [ 'city' ]= $params [ 'city' ];
     $info [ 'nickName' ]= $params [ 'nickName' ];
     return  json([ 'status' =>1]);
}
 
 
public  function  doCurl( $url )
{
     $curl  = curl_init();
     // 使用curl_setopt()設置要獲取的URL地址
     curl_setopt( $curl , CURLOPT_URL,  $url );
     // 設置是否輸出header
     curl_setopt( $curl , CURLOPT_HEADER, false);
     // 設置是否輸出結果
     curl_setopt( $curl , CURLOPT_RETURNTRANSFER, 1);
     // 設置是否檢查服務器端的證書
     curl_setopt( $curl , CURLOPT_SSL_VERIFYPEER, false);
     // 使用curl_exec()將CURL返回的結果轉換成正常數據並保存到一個變量
     $data  = curl_exec( $curl );
     // 使用 curl_close() 關閉CURL會話
     curl_close( $curl );
     return  json_decode( $data );
}

相關資料:


免責聲明!

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



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