AiVideoService.php 4.5 KB
<?php
/**
 * @remark :
 * @name   :AiVideoService.php
 * @author :lyh
 * @method :post
 * @time   :2025/4/29 17:39
 */

namespace App\Services;

use App\Helper\Translate;
use App\Models\Project\ProjectAiSetting;

class AiVideoService
{
    public $url = 'https://ai-extend.ai.cc/';

    public $mch_id = 1;//默认配置
    public $sign = '';//签名
    public $key = 'b3e4c722b821';//默认key
    public $route = '';//回调地址
    public $task_id = '';//任务id
    public $author_id = '';//作者id

    public function __construct($project_id = 0)
    {
        if($project_id){
            $projectAiSettingModel = new ProjectAiSetting();
            $aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]);
            if($aiSettingInfo !== false){
                $this->mch_id = $aiSettingInfo['mch_id'];
                $this->key = $aiSettingInfo['key'];
            }
        }
    }

    /**
     * @remark :设置路由
     * @name   :setRoute
     * @author :lyh
     * @method :post
     * @time   :2025/3/25 9:45
     */
    public function setRoute($keyword)
    {
        $this->route = generateRoute(Translate::tran($keyword, 'en'));
        return $this;
    }

    /**
     * @remark :创建任务
     * @name   :createTask
     * @author :lyh
     * @method :post
     * @time   :2025/4/29 17:59
     */
    public function createTask($title,$description,$images = [],$anchor = [],$storage = 'CDN'){
        $request_url = $this->url.'api/video/create';
        $param = ['title'=>$title, 'description'=>$description, 'images'=>$images,'anchor'=>$anchor,'storage'=>$storage];
        $param['mch_id'] = $this->mch_id;
        $this->sign = $this->generateSign($param,$this->key);
        $param['sign'] = $this->sign;
        return http_post($request_url,json_encode($param,true));
    }

    /**
     * @remark :获取视频详情
     * @name   :getVideoDetail
     * @author :lyh
     * @method :post
     * @time   :2025/2/14 11:23
     */
    public function getVideoDetail(){
        $request_url = $this->url.'api/video/detail';
        $param = [
            'mch_id'=>$this->mch_id,
            'task_id'=>$this->task_id,
        ];
        $this->sign = $this->generateSign($param,$this->key);
        $param['sign'] = $this->sign;
        $result = http_post($request_url,json_encode($param,true));
        return $result;
    }

    /**
     * @remark :更新文章
     * @name   :updateDetail
     * @author :lyh
     * @method :post
     * @time   :2025/2/21 14:38
     * @param  :task_id , title , video_url ,thumb , content , author_id  , url ,
     */
    public function updateDetail($param){
        $request_url = $this->url.'api/video/save';
        $param['mch_id'] = $this->mch_id;
        $this->sign = $this->generateSign($param,$this->key);
        $param['sign'] = $this->sign;
        $result = http_post($request_url,json_encode($param,true));
        return $result;
    }

    /**
     * @remark :获取列表页数据
     * @name   :getAiVideoList
     * @author :lyh
     * @method :post
     * @time   :2025/4/30 15:48
     */
    public function getAiVideoList($page,$page_size){
        $request_url = $this->url.'api/video/list';
        $param['mch_id'] = $this->mch_id;
        $param['page'] = $page;
        $param['page_size'] = $page_size;
        $this->sign = $this->generateSign($param,$this->key);
        $param['sign'] = $this->sign;
        $result = http_post($request_url,json_encode($param,true));
        return $result;
    }

    /**
     * @remark :删除详情数据
     * @name   :delDetail
     * @author :lyh
     * @method :post
     * @time   :2025/4/30 16:00
     */
    public function delVideoDetail($task_id){
        $param['task_id'] = $task_id;
        $request_url = $this->url.'api/video/delete';
        $param['mch_id'] = $this->mch_id;
        $this->sign = $this->generateSign($param,$this->key);
        $param['sign'] = $this->sign;
        $result = http_post($request_url,json_encode($param,true));
        return $result;
    }

    /**
     * @remark :计算签名
     * @name   :generateSign
     * @author :lyh
     * @method :post
     * @time   :2025/2/13 15:07
     */
    public function generateSign($params, $key)
    {
        // 去除数组中所有值为空的项
        array_filter($params);
        // 按照key值的ASCII码从小到大排序
        ksort($params);
        // 生成URL的查询字符串
        $string = http_build_query($params);
        // 生成签名
        $sign = md5($string . $key);
        // 转换成大写
        $sign = strtoupper($sign);
        return $sign;
    }
}