12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Api\Api;
- use EasyWeChat\Factory;
- use EasyWeChat\OpenPlatform\Server\Guard;
- /**
- * 微信开放平台,第三方平台
- *
- * @author 刘相欣
- *
- * */
- class OpenPlatform extends Api{
-
- /**
- * 接收授权事件 api/openplatform/authorized_callback
- * 用于接收平台推送给第三方平台账号的消息与事件,如授权事件通知、component_verify_ticket等
- *
- */
- public function authorized_callback(){
- $openPlatform = Factory::openPlatform(config('wechat.openplatform'));
- $server = $openPlatform->server;
- // 处理授权成功事件
- $server->push(function ($message) {
- json_send(['code'=>'success','msg'=>'EVENT_AUTHORIZED','data'=>$message]);
- }, Guard::EVENT_AUTHORIZED);
- // 处理授权更新事件
- $server->push(function ($message) {
- json_send(['code'=>'success','msg'=>'EVENT_UPDATE_AUTHORIZED','data'=>$message]);
- }, Guard::EVENT_UPDATE_AUTHORIZED);
- // 处理授权取消事件
- $server->push(function ($message) {
- json_send(['code'=>'success','msg'=>'EVENT_UNAUTHORIZED','data'=>$message]);
- }, Guard::EVENT_UNAUTHORIZED);
- return $server->serve();
- }
- /**
- * 接收授权事件 api/openplatform/callback
- *
- * 用于接收平台推送给第三方平台账号的消息与事件,如授权事件通知、component_verify_ticket等
- *
- */
- public function callback($appid){
- json_send(['code'=>'success','msg'=>'EVENT_APPID','data'=>$appid]);
- }
- /**
- * 使用授权码换取接口调用凭据和授权信息 api/openplatform/auth_code
- *
- */
- public function auth_code(){
- // 授权码
- $authCode = request('auth_code');
- // 实例
- $openPlatform = Factory::openPlatform(config('wechat.openplatform'));
- // 没有授权码
- if( !$authCode ) {
- // 获取预授权URL
- $url = $openPlatform->getPreAuthorizationUrl(url('api/openplatform/auth_code'));
- //
- return redirect($url);
- }
- // 使用授权码换取接口调用凭据和授权信息
- $result = $openPlatform->handleAuthorize($authCode);
- dd($result);
- }
- }
|