MessageHandle.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Wechat\Official;
  3. use App\Services\Auth\AuthService;
  4. use App\Services\Common\WechatService;
  5. use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
  6. use Illuminate\Support\Facades\Cache;
  7. use App\Repositories\Recruit;
  8. class MessageHandle implements EventHandlerInterface
  9. {
  10. /**
  11. * @var AuthService
  12. */
  13. private $authService;
  14. /**
  15. * @var WechatService
  16. */
  17. private $wechatService;
  18. /**
  19. * EventHandle constructor.
  20. */
  21. public function __construct()
  22. {
  23. $this->authService=app('App\Services\Auth\AuthService');
  24. $this->wechatService=app('App\Services\Common\WechatService');
  25. }
  26. /**
  27. * @param mixed $payload
  28. * @return string
  29. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  30. */
  31. public function handle($payload = null)
  32. {
  33. $data = Cache::get($payload['FromUserName']);//查找是否有记录
  34. if($data){
  35. if(array_key_exists('module',$data)){
  36. switch ($data['module']){
  37. case 'recruit':
  38. return $this->wakeUp("App\\Wechat\\Official\\Service\\", 'RecruitService', $payload, $data);
  39. break;
  40. }
  41. }
  42. }
  43. Cache::forget($payload['FromUserName']);
  44. if($payload['MsgType'] == 'text'){
  45. switch ($payload['Content']){
  46. case '报名':
  47. $data = [
  48. 'module' => 'recruit',
  49. 'action' => 'init'//报名主菜单(此场景下,查询当前仍可报名的招考场次,做成序号回复,并加上一些常用功能服务)
  50. ];
  51. return $this->wakeUp("App\\Wechat\\Official\\Service\\", 'RecruitService', $payload, $data);
  52. break;
  53. }
  54. }
  55. }
  56. private function wakeUp($namespace, $classname, $payload, $data = null)
  57. {
  58. $official=$this->wechatService->getOfficialAccount();
  59. $wechatUser=$official->user->get($payload['FromUserName']);
  60. $user=$this->authService->wechatCheck($wechatUser);
  61. $class =$namespace.$classname;
  62. if (!class_exists($class)) {
  63. return "服务暂不可用";
  64. }
  65. $class =app($class);
  66. return $class->handle($wechatUser, $user, $payload, $data);
  67. }
  68. }