AgentUserLoginJob.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace App\Jobs;
  3. use App\Repositories\ConfigRepoistory;
  4. use Hyperf\AsyncQueue\Job;
  5. use Hyperf\Guzzle\ClientFactory;
  6. /**
  7. * 代理系统用户登陆
  8. */
  9. class AgentUserLoginJob extends Job
  10. {
  11. public $params;
  12. public function __construct($params)
  13. {
  14. // 这里最好是普通数据,不要使用携带 IO 的对象,比如 PDO 对象
  15. $this->params = $params;
  16. }
  17. public function handle()
  18. {
  19. $agentConf = make(ConfigRepoistory::class)->pickByKeys(['agent_url', 'app_uid', 'agent_token'], 'sys');
  20. $url = $agentConf['agent_url']['svalue'] . 'user/login';
  21. $data = [
  22. [
  23. 'channel_uid' => $this->params['channel_id'],
  24. 'created_at' => $this->params['created_at'],
  25. 'device_type' => $this->params['device_type'],
  26. 'mom_app_uid' => $agentConf['app_uid']['svalue'],
  27. 'user_uid' => $this->params['code'],
  28. 'app_version' => $this->params['version'],
  29. 'device_no' => $this->params['device_no']
  30. ]
  31. ];
  32. $options = array(
  33. 'timeout' => 20, // 超时,单位秒.
  34. 'verify' => false, // 是否校验SSL证书.
  35. 'http_errors' => false, // 如果跟随http异常头部是否中断.
  36. 'debug' => false, // 是否启用调试模式.
  37. 'allow_redirects' => true, // 不跟随301/302跳转.
  38. );
  39. $client = make(ClientFactory::class)->create($options);
  40. try {
  41. $params = [
  42. 'headers' => [
  43. 'Content-Type'=>'application/json',
  44. 'X-TOKEN' => $agentConf['agent_token']['svalue']
  45. ],
  46. 'json'=>$data,
  47. ];
  48. $result = $client->post($url, $params);
  49. $code = $result->getStatusCode();
  50. if ($code == 200) {
  51. return $result->getBody()->getContents();
  52. } else {
  53. logger('AgentUserLoginJob', ['代理系统登陆失败', $data]);
  54. }
  55. } catch (\Exception $exception) {
  56. logger('AgentUserLoginJob', [$exception->getMessage(), $data]);
  57. }
  58. // 异步处理该用户的系统消息分配给用户消息.
  59. dispatch_job(new InitSysMessageJob(['receive_uid' => $this->params['code']]));
  60. }
  61. }