WechatCheck.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace App\Jobs\Video;
  3. use App\Service\Miniprogram\Wechat\AccessTokenService;
  4. use App\Service\Util\Support\Http\WechatURL;
  5. use App\Service\Util\Support\Trace\TraceContext;
  6. use App\Service\Util\Support\Http\HttpRequestService;
  7. use Illuminate\Bus\Queueable;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Arr;
  13. use Illuminate\Support\Facades\Redis;
  14. use Illuminate\Support\Facades\DB;
  15. class WechatCheck implements ShouldQueue
  16. {
  17. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  18. private $info;
  19. /**
  20. * Create a new job instance.
  21. */
  22. public function __construct($info)
  23. {
  24. $this->info = $info;
  25. }
  26. /**
  27. * Execute the job.
  28. */
  29. public function handle(): void
  30. {
  31. myLog('WechatCheck')->info('开始处理微信提审', [
  32. 'info' => $this->info
  33. ]);
  34. $traceContext = TraceContext::newFromParent($this->info['traceInfo']);
  35. $id = $this->info['id'];
  36. $record = DB::table('video_wechat_check')
  37. ->join('videos', 'videos.id', 'video_wechat_check.video_id')
  38. ->where(['video_wechat_check.id' => $id,
  39. 'video_wechat_check.is_enabled' => 1, 'video_wechat_check.status' => 5])
  40. ->select('video_wechat_check.*', 'videos.name as video_name', 'videos.cover_image as video_cover_image')
  41. ->first();
  42. if(!$record) {
  43. myLog('WechatCheck')->info('当前状态不支持提审', [
  44. 'traceInfo' => $traceContext->getTraceInfo()
  45. ]);
  46. return;
  47. }
  48. $appid = $record->appid ?: config('wechat.duanju.masterAppid');
  49. $accessToken = Redis::get(AccessTokenService::getAccessTokenRedisKey($appid));
  50. $medias = DB::table('video_series_wechat_check')
  51. ->where('video_id', $record->video_id)
  52. ->where(['sync_status' => 4, 'is_enabled' => 1])
  53. ->where('media_id', '<>', 0)
  54. ->get();
  55. $cover_material_id = $this->getMaterialId($record->video_cover_image, $accessToken);
  56. $authorized_material_id = $this->getMaterialId($record->authorized_img, $accessToken);
  57. if(!($cover_material_id && $authorized_material_id)) {
  58. myLog('WechatCheck')->error('上传短剧封面和授权材料到临时素材失败, 请重新提审', [
  59. 'appid' => $appid,
  60. 'traceInfo' => $traceContext->getTraceInfo()
  61. ]);
  62. return ;
  63. }
  64. $postData = [
  65. 'name' => $record->video_name,
  66. 'media_count' => $medias->count(),
  67. 'media_id_list' => $medias->pluck('media_id')->toArray(),
  68. 'producer' => $record->producer,
  69. 'cover_material_id' =>$cover_material_id,
  70. 'authorized_material_id' => $authorized_material_id,
  71. 'registration_number' => $record->registration_number,
  72. ];
  73. if($record->drama_id) {
  74. $postData['drama_id'] = $record->drama_id;
  75. }
  76. $result = HttpRequestService::simplePost(WechatURL::vod_auditdrama . $accessToken, $postData);
  77. if(false === $result || (0 != ($result['errcode'] ?? 0)) || (! ($result['drama_id'] ?? ''))) {
  78. myLog('WechatCheck')->error('提审请求失败', [
  79. 'appid' => $appid,
  80. 'post' => $postData,
  81. 'result' => $result,
  82. 'traceInfo' => $traceContext->getTraceInfo()
  83. ]);
  84. return;
  85. }
  86. $drama_id = $result['drama_id'];
  87. $now = date('Y-m-d H:i:s');
  88. DB::table('video_wechat_check')
  89. ->where('id', $record->id)
  90. ->update([
  91. 'status' =>1,
  92. 'apply_at' => $now,
  93. 'drama_id' => $drama_id,
  94. 'updated_at' => $now,
  95. ]);
  96. }
  97. /**
  98. * 上传临时图片素材
  99. * @param string $url 图片http地址
  100. * @param string $accessToken
  101. * @return string media_id
  102. */
  103. public function getMaterialId($url, $accessToken){
  104. $result = HttpRequestService::post(WechatURL::media_upload. $accessToken, [
  105. 'multipart' => [
  106. [
  107. 'name' => 'type',
  108. 'contents' => 'image',
  109. ],
  110. [
  111. 'name' => 'media',
  112. 'contents' => file_get_contents($url),
  113. 'filename' => Arr::last(explode('/', $url)),
  114. ]
  115. ]
  116. ]);
  117. return $result['media_id'] ?? '';
  118. }
  119. }