微信小程序實現登錄功能 (第一種模式)


------------恢復內容開始------------

第一種模式:設置小程序啟動頁為登錄頁

1.wxml代碼(布局的,最終要的是按鈕)

 

 1 <view class='header'>
 2   <image src='/image/1.png'></image> //圖片自己引入
 3 </view>
 4 <view class='content'>
 5   <view>申請獲取以下權限</view>
 6   <text>獲得你的公開信息(昵稱,頭像等)</text>
 7 </view>
 8 <button class='bottom' type='primary' open-type="getUserInfo" bind:getuserinfo="bindGetUserInfo">
 9   授權登錄
10 </button>

 

 

 

 

2.butten按鈕的設置

 

 注:上圖來源於網絡截圖,如有冒犯請聯系撤回,謝謝!

  

給按鈕綁定觸發事件 放可進入授權登錄的小窗口

 

 

3.綁定觸發事件后在js文件中寫相對應的方法(以下是login.js中的全部代碼)

 

 1 Page({
 2   data: {
 3     //判斷小程序的API,回調,參數,組件等是否在當前版本可用。
 4     canIUse: wx.canIUse('button.open-type.getUserInfo'),
 5     isHide: false
 6   },
 7   onLoad: function () {
 8     //頁面初次加載判斷用戶是否授權過 去緩存中讀取是否有
 9     wx.getStorage({
10       key: 'openid',
11       success(res) {
12        //判斷是否有openid  如果不為空則跳轉到首頁
13         if (res.data != "") {
14           //跳轉到 tabBar 頁面,並關閉其他所有非 tabBar 頁面
15           wx.switchTab({
16             url: '/pages/index/index'
17           })
18         }
19       }
20     })
21   },
22   bindGetUserInfo(e) {
23     console.log(e)
24     //如果不允許  則沒有userInfo這個值
25     //獲取用戶的昵稱 判斷用戶點擊的是允許還是拒絕
26     if (e.detail.userInfo) {
27       //如果用戶允許,則能得到userInfo
28       console.log(e.detail.userInfo)
29       //獲取用戶的昵稱 
30       let nickname = e.detail.userInfo.nickName
31       // console.log(nickname)
32       //獲取用戶的昵稱  去獲取code
33       wx.login({
34 
35         success(res) {
36           if (res.code) {
37             //得到了code值  攜帶參數發送請求
38             console.log(res.code)
39             //發起網絡請求
40             wx.request({
41               url: 'http://www.xxxxxx.com/index.php/users/index/index',
42               data: {
43                 code: res.code,
44                 nickname: nickname,
45               },
46               dataType: "json",
47               method: "GET",
48               success(res) {
49                 console.log(res.data.data.openid)
50                 console.log(res.data.code)
51                 //判斷是否授權成功
52                 if (res.data.code == 200) {
53                   //將用戶的openid緩存起來
54                   wx.setStorage({
55                     key: "openid",
56                     data: res.data.data.openid
57                   })
58                   //頁面跳轉
59                   wx.switchTab({
60                     //跳轉地址
61                     url: '/pages/index/index',
62                   })
63                 } else {
64 
65                 }
66               }
67             })
68           } else {
69             console.log('登錄失敗!' + res.errMsg)
70           }
71         }
72       })
73 
74     }
75 
76   }
77 
78 })

 

4.php中的邏輯處理 如何獲取用戶信息

 1     public function index()
 2     {
 3         //獲取到code值
 4         $code=input('code');
 5        //已知appid 與 secret   從自己的微信公眾平台注冊獲取
 6         $appid="xxxxxxx";
 7         $secret="xxxxxxxxx";
 8         $url="https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";
 9         //通過curl函數獲取用戶的oppid和sessionkey  將其進行添加入庫
10         $res=curl_request($url,false,[],true);
11         //對 JSON 格式的字符串進行解碼,轉換為 PHP 變量   此次將 JSON 格式的字符串轉化為數組
12         $res=\Qiniu\json_decode($res,true);
13         //判斷用戶是否已經授權過
14         $user=\app\wxxcx\model\Users::where('openid',$res['openid'])->find();
15         //如果用戶已將存在則直接返回數據
16         if ($user){
17             return json(['code'=>200,'msg'=>'success','data'=>$user]);
18         }
19         //拼接數組入庫保存
20         $info=[
21             'openid'=>$res['openid'],
22             'sessionkey'=>$res['session_key']
23         ];
24         //添加入庫
25         $data=\app\wxxcx\model\Users::create($info)->toArray();
26         //將數據返回
27         if ($data){
28             return json(['code'=>200,'msg'=>'success','data'=>$data]);
29         }else{
30             return json(['code'=>500,'msg'=>'error','data'=>""]);
31         }
32 
33     }

 

 

 

5.wxss

/**index.wxss**/
.userinfo {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.userinfo-avatar {
  width: 128rpx;
  height: 128rpx;
  margin: 20rpx;
  border-radius: 50%;
}

.userinfo-nickname {
  color: #aaa;
}

.usermotto {
  margin-top: 200px;
}

 

 注釋:上面這種做法的好處的,設置登錄頁為默認啟動頁,而tabbar並未設置該頁,所以說不用考慮tabbar在未登錄狀態下的是否顯示問題,后端處理成功后考慮到后面會驗證用戶是否登錄,所以要將用戶的openid存入緩存中,等待隨時取值

 

以上是以啟動頁來獲取用戶個人信息,微信小程序還提供了可以在app.js中通過獲取當前瀏覽者的code進而獲取openid和session_key

 

 

 

 


免責聲明!

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



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