123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace app\service\api;
- use app\service\ConfServiceFacade;
- use laytp\traits\Error;
- use think\facade\Cache;
- use think\facade\Config;
- use think\facade\Request;
- class Yijian
- {
- use Error;
- /**
- * phpmailer对象
- */
- protected $client;
- protected $conf;
- const API_URL = 'http://api.yjai.art:8080/painting-open-api/site/';
- private $AK;
- private $SK;
- private $engine;
- private $timestamp;
- /**
- * 构造函数
- */
- public function __construct()
- {
- global $_GPC;
- $conf = ConfServiceFacade::groupGet('system.ai');
- $config = ConfServiceFacade::groupGet('system.config');
- if(empty($conf)){
- return $this->error('未配置绘画接口参数');
- }
- if(!empty($config['yjai_engine'])){
- $this->engine = $config['yjai_engine'];
- }else{
- $this->engine = 'stable_diffusion';
- }
- $this->AK = $conf['api_key_yj'];
- $this->SK = $conf['api_secret_yj'];
- $this->timestamp = time();
- }
- // 开始绘画
- public function put_task($prompt){
- $url = self::API_URL."put_task";
- $data = array(
- 'apikey'=>$this->AK,
- 'timestamp'=>$this->timestamp,
- 'prompt'=>$prompt,
- 'engine'=>$this->engine,
- 'ratio'=>0,
- 'style'=>'',
- 'guidence_scale'=>7.5
- );
- $n = $this->http_post($url,$data);
- return $n;
- }
- // 获取任务详情
- public function show_task($uuid){
- $url = self::API_URL."show_task_detail";
- $data = array(
- 'apikey'=>$this->AK,
- 'timestamp'=>$this->timestamp,
- 'uuid'=>$uuid,
- );
- $n = $this->http_post($url,$data);
- return $n;
- }
- // 获取完成的任务
- public function show_complete_tasks($uuid){
- $url = self::API_URL."show_complete_tasks";
- $data = array(
- 'apikey'=>$this->AK,
- 'timestamp'=>$this->timestamp
- );
- $n = $this->http_post($url,$data);
- return $n;
- }
- // 获取用户信息
- public function getUserInfo(){
- // return $this->getsign();
- $url = self::API_URL."getUserInfo";
- $data = array(
- 'apikey'=>$this->AK,
- 'timestamp'=>$this->timestamp,
- );
- $n = $this->http_post($url,$data);
- return $n;
- }
- private function getsign($data=array()) {
- $arr = array_merge(array('apisecret'=>$this->SK),$data);
- ksort($arr);
- foreach ($arr as $key => $val) {
- $param[] = $key . "=" .($val);
- }
- $sparam = join("&", $param);
- // return $sparam;
- $s = md5($sparam);
- return $s;
- }
- /**
- * POST 请求
- * @param string $url
- * @param array $param
- * @param boolean $post_file 是否文件上传
- * @return string content
- */
- private function http_post($url, $param, $jsonpost = false, $post_file = false) {
- $oCurl = curl_init();
- if (stripos($url, "https://") !== FALSE) {
- curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
- }
- $httpHeaders = array("Content-Type:application/x-www-form-urlencoded","sign:".$this->getsign($param));
- // return array($this->getsign($param),$param);
- if ($jsonpost) {
- $strPOST = str_replace('\/', '/', json_encode($param));
- } elseif (is_string($param) || $post_file) {
- $strPOST = $param;
- } else {
- $aPOST = array();
- foreach ($param as $key => $val) {
- $aPOST[] = $key . "=" .($val);
- }
- $strPOST = join("&", $aPOST);
- }
- curl_setopt($oCurl, CURLOPT_URL, $url);
- curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($oCurl, CURLOPT_POST, true);
- curl_setopt($oCurl, CURLOPT_HTTPHEADER, $httpHeaders);
- curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
- $sContent = curl_exec($oCurl);
- $sContent = json_decode($sContent,true);
- return $sContent;
- $aStatus = curl_getinfo($oCurl);
- curl_close($oCurl);
- if ($sContent["status"] == 200) {
- return $sContent;
- } else {
- return false;
- }
- }
- public function getError() {
- return $this->error;
- }
- }
|