Yijian.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace app\service\api;
  3. use app\service\ConfServiceFacade;
  4. use laytp\traits\Error;
  5. use think\facade\Cache;
  6. use think\facade\Config;
  7. use think\facade\Request;
  8. class Yijian
  9. {
  10. use Error;
  11. /**
  12. * phpmailer对象
  13. */
  14. protected $client;
  15. protected $conf;
  16. const API_URL = 'http://api.yjai.art:8080/painting-open-api/site/';
  17. private $AK;
  18. private $SK;
  19. private $engine;
  20. private $timestamp;
  21. /**
  22. * 构造函数
  23. */
  24. public function __construct()
  25. {
  26. global $_GPC;
  27. $conf = ConfServiceFacade::groupGet('system.ai');
  28. $config = ConfServiceFacade::groupGet('system.config');
  29. if(empty($conf)){
  30. return $this->error('未配置绘画接口参数');
  31. }
  32. if(!empty($config['yjai_engine'])){
  33. $this->engine = $config['yjai_engine'];
  34. }else{
  35. $this->engine = 'stable_diffusion';
  36. }
  37. $this->AK = $conf['api_key_yj'];
  38. $this->SK = $conf['api_secret_yj'];
  39. $this->timestamp = time();
  40. }
  41. // 开始绘画
  42. public function put_task($prompt){
  43. $url = self::API_URL."put_task";
  44. $data = array(
  45. 'apikey'=>$this->AK,
  46. 'timestamp'=>$this->timestamp,
  47. 'prompt'=>$prompt,
  48. 'engine'=>$this->engine,
  49. 'ratio'=>0,
  50. 'style'=>'',
  51. 'guidence_scale'=>7.5
  52. );
  53. $n = $this->http_post($url,$data);
  54. return $n;
  55. }
  56. // 获取任务详情
  57. public function show_task($uuid){
  58. $url = self::API_URL."show_task_detail";
  59. $data = array(
  60. 'apikey'=>$this->AK,
  61. 'timestamp'=>$this->timestamp,
  62. 'uuid'=>$uuid,
  63. );
  64. $n = $this->http_post($url,$data);
  65. return $n;
  66. }
  67. // 获取完成的任务
  68. public function show_complete_tasks($uuid){
  69. $url = self::API_URL."show_complete_tasks";
  70. $data = array(
  71. 'apikey'=>$this->AK,
  72. 'timestamp'=>$this->timestamp
  73. );
  74. $n = $this->http_post($url,$data);
  75. return $n;
  76. }
  77. // 获取用户信息
  78. public function getUserInfo(){
  79. // return $this->getsign();
  80. $url = self::API_URL."getUserInfo";
  81. $data = array(
  82. 'apikey'=>$this->AK,
  83. 'timestamp'=>$this->timestamp,
  84. );
  85. $n = $this->http_post($url,$data);
  86. return $n;
  87. }
  88. private function getsign($data=array()) {
  89. $arr = array_merge(array('apisecret'=>$this->SK),$data);
  90. ksort($arr);
  91. foreach ($arr as $key => $val) {
  92. $param[] = $key . "=" .($val);
  93. }
  94. $sparam = join("&", $param);
  95. // return $sparam;
  96. $s = md5($sparam);
  97. return $s;
  98. }
  99. /**
  100. * POST 请求
  101. * @param string $url
  102. * @param array $param
  103. * @param boolean $post_file 是否文件上传
  104. * @return string content
  105. */
  106. private function http_post($url, $param, $jsonpost = false, $post_file = false) {
  107. $oCurl = curl_init();
  108. if (stripos($url, "https://") !== FALSE) {
  109. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  110. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, false);
  111. curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
  112. }
  113. $httpHeaders = array("Content-Type:application/x-www-form-urlencoded","sign:".$this->getsign($param));
  114. // return array($this->getsign($param),$param);
  115. if ($jsonpost) {
  116. $strPOST = str_replace('\/', '/', json_encode($param));
  117. } elseif (is_string($param) || $post_file) {
  118. $strPOST = $param;
  119. } else {
  120. $aPOST = array();
  121. foreach ($param as $key => $val) {
  122. $aPOST[] = $key . "=" .($val);
  123. }
  124. $strPOST = join("&", $aPOST);
  125. }
  126. curl_setopt($oCurl, CURLOPT_URL, $url);
  127. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  128. curl_setopt($oCurl, CURLOPT_POST, true);
  129. curl_setopt($oCurl, CURLOPT_HTTPHEADER, $httpHeaders);
  130. curl_setopt($oCurl, CURLOPT_POSTFIELDS, $strPOST);
  131. $sContent = curl_exec($oCurl);
  132. $sContent = json_decode($sContent,true);
  133. return $sContent;
  134. $aStatus = curl_getinfo($oCurl);
  135. curl_close($oCurl);
  136. if ($sContent["status"] == 200) {
  137. return $sContent;
  138. } else {
  139. return false;
  140. }
  141. }
  142. public function getError() {
  143. return $this->error;
  144. }
  145. }