Gallery.php 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. <?php
  2. namespace app\controller\api;
  3. use laytp\controller\Api;
  4. use app\service\api\UserServiceFacade;
  5. use app\service\ConfServiceFacade;
  6. use app\service\api\SdapiServiceFacade;
  7. use app\service\ChatgptServiceFacade;
  8. use app\service\MemberServiceFacade;
  9. use app\service\api\MiniappServiceFacade;//微信小程序服务
  10. use app\service\api\ImageCensorServiceFacade;
  11. use app\service\api\InsServiceFacade;
  12. use app\service\BillServiceFacade;
  13. use think\facade\Db;
  14. use laytp\library\UploadDomain;
  15. use think\facade\Filesystem;
  16. use plugin\ali_oss\service\Oss;
  17. use plugin\qiniu_kodo\service\Kodo;
  18. use laytp\library\Random;
  19. /**
  20. * 收藏相关
  21. * @ApiWeigh (90)
  22. */
  23. class Gallery extends Api
  24. {
  25. // 无需无需登录的接口,*表示全部
  26. public $noNeedLogin = ['all','detail','notify','chat','test','mjNotify','getModels','getWords'];
  27. protected function _initialize()
  28. {
  29. $this->model = new \app\model\Gallery();
  30. }
  31. public function create()
  32. {
  33. global $_GPC;
  34. $modelVipinfo =new \app\model\Vipinfo();
  35. $uuid = Random::uuid();
  36. $platform = $this->request->header('platform','H5');
  37. $post = $this->request->post();
  38. $negativePrompt = $this->request->post('negative_prompt','');
  39. $engine = $this->request->post('engine','sd');
  40. $samples = $this->request->post('samples',1);
  41. $conf = ConfServiceFacade::groupGet('system.config');
  42. $coin = ConfServiceFacade::get('system.plan.unlock_aipainting',0);
  43. if(empty($post['prompt'])){
  44. return $this->error('请填写文本');
  45. }
  46. // if(empty($post['type'])){
  47. // return $this->error('请设置类型');
  48. // }
  49. // if(empty($samples) || $samples >4){
  50. // return $this->error('张数设置不对');
  51. // }
  52. $loginUserInfo = UserServiceFacade::getUserInfo();
  53. $checkMember = MemberServiceFacade::check($loginUserInfo,$engine,$coin*$samples,$conf);
  54. if(isset($checkMember['status']) && !$checkMember['status']){
  55. return $this->error($checkMember['msg'],2);
  56. }
  57. $res = InsServiceFacade::opensd($post);
  58. if(empty($res)){
  59. return $this->error('请求错误');
  60. }
  61. $res =@json_decode($res,true);
  62. if($res['code'] != 200){
  63. return $this->error($res['msg']);
  64. }
  65. $post['paid_coin'] = $coin;
  66. $post['uid'] =$loginUserInfo['id'];
  67. $post['uniacid'] = $_GPC['uniacid'];
  68. $post['engine'] = 'sd';
  69. $post['uuid'] = $res['data']['id'];
  70. $post['job_id'] = $res['data']['id'];
  71. $post['status'] = 1;
  72. $response = $this->model->save($post);
  73. if(!$response){
  74. return $this->error('数据库存入失败');
  75. }
  76. // if($res2['data']['status'] == 'success'){
  77. // if($platform == 'wxMiniProgram'){
  78. // $app = MiniappServiceFacade::option();
  79. // $filePath = $this->_put_file_from_url_content($res2['data']['output'][0]);
  80. // $check = $app->content_security->checkImage(IA_ROOT_WK.'/public/static/storage/' . $filePath['saveName']);
  81. // // print_r($check);
  82. // if($check['errcode'] !=0){
  83. // $this->model->destroy($this->model->id);
  84. // @unlink(IA_ROOT_WK.'/public/static/storage/' . $filePath['saveName']);
  85. // return $this->error('抱歉,图片内容安全监测不通过',2);
  86. // }
  87. // $this->model->where(['id'=>$this->model->id ])->update(['status'=>1]);
  88. // }elseif($platform == 'wxOfficialAccount' || $platform == 'H5'){
  89. // if(!empty($conf) && !empty($conf['is_h5_pic_filter']) && $conf['is_h5_pic_filter'] == 1){
  90. // $check = ImageCensorServiceFacade::imageCensorUserDefined($res2['data']['output'][0]);
  91. // // print_r($check);
  92. // // if(isset($check['error_code'])){
  93. // // $this->model->destroy($this->model->id);
  94. // // return $this->error('抱歉,图片内容安全检测不通过:'.$check['error_msg'],2);
  95. // // }
  96. // if(isset($check['conclusion']) && $check['conclusion']=='不合规'){
  97. // $this->model->destroy($this->model->id);
  98. // return $this->error('抱歉,图片内容安全检测不通过',2);
  99. // }
  100. // $this->model->where(['id'=>$this->model->id ])->update(['status'=>1]);
  101. // }
  102. // }
  103. // }
  104. if($res['code'] ==200){
  105. if($coin*$samples > 0){
  106. // \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
  107. // BillServiceFacade::record(0,$coin*$samples,'sd','stable diffusion 绘画生成消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
  108. $checkMember = MemberServiceFacade::cash($loginUserInfo,$engine,$coin*$samples,$conf);
  109. if(isset($checkMember['status']) && !$checkMember['status']){
  110. return $this->error($checkMember['msg'],2);
  111. }
  112. }
  113. }
  114. // print_r($res['data']);
  115. return $this->success('操作成功',$res['data']);
  116. }
  117. public function getOpensdDetail(){
  118. global $_GPC;
  119. $id = $this->request->param('id','');
  120. if(!$id){
  121. return $this->error('请上传查询ID');
  122. }
  123. $res = InsServiceFacade::opensdDetail($id);
  124. $res = @json_decode($res,true);
  125. if($res['code'] != 200){
  126. return error($res['msg']);
  127. }
  128. if($res['data']['state'] =='fail'){
  129. $info = $this->model->where('uuid','=',$id)->find();
  130. if(!empty($info)){
  131. $this->model->destroy($info['id']);
  132. return $this->error('抱歉,图片内容安全监测不通过'.$res['data']['fail_reason']);
  133. }
  134. }
  135. if($res['data']['state'] == 'success'){
  136. $uploadImageInfo = $this->uploadImage($res['data']['gen_img'],$_GPC['uniacid']);
  137. $data=[
  138. 'done'=>1,
  139. 'imgs' =>$uploadImageInfo['id']
  140. ];
  141. $this->model->where('uuid',$id)->update($data);
  142. }
  143. return $this->success('操作成功',$res['data']);
  144. }
  145. public function imagine()
  146. {
  147. global $_GPC;
  148. $uuid = Random::uuid();
  149. $modelGallery = new \app\model\Gallery();
  150. $prompt = $this->request->post('prompt','');
  151. $platform = $this->request->header('platform','H5');
  152. $action = $this->request->post('action','imagine');
  153. $loginUserInfo = UserServiceFacade::getUserInfo();
  154. $conf = ConfServiceFacade::groupGet('system.config');
  155. $coin = ConfServiceFacade::get('system.plan.unlock_mj_aipainting',0);
  156. $engine = ConfServiceFacade::get('system.config.mj_api_engine','ins');
  157. $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
  158. $hash = $this->request->post('hash','');
  159. $id = $this->request->post('id',0);
  160. $index = $this->request->post('index',1);
  161. $pid = $this->request->post('pid',0);
  162. $version='--v 5.1';
  163. if(!$action){
  164. return $this->error('请求方式不正确');
  165. }
  166. $checkMember = MemberServiceFacade::check($loginUserInfo,'mj',$coin,$conf);
  167. if(isset($checkMember['status']) && !$checkMember['status']){
  168. return $this->error($checkMember['msg'],2);
  169. }
  170. if($action == 'upscale' || $action == 'variation'){
  171. if($engine=='ins'&&!$hash){
  172. return $this->error('请传入hash');
  173. }
  174. if(!$id){
  175. return $this->error('请传入请传入id');
  176. }
  177. // $info = $this->model->find($pid);
  178. // if(!$info){
  179. // return $this->error('请传入上张图片id');
  180. // }
  181. }
  182. $data['prompt'] = $prompt;
  183. $data['paid_coin'] = $coin;
  184. $data['uid'] =$loginUserInfo['id'];
  185. $data['uniacid'] = $_GPC['uniacid'];
  186. $data['engine'] = 'mj';
  187. $data['uuid'] = $uuid;
  188. $data['mj_action'] = $action;
  189. $data['mj_index'] = $index;
  190. $data['job_id'] = $uuid;
  191. $data['status'] = 0;
  192. $data['mj_api_engine'] =$engine;
  193. $response = $modelGallery->save($data);
  194. if(!$response){
  195. return $this->error('数据库存入失败');
  196. }
  197. $API_URL = 'https://api.wike.cc/api/painting/zsy';
  198. header('Content-Type: text/event-stream');
  199. header('Cache-Control: no-cache');
  200. header('Connection: keep-alive');
  201. header('X-Accel-Buffering: no');
  202. $headers = [
  203. 'Accept: application/x-ndjson',
  204. 'Content-Type: application/json'
  205. ];
  206. // $prompt='1girl';
  207. $notify = IS_R==1?request()->domain() . SURL . '/api.gallery/mjNotify?uuid='.$data['uuid']:request()->domain() . SURL . '/index.php/api.gallery/mjNotify?uuid='.$data['uuid'];
  208. if($platform == 'wxMiniProgram'){
  209. $callback=$notify;
  210. }else{
  211. $callback='';
  212. $data['stream'] = true;
  213. }
  214. $ins = ConfServiceFacade::groupGet('system.ai');
  215. $data['key']=$ins['ins_api_key'];
  216. $data['id'] = $id;
  217. $get_post_data = http_build_query($data);
  218. $callbacks = function ($ch, $data) use($platform,$uuid,$loginUserInfo,$engine,$coin,$conf,$_GPC,$modelGallery) {
  219. $complete = @json_decode($data,true);
  220. if(!empty($complete['code'])){
  221. // if (strpos($data, 'The prompt has blocked words') !== false) {
  222. // echo '包含违禁词';
  223. // }else{
  224. echo $data;
  225. $this->model->destroy($modelGallery->id);
  226. // }
  227. exit;
  228. }
  229. if(!empty($complete['progress']) && $complete['progress'] == 100){
  230. if($coin >0){
  231. $checkMember = MemberServiceFacade::cash($loginUserInfo,'mj',$coin,$conf);
  232. if(isset($checkMember['status']) && !$checkMember['status']){
  233. echo $checkMember['msg'];
  234. exit;
  235. }
  236. }
  237. $mjCdn = 'mjcdn.iduomi.cc';
  238. $reg = '/(https):\/\/([^\/]+)/i';
  239. $replace = preg_replace($reg, 'https://'.$mjCdn, $complete['image_url']);
  240. $arr['mj_id'] = $complete['image_id'];
  241. $arr['origin_url'] = $replace;
  242. // $uploadImageInfo = $this->uploadImage($complete['image_url'],$_GPC['uniacid']);
  243. // print_r($uploadImageInfo);
  244. // if(!empty($uploadImageInfo)){
  245. // $arr['imgs'] = $uploadImageInfo['id'];
  246. // }
  247. $arr['done'] = 1;
  248. $arr['status'] = 1;
  249. $res = $modelGallery->where('uuid',$uuid)->update($arr);
  250. echo $data;
  251. exit;
  252. }else{
  253. echo $data;
  254. }
  255. ob_flush();
  256. flush();
  257. return strlen($data);
  258. };
  259. if($platform == 'wxMiniProgram' || $engine == 'ins'){
  260. if($engine == 'ins'){
  261. $res = InsServiceFacade::mj($data);
  262. }elseif($engine == 'zsy'){
  263. $res = InsServiceFacade::zsy($data);
  264. }
  265. if(empty($res)){
  266. return $this->error('请求错误');
  267. }
  268. $res2 =json_decode($res,true);
  269. if($res2['code'] ==400 || $res2['code'] ==403){
  270. $this->model->destroy($this->model->id);
  271. return $this->error($res2['msg'],2);
  272. }
  273. if($res2['code'] ==401 || $res2['code'] ==402){
  274. $this->model->destroy($this->model->id);
  275. return $this->error($res2['msg']);
  276. }
  277. // print_r($res2);
  278. // return;
  279. if($coin >0){
  280. $checkMember = MemberServiceFacade::cash($loginUserInfo,'mj',$coin,$conf);
  281. if(isset($checkMember['status']) && !$checkMember['status']){
  282. return $this->error($checkMember['msg'],2);
  283. }
  284. }
  285. return $this->success('操作成功',$uuid);
  286. }else{
  287. $ch = curl_init();
  288. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  289. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  290. curl_setopt($ch, CURLOPT_URL, $API_URL. '?' . $get_post_data);
  291. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  292. // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  293. curl_setopt($ch, CURLOPT_POST, true);
  294. curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
  295. curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callbacks);
  296. curl_exec($ch);
  297. }
  298. }
  299. public function upscale(){
  300. global $_GPC;
  301. $uuid = Random::uuid();
  302. $post = $this->request->post();
  303. $prompt = $this->request->post('prompt','');
  304. $hash = $this->request->post('hash','');
  305. $id = $this->request->post('id',0);
  306. $index = $this->request->post('index',1);
  307. $pid = $this->request->post('pid',0);
  308. // $pid = $this->request->post('pid',1);
  309. $loginUserInfo = UserServiceFacade::getUserInfo();
  310. $coin = ConfServiceFacade::get('system.plan.unlock_mj_aipainting',0);
  311. $engine = ConfServiceFacade::get('system.config.mj_api_engine','ins');
  312. $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
  313. $version='--v 5.1';
  314. if($loginUserInfo['coin'] <$coin){
  315. return $this->error($numberAlias.'不足',2);
  316. }
  317. if($engine=='ins'&&!$hash){
  318. return $this->error('请传入hash');
  319. }
  320. if(!$id){
  321. return $this->error('请传入请传入id');
  322. }
  323. $info = $this->model->find($pid);
  324. if(!$info){
  325. return $this->error('请传入上张图片id');
  326. }
  327. $data['prompt'] = $info['prompt'];
  328. $data['paid_coin'] = $coin;
  329. $data['uid'] =$loginUserInfo['id'];
  330. $data['uniacid'] = $_GPC['uniacid'];
  331. $data['engine'] = 'mj';
  332. $data['uuid'] = $uuid;
  333. $data['mj_action'] = 'upscale';
  334. $data['mj_index'] = $index;
  335. $data['hash'] = $hash;
  336. $data['pid'] = $pid;
  337. $data['job_id'] = $uuid;
  338. $data['status'] = 0;
  339. $data['mj_api_engine'] =$engine;
  340. $response = $this->model->save($data);
  341. if(!$response){
  342. return $this->error('数据库存入失败');
  343. }
  344. $data['id'] = $id;
  345. if($engine == 'ins'){
  346. $res = InsServiceFacade::mj($data);
  347. }elseif($engine == 'zsy'){
  348. $res = InsServiceFacade::zsy($data);
  349. }
  350. // print_r($res);
  351. if(empty($res)){
  352. return $this->error('请求错误');
  353. }
  354. $res2 =json_decode($res,true);
  355. // print_r($res2);
  356. // return;
  357. if(empty($res2) || (!empty($res2['code']) && $res2['code'] ==403) || !empty($res2['code']) && $res2['code'] ==400){
  358. return $this->error('参数错误:'.$res2['msg']);
  359. }
  360. if($coin >0){
  361. \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
  362. BillServiceFacade::record(0,$coin,'mj','Midjourney 绘画生成消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
  363. }
  364. return $this->success('操作成功',$uuid);
  365. }
  366. public function variation(){
  367. global $_GPC;
  368. $uuid = Random::uuid();
  369. $post = $this->request->post();
  370. $prompt = $this->request->post('prompt','');
  371. $hash = $this->request->post('hash','');
  372. $id = $this->request->post('id',0);
  373. $index = $this->request->post('index',1);
  374. $pid = $this->request->post('pid',0);
  375. // $pid = $this->request->post('pid',1);
  376. $loginUserInfo = UserServiceFacade::getUserInfo();
  377. $coin = ConfServiceFacade::get('system.plan.unlock_mj_aipainting',0);
  378. $engine = ConfServiceFacade::get('system.config.mj_api_engine','ins');
  379. $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
  380. $version='--v 5.1';
  381. if($loginUserInfo['coin'] <$coin){
  382. return $this->error($numberAlias.'不足',2);
  383. }
  384. if($engine=='ins'&&!$hash){
  385. return $this->error('请传入hash');
  386. }
  387. if(!$id){
  388. return $this->error('请传入请传入id');
  389. }
  390. $info = $this->model->find($pid);
  391. if(!$info){
  392. return $this->error('请传入上张图片id');
  393. }
  394. $data['prompt'] = $info['prompt'];
  395. $data['paid_coin'] = $coin;
  396. $data['uid'] =$loginUserInfo['id'];
  397. $data['uniacid'] = $_GPC['uniacid'];
  398. $data['engine'] = 'mj';
  399. $data['uuid'] = $uuid;
  400. $data['mj_action'] = 'variation';
  401. $data['mj_index'] = $index;
  402. $data['hash'] = $hash;
  403. $data['pid'] = $pid;
  404. $data['job_id'] = $uuid;
  405. $data['status'] = 0;
  406. $data['mj_api_engine'] =$engine;
  407. $response = $this->model->save($data);
  408. if(!$response){
  409. return $this->error('数据库存入失败');
  410. }
  411. $data['id'] = $id;
  412. if($engine == 'ins'){
  413. $res = InsServiceFacade::mj($data);
  414. }elseif($engine == 'zsy'){
  415. $res = InsServiceFacade::zsy($data);
  416. }
  417. // print_r($res);
  418. if(empty($res)){
  419. return $this->error('请求错误');
  420. }
  421. $res2 =json_decode($res,true);
  422. // print_r($res2);
  423. // return;
  424. if(empty($res2) || (!empty($res2['code']) && $res2['code'] ==403) || !empty($res2['code']) && $res2['code'] ==400){
  425. return $this->error('参数错误:'.$res2['msg']);
  426. }
  427. \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
  428. BillServiceFacade::record(0,$coin,'mj','Midjourney 绘画生成消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
  429. return $this->success('操作成功',$uuid);
  430. }
  431. public function superResolution()
  432. {
  433. $uuid = Random::uuid();
  434. $post = $this->request->post();
  435. $post['uuid']= $uuid;
  436. $res = InsServiceFacade::super_resolution($post);
  437. if(empty($res)){
  438. return $this->error('请求错误');
  439. }
  440. $res2 =json_decode($res,true);
  441. // print_r($res2);
  442. // return;
  443. if(empty($res2) || (!empty($res2['code']) && $res2['code'] ==403) || !empty($res2['code']) && $res2['code'] ==400){
  444. return $this->error('参数错误:'.$res2['msg']);
  445. }
  446. // print_r($res['data']);
  447. return $this->success('操作成功',$res2['data']);
  448. }
  449. public function getAsyncResult()
  450. {
  451. $uuid = $this->request->param('uuid',0);
  452. if (!$uuid) {
  453. return $this->error('请上传任务请求id',2);
  454. }
  455. $res = $this->model->where('uuid',$uuid)->find();
  456. if(!$res){
  457. return $this->error('不存在此uuid',2);
  458. }
  459. return $this->success('操作成功',$res);
  460. }
  461. public function fetch()
  462. {
  463. global $_GPC;
  464. $param = $this->request->param();
  465. $platform = $this->request->header('platform','H5');
  466. $id = $this->request->param('id','');
  467. $uuid = $this->request->param('uuid','');
  468. if(!$id){
  469. return $this->error('请上传请求id',2);
  470. }
  471. $res = InsServiceFacade::sd_result($id);
  472. if(empty($res)){
  473. return $this->error('请求错误');
  474. }
  475. $res2 =json_decode($res,true);
  476. // print_r($res2);
  477. // return;
  478. if(empty($res2) || (!empty($res2['code']) && $res2['code'] ==403) || !empty($res2['code']) && $res2['code'] ==400){
  479. return $this->error('参数错误.'.$res2['msg']);
  480. }
  481. if($res2['data']['status'] == 'success'){
  482. if($platform == 'wxMiniProgram'){
  483. $app = MiniappServiceFacade::option();
  484. $filePath = $this->_put_file_from_url_content($res2['data']['output'][0]);
  485. $check = $app->content_security->checkImage(IA_ROOT_WK.'/public/static/storage/' . $filePath['saveName']);
  486. if($check['errcode'] !=0){
  487. $info = $this->model->where('uuid','=',$uuid)->find();
  488. if(!empty($info)){
  489. $this->model->destroy($info['id']);
  490. return $this->error('抱歉,图片内容安全监测不通过',2);
  491. }
  492. }
  493. }elseif($platform == 'wxOfficialAccount' || $platform == 'H5'){
  494. $check = ImageCensorServiceFacade::imageCensorUserDefined($res2['data']['output'][0]);
  495. // print_r($check);
  496. if(isset($check['error_code'])){
  497. $info = $this->model->where('uuid','=',$uuid)->find();
  498. if(!empty($info)){
  499. $this->model->destroy($info['id']);
  500. return $this->error('抱歉,图片内容安全检测不通过:'.$check['error_msg'],2);
  501. }
  502. }
  503. if(isset($check['conclusion']) && $check['conclusion']=='不合规'){
  504. $info = $this->model->where('uuid','=',$uuid)->find();
  505. if(!empty($info)){
  506. $this->model->destroy($info['id']);
  507. return $this->error('抱歉,图片内容安全监测不通过',2);
  508. }
  509. }
  510. }
  511. }
  512. return $this->success('操作成功',$res2['data']);
  513. }
  514. public function chat(){
  515. global $_GPC;
  516. $loginUserInfo = UserServiceFacade::getUserInfo();
  517. $modelQuestion = new \app\model\Question();
  518. $prompt = $this->request->post('prompt','');
  519. $guide = $this->request->post('guide','');
  520. $answer = $this->request->post('answer','');
  521. $platform = $this->request->header('platform','');
  522. $conf = ConfServiceFacade::groupGet('system.config', 0);
  523. $type = $this->request->post('type','gpt35');
  524. $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
  525. if(!$prompt){
  526. return $this->error('请填写问题');
  527. }
  528. // $clearPrompt = $this->wordFilter($prompt);
  529. // $proxy = $this->getUrl($type,$conf);
  530. $guide='分别以"中文:"和"英文:"格式返回返回,将用户的话生成高质量的描述词';
  531. $text='我希望你能担任英语翻译、拼写校对和修辞改进的角色。我会将翻译的结果用于如stable diffusion、midjourney等绘画场景生成图片,语言要求尽量优美。我会用任何语言和你交流,你会识别语言,将其翻译为英语并仅回答翻译的最终结果,不要写解释。我的第一句话是:'.$prompt.'。请立刻翻译,不要回复其它内容。';
  532. $res = ChatgptServiceFacade::chat($prompt,$guide,'',$platform);
  533. if(!$res){
  534. return $this->error('请求错误,请稍后重试',3);
  535. }
  536. // if($platform == 'wxMiniProgram'){
  537. // $check = MiniappServiceFacade::msgSecCheck($res,$loginUserInfo['openid_miniapp']);
  538. // if(!$check){
  539. // return $this->error('失败,文字内容安全检测不通过!');
  540. // }
  541. // }elseif($platform == 'wxOfficialAccount' || $platform == 'H5'){
  542. // if(!empty($conf) && !empty($conf['is_h5_pic_filter']) && $conf['is_h5_pic_filter'] == 1){
  543. // $check = ImageCensorServiceFacade::textCensorUserDefined($res);
  544. // if(!empty($check['conclusion']) && $check['conclusion'] == '不合规' ){
  545. // return $this->error('失败,文字内容安全检测不通过!');
  546. // }
  547. // }
  548. // }
  549. // if(!empty($conf['time_member']) && $conf['time_member'] ==1){
  550. // if(empty($loginUserInfo['vip_time'])){
  551. // if($loginUserInfo['coin'] <=0){
  552. // return $this->error($numberAlias.'不足',2);
  553. // }else{
  554. // \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',1)->update();
  555. // BillServiceFacade::record(0,1,'chatgpt','问答消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
  556. // }
  557. // }else{
  558. // if($loginUserInfo['vip_time'] < time() && $loginUserInfo['vip_time'] !=9999){
  559. // if($loginUserInfo['coin'] <=0){
  560. // return $this->error($numberAlias.'不足',2);
  561. // }else{
  562. // \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',1)->update();
  563. // BillServiceFacade::record(0,1,'chatgpt','问答消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
  564. // }
  565. // }
  566. // }
  567. // }else{
  568. // if($loginUserInfo['coin'] <=0){
  569. // return $this->error($numberAlias.'不足',2);
  570. // }else{
  571. // \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',1)->update();
  572. // BillServiceFacade::record(0,1,'chatgpt','问答消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
  573. // }
  574. // }
  575. // $question = $modelQuestion->save(['uid'=>$loginUserInfo['id'],'question'=>$prompt,'answer'=>$res,'uniacid'=>$_GPC['uniacid'] ]);
  576. return $this->success('操作成功',$res);
  577. }
  578. public function getWords()
  579. {
  580. global $_GPC;
  581. $where = ['uniacid' =>$_GPC['uniacid']];
  582. $order = ['sort' => 'DESC','id'=>'DESC'];
  583. $words = \app\model\Words::where($where)->orderRaw("rand() , id desc")->limit(20)->select();
  584. if(!$words){
  585. return $this->error('暂无描述提示词',2);
  586. }
  587. return $this->success('数据获取成功', $words);
  588. }
  589. public function setOpen()
  590. {
  591. global $_GPC;
  592. $id = $this->request->post('id',0);
  593. if(!$id){
  594. return $this->error('未获取到要修改的信息id',2);
  595. }
  596. $info = $this->model->where('id','=',$id)->update(['is_open'=>1]);
  597. return $this->success('获取成功', $info);
  598. }
  599. public function getModels()
  600. {
  601. global $_GPC;
  602. $engine = $this->request->param('engine','sd');
  603. // $where = ['engine'=>$engine];
  604. // $order = ['sort' => 'DESC','id'=>'DESC'];
  605. // $data =\app\model\gallery\Models::where($where)->order($order)->sheepFilter()->paginate(request()->param('pageSize', 10));
  606. $res = InsServiceFacade::model($engine);
  607. $res = @json_decode($res,true);
  608. return $this->success('获取成功', $res['data']);
  609. }
  610. // 列表
  611. public function list(){
  612. global $_GPC;
  613. $loginUserInfo = UserServiceFacade::getUserInfo();
  614. $param = $this->request->param();
  615. // $where[] = ['status','=',1];
  616. $where[] = ['uniacid','=',$_GPC['uniacid']];
  617. $where[] = ['uid','=',$loginUserInfo['id']];
  618. // $where[] = ['img','<>',NULL];
  619. $order = ['id' => 'desc'];
  620. $limit = $this->request->param('limit', 10);
  621. $data = $this->model->order($order)->where($where)->with(['user_info'])->paginate($limit)->toArray();
  622. if(!$data){
  623. return $this->error('数据获取失败');
  624. }
  625. return $this->success('数据获取成功', $data);
  626. }
  627. // 列表
  628. public function all(){
  629. global $_GPC;
  630. // $loginUserInfo = UserServiceFacade::getUserInfo();
  631. $param = $this->request->param();
  632. $engine = $this->request->param('engine','');
  633. if(!empty($engine)){
  634. $where[] = ['engine','=',$engine];
  635. }
  636. $where[] = ['uniacid','=',$_GPC['uniacid']];
  637. $where[] = ['is_open','=',1];
  638. $order = ['id' => 'desc'];
  639. $limit = $this->request->param('limit', 10);
  640. $data = $this->model->order($order)->where($where)->with(['user_info','models_info'])->paginate($limit)->toArray();
  641. if(!$data){
  642. return $this->error('数据获取失败');
  643. }
  644. return $this->success('数据获取成功', $data);
  645. }
  646. /**
  647. * 详情
  648. * @param $file
  649. * @return array|boolean
  650. */
  651. public function detail()
  652. {
  653. $return = [];
  654. $id = $this->request->param('id');
  655. $info = $this->model->with(['user_info','models_info'])->find($id);
  656. if(!$info){
  657. return $this->error('详情获取失败');
  658. }
  659. // $this->model->where('id','=',$id)->inc('browse', 1)->update();
  660. return $this->success('获取成功', $info);
  661. }
  662. public function del(){
  663. global $_GPC;
  664. $loginUserInfo = UserServiceFacade::getUserInfo();
  665. $id = $this->request->post('id');
  666. if (!$id) {
  667. return $this->error('参数id不能为空',2);
  668. }
  669. $jobUid = $this->model->where('id',$id)->value('uid');
  670. if($jobUid != $loginUserInfo['id']){
  671. return $this->error('您没有删除的权限');
  672. }
  673. try{
  674. $delRes = $this->model->destroy($id);
  675. if (!$delRes) {
  676. return $this->error('删除失败');
  677. }
  678. return $this->success('删除成功');
  679. }catch (\Exception $e){
  680. return $this->exceptionError($e);
  681. }
  682. }
  683. public function notify(){
  684. global $_GPC;
  685. $sdProxy = 'sd.iduomi.cc';
  686. $reg = '/(https):\/\/([^\/]+)/i';
  687. $post = $this->request->post();
  688. // file_put_contents(__DIR__ . '/notify.json', json_encode($post));
  689. if(!empty($post) && isset($post['webhook_type']) && $post['webhook_type'] =='success'){
  690. $info = $this->model->where('uuid','=',$post['track_id'])->find();
  691. if(!$info || $info['done'] == 1){
  692. return true; //已经处理过
  693. }
  694. $_GPC['uniacid'] =$info['uniacid'];
  695. $count = count($post['output']);
  696. $arr=[];$originArr=[];
  697. if($count > 0){
  698. foreach($post['output'] as $k=>$v) {
  699. preg_match($reg, $v,$res);
  700. $replace = preg_replace($reg, 'https://'.$sdProxy, $v);
  701. // file_put_contents(__DIR__ . '/replace.json', json_encode($replace));
  702. $originArr[$k] = $replace;
  703. $uploadImageInfo = $this->uploadImage($v,$info['uniacid']);
  704. array_push($arr,$uploadImageInfo['id']);
  705. }
  706. }
  707. $data['imgs'] =implode(',',$arr);
  708. $data['origin_url']=implode(',',$originArr);
  709. $data['done'] =1;
  710. $data['real_prompt'] =$post['meta']['prompt'];
  711. $res = $this->model->where('uuid',$post['track_id'])->update($data);
  712. // file_put_contents(__DIR__ . '/res.json', json_encode($res));
  713. }
  714. }
  715. public function mjNotify(){
  716. global $_GPC;
  717. $mjCdn = 'mjcdn.iduomi.cc';
  718. $reg = '/(https):\/\/([^\/]+)/i';
  719. $post = $this->request->post();
  720. // file_put_contents(__DIR__ . '/mjNotify.json', json_encode($post));
  721. // file_put_contents(__DIR__ . '/get.json', json_encode($_GET));
  722. if(empty($_GET['uuid'])){
  723. echo false;
  724. return;
  725. }
  726. $info = $this->model->where('uuid','=',$_GET['uuid'])->find();
  727. if(empty($info) || $info['done'] == 1){
  728. echo true; //已经处理过
  729. return;
  730. }
  731. // file_put_contents(__DIR__ . '/info.json', json_encode($info));
  732. $_GPC['uniacid'] =$info['uniacid'];
  733. // if($info['mj_action'] =='imagine' || $info['mj_action'] =='imagine'){
  734. if($info['mj_api_engine'] =='ins'){
  735. $data['mj_id'] = $post['id'];
  736. $data['mj_hash'] = $post['hash'];
  737. $data['origin_url'] = $post['url'];
  738. $uploadImageInfo = $this->uploadImage($post['url'],$info['uniacid']);
  739. }elseif($info['mj_api_engine'] =='zsy'){
  740. // preg_match($reg, $post['image_url'],$res);
  741. $replace = preg_replace($reg, 'https://'.$mjCdn, $post['image_url']);
  742. $data['mj_id'] = $post['image_id'];
  743. $data['origin_url'] = $replace;
  744. $uploadImageInfo = $this->uploadImage($post['image_url'],$info['uniacid']);
  745. }
  746. if(!empty($uploadImageInfo)){
  747. $data['imgs'] = $uploadImageInfo['id'];
  748. }
  749. $data['done'] = 1;
  750. $data['status'] = 1;
  751. $res = $this->model->where('uuid',$_GET['uuid'])->update($data);
  752. // }
  753. }
  754. public function test(){
  755. $uploadImageInfo = $this->uploadImage('',2);
  756. if(!empty($uploadImageInfo)){
  757. $arr['imgs'] = $uploadImageInfo['id'];
  758. }
  759. }
  760. public function download_remote_file($file_url)
  761. {
  762. $name =md5(uniqid(mt_rand())) . ".png";
  763. $saveName = date("Ymd") . "/" . $name;
  764. $path = IA_ROOT_WK.'/public/static/storage/';
  765. $filename = $path . $saveName;
  766. if(!file_exists($path. date("Ymd")))mkdir($path. date("Ymd"), 0755,TRUE);//如果文件夹不存在,则创建
  767. $opts = [
  768. "ssl" => [
  769. // "allow_self_signed" => true,
  770. "verify_peer"=>false,
  771. "verify_peer_name"=>false,
  772. ]
  773. ];
  774. $content = file_get_contents($file_url,false, stream_context_create($opts));
  775. $res = file_put_contents($filename, $content);
  776. // print_r('file_get'.$res);
  777. if($res){
  778. $data['filename'] = $filename;
  779. $data['url'] = request()->domain() . STATIC_PATH . '/storage/' . $saveName;
  780. $data['saveName'] = $saveName;
  781. $data['name'] = $name;
  782. return $data;
  783. }else{
  784. return false;
  785. }
  786. }
  787. public function uploadImage($url,$uniacid=0){
  788. global $_GPC;
  789. if($uniacid){
  790. $_GPC['uniacid'] = $uniacid;
  791. }
  792. try {
  793. $file_url =$url;
  794. $filePath = $this->download_remote_file($file_url);
  795. if(!$filePath){
  796. return false;
  797. }
  798. $pathname = $filePath['filename'];
  799. $object = $filePath['name'];
  800. $uploadType= ConfServiceFacade::get('system.upload.defaultType', 'local');
  801. $inputValue = "";
  802. //上传至七牛云
  803. if ($uploadType == 'qiniu-kodo') {
  804. if(ConfServiceFacade::get('plugin.qiniu_kodo.switch') != 1){
  805. return $this->error('未开启七牛云KODO存储,请到七牛云KODO配置中开启');
  806. }
  807. $kodoConf = [
  808. 'accessKey' => ConfServiceFacade::get('plugin.qiniu_kodo.accessKeyID'),
  809. 'secretKey' => ConfServiceFacade::get('plugin.qiniu_kodo.secretKey'),
  810. 'bucket' => ConfServiceFacade::get('plugin.qiniu_kodo.bucket'),
  811. 'domain' => ConfServiceFacade::get('plugin.qiniu_kodo.domain'),
  812. ];
  813. $kodo = Kodo::instance();
  814. $kodoRes = $kodo->upload($pathname, $object, $kodoConf);
  815. if ($kodoRes) {
  816. $inputValue = $kodoRes;
  817. $pathname = $object;
  818. } else {
  819. return $this->error($kodo->getError());
  820. }
  821. }
  822. //上传至阿里云
  823. if ($uploadType == 'ali-oss') {
  824. if (ConfServiceFacade::get('plugin.ali_oss.switch') != 1) {
  825. return $this->error('未开启阿里云OSS存储,请到阿里云OSS配置中开启');
  826. }
  827. $ossConf = [
  828. 'accessKeyID' => ConfServiceFacade::get('plugin.ali_oss.accessKeyID'),
  829. 'accessKeySecret' => ConfServiceFacade::get('plugin.ali_oss.accessKeySecret'),
  830. 'bucket' => ConfServiceFacade::get('plugin.ali_oss.bucket'),
  831. 'endpoint' => ConfServiceFacade::get('plugin.ali_oss.endpoint'),
  832. 'domain' => ConfServiceFacade::get('plugin.ali_oss.domain'),
  833. ];
  834. $oss = Oss::instance();
  835. $ossUploadRes = $oss->upload($pathname, $object, $ossConf);
  836. if ($ossUploadRes) {
  837. $inputValue = $ossUploadRes;
  838. $pathname = $object;
  839. } else {
  840. return $this->error($oss->getError());
  841. }
  842. }
  843. //本地上传
  844. if ($uploadType == 'local') {
  845. $inputValue = request()->domain() . STATIC_PATH . '/storage/' . $filePath['saveName'];
  846. $pathname = $filePath['saveName'];
  847. }
  848. //将inputValue存入lt_files表中
  849. $filesModel = new \app\model\Files();
  850. $fileId = $filesModel->insertGetId([
  851. 'category_id' => (int)$this->request->param('file_category_id', 0),
  852. 'name' => $object,
  853. 'file_type' => 'image',
  854. 'path' => $pathname,
  855. 'upload_type' => $uploadType,
  856. 'size' => 0,
  857. 'ext' => 'png',
  858. 'create_admin_user_id' => 0,
  859. 'update_admin_user_id' => 0,
  860. 'create_time' => date('Y-m-d H:i:s'),
  861. 'update_time' => date('Y-m-d H:i:s'),
  862. 'uniacid' =>$_GPC['uniacid']
  863. ]);
  864. return ['id' => $fileId,'path' => $inputValue,
  865. 'name' => $filePath['saveName']];
  866. }catch (\Exception $e) {
  867. return $this->exceptionError($e);
  868. }
  869. }
  870. private function _put_file_from_url_content($url) {
  871. $name =md5(uniqid(mt_rand())) . ".png";
  872. $saveName = date("Ymd") . "/" . $name;
  873. $path = IA_ROOT_WK.'/public/static/storage/';
  874. // 设置运行时间为无限制
  875. set_time_limit ( 0 );
  876. $url = trim ( $url );
  877. $curl = curl_init ();
  878. // 设置你需要抓取的URL
  879. curl_setopt ( $curl, CURLOPT_URL, $url );
  880. // 设置header
  881. curl_setopt ( $curl, CURLOPT_HEADER, 0 );
  882. // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
  883. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
  884. // 运行cURL,请求网页
  885. $file = curl_exec ( $curl );
  886. // 关闭URL请求
  887. curl_close ( $curl );
  888. // 将文件写入获得的数据
  889. $filename = $path . $saveName;
  890. // 打开用来保存数据的文件,如果没有则会创建
  891. if (!file_exists($path.date("Ymd"))) {
  892. mkdir($path.date("Ymd"), 0755,true);
  893. }
  894. $write = @fopen ( $filename, "w" );
  895. if ($write == false) {
  896. return false;
  897. }
  898. // 将CURL请求返回的数据写入文件
  899. if (fwrite ( $write, $file ) == false) {
  900. return false;
  901. }
  902. // 关闭文档流
  903. if (fclose ( $write ) == false) {
  904. return false;
  905. }
  906. // file_put_contents($imgName, file_get_contents($imgFile));
  907. $data['filename'] = $filename;
  908. $data['url'] = request()->domain() . STATIC_PATH . '/storage/' . $saveName;
  909. $data['saveName'] = $saveName;
  910. $data['name'] = $name;
  911. return $data;
  912. }
  913. //小知识:php中使用最多的是通过curl来模拟get和post请求
  914. public function http_get_data($url) //框架放common.php
  915. {
  916. $ch = curl_init();
  917. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
  918. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  919. curl_setopt($ch, CURLOPT_URL, $url);
  920. ob_start();
  921. curl_exec($ch);
  922. $return_content = ob_get_contents();
  923. ob_end_clean();
  924. $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  925. return $return_content;
  926. }
  927. public function saveTheFile($url,$savePath){
  928. $file_content = $this->http_get_data($url);
  929. $fp = @fopen($savePath,'a');
  930. return fwrite($fp,$file_content);
  931. }
  932. }