OpenPlatform.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Http\Controllers\Api\Api;
  3. use EasyWeChat\Factory;
  4. use EasyWeChat\OpenPlatform\Server\Guard;
  5. /**
  6. * 微信开放平台,第三方平台
  7. *
  8. * @author 刘相欣
  9. *
  10. * */
  11. class OpenPlatform extends Api{
  12. /**
  13. * 接收授权事件 api/openplatform/authorized_callback
  14. * 用于接收平台推送给第三方平台账号的消息与事件,如授权事件通知、component_verify_ticket等
  15. *
  16. */
  17. public function authorized_callback(){
  18. $openPlatform = Factory::openPlatform(config('wechat.openplatform'));
  19. $server = $openPlatform->server;
  20. // 处理授权成功事件
  21. $server->push(function ($message) {
  22. json_send(['code'=>'success','msg'=>'EVENT_AUTHORIZED','data'=>$message]);
  23. }, Guard::EVENT_AUTHORIZED);
  24. // 处理授权更新事件
  25. $server->push(function ($message) {
  26. json_send(['code'=>'success','msg'=>'EVENT_UPDATE_AUTHORIZED','data'=>$message]);
  27. }, Guard::EVENT_UPDATE_AUTHORIZED);
  28. // 处理授权取消事件
  29. $server->push(function ($message) {
  30. json_send(['code'=>'success','msg'=>'EVENT_UNAUTHORIZED','data'=>$message]);
  31. }, Guard::EVENT_UNAUTHORIZED);
  32. return $server->serve();
  33. }
  34. /**
  35. * 接收授权事件 api/openplatform/callback
  36. *
  37. * 用于接收平台推送给第三方平台账号的消息与事件,如授权事件通知、component_verify_ticket等
  38. *
  39. */
  40. public function callback($appid){
  41. json_send(['code'=>'success','msg'=>'EVENT_APPID','data'=>$appid]);
  42. }
  43. /**
  44. * 使用授权码换取接口调用凭据和授权信息 api/openplatform/auth_code
  45. *
  46. */
  47. public function auth_code(){
  48. // 授权码
  49. $authCode = request('auth_code');
  50. // 实例
  51. $openPlatform = Factory::openPlatform(config('wechat.openplatform'));
  52. // 没有授权码
  53. if( !$authCode ) {
  54. // 获取预授权URL
  55. $url = $openPlatform->getPreAuthorizationUrl(url('api/openplatform/auth_code'));
  56. //
  57. return redirect($url);
  58. }
  59. // 使用授权码换取接口调用凭据和授权信息
  60. $result = $openPlatform->handleAuthorize($authCode);
  61. dd($result);
  62. }
  63. }