Test3.php 4.2 KB

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