123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace App\Console\Commands;
- use App\Modules\OfficialAccount\Services\CustomMsgService;
- use App\Modules\WechatMaterial\Models\BatchWechatMaterial;
- use App\Modules\WechatMaterial\Models\WechatMaterial;
- use App\Modules\WechatMaterial\Models\WechatMaterialSendMsg;
- use Illuminate\Console\Command;
- class Test3 extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'test:3 {appId} {channelId} {batchId}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command description';
- private $_task = 'ActionTrigger:sync_zs_wechat_material ';
- /**
- * @return bool
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function handle()
- {
- [$appId, $batchId, $channelId] = [$this->argument('appId'), $this->argument('batchId'), $this->argument('channelId')];
- // 获取参数
- if (!$appId || !$batchId || !$channelId) {
- return false;
- }
- // 获取msg信息,未查询到数据或者media_id已存在则不再继续往下执行
- $msgInfo = WechatMaterialSendMsg::getMsgByAppBatchChannelId($appId, $batchId, $channelId);
- \Log::info($this->_task . 'msgInfo');
- \Log::info($msgInfo);
- if (!$msgInfo) {
- return false;
- }
- // 获取素材数据
- $materials = WechatMaterial::getWeChatMaterials($batchId);
- $materials = $materials ? $materials->toArray() : [];
- \Log::info($this->_task . 'materials_ids');
- \Log::info(array_column($materials, 'id'));
- if (!$materials) {
- return false;
- }
- // 筛选出内容中的所有图片
- $contents = implode(',', array_column($materials, 'content'));
- $images = CustomMsgService::extract_content_images($contents);
- \Log::info($this->_task . 'extract_content_image_urls');
- \Log::info($images);
- // 上传内容中的图片,替换内容中图片链接
- $upImagesRes = CustomMsgService::multi_upload_material_imgs($appId, $images);
- [$oldUrls, $newUrls] = [array_column($upImagesRes['urls'], 'old_url'), array_column($upImagesRes['urls'], 'new_url')];
- $materials = CustomMsgService::replace_content_images($materials, $oldUrls, $newUrls);
- dd($images, $upImagesRes, $oldUrls, $newUrls, $materials);
- // 批量上传缩略图
- $thumbImages = array_column($materials, 'cover_url');
- $upThumbsRes = CustomMsgService::multi_upload_material_imgs($appId, $thumbImages, 'thumb');
- // 上传article
- $upArticleRes = CustomMsgService::upload_articles($channelId, $msgInfo, $materials, $upThumbsRes['urls']);
- $wxMediaId = getProp($upArticleRes['data'], 'media_id');
- if ($wxMediaId) {
- // 更新media_id
- WechatMaterialSendMsg::where('id', getProp($msgInfo, 'id'))->update([
- 'wechat_media_id' => $wxMediaId,
- 'upload_result' => json_encode([
- 'up_images' => $upImagesRes['urls'],
- 'up_thumbs' => $upThumbsRes['urls'],
- 'up_articles' => $upArticleRes
- ]),
- 'status' => 'push_wechat_material',
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- // 更新batch同步状态
- BatchWechatMaterial::where('id', $batchId)->update([
- 'status' => 'push_wechat_material',
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- } else {
- $msg = 'upload fail';
- if ($upArticleRes && !$upArticleRes['code']) $msg = $upArticleRes['msg'];
- if (getProp($upThumbsRes['error'], 'thumb')) $msg = getProp($upThumbsRes['error'], 'thumb');
- if (getProp($upImagesRes['error'], 'common')) $msg = getProp($upImagesRes['error'], 'common');
- // 更新上传结果
- WechatMaterialSendMsg::where('id', getProp($msgInfo, 'id'))->update([
- 'upload_result' => $msg,
- 'status' => 'fail_push_wechat_material',
- 'updated_at' => date('Y-m-d H:i:s')
- ]);
- }
- }
- }
|