AuditMedia.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\AuditMediaDeal;
  4. use App\Jobs\XunFeiVoiceJob;
  5. use App\Models\Media\MediaSecure;
  6. use App\Services\Vendor\BaiduAi\ImageCensor;
  7. use App\Services\Vendor\Miniprogram\Security;
  8. use App\Services\Vendor\Xunfei\Voice2Text;
  9. use Carbon\Carbon;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Support\Facades\File;
  12. use Ixudra\Curl\Facades\Curl;
  13. class AuditMedia implements ShouldQueue
  14. {
  15. /**
  16. * Create the event listener.
  17. */
  18. public function __construct()
  19. {
  20. }
  21. /**
  22. * Handle the event.
  23. *
  24. * @param \App\Events\AuditMedia $event
  25. * @return void
  26. */
  27. public function handle(\App\Events\AuditMedia $event)
  28. {
  29. $media = $event->storage->updateOrCreate([
  30. 'result' => 0,
  31. 'uid' => $event->uid,
  32. 'field' => $event->field,
  33. 'field_primary' => $event->field_primary
  34. ], [
  35. 'type' => $event->type,
  36. 'origin' => $event->origin,
  37. 'audit_suggestion' => 0,
  38. ]);
  39. switch ($media->type) {
  40. case 'text':
  41. $security = new Security();
  42. $result = $security->msgSecCheck($media->origin);
  43. switch ($result['errcode']) {
  44. case 0:
  45. $media->audit_suggestion = 1;
  46. $media->result = 1;
  47. $media->save();
  48. break;
  49. case 87014:
  50. $media->machine_result = $result;
  51. $media->audit_suggestion = -1;
  52. $media->result = -1;
  53. $media->save();
  54. event(new AuditMediaDeal($media));
  55. break;
  56. default:
  57. $media->audit_suggestion = 0;
  58. $media->result = 0;
  59. $media->save();
  60. break;
  61. }
  62. break;
  63. case 'image':
  64. $contentSecure = new ImageCensor();
  65. $result = $contentSecure->image($media->origin);
  66. $media->machine_result = $result['machine_result'];
  67. $media->audit_suggestion = $result['suggestion'];
  68. $media->labels = $result['labels'];
  69. $media->save();
  70. switch ($result['suggestion']) {
  71. case MediaSecure::MACHINE_SUGGESTION_REVIEW:
  72. break;
  73. case MediaSecure::MACHINE_SUGGESTION_PASS:
  74. if ($media->field == 'headimgurl') {
  75. $media->result = MediaSecure::MANUAL_SUGGESTION_PASS;
  76. $media->save();
  77. }
  78. break;
  79. case MediaSecure::MACHINE_SUGGESTION_BLOCK:
  80. $media->result = MediaSecure::MANUAL_SUGGESTION_BLOCK;
  81. $media->channel = MediaSecure::CHANNEL_MACHINE;
  82. $media->save();
  83. event(new AuditMediaDeal($media));
  84. break;
  85. }
  86. break;
  87. case "voice":
  88. $url = $media->origin;
  89. // 文件下载
  90. $parseUrl = parse_url($url);
  91. $fileBasename = pathinfo($parseUrl['path'], PATHINFO_EXTENSION);
  92. $fileName = storage_path('app/' . uniqid() . '.' . $fileBasename);
  93. File::put($fileName, Curl::to($url)->get());
  94. $fileSize = filesize($fileName);
  95. $fileContent = fopen($fileName, 'r');
  96. File::delete($fileName);
  97. // 预处理
  98. $extData = [
  99. 'file_len' => $fileSize,
  100. 'file_name' => $fileName,
  101. 'slice_num' => 1,
  102. ];
  103. $XunFeiService = new Voice2Text();
  104. $data = $XunFeiService->prepare($extData);
  105. $taskId = $data['data'];
  106. $extData = [
  107. 'task_id' => $taskId,
  108. 'slice_id' => 'aaaaaaaaaa',
  109. 'content' => $fileContent
  110. ];
  111. // 上传
  112. $XunFeiService->upload($extData);
  113. // 合并
  114. $XunFeiService->merge($taskId);
  115. $media->machine_result = null;
  116. $media->translate = "【队列中】";
  117. $media->task_id = $taskId;
  118. $media->save();
  119. XunFeiVoiceJob::dispatch($media)
  120. ->onQueue('{xunfei:voice}')
  121. ->delay(Carbon::now()->addMinutes(1));
  122. break;
  123. }
  124. }
  125. }