OpenWeixinController.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. namespace App\Admin\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\Redis;
  7. use Illuminate\Support\Facades\DB;
  8. use Hanson\LaravelAdminWechat\Models\WechatConfig;
  9. use EasyWeChat\Factory;
  10. use EasyWeChat\OpenPlatform\Server\Guard;
  11. use App\Models\WechatTicket;
  12. use App\Modelsgbapp\SpecialistInfogbapp;
  13. /**
  14. * 微信开发平台第三方平台接入
  15. * EasyWeChat 4.0
  16. */
  17. class OpenWeixinController extends Controller
  18. {
  19. protected $account;
  20. protected $app;
  21. protected $config = [
  22. 'app_id' => 'wxe81efa7fd6a5fd53',
  23. 'secret' => '7d22f52938e4c9fbb5f2d07e8f5b83b6',
  24. 'token' => '6lfq90croi6nsyia63vbhaqnq4lzm66d',
  25. 'aes_key' => 'uOIvZHtyDKKd7xT4KKgpikGOtgW7ZskeOzVIFgIs2SM'
  26. ];
  27. /**
  28. * Display a listing of the resource.
  29. *
  30. * @return Response
  31. */
  32. public function index(Request $request)
  33. {
  34. $this->request = $request;;
  35. try {
  36. $openPlatform = Factory::openPlatform($this->config);
  37. // 第三方平台接入处理
  38. $server = $openPlatform->server;
  39. return $server->serve();
  40. // 处理授权成功事件
  41. if (!empty($request["auth_code"])) {
  42. // $server->push(function ($event) use ($openPlatform) {
  43. // DB::table("test_content")->insert(array("test"=>json_encode($request["auth_code"])));
  44. // //获取(刷新)授权公众号或小程序的接口调用凭据(令牌)
  45. // $res = $openPlatform->handleAuthorize($request["auth_code"]);
  46. // $appid2 = $res['authorization_info']['authorizer_appid'];
  47. // $refresh_token = $res['authorization_info']['authorizer_refresh_token'];
  48. //
  49. // //获取授权方的帐号基本信息
  50. // $data = $openPlatform->getAuthorizer($appid2);
  51. // $account = WechatConfig::where('app_id', $appid2)->first();
  52. // $info2 = $data['authorizer_info'];
  53. // $info1 = $data['authorization_info'];
  54. // if (is_null($account)) {
  55. // $account = WechatConfig::create([
  56. // 'app_id' => $info1['authorizer_appid'],
  57. // 'refresh_token' => $refresh_token,
  58. // 'name' => $info2['nick_name'],
  59. // 'head_img' => $info2['head_img'],
  60. // 'original' => $info2['user_name'],
  61. // 'com_main_body' => $info2['principal_name'],
  62. // 'headimg' => $info2['qrcode_url'], //二维码
  63. // 'status' => 1,
  64. // 'level' => $this->getLevel($info2),
  65. // 'type' => 1,
  66. //// 'token' => random(32),
  67. //// 'encodingaeskey' => random(43)
  68. // ]);
  69. // } else {
  70. // $account->refresh_token = $refresh_token;
  71. // $account->name = $info2['nick_name'];
  72. // $account->type = 3;
  73. // $account->original = $info2['user_name'];
  74. // $account->level = $this->getLevel($info2);
  75. // $account->head_img = $info2['head_img'];
  76. // $account->save();
  77. // }
  78. // }, Guard::EVENT_AUTHORIZED);
  79. }
  80. // 处理授权更新事件
  81. $server->push(function ($event) use ($openPlatform) {
  82. // DB::table("test_content")->insert(array("test"=>json_encode($event)));
  83. //
  84. // $vl=json_encode($event);
  85. // $event=json_decode($vl);
  86. }, Guard::EVENT_UPDATE_AUTHORIZED);
  87. // 处理授权取消事件
  88. $server->push(function ($event) {
  89. // DB::table("test_content")->insert(array("test"=>json_encode($event)));
  90. //
  91. // $vl=json_encode($event);
  92. // $event=json_decode($vl);
  93. // $appid = $event->AuthorizerAppid;
  94. // $account = WechatConfig::where('appid', $appid)->first();
  95. // if (!is_null($account)) {
  96. // $account->refresh_token = '';
  97. // $account->save();
  98. // }
  99. }, Guard::EVENT_UNAUTHORIZED);
  100. //VerifyTicket component_verify_ticket协议推送
  101. $server->push(function ($event) use ($openPlatform) {
  102. // $vl=json_encode($event);
  103. // $event=json_decode($vl);
  104. // $appid = $event->AppId;
  105. // $ticket = $event->ComponentVerifyTicket;
  106. // //保存component_verify_ticket协议
  107. // Redis::set($appid . '_component_verify_ticket', $ticket);//存储起来方便后续多台服务器并发调用
  108. //// $wechatTicket = WechatTicket::where('appid', $appid)->orderBy('id')->first();
  109. // $wechatTicket = DB::table("wechat_ticket")->where('appid', $appid)->orderBy('id')->first();
  110. // if (empty($wechatTicket)) {
  111. // $wechatTicket = DB::table("wechat_ticket")->insert(array('appid'=>$appid,'ticket'=>$ticket
  112. // ,'create_time'=>time(),'update_time' =>time()));
  113. // } else {
  114. // DB::table("wechat_ticket")->where('appid', $appid)->update(array(
  115. // 'ticket' => $ticket,
  116. // 'update_time' => time()
  117. // ));
  118. //// $wechatTicket->ticket = $ticket;
  119. //// $wechatTicket->update_time = time();
  120. //// $wechatTicket->save();
  121. // }
  122. }, Guard::EVENT_COMPONENT_VERIFY_TICKET);
  123. return $server->serve();
  124. } catch (\Exception $e) {
  125. if (env('APP_DEBUG')) {
  126. echo $e->getMessage();
  127. } else {
  128. abort(404);
  129. }
  130. }
  131. }
  132. public function auth(Request $request)
  133. {
  134. try {
  135. $openPlatform = Factory::openPlatform($this->config);
  136. // 获取跳转的链接
  137. $url = $openPlatform->getPreAuthorizationUrl('http://zyyd-api.qingerai.com/admin/openweixin/index');
  138. // 获取跳转的链接
  139. echo '<a href="' . $url . '">点击第三方授权</a>';
  140. } catch (\Exception $e) {
  141. echo $e->getMessage();
  142. $url = '';
  143. }
  144. //return view('admin.auth.note', compact('url'));
  145. }
  146. public function callback(Request $request)
  147. {
  148. // echo \GuzzleHttp\json_encode($filter);exit;
  149. // return redirect()->to(site_path('account/wechat', 'admin'))->with('message', '第三方接入成功!');
  150. return redirect()->to('http://zyyd-api.qingerai.com/admin/openweixin/index')->with('message', '第三方接入成功!');
  151. }
  152. public function getThirdToken(Request $request)
  153. {
  154. $wechatTicket = SpecialistInfogbapp::first()['name'];
  155. echo \GuzzleHttp\json_encode($wechatTicket);
  156. // $openPlatform = Factory::openPlatform($this->config);
  157. // // 第三方平台接入处理
  158. // $server = $openPlatform->getAuthorizer("wx382b6315fd4f1760");
  159. // echo \GuzzleHttp\json_encode($server);exit;
  160. // return redirect()->to(site_path('account/wechat', 'admin'))->with('message', '第三方接入成功!');
  161. }
  162. // 公众号分组
  163. public function getLevel($info = array())
  164. {
  165. $level = 0;
  166. if (!empty($info['MiniProgramInfo'])) {
  167. $level = 5;
  168. } else if ($info['service_type_info']['id'] == 0 || $info['service_type_info']['id'] == 1) {
  169. //订阅号
  170. if ($info['verify_type_info']['id'] >= 0) {
  171. //已认证
  172. $level = 3;
  173. } else {
  174. $level = 1;
  175. }
  176. } else if ($info['service_type_info']['id'] == 2) {
  177. //服务号
  178. if ($info['verify_type_info']['id'] >= 0) {
  179. //已认证
  180. $level = 4;
  181. } else {
  182. $level = 2;
  183. }
  184. }
  185. return $level;
  186. }
  187. }