AiBlogService.php 7.3 KB
<?php
/**
 * @remark :
 * @name   :AiBlogService.php
 * @author :lyh
 * @method :post
 * @time   :2025/2/13 14:15
 */

namespace App\Services;

class AiBlogService
{
    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
    /**
     * @remark :创建项目
     * @name   :createProject
     * @author :lyh
     * @method :post
     * @time   :2025/2/13 14:28
     */
    public function createProject($project_name,$language = 'en',$profile){
        $request_url = $this->url.'api/project/create';
        $param = [
            'mch_id'=>$this->mch_id,
            'title'=>$project_name,
            'language'=>$language,
            'profile'=>$profile
        ];
        $this->sign = $this->generateSign($param,$this->key);
        $param['sign'] = $this->sign;
        $result = http_post($request_url,json_encode($param,true));
        return $result;
    }

    /**
     * @remark :更新项目
     * @name   :updatedProject
     * @author :lyh
     * @method :post
     * @time   :2025/2/13 14:35
     */
    public function updatedProject($project_name,$language = 'en'){
        $request_url = $this->url.'api/project/save';
        $param = [
            'mch_id'=>$this->mch_id,
            'title'=>$project_name,
            'language'=>$language
        ];
        $this->sign = $this->generateSign($param,$this->key);
        $param['sign'] = $this->sign;
        $result = http_post($request_url,json_encode($param,true));
        return $result;
    }

    /**
     * @remark :创建任务
     * @name   :createTask
     * @author :lyh
     * @method :post
     * @time   :2025/2/13 14:39
     * @param  :type=(1作者2文章) keyword=关键词 subtype=blog url=回调url
     */
    public function createTask($keyword,$type = 2,$subtype = 'Blog',$anchor = []){
        $request_url = $this->url.'api/task/create';
        $param = [
            'keyword'=>$keyword,
            'type'=>$type,
            'subtype'=>$subtype,
        ];
        $param['anchor'] = $anchor;
        $param['url'] = $this->route;
        $param['mch_id'] = $this->mch_id;
        $param['template_id'] = 1;
        $this->sign = $this->generateSign($param,$this->key);
        $param['sign'] = $this->sign;
        $result = http_post($request_url,json_encode($param,true));
        return $result;
    }

    /**
     * @remark :创建作者
     * @name   :createAuthor
     * @author :lyh
     * @method :post
     * @time   :2025/2/13 14:43
     */
    public function createAuthor(){
        $request_url = $this->url.'api/author/create';
        $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   :getAuthor
     * @author :lyh
     * @method :post
     * @time   :2025/2/21 10:57
     */
    public function getAuthor(){
        $request_url = $this->url.'api/author/list';
        $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;
    }

    /**
     * @remark :获取文章详情
     * @name   :getDetail
     * @author :lyh
     * @method :post
     * @time   :2025/2/14 11:23
     */
    public function getDetail(){
        $request_url = $this->url.'api/result/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   :getAuthorDetail
     * @author :lyh
     * @method :post
     * @time   :2025/2/21 11:55
     */
    public function getAuthorDetail(){
        $request_url = $this->url.'api/author/detail';
        $param = [
            'mch_id'=>$this->mch_id,
            'author_id'=>$this->author_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  :title , thumb , route , task_id
     */
    public function updateDetail($param){
        $request_url = $this->url.'api/result/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   :getAiBlogList
     * @author :lyh
     * @method :post
     * @time   :2025/2/21 15:51
     */
    public function getAiBlogList($page,$page_size){
        $request_url = $this->url.'api/result/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   :updateAuthorInfo
     * @author :lyh
     * @method :post
     * @time   :2025/2/21 16:00
     * @param  :author_id ,title , picture, description
     */
    public function updateAuthorInfo($param){
        $request_url = $this->url.'api/author/update';
        $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   :updateAuthorInfo
     * @author :lyh
     * @method :post
     * @time   :2025/3/4 16:22
     * @param  :task_id
     */
    public function delDetail($task_id){
        $param['task_id'] = $task_id;
        $request_url = $this->url.'api/result/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;
    }
}