AiVideoController.php 3.6 KB
<?php
/**
 * @remark :
 * @name   :AiVideoController.php
 * @author :lyh
 * @method :post
 * @time   :2025/8/2 11:18
 */

namespace App\Http\Controllers\Api;

use App\Enums\Common\Code;
use App\Models\Project\AiVideoAutoLog;
use App\Services\CosService;
use Illuminate\Support\Facades\Redis;

class AiVideoController extends BaseController
{
    /**
     * @remark :回调方法
     * @name   :ImageCallBack
     * @author :lyh
     * @method :post
     * @time   :2025/8/2 11:19
     */
    public function ImageCallBack(){
        $str = '{"type":"end","id":3383387788,"content":"Top Demanding Applications from China: Leading Products & Companies\uff0cTop Demanding Applications from China: Leading Products & Companies\uff0c4K\uff0c\u9ad8\u6e05 --no logo --ar 16:9","attachments":[{"id":1048093549,"url":"https:\/\/mjcdn.ttapi.io\/attachments\/1401125993464332303\/1401774677432467546\/hugheliot356883_Top_Demanding_Applications_from_China_Leading_P_505cb786-2342-4bc3-9f3f-7b8d5227e38a.png?ex=68917fe6&is=68902e66&hm=faf2f6965425718185a3e6f4a36f654d89277250c99cfbfe8fc2714a13596979&","proxy_url":null,"filename":null,"content_type":"image\/png","width":null,"height":null,"size":null,"ephemeral":null}],"embeds":[],"trigger_id":"3383387788"}';
        $this->param = json_decode($str,true);
        $this->saveLog('数据详情:'.json_encode($this->param,true));
        $count = Redis::decr('ai_video_image');
        if ($count < 0) {
            Redis::set('ai_video_image', 0);
        }
        $data = $this->param['attachments'][0] ?? [];
        $aiVideoAutoLogModel = new AiVideoAutoLog();
        if(empty($data) || empty($data['url'])){
            $this->saveLog('未获取到回调数据详情');
            $aiVideoAutoLogModel->edit(['status'=>9,'result'=>json_encode($this->param,true)],['trigger_id'=>$this->param['id']]);
        }
        //获取当前数据详情
        $info = $aiVideoAutoLogModel->read(['trigger_id'=>$this->param['id']]);
        if($info === false){
            $this->saveLog('未获取到当前数据详情。'.$info);
            $this->response('success');
        }
        //上传图片  返回cdn链接
        $cosService = new CosService();
        $imagePath = $cosService->uploadRemote($info['project_id'],'video',$data['url']);
        $this->saveLog($imagePath);
        try {
            if($imagePath){
                $cos = config('filesystems.disks.cos');
                $url = $cos['cdn1'].'/'.$imagePath;
                //裁剪图片为4张
                $images = [];
                $cosService = new CosService();
                $data = $cosService->cropAndUploadToCOS($url);
                if(!empty($data)){
                    foreach ($data as $item){
                        $images[] = ['url'=>$item,'alt'=>$info['title']];
                    }
                }
                $images = array_merge($images,$info['images']);
                $this->saveLog('返回的数据详情。'.$images);
                $aiVideoAutoLogModel->edit(['images'=>$images,'result'=>json_encode($this->param,true),'status'=>1],['id'=>$info['id']]);
            }
        }catch (\Exception $e){
            $this->saveLog('上传图片失败,获取到数据详情。'.$e->getMessage());
        }
        $this->response('success');
    }

    /**
     * @remark :保存日志
     * @name   :saveLog
     * @author :lyh
     * @method :post
     * @time   :2025/8/4 11:54
     */
    public function saveLog($message){
        @file_put_contents(storage_path('logs/ai_video.log'), var_export($message, true) . PHP_EOL, FILE_APPEND);
        return true;
    }
}