12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace App\Wechat\Official;
- use App\Services\Auth\AuthService;
- use App\Services\Common\WechatService;
- use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
- use Illuminate\Support\Facades\Cache;
- use App\Repositories\Recruit;
- class MessageHandle implements EventHandlerInterface
- {
- /**
- * @var AuthService
- */
- private $authService;
- /**
- * @var WechatService
- */
- private $wechatService;
- /**
- * EventHandle constructor.
- */
- public function __construct()
- {
- $this->authService=app('App\Services\Auth\AuthService');
- $this->wechatService=app('App\Services\Common\WechatService');
- }
- /**
- * @param mixed $payload
- * @return string
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- */
- public function handle($payload = null)
- {
- $data = Cache::get($payload['FromUserName']);//查找是否有记录
- if($data){
- if(array_key_exists('module',$data)){
- switch ($data['module']){
- case 'recruit':
- return $this->wakeUp("App\\Wechat\\Official\\Service\\", 'RecruitService', $payload, $data);
- break;
- }
- }
- }
- Cache::forget($payload['FromUserName']);
- if($payload['MsgType'] == 'text'){
- switch ($payload['Content']){
- case '报名':
- $data = [
- 'module' => 'recruit',
- 'action' => 'init'//报名主菜单(此场景下,查询当前仍可报名的招考场次,做成序号回复,并加上一些常用功能服务)
- ];
- return $this->wakeUp("App\\Wechat\\Official\\Service\\", 'RecruitService', $payload, $data);
- break;
- }
- }
- }
- private function wakeUp($namespace, $classname, $payload, $data = null)
- {
- $official=$this->wechatService->getOfficialAccount();
- $wechatUser=$official->user->get($payload['FromUserName']);
- $user=$this->authService->wechatCheck($wechatUser);
- $class =$namespace.$classname;
- if (!class_exists($class)) {
- return "服务暂不可用";
- }
- $class =app($class);
- return $class->handle($wechatUser, $user, $payload, $data);
- }
- }
|