123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- <?php
- namespace App\Admin\Controllers;
- use App\Http\Controllers\Controller;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Redis;
- use Illuminate\Support\Facades\DB;
- use Hanson\LaravelAdminWechat\Models\WechatConfig;
- use EasyWeChat\Factory;
- use EasyWeChat\OpenPlatform\Server\Guard;
- use App\Models\WechatTicket;
- use App\Modelsgbapp\SpecialistInfogbapp;
- /**
- * 微信开发平台第三方平台接入
- * EasyWeChat 4.0
- */
- class OpenWeixinController extends Controller
- {
- protected $account;
- protected $app;
- protected $config = [
- 'app_id' => 'wxe81efa7fd6a5fd53',
- 'secret' => '7d22f52938e4c9fbb5f2d07e8f5b83b6',
- 'token' => '6lfq90croi6nsyia63vbhaqnq4lzm66d',
- 'aes_key' => 'uOIvZHtyDKKd7xT4KKgpikGOtgW7ZskeOzVIFgIs2SM'
- ];
- /**
- * Display a listing of the resource.
- *
- * @return Response
- */
- public function index(Request $request)
- {
- $this->request = $request;;
- try {
- $openPlatform = Factory::openPlatform($this->config);
- // 第三方平台接入处理
- $server = $openPlatform->server;
- return $server->serve();
- // 处理授权成功事件
- if (!empty($request["auth_code"])) {
- // $server->push(function ($event) use ($openPlatform) {
- // DB::table("test_content")->insert(array("test"=>json_encode($request["auth_code"])));
- // //获取(刷新)授权公众号或小程序的接口调用凭据(令牌)
- // $res = $openPlatform->handleAuthorize($request["auth_code"]);
- // $appid2 = $res['authorization_info']['authorizer_appid'];
- // $refresh_token = $res['authorization_info']['authorizer_refresh_token'];
- //
- // //获取授权方的帐号基本信息
- // $data = $openPlatform->getAuthorizer($appid2);
- // $account = WechatConfig::where('app_id', $appid2)->first();
- // $info2 = $data['authorizer_info'];
- // $info1 = $data['authorization_info'];
- // if (is_null($account)) {
- // $account = WechatConfig::create([
- // 'app_id' => $info1['authorizer_appid'],
- // 'refresh_token' => $refresh_token,
- // 'name' => $info2['nick_name'],
- // 'head_img' => $info2['head_img'],
- // 'original' => $info2['user_name'],
- // 'com_main_body' => $info2['principal_name'],
- // 'headimg' => $info2['qrcode_url'], //二维码
- // 'status' => 1,
- // 'level' => $this->getLevel($info2),
- // 'type' => 1,
- //// 'token' => random(32),
- //// 'encodingaeskey' => random(43)
- // ]);
- // } else {
- // $account->refresh_token = $refresh_token;
- // $account->name = $info2['nick_name'];
- // $account->type = 3;
- // $account->original = $info2['user_name'];
- // $account->level = $this->getLevel($info2);
- // $account->head_img = $info2['head_img'];
- // $account->save();
- // }
- // }, Guard::EVENT_AUTHORIZED);
- }
- // 处理授权更新事件
- $server->push(function ($event) use ($openPlatform) {
- // DB::table("test_content")->insert(array("test"=>json_encode($event)));
- //
- // $vl=json_encode($event);
- // $event=json_decode($vl);
- }, Guard::EVENT_UPDATE_AUTHORIZED);
- // 处理授权取消事件
- $server->push(function ($event) {
- // DB::table("test_content")->insert(array("test"=>json_encode($event)));
- //
- // $vl=json_encode($event);
- // $event=json_decode($vl);
- // $appid = $event->AuthorizerAppid;
- // $account = WechatConfig::where('appid', $appid)->first();
- // if (!is_null($account)) {
- // $account->refresh_token = '';
- // $account->save();
- // }
- }, Guard::EVENT_UNAUTHORIZED);
- //VerifyTicket component_verify_ticket协议推送
- $server->push(function ($event) use ($openPlatform) {
- // $vl=json_encode($event);
- // $event=json_decode($vl);
- // $appid = $event->AppId;
- // $ticket = $event->ComponentVerifyTicket;
- // //保存component_verify_ticket协议
- // Redis::set($appid . '_component_verify_ticket', $ticket);//存储起来方便后续多台服务器并发调用
- //// $wechatTicket = WechatTicket::where('appid', $appid)->orderBy('id')->first();
- // $wechatTicket = DB::table("wechat_ticket")->where('appid', $appid)->orderBy('id')->first();
- // if (empty($wechatTicket)) {
- // $wechatTicket = DB::table("wechat_ticket")->insert(array('appid'=>$appid,'ticket'=>$ticket
- // ,'create_time'=>time(),'update_time' =>time()));
- // } else {
- // DB::table("wechat_ticket")->where('appid', $appid)->update(array(
- // 'ticket' => $ticket,
- // 'update_time' => time()
- // ));
- //// $wechatTicket->ticket = $ticket;
- //// $wechatTicket->update_time = time();
- //// $wechatTicket->save();
- // }
- }, Guard::EVENT_COMPONENT_VERIFY_TICKET);
- return $server->serve();
- } catch (\Exception $e) {
- if (env('APP_DEBUG')) {
- echo $e->getMessage();
- } else {
- abort(404);
- }
- }
- }
- public function auth(Request $request)
- {
- try {
- $openPlatform = Factory::openPlatform($this->config);
- // 获取跳转的链接
- $url = $openPlatform->getPreAuthorizationUrl('http://zyyd-api.qingerai.com/admin/openweixin/index');
- // 获取跳转的链接
- echo '<a href="' . $url . '">点击第三方授权</a>';
- } catch (\Exception $e) {
- echo $e->getMessage();
- $url = '';
- }
- //return view('admin.auth.note', compact('url'));
- }
- public function callback(Request $request)
- {
- // echo \GuzzleHttp\json_encode($filter);exit;
- // return redirect()->to(site_path('account/wechat', 'admin'))->with('message', '第三方接入成功!');
- return redirect()->to('http://zyyd-api.qingerai.com/admin/openweixin/index')->with('message', '第三方接入成功!');
- }
- public function getThirdToken(Request $request)
- {
- $wechatTicket = SpecialistInfogbapp::first()['name'];
- echo \GuzzleHttp\json_encode($wechatTicket);
- // $openPlatform = Factory::openPlatform($this->config);
- // // 第三方平台接入处理
- // $server = $openPlatform->getAuthorizer("wx382b6315fd4f1760");
- // echo \GuzzleHttp\json_encode($server);exit;
- // return redirect()->to(site_path('account/wechat', 'admin'))->with('message', '第三方接入成功!');
- }
- // 公众号分组
- public function getLevel($info = array())
- {
- $level = 0;
- if (!empty($info['MiniProgramInfo'])) {
- $level = 5;
- } else if ($info['service_type_info']['id'] == 0 || $info['service_type_info']['id'] == 1) {
- //订阅号
- if ($info['verify_type_info']['id'] >= 0) {
- //已认证
- $level = 3;
- } else {
- $level = 1;
- }
- } else if ($info['service_type_info']['id'] == 2) {
- //服务号
- if ($info['verify_type_info']['id'] >= 0) {
- //已认证
- $level = 4;
- } else {
- $level = 2;
- }
- }
- return $level;
- }
- }
|