| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 | <?phpnamespace App\Console\Commands\ActionTrigger\BatchWechatMaterial;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 SyncZsWechatMaterial extends Command{    /**     * The name and signature of the console command.     *     * @var string     */    protected $signature = 'ActionTrigger:sync_zs_wechat_material {data}';    /**     * The console command description.     *     * @var string     */    protected $description = '同步微信素材';    private $_task = 'ActionTrigger:sync_zs_wechat_material ';    /**     * Create a new command instance.     *     * @return void     */    public function __construct()    {        parent::__construct();    }    /**     * Execute the console command.     *     * @return mixed     */    public function handle()    {        // 获取数据        $reqData = $this->argument('data');        $data    = getProp($reqData, 'data', []);        \Log::info($this->_task . 'request_data');        \Log::info($data);        \Log::info($this->_task . '[start]');        $this->start($data);        \Log::info($this->_task . '[end]');    }    public function start($data)    {        // 获取参数        $appId     = getProp($data, 'appid');        $batchId   = getProp($data, 'batch_id', 0);        $channelId = getProp($data, 'distribution_channel_id', 0);        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);        // 批量上传缩略图        $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')            ]);        }    }}
 |