AuthCorpJobs.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Jobs\OpenWork\Company;
  3. use App\Servers\DB\DbService;
  4. use Illuminate\Bus\Queueable;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Facades\Servers\Wechat\OpenWork;
  10. use App\Models\OpenWork\Corp\AuthCorp as AuthCorpModel;
  11. use App\Servers\OpenWork\Corp\AuthCorp as AuthCorpService;
  12. use App\Facades\Servers\Logs\Log;
  13. use App\Models\OpenWork\job\Records;
  14. /**
  15. * 更新企业授权信息
  16. * @author 唐远望
  17. * @version 1.0
  18. * @date 2025-04-21
  19. *
  20. */
  21. class AuthCorpJobs implements ShouldQueue
  22. {
  23. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  24. protected $message_data;
  25. protected $Records;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(array $message_data)
  32. {
  33. //
  34. $this->message_data = $message_data;
  35. }
  36. public function getCorpId()
  37. {
  38. return $this->message_data['authCorpId'] ?? null;
  39. }
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. //
  48. try {
  49. $corpId = $this->message_data['authCorpId'];
  50. (new DbService())->getConnectionNameByCorpId($corpId);
  51. // 创建任务记录
  52. $this->Records = Records::create([
  53. 'job_id' => $this->message_data['corpid'] . '_AuthCorpJobs',
  54. 'name' => static::class,
  55. 'payload' => json_encode($this->message_data),
  56. 'status' => 'processing',
  57. 'started_at' => now()
  58. ]);
  59. $this->update_authcorp_info($this->message_data['corpid']);
  60. //删除任务记录
  61. $this->Records->delete();
  62. // 成功处理...
  63. } catch (\Exception $e) {
  64. // 失败处理...
  65. if ($this->Records) {
  66. $this->Records->delete();
  67. }
  68. Log::info('job_error', '更新企业授权信息失败',['error' => $e->getMessage()]);
  69. }
  70. }
  71. /**
  72. * 更新企业授权信息
  73. * @author 唐远望
  74. * @version 1.0
  75. * @date 2025-04-21
  76. *
  77. */
  78. private function update_authcorp_info($authCorpId)
  79. {
  80. $permanent_code = (new AuthCorpModel())->query()->where([['corpid', '=', $authCorpId]])->value('permanent_code');
  81. if (!$permanent_code) {
  82. return json_send(['code' => 'error', 'msg' => '获取永久授权码失败']);
  83. }
  84. // 获取配置
  85. $app = OpenWork::getApp();
  86. // 传入临时授权码,获取永久授权码
  87. $requert_data = $app->corp->getAuthorization($authCorpId, $permanent_code);
  88. $result = (new AuthCorpService())->update_corp_authcorp($requert_data);
  89. // 如果获取结果失败
  90. if (!$result) return json_send(['code' => 'error', 'msg' => '更新失败']);
  91. // 错误提示
  92. if (isset($result['errcode'])) return json_send(['code' => 'error', 'msg' => $result['errmsg']]);
  93. // 输出结果
  94. return json_send(['code' => 'success', 'msg' => '更新成功', 'data' => $result]);
  95. }
  96. public function failed(\Throwable $exception)
  97. {
  98. Log::info('job_error', 'AuthCorpJobs彻底失败', ['data'=>$this->message_data,'error' => $exception->getMessage()]);
  99. // 失败处理...
  100. if ($this->Records) {
  101. $this->Records->delete();
  102. }
  103. }
  104. }