微信扫一扫打赏支持

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

 

 

 

 
posted @ 2018-11-21 15:53  范仁义  阅读(1705)  评论(1编辑  收藏  举报