基于thinkphp5.1和easyWeChat实现第三方平台扫码授权

1 <?php
  2
  3 namespace app\wechat\controller;
  4
  5 use EasyWeChat\Factory;
  6 use EasyWeChat\Kernel\Messages\News;
  7 use EasyWeChat\Kernel\Messages\NewsItem;
  8 use EasyWeChat\OpenPlatform\Server\Guard;
  9 use EasyWeChat\OpenPlatform\Auth;
 10 use think\Controller;
 11 use think\Db;
 12 use think\facade\Cookie;
 13 use think\facade\Cache;
 14
 15 class Wxnotify extends Action
 16 {
 17
 18     public function notify(){
 19
 20         $config = $this->getOpenConfig();
 21         $openPlatform = Factory::openPlatform($config);
 22
 23         $server = $openPlatform->server;
 24
 25         return $server->serve();
 26     }
 27
 28     public function auth(){
 29
 30         $config = $this->getOpenConfig();
 31         $openPlatform = Factory::openPlatform($config);
 32         $response = $openPlatform->getPreAuthorizationUrl('http://t.lznews.cn/wxcallback');
 33
 34         header("Location: $response");
 35     }
 36
 37     public function wxcallback(){
 38         $config = $this->getOpenConfig();
 39         $openPlatform = Factory::openPlatform($config);
 40         $server = $openPlatform->server;
 41         $server->push(function ($message) {
 42
 43         }, Guard::EVENT_AUTHORIZED);
 44
 45         $aa = request()->param();
 46
 47         /*array(2) {
 48           ["auth_code"] => string(102) "queryauthcode@@@wZUEjJoHPKu-tQhOIsMDA-soRJGxfIp6frb42Geeoqj4F7-qoNV1TgooEsA2GnkaA9kAXa3Ee8udYgsSop1rUQ"
 49           ["expires_in"] => string(4) "3600"
 50         }*/
 51
 52         $code = $openPlatform->handleAuthorize($aa['auth_code']);
 53
 54         /*array(1) {
 55           ["authorization_info"] => array(5) {
 56             ["authorizer_appid"] => string(18) "wx99ed51ac55c8b9c1"
 57             ["authorizer_access_token"] => string(157) "17_IoCi_gV1D_o3xaU1Ifsh8RR5DNKEUldVQoR975StqVHRs5pDpvIPYu8NNGwUjc9NezrQiATPiyMweLSo0OQg3wc6OQDYSdbVorUQGVk403sCNm-DrT5ZksWLw2J8exZI-1caib3gJguiED8YZPNeAMDMCA"
 58             ["expires_in"] => int(7200)
 59             ["authorizer_refresh_token"] => string(58) "refreshtoken@@@G-xqVs6CF_cPNX2z24m74GoCse9kCvzMJlUqd9Xrrn8"
 60             ["func_info"] => array(21) {
 61               [0] => array(1) {
 62                 ["funcscope_category"] => array(1) {
 63                   ["id"] => int(1)
 64                 }
 65               }
 66               [1] => array(1) {
 67                 ["funcscope_category"] => array(1) {
 68                   ["id"] => int(15)
 69                 }
 70               }
 71               [2] => array(1) {
 72                 ["funcscope_category"] => array(1) {
 73                   ["id"] => int(4)
 74                 }
 75               }
 76               [3] => array(1) {
 77                 ["funcscope_category"] => array(1) {
 78                   ["id"] => int(7)
 79                 }
 80               }
 81               [4] => array(1) {
 82                 ["funcscope_category"] => array(1) {
 83                   ["id"] => int(2)
 84                 }
 85               }
 86               [5] => array(1) {
 87                 ["funcscope_category"] => array(1) {
 88                   ["id"] => int(3)
 89                 }
 90               }
 91               [6] => array(1) {
 92                 ["funcscope_category"] => array(1) {
 93                   ["id"] => int(11)
 94                 }
 95               }
 96               [7] => array(1) {
 97                 ["funcscope_category"] => array(1) {
 98                   ["id"] => int(6)
 99                 }
100               }
101               [8] => array(1) {
102                 ["funcscope_category"] => array(1) {
103                   ["id"] => int(5)
104                 }
105               }
106               [9] => array(1) {
107                 ["funcscope_category"] => array(1) {
108                   ["id"] => int(8)
109                 }
110               }
111               [10] => array(1) {
112                 ["funcscope_category"] => array(1) {
113                   ["id"] => int(13)
114                 }
115               }
116               [11] => array(1) {
117                 ["funcscope_category"] => array(1) {
118                   ["id"] => int(9)
119                 }
120               }
121               [12] => array(1) {
122                 ["funcscope_category"] => array(1) {
123                   ["id"] => int(10)
124                 }
125               }
126               [13] => array(1) {
127                 ["funcscope_category"] => array(1) {
128                   ["id"] => int(12)
129                 }
130               }
131               [14] => array(1) {
132                 ["funcscope_category"] => array(1) {
133                   ["id"] => int(22)
134                 }
135               }
136               [15] => array(1) {
137                 ["funcscope_category"] => array(1) {
138                   ["id"] => int(23)
139                 }
140               }
141               [16] => array(1) {
142                 ["funcscope_category"] => array(1) {
143                   ["id"] => int(26)
144                 }
145               }
146               [17] => array(2) {
147                 ["funcscope_category"] => array(1) {
148                   ["id"] => int(27)
149                 }
150                 ["confirm_info"] => array(3) {
151                   ["need_confirm"] => int(0)
152                   ["already_confirm"] => int(0)
153                   ["can_confirm"] => int(0)
154                 }
155               }
156               [18] => array(2) {
157                 ["funcscope_category"] => array(1) {
158                   ["id"] => int(33)
159                 }
160                 ["confirm_info"] => array(3) {
161                   ["need_confirm"] => int(0)
162                   ["already_confirm"] => int(0)
163                   ["can_confirm"] => int(0)
164                 }
165               }
166               [19] => array(1) {
167                 ["funcscope_category"] => array(1) {
168                   ["id"] => int(35)
169                 }
170               }
171               [20] => array(2) {
172                 ["funcscope_category"] => array(1) {
173                   ["id"] => int(44)
174                 }
175                 ["confirm_info"] => array(3) {
176                   ["need_confirm"] => int(0)
177                   ["already_confirm"] => int(0)
178                   ["can_confirm"] => int(0)
179                 }
180               }
181             }
182           }
183         }*/
184
185         $sq = Db::name('wechat_account')->where([['appid','=',$code['authorization_info']['authorizer_appid']], ['shouquan','=','1']])->find();
186
187         if(!$sq) {
188             return '请先注册公众号或小程序信息,并勾选为扫码授权';
189         }
190
191         $name = $openPlatform->getAuthorizer($code['authorization_info']['authorizer_appid']);
192         /*array(2) {
193           ["authorizer_info"] => array(11) {
194             ["nick_name"] => string(9) "小鲁哥"
195             ["head_img"] => string(128) "http://wx.qlogo.cn/mmopen/2h8qNVmIGxDBibx0LzLo7zbibVxvzwVWkVeNwWpLoiaw8HFtDf9TvhBkTkIfU6KJMJgibH7ulKI2MafvudBP4moEb98tz3N8n72O/0"
196             ["service_type_info"] => array(1) {
197               ["id"] => int(2)
198             }
199             ["verify_type_info"] => array(1) {
200               ["id"] => int(0)
201             }
202             ["user_name"] => string(15) "gh_f2ee5d98dbd4"
203             ["alias"] => string(9) "lznewsxlg"
204             ["qrcode_url"] => string(122) "http://mmbiz.qpic.cn/mmbiz_jpg/9qH8V6o0A0J2joL7hbYgGCt5m0icdMXTh52C2EBEBh3Hzuiah6E9eYqHgYGGBs1Z6M9aiayho0Zl8w7sP4MI6zFmA/0"
205             ["business_info"] => array(5) {
206               ["open_pay"] => int(1)
207               ["open_shake"] => int(0)
208               ["open_scan"] => int(0)
209               ["open_card"] => int(1)
210               ["open_store"] => int(1)
211             }
212             ["idc"] => int(1)
213             ["principal_name"] => string(30) "山东大鲁网络有限公司"
214             ["signature"] => string(108) "线索提供、闲聊扯皮、互动交流、同城活动。小鲁哥,一个逼格很高的公众平台。"
215           }
216           ["authorization_info"] => array(3) {
217             ["authorizer_appid"] => string(18) "wx99ed51ac55c8b9c1"
218             ["authorizer_refresh_token"] => string(58) "refreshtoken@@@G-xqVs6CF_cPNX2z24m74GoCse9kCvzMJlUqd9Xrrn8"
219             ["func_info"] => array(21) {
220               [0] => array(1) {
221                 ["funcscope_category"] => array(1) {
222                   ["id"] => int(1)
223                 }
224               }
225               [1] => array(1) {
226                 ["funcscope_category"] => array(1) {
227                   ["id"] => int(15)
228                 }
229               }
230               [2] => array(1) {
231                 ["funcscope_category"] => array(1) {
232                   ["id"] => int(4)
233                 }
234               }
235               [3] => array(1) {
236                 ["funcscope_category"] => array(1) {
237                   ["id"] => int(7)
238                 }
239               }
240               [4] => array(1) {
241                 ["funcscope_category"] => array(1) {
242                   ["id"] => int(2)
243                 }
244               }
245               [5] => array(1) {
246                 ["funcscope_category"] => array(1) {
247                   ["id"] => int(3)
248                 }
249               }
250               [6] => array(1) {
251                 ["funcscope_category"] => array(1) {
252                   ["id"] => int(11)
253                 }
254               }
255               [7] => array(1) {
256                 ["funcscope_category"] => array(1) {
257                   ["id"] => int(6)
258                 }
259               }
260               [8] => array(1) {
261                 ["funcscope_category"] => array(1) {
262                   ["id"] => int(5)
263                 }
264               }
265               [9] => array(1) {
266                 ["funcscope_category"] => array(1) {
267                   ["id"] => int(8)
268                 }
269               }
270               [10] => array(1) {
271                 ["funcscope_category"] => array(1) {
272                   ["id"] => int(13)
273                 }
274               }
275               [11] => array(1) {
276                 ["funcscope_category"] => array(1) {
277                   ["id"] => int(9)
278                 }
279               }
280               [12] => array(1) {
281                 ["funcscope_category"] => array(1) {
282                   ["id"] => int(10)
283                 }
284               }
285               [13] => array(1) {
286                 ["funcscope_category"] => array(1) {
287                   ["id"] => int(12)
288                 }
289               }
290               [14] => array(1) {
291                 ["funcscope_category"] => array(1) {
292                   ["id"] => int(22)
293                 }
294               }
295               [15] => array(1) {
296                 ["funcscope_category"] => array(1) {
297                   ["id"] => int(23)
298                 }
299               }
300               [16] => array(1) {
301                 ["funcscope_category"] => array(1) {
302                   ["id"] => int(26)
303                 }
304               }
305               [17] => array(2) {
306                 ["funcscope_category"] => array(1) {
307                   ["id"] => int(27)
308                 }
309                 ["confirm_info"] => array(3) {
310                   ["need_confirm"] => int(0)
311                   ["already_confirm"] => int(0)
312                   ["can_confirm"] => int(0)
313                 }
314               }
315               [18] => array(2) {
316                 ["funcscope_category"] => array(1) {
317                   ["id"] => int(33)
318                 }
319                 ["confirm_info"] => array(3) {
320                   ["need_confirm"] => int(0)
321                   ["already_confirm"] => int(0)
322                   ["can_confirm"] => int(0)
323                 }
324               }
325               [19] => array(1) {
326                 ["funcscope_category"] => array(1) {
327                   ["id"] => int(35)
328                 }
329               }
330               [20] => array(2) {
331                 ["funcscope_category"] => array(1) {
332                   ["id"] => int(44)
333                 }
334                 ["confirm_info"] => array(3) {
335                   ["need_confirm"] => int(0)
336                   ["already_confirm"] => int(0)
337                   ["can_confirm"] => int(0)
338                 }
339               }
340             }
341           }
342         }*/
343
344         Db::name('wechat_account')->where('id', $sq['id'])->update(['token' => $code['authorization_info']['authorizer_refresh_token']]);
345
346         return $server->serve();
347     }
348 }
(0)

相关推荐