SyncZsWechatMaterial.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Console\Commands\ActionTrigger\BatchWechatMaterial;
  3. use App\Modules\OfficialAccount\Services\CustomMsgService;
  4. use App\Modules\WechatMaterial\Models\BatchWechatMaterial;
  5. use App\Modules\WechatMaterial\Models\WechatMaterial;
  6. use App\Modules\WechatMaterial\Models\WechatMaterialSendMsg;
  7. use Illuminate\Console\Command;
  8. /**
  9. * 同步微信素材
  10. *
  11. */
  12. class SyncZsWechatMaterial extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'ActionTrigger:sync_zs_wechat_material {data}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '同步微信素材';
  26. private $_task = 'ActionTrigger:sync_zs_wechat_material ';
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return mixed
  40. */
  41. public function handle()
  42. {
  43. // 获取数据
  44. $reqData = $this->argument('data');
  45. $data = getProp($reqData, 'data', []);
  46. \Log::info($this->_task . 'request_data');
  47. \Log::info($data);
  48. \Log::info($this->_task . '[start]');
  49. $this->start($data);
  50. \Log::info($this->_task . '[end]');
  51. }
  52. public function start($data)
  53. {
  54. // 获取参数
  55. $appId = getProp($data, 'appid');
  56. $batchId = getProp($data, 'batch_id', 0);
  57. $channelId = getProp($data, 'distribution_channel_id', 0);
  58. if (!$appId || !$batchId || !$channelId) {
  59. return false;
  60. }
  61. // 获取msg信息,未查询到数据或者media_id已存在则不再继续往下执行
  62. $msgInfo = WechatMaterialSendMsg::getMsgByAppBatchChannelId($appId, $batchId, $channelId);
  63. \Log::info($this->_task . 'msgInfo');
  64. \Log::info($msgInfo);
  65. if (!$msgInfo) {
  66. return false;
  67. }
  68. // 获取素材数据
  69. $materials = WechatMaterial::getWeChatMaterials($batchId);
  70. $materials = $materials ? $materials->toArray() : [];
  71. \Log::info($this->_task . 'materials_ids');
  72. \Log::info(array_column($materials, 'id'));
  73. if (!$materials) {
  74. return false;
  75. }
  76. // 筛选出内容中的所有图片
  77. $contents = implode(',', array_column($materials, 'content'));
  78. $images = CustomMsgService::extract_content_images($contents);
  79. \Log::info($this->_task . 'extract_content_image_urls');
  80. \Log::info($images);
  81. // 上传内容中的图片,替换内容中图片链接
  82. $upImagesRes = CustomMsgService::multi_upload_material_imgs($appId, $images);
  83. [$oldUrls, $newUrls] = [array_column($upImagesRes['urls'], 'old_url'), array_column($upImagesRes['urls'], 'new_url')];
  84. $materials = CustomMsgService::replace_content_images($materials, $oldUrls, $newUrls);
  85. // 批量上传缩略图
  86. $thumbImages = array_column($materials, 'cover_url');
  87. $upThumbsRes = CustomMsgService::multi_upload_material_imgs($appId, $thumbImages, 'thumb');
  88. // 上传article
  89. $upArticleRes = CustomMsgService::upload_articles($channelId, $msgInfo, $materials, $upThumbsRes['urls']);
  90. $wxMediaId = getProp($upArticleRes['data'], 'media_id');
  91. if ($wxMediaId) {
  92. // 更新media_id
  93. WechatMaterialSendMsg::where('id', getProp($msgInfo, 'id'))->update([
  94. 'wechat_media_id' => $wxMediaId,
  95. 'upload_result' => json_encode([
  96. 'up_images' => $upImagesRes['urls'],
  97. 'up_thumbs' => $upThumbsRes['urls'],
  98. 'up_articles' => $upArticleRes
  99. ]),
  100. 'status' => 'push_wechat_material',
  101. 'updated_at' => date('Y-m-d H:i:s')
  102. ]);
  103. // 更新batch同步状态
  104. BatchWechatMaterial::where('id', $batchId)->update([
  105. 'status' => 'push_wechat_material',
  106. 'updated_at' => date('Y-m-d H:i:s')
  107. ]);
  108. } else {
  109. $msg = 'upload fail';
  110. if ($upArticleRes && !$upArticleRes['code']) $msg = $upArticleRes['msg'];
  111. if (getProp($upThumbsRes['error'], 'thumb')) $msg = getProp($upThumbsRes['error'], 'thumb');
  112. if (getProp($upImagesRes['error'], 'common')) $msg = getProp($upImagesRes['error'], 'common');
  113. // 更新上传结果
  114. WechatMaterialSendMsg::where('id', getProp($msgInfo, 'id'))->update([
  115. 'upload_result' => $msg,
  116. 'status' => 'fail_push_wechat_material',
  117. 'updated_at' => date('Y-m-d H:i:s')
  118. ]);
  119. }
  120. }
  121. }