tp5集成淘寶,微信,網易,新浪等第三方登錄


tp5集成淘寶,微信,網易,新浪等第三方登錄

一、總結

一句話總結:

接口 鏈接

實現的話就是這些平台給的一個接口(鏈接),你通過這些接口登錄進去之后,它會給你返回用戶名,頭像之類的信息,我們的網站存儲這些信息就好

比如微信登錄

121     /** 122  * 微信登錄 123  * @author tangtanglove 124 */ 125 public function wechat() 126  { 127 $state = input('get.state'); 128 if ($state != session('state')) { 129 return $this->error('授權出錯!'); 130  } 131 $config = config('think_sdk_wechat'); 132 $response_type = input('get.response_type'); 133 $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$config['app_id'].'&secret='.$config['app_secret'].'&code='.$response_type.'&grant_type=authorization_code'; 134 $result = json_decode(httpMethod($url)); 135 $openid = $result['openid']; 136 $access_token = $result['access_token']; 137 138 // 獲取用戶信息 139 $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid; 140 $wechatInfo = json_decode(httpMethod($url)); 141 142 if (empty($wechatInfo['openid'])) { 143 return $this->error('錯誤!'); 144  } 145 146 if (empty($openid)) { 147 return $this->error('錯誤!'); 148  } 149 150 $where['openid'] = $openid; 151 $userInfo = Db::name('Users')->where($where)->find(); 152 if (!empty($userInfo) && $userInfo['status']!=1) { 153 return $this->error('用戶被禁用!'); 154  } 155 156 if (!empty($userInfo)) { 157 $session['uid'] = $userInfo['id']; 158 $session['username'] = $userInfo['username']; 159 $session['nickname'] = $userInfo['nickname']; 160 $session['mobile'] = $userInfo['mobile']; 161 $session['last_login']= $userInfo['last_login']; 162 // 記錄用戶登錄信息 163 session('index_user_auth',$session); 164 return $this->success('登陸成功!',url('index/user/userCenter')); 165 } else { 166 $data['openid'] = $openid; 167 $data['nickname'] = $wechatInfo['nickname']; 168 $data['uuid'] = create_uuid(); 169 $data['salt'] = create_salt(); 170 $data['regdate'] = time(); 171 $data['last_login'] = $data['regdate']; 172 $data['status'] = '1'; 173 $result = Db::name('Users')->insert($data); 174 if ($result) { 175 $openid = $result['openid']; 176 $session['uid'] = Db::getLastInsID(); 177 $session['nickname'] = $wechatInfo['nickname']; 178 $session['last_login']= $userInfo['last_login']; 179 // 記錄用戶登錄信息 180 session('index_user_auth',$session); 181 return $this->success('登陸成功!',url('index/user/userCenter')); 182 } else { 183 return $this->error('錯誤!'); 184 } 185 } 186 }

 

1、如何用thinkphp實現第三方登錄?、

搜索

比如你要用thinkphp實現第三方登錄,直接在網上搜索thinkphp第三方登錄,代碼大堆

搜索的關鍵詞

需要的功能直接搜索就好,網上很多,可以多參考幾個來做

 

2、微信第三方登錄接口?

搜索

直接搜索“微信第三方登錄接口”,搜索到網站,照着里面的文檔來,非常簡單的

WeChat Open Platform
https://open.weixin.qq.com/

 

3、如何實現第三方支付?

網址 參考文檔
搜索 代碼

照着參考文檔來,非常簡單

微信支付

微信支付 - 中國領先的第三方支付平台 | 微信支付提供安全快捷的支付方式
https://pay.weixin.qq.com/index.php/core/home/login?return_url=%2F

 

 

支付寶支付

 開放平台文檔中心
https://docs.open.alipay.com/200

 

參考文檔開發是一份方面,直接找代碼的話就更加快了,而且可以多找幾個進行參考

 

 

二、tp5集成淘寶,微信,網易,新浪等第三方登錄

參考的thinkphp官網上面的代碼

  1 namespace app\index\controller;
  2 
  3 use think\Controller;
  4 use think\Request;
  5 use think\Db;
  6 use org\ThinkOauth;
  7 
  8 /**
  9  * 第三方登錄
 10  * @author  tangtnglove <dai_hang_love@126.com>
 11  */
 12 class OpenAuth extends Base
 13 {
 14     /**
 15      * 統一登錄方法
 16      * @author tangtanglove
 17      */
 18     public function login($type = null){
 19 
 20         if (empty($type)) {
 21             return $this->error('參數錯誤');
 22         }
 23         if ($type == 'wechat') {
 24             // 生成一個token
 25             $state = md5(time());
 26             // 儲存token
 27             session('state',$state);
 28             $config = config('think_sdk_wechat');
 29             $wechatUrl = 'https://open.weixin.qq.com/connect/qrconnect?appid='.$config['app_id']
 30             .'&redirect_uri='.$config['callback']
 31             .'&response_type=code&scope=snsapi_login&state='.$state
 32             .'#wechat_redirect';
 33 
 34             return $this->redirect($wechatUrl);
 35         } else {
 36 
 37             //加載ThinkOauth類並實例化一個對象
 38             import('org.util.thinksdk.ThinkOauth');
 39             $sns  = ThinkOauth::getInstance($type);
 40 
 41             //跳轉到授權頁面
 42             return $this->redirect($sns->getRequestCodeURL());
 43         }
 44 
 45     }
 46 
 47     /**
 48      * 授權回調
 49      * @author tangtanglove
 50      */
 51     public function callback($type = null, $code = null){
 52         (empty($type) || empty($code)) && $this->error('參數錯誤');
 53         
 54         //加載ThinkOauth類並實例化一個對象
 55         import('org.util.thinksdk.ThinkOauth');
 56         $sns  = ThinkOauth::getInstance($type);
 57 
 58         //騰訊微博需傳遞的額外參數
 59         $extend = null;
 60         if($type == 'tencent'){
 61             $extend = array('openid' => input('openid'), 'openkey' => input('openkey'));
 62         }
 63 
 64         //請妥善保管這里獲取到的Token信息,方便以后API調用
 65         //調用方法,實例化SDK對象的時候直接作為構造函數的第二個參數傳入
 66         //如: $qq = ThinkOauth::getInstance('qq', $token);
 67         $token = $sns->getAccessToken($code , $extend);
 68         //獲取當前登錄用戶信息
 69         if(is_array($token)){
 70             //$user_info = $this->$type($token);
 71             $openAuthInfo = call_user_func_array(array($this,$type), array($token));
 72             // echo("<h1>恭喜!使用 {$type} 用戶登錄成功</h1><br>");
 73             // echo("授權信息為:<br>");
 74             // dump($token);
 75             // echo("當前登錄用戶信息為:<br>");
 76             // dump($openAuthInfo);
 77 
 78             if (empty($openAuthInfo)) {
 79                 return $this->error('錯誤!');
 80             }
 81 
 82             $where[$type.'_openid'] = $token['openid'];
 83             $userInfo = Db::name('Users')->where($where)->find();
 84             if (!empty($userInfo) && $userInfo['status']!=1) {
 85                 return $this->error('用戶被禁用!');
 86             }
 87 
 88             if (!empty($userInfo)) {
 89                 $session['uid']       = $userInfo['id'];
 90                 $session['username']  = $userInfo['username'];
 91                 $session['nickname']  = $userInfo['nickname'];
 92                 $session['mobile']    = $userInfo['mobile'];
 93                 $session['last_login']= $userInfo['last_login'];                                            
 94                 // 記錄用戶登錄信息
 95                 session('index_user_auth',$session);
 96                 return $this->success('登陸成功!',url('index/user/userCenter'));
 97             } else {
 98                 $data[$type.'_openid']  = $token['openid'];
 99                 $data['nickname']       = $openAuthInfo['nick'];
100                 $data['uuid']           = create_uuid();
101                 $data['salt']           = create_salt();
102                 $data['regdate']        = time();
103                 $data['last_login']     = $data['regdate'];
104                 $data['status']         = '1';
105                 $result = Db::name('Users')->insert($data);
106                 if ($result) {
107                     $openid = $result['openid'];
108                     $session['uid']       = Db::getLastInsID();
109                     $session['nickname']  = $openAuthInfo['nick'];
110                     $session['last_login']= $userInfo['last_login'];
111                     // 記錄用戶登錄信息
112                     session('index_user_auth',$session);
113                     return $this->success('登陸成功!',url('index/user/userCenter'));
114                 } else {
115                     return $this->error('錯誤!');
116                 }
117             }
118         }
119     }
120 
121     /**
122      * 微信登錄
123      * @author tangtanglove
124      */
125     public function wechat()
126     {
127         $state = input('get.state');
128         if ($state != session('state')) {
129             return $this->error('授權出錯!');
130         }
131         $config = config('think_sdk_wechat');
132         $response_type = input('get.response_type');
133         $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$config['app_id'].'&secret='.$config['app_secret'].'&code='.$response_type.'&grant_type=authorization_code';
134         $result = json_decode(httpMethod($url));
135         $openid       = $result['openid'];
136         $access_token = $result['access_token'];
137 
138         // 獲取用戶信息
139         $url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid;
140         $wechatInfo = json_decode(httpMethod($url));
141 
142         if (empty($wechatInfo['openid'])) {
143             return $this->error('錯誤!');
144         }
145 
146         if (empty($openid)) {
147             return $this->error('錯誤!');
148         }
149 
150         $where['openid'] = $openid;
151         $userInfo = Db::name('Users')->where($where)->find();
152         if (!empty($userInfo) && $userInfo['status']!=1) {
153             return $this->error('用戶被禁用!');
154         }
155 
156         if (!empty($userInfo)) {
157             $session['uid']       = $userInfo['id'];
158             $session['username']  = $userInfo['username'];
159             $session['nickname']  = $userInfo['nickname'];
160             $session['mobile']    = $userInfo['mobile'];
161             $session['last_login']= $userInfo['last_login'];                                            
162             // 記錄用戶登錄信息
163             session('index_user_auth',$session);
164             return $this->success('登陸成功!',url('index/user/userCenter'));
165         } else {
166             $data['openid']         = $openid;
167             $data['nickname']       = $wechatInfo['nickname'];
168             $data['uuid']           = create_uuid();
169             $data['salt']           = create_salt();
170             $data['regdate']        = time();
171             $data['last_login']     = $data['regdate'];
172             $data['status']         = '1';
173             $result = Db::name('Users')->insert($data);
174             if ($result) {
175                 $openid = $result['openid'];
176                 $session['uid']       = Db::getLastInsID();
177                 $session['nickname']  = $wechatInfo['nickname'];
178                 $session['last_login']= $userInfo['last_login'];
179                 // 記錄用戶登錄信息
180                 session('index_user_auth',$session);
181                 return $this->success('登陸成功!',url('index/user/userCenter'));
182             } else {
183                 return $this->error('錯誤!');
184             }
185         }
186     }
187 
188     //登錄成功,獲取騰訊QQ用戶信息
189     public function qq($token){
190         //加載ThinkOauth類並實例化一個對象
191         import('org.util.thinksdk.ThinkOauth');
192         $qq   = ThinkOauth::getInstance('qq', $token);
193         $data = $qq->call('user/get_user_info');
194         if($data['ret'] == 0){
195             $userInfo['type'] = 'QQ';
196             $userInfo['name'] = $data['nickname'];
197             $userInfo['nick'] = $data['nickname'];
198             $userInfo['head'] = $data['figureurl_2'];
199             return $userInfo;
200         } else {
201             throw_exception("獲取騰訊QQ用戶信息失敗:{$data['msg']}");
202         }
203     }
204 
205     //登錄成功,獲取騰訊微博用戶信息
206     public function tencent($token){
207         //加載ThinkOauth類並實例化一個對象
208         import('org.util.thinksdk.ThinkOauth');
209         $tencent = ThinkOauth::getInstance('tencent', $token);
210         $data    = $tencent->call('user/info');
211 
212         if($data['ret'] == 0){
213             $userInfo['type'] = 'TENCENT';
214             $userInfo['name'] = $data['data']['name'];
215             $userInfo['nick'] = $data['data']['nick'];
216             $userInfo['head'] = $data['data']['head'];
217             return $userInfo;
218         } else {
219             throw_exception("獲取騰訊微博用戶信息失敗:{$data['msg']}");
220         }
221     }
222 
223     //登錄成功,獲取新浪微博用戶信息
224     public function sina($token){
225         //加載ThinkOauth類並實例化一個對象
226         import('org.util.thinksdk.ThinkOauth');
227         $sina = ThinkOauth::getInstance('sina', $token);
228         $data = $sina->call('users/show', "uid={$sina->openid()}");
229 
230         if($data['error_code'] == 0){
231             $userInfo['type'] = 'SINA';
232             $userInfo['name'] = $data['name'];
233             $userInfo['nick'] = $data['screen_name'];
234             $userInfo['head'] = $data['avatar_large'];
235             return $userInfo;
236         } else {
237             throw_exception("獲取新浪微博用戶信息失敗:{$data['error']}");
238         }
239     }
240 
241     //登錄成功,獲取網易微博用戶信息
242     public function t163($token){
243         //加載ThinkOauth類並實例化一個對象
244         import('org.util.thinksdk.ThinkOauth');
245         $t163 = ThinkOauth::getInstance('t163', $token);
246         $data = $t163->call('users/show');
247 
248         if($data['error_code'] == 0){
249             $userInfo['type'] = 'T163';
250             $userInfo['name'] = $data['name'];
251             $userInfo['nick'] = $data['screen_name'];
252             $userInfo['head'] = str_replace('w=48&h=48', 'w=180&h=180', $data['profile_image_url']);
253             return $userInfo;
254         } else {
255             throw_exception("獲取網易微博用戶信息失敗:{$data['error']}");
256         }
257     }
258

 

參考:tp5集成淘寶,微信,網易,新浪等第三方登錄 - ThinkPHP框架
http://www.thinkphp.cn/topic/43566.html

 

 

 

 


免責聲明!

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



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