12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022 |
- <?php
- namespace app\controller\api;
- use laytp\controller\Api;
- use app\service\api\UserServiceFacade;
- use app\service\ConfServiceFacade;
- use app\service\api\SdapiServiceFacade;
- use app\service\ChatgptServiceFacade;
- use app\service\MemberServiceFacade;
- use app\service\api\MiniappServiceFacade;//微信小程序服务
- use app\service\api\ImageCensorServiceFacade;
- use app\service\api\InsServiceFacade;
- use app\service\BillServiceFacade;
- use think\facade\Db;
- use laytp\library\UploadDomain;
- use think\facade\Filesystem;
- use plugin\ali_oss\service\Oss;
- use plugin\qiniu_kodo\service\Kodo;
- use laytp\library\Random;
- /**
- * 收藏相关
- * @ApiWeigh (90)
- */
- class Gallery extends Api
- {
- // 无需无需登录的接口,*表示全部
- public $noNeedLogin = ['all','detail','notify','chat','test','mjNotify','getModels','getWords'];
- protected function _initialize()
- {
- $this->model = new \app\model\Gallery();
- }
- public function create()
- {
- global $_GPC;
- $modelVipinfo =new \app\model\Vipinfo();
- $uuid = Random::uuid();
- $platform = $this->request->header('platform','H5');
- $post = $this->request->post();
- $negativePrompt = $this->request->post('negative_prompt','');
- $engine = $this->request->post('engine','sd');
- $samples = $this->request->post('samples',1);
- $conf = ConfServiceFacade::groupGet('system.config');
- $coin = ConfServiceFacade::get('system.plan.unlock_aipainting',0);
- if(empty($post['prompt'])){
- return $this->error('请填写文本');
- }
- // if(empty($post['type'])){
- // return $this->error('请设置类型');
- // }
- // if(empty($samples) || $samples >4){
- // return $this->error('张数设置不对');
- // }
- $loginUserInfo = UserServiceFacade::getUserInfo();
- $checkMember = MemberServiceFacade::check($loginUserInfo,$engine,$coin*$samples,$conf);
- if(isset($checkMember['status']) && !$checkMember['status']){
- return $this->error($checkMember['msg'],2);
- }
- $res = InsServiceFacade::opensd($post);
- if(empty($res)){
- return $this->error('请求错误');
- }
- $res =@json_decode($res,true);
- if($res['code'] != 200){
- return $this->error($res['msg']);
- }
- $post['paid_coin'] = $coin;
- $post['uid'] =$loginUserInfo['id'];
- $post['uniacid'] = $_GPC['uniacid'];
- $post['engine'] = 'sd';
- $post['uuid'] = $res['data']['id'];
- $post['job_id'] = $res['data']['id'];
- $post['status'] = 1;
- $response = $this->model->save($post);
- if(!$response){
- return $this->error('数据库存入失败');
- }
- // if($res2['data']['status'] == 'success'){
- // if($platform == 'wxMiniProgram'){
- // $app = MiniappServiceFacade::option();
- // $filePath = $this->_put_file_from_url_content($res2['data']['output'][0]);
- // $check = $app->content_security->checkImage(IA_ROOT_WK.'/public/static/storage/' . $filePath['saveName']);
- // // print_r($check);
- // if($check['errcode'] !=0){
- // $this->model->destroy($this->model->id);
- // @unlink(IA_ROOT_WK.'/public/static/storage/' . $filePath['saveName']);
- // return $this->error('抱歉,图片内容安全监测不通过',2);
- // }
- // $this->model->where(['id'=>$this->model->id ])->update(['status'=>1]);
- // }elseif($platform == 'wxOfficialAccount' || $platform == 'H5'){
- // if(!empty($conf) && !empty($conf['is_h5_pic_filter']) && $conf['is_h5_pic_filter'] == 1){
- // $check = ImageCensorServiceFacade::imageCensorUserDefined($res2['data']['output'][0]);
- // // print_r($check);
- // // if(isset($check['error_code'])){
- // // $this->model->destroy($this->model->id);
- // // return $this->error('抱歉,图片内容安全检测不通过:'.$check['error_msg'],2);
- // // }
- // if(isset($check['conclusion']) && $check['conclusion']=='不合规'){
- // $this->model->destroy($this->model->id);
- // return $this->error('抱歉,图片内容安全检测不通过',2);
- // }
- // $this->model->where(['id'=>$this->model->id ])->update(['status'=>1]);
- // }
- // }
- // }
- if($res['code'] ==200){
- if($coin*$samples > 0){
- // \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
- // BillServiceFacade::record(0,$coin*$samples,'sd','stable diffusion 绘画生成消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
- $checkMember = MemberServiceFacade::cash($loginUserInfo,$engine,$coin*$samples,$conf);
- if(isset($checkMember['status']) && !$checkMember['status']){
- return $this->error($checkMember['msg'],2);
- }
- }
- }
- // print_r($res['data']);
- return $this->success('操作成功',$res['data']);
- }
- public function getOpensdDetail(){
- global $_GPC;
- $id = $this->request->param('id','');
- if(!$id){
- return $this->error('请上传查询ID');
- }
- $res = InsServiceFacade::opensdDetail($id);
- $res = @json_decode($res,true);
- if($res['code'] != 200){
- return error($res['msg']);
- }
- if($res['data']['state'] =='fail'){
- $info = $this->model->where('uuid','=',$id)->find();
- if(!empty($info)){
- $this->model->destroy($info['id']);
- return $this->error('抱歉,图片内容安全监测不通过'.$res['data']['fail_reason']);
- }
- }
- if($res['data']['state'] == 'success'){
- $uploadImageInfo = $this->uploadImage($res['data']['gen_img'],$_GPC['uniacid']);
- $data=[
- 'done'=>1,
- 'imgs' =>$uploadImageInfo['id']
- ];
- $this->model->where('uuid',$id)->update($data);
- }
- return $this->success('操作成功',$res['data']);
- }
- public function imagine()
- {
- global $_GPC;
- $uuid = Random::uuid();
- $modelGallery = new \app\model\Gallery();
- $prompt = $this->request->post('prompt','');
- $platform = $this->request->header('platform','H5');
- $action = $this->request->post('action','imagine');
- $loginUserInfo = UserServiceFacade::getUserInfo();
- $conf = ConfServiceFacade::groupGet('system.config');
- $coin = ConfServiceFacade::get('system.plan.unlock_mj_aipainting',0);
- $engine = ConfServiceFacade::get('system.config.mj_api_engine','ins');
- $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
- $hash = $this->request->post('hash','');
- $id = $this->request->post('id',0);
- $index = $this->request->post('index',1);
- $pid = $this->request->post('pid',0);
- $version='--v 5.1';
- if(!$action){
- return $this->error('请求方式不正确');
- }
- $checkMember = MemberServiceFacade::check($loginUserInfo,'mj',$coin,$conf);
- if(isset($checkMember['status']) && !$checkMember['status']){
- return $this->error($checkMember['msg'],2);
- }
- if($action == 'upscale' || $action == 'variation'){
- if($engine=='ins'&&!$hash){
- return $this->error('请传入hash');
- }
- if(!$id){
- return $this->error('请传入请传入id');
- }
- // $info = $this->model->find($pid);
- // if(!$info){
- // return $this->error('请传入上张图片id');
- // }
- }
- $data['prompt'] = $prompt;
- $data['paid_coin'] = $coin;
- $data['uid'] =$loginUserInfo['id'];
- $data['uniacid'] = $_GPC['uniacid'];
- $data['engine'] = 'mj';
- $data['uuid'] = $uuid;
- $data['mj_action'] = $action;
- $data['mj_index'] = $index;
- $data['job_id'] = $uuid;
- $data['status'] = 0;
- $data['mj_api_engine'] =$engine;
- $response = $modelGallery->save($data);
- if(!$response){
- return $this->error('数据库存入失败');
- }
- $API_URL = 'https://api.wike.cc/api/painting/zsy';
- header('Content-Type: text/event-stream');
- header('Cache-Control: no-cache');
- header('Connection: keep-alive');
- header('X-Accel-Buffering: no');
- $headers = [
- 'Accept: application/x-ndjson',
- 'Content-Type: application/json'
- ];
- // $prompt='1girl';
- $notify = IS_R==1?request()->domain() . SURL . '/api.gallery/mjNotify?uuid='.$data['uuid']:request()->domain() . SURL . '/index.php/api.gallery/mjNotify?uuid='.$data['uuid'];
- if($platform == 'wxMiniProgram'){
- $callback=$notify;
- }else{
- $callback='';
- $data['stream'] = true;
- }
- $ins = ConfServiceFacade::groupGet('system.ai');
- $data['key']=$ins['ins_api_key'];
- $data['id'] = $id;
- $get_post_data = http_build_query($data);
- $callbacks = function ($ch, $data) use($platform,$uuid,$loginUserInfo,$engine,$coin,$conf,$_GPC,$modelGallery) {
- $complete = @json_decode($data,true);
- if(!empty($complete['code'])){
- // if (strpos($data, 'The prompt has blocked words') !== false) {
- // echo '包含违禁词';
- // }else{
- echo $data;
- $this->model->destroy($modelGallery->id);
- // }
- exit;
- }
- if(!empty($complete['progress']) && $complete['progress'] == 100){
- if($coin >0){
- $checkMember = MemberServiceFacade::cash($loginUserInfo,'mj',$coin,$conf);
- if(isset($checkMember['status']) && !$checkMember['status']){
- echo $checkMember['msg'];
- exit;
- }
- }
- $mjCdn = 'mjcdn.iduomi.cc';
- $reg = '/(https):\/\/([^\/]+)/i';
- $replace = preg_replace($reg, 'https://'.$mjCdn, $complete['image_url']);
- $arr['mj_id'] = $complete['image_id'];
- $arr['origin_url'] = $replace;
- // $uploadImageInfo = $this->uploadImage($complete['image_url'],$_GPC['uniacid']);
- // print_r($uploadImageInfo);
- // if(!empty($uploadImageInfo)){
- // $arr['imgs'] = $uploadImageInfo['id'];
- // }
- $arr['done'] = 1;
- $arr['status'] = 1;
- $res = $modelGallery->where('uuid',$uuid)->update($arr);
- echo $data;
- exit;
- }else{
- echo $data;
- }
- ob_flush();
- flush();
- return strlen($data);
- };
- if($platform == 'wxMiniProgram' || $engine == 'ins'){
- if($engine == 'ins'){
- $res = InsServiceFacade::mj($data);
- }elseif($engine == 'zsy'){
- $res = InsServiceFacade::zsy($data);
- }
- if(empty($res)){
- return $this->error('请求错误');
- }
- $res2 =json_decode($res,true);
- if($res2['code'] ==400 || $res2['code'] ==403){
- $this->model->destroy($this->model->id);
- return $this->error($res2['msg'],2);
- }
- if($res2['code'] ==401 || $res2['code'] ==402){
- $this->model->destroy($this->model->id);
- return $this->error($res2['msg']);
- }
- // print_r($res2);
- // return;
- if($coin >0){
- $checkMember = MemberServiceFacade::cash($loginUserInfo,'mj',$coin,$conf);
- if(isset($checkMember['status']) && !$checkMember['status']){
- return $this->error($checkMember['msg'],2);
- }
- }
- return $this->success('操作成功',$uuid);
- }else{
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- curl_setopt($ch, CURLOPT_URL, $API_URL. '?' . $get_post_data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
- curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callbacks);
- curl_exec($ch);
- }
- }
- public function upscale(){
- global $_GPC;
- $uuid = Random::uuid();
- $post = $this->request->post();
- $prompt = $this->request->post('prompt','');
- $hash = $this->request->post('hash','');
- $id = $this->request->post('id',0);
- $index = $this->request->post('index',1);
- $pid = $this->request->post('pid',0);
- // $pid = $this->request->post('pid',1);
- $loginUserInfo = UserServiceFacade::getUserInfo();
- $coin = ConfServiceFacade::get('system.plan.unlock_mj_aipainting',0);
- $engine = ConfServiceFacade::get('system.config.mj_api_engine','ins');
- $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
- $version='--v 5.1';
- if($loginUserInfo['coin'] <$coin){
- return $this->error($numberAlias.'不足',2);
- }
- if($engine=='ins'&&!$hash){
- return $this->error('请传入hash');
- }
- if(!$id){
- return $this->error('请传入请传入id');
- }
- $info = $this->model->find($pid);
- if(!$info){
- return $this->error('请传入上张图片id');
- }
- $data['prompt'] = $info['prompt'];
- $data['paid_coin'] = $coin;
- $data['uid'] =$loginUserInfo['id'];
- $data['uniacid'] = $_GPC['uniacid'];
- $data['engine'] = 'mj';
- $data['uuid'] = $uuid;
- $data['mj_action'] = 'upscale';
- $data['mj_index'] = $index;
- $data['hash'] = $hash;
- $data['pid'] = $pid;
- $data['job_id'] = $uuid;
- $data['status'] = 0;
- $data['mj_api_engine'] =$engine;
- $response = $this->model->save($data);
- if(!$response){
- return $this->error('数据库存入失败');
- }
- $data['id'] = $id;
- if($engine == 'ins'){
- $res = InsServiceFacade::mj($data);
- }elseif($engine == 'zsy'){
- $res = InsServiceFacade::zsy($data);
- }
- // print_r($res);
- if(empty($res)){
- return $this->error('请求错误');
- }
- $res2 =json_decode($res,true);
- // print_r($res2);
- // return;
- if(empty($res2) || (!empty($res2['code']) && $res2['code'] ==403) || !empty($res2['code']) && $res2['code'] ==400){
- return $this->error('参数错误:'.$res2['msg']);
- }
- if($coin >0){
- \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
- BillServiceFacade::record(0,$coin,'mj','Midjourney 绘画生成消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
- }
- return $this->success('操作成功',$uuid);
- }
- public function variation(){
- global $_GPC;
- $uuid = Random::uuid();
- $post = $this->request->post();
- $prompt = $this->request->post('prompt','');
- $hash = $this->request->post('hash','');
- $id = $this->request->post('id',0);
- $index = $this->request->post('index',1);
- $pid = $this->request->post('pid',0);
- // $pid = $this->request->post('pid',1);
- $loginUserInfo = UserServiceFacade::getUserInfo();
- $coin = ConfServiceFacade::get('system.plan.unlock_mj_aipainting',0);
- $engine = ConfServiceFacade::get('system.config.mj_api_engine','ins');
- $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
- $version='--v 5.1';
- if($loginUserInfo['coin'] <$coin){
- return $this->error($numberAlias.'不足',2);
- }
- if($engine=='ins'&&!$hash){
- return $this->error('请传入hash');
- }
- if(!$id){
- return $this->error('请传入请传入id');
- }
- $info = $this->model->find($pid);
- if(!$info){
- return $this->error('请传入上张图片id');
- }
- $data['prompt'] = $info['prompt'];
- $data['paid_coin'] = $coin;
- $data['uid'] =$loginUserInfo['id'];
- $data['uniacid'] = $_GPC['uniacid'];
- $data['engine'] = 'mj';
- $data['uuid'] = $uuid;
- $data['mj_action'] = 'variation';
- $data['mj_index'] = $index;
- $data['hash'] = $hash;
- $data['pid'] = $pid;
- $data['job_id'] = $uuid;
- $data['status'] = 0;
- $data['mj_api_engine'] =$engine;
- $response = $this->model->save($data);
- if(!$response){
- return $this->error('数据库存入失败');
- }
- $data['id'] = $id;
- if($engine == 'ins'){
- $res = InsServiceFacade::mj($data);
- }elseif($engine == 'zsy'){
- $res = InsServiceFacade::zsy($data);
- }
- // print_r($res);
- if(empty($res)){
- return $this->error('请求错误');
- }
- $res2 =json_decode($res,true);
- // print_r($res2);
- // return;
- if(empty($res2) || (!empty($res2['code']) && $res2['code'] ==403) || !empty($res2['code']) && $res2['code'] ==400){
- return $this->error('参数错误:'.$res2['msg']);
- }
- \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',$coin)->update();
- BillServiceFacade::record(0,$coin,'mj','Midjourney 绘画生成消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
- return $this->success('操作成功',$uuid);
- }
- public function superResolution()
- {
- $uuid = Random::uuid();
- $post = $this->request->post();
- $post['uuid']= $uuid;
- $res = InsServiceFacade::super_resolution($post);
- if(empty($res)){
- return $this->error('请求错误');
- }
- $res2 =json_decode($res,true);
- // print_r($res2);
- // return;
- if(empty($res2) || (!empty($res2['code']) && $res2['code'] ==403) || !empty($res2['code']) && $res2['code'] ==400){
- return $this->error('参数错误:'.$res2['msg']);
- }
- // print_r($res['data']);
- return $this->success('操作成功',$res2['data']);
- }
- public function getAsyncResult()
- {
- $uuid = $this->request->param('uuid',0);
- if (!$uuid) {
- return $this->error('请上传任务请求id',2);
- }
- $res = $this->model->where('uuid',$uuid)->find();
- if(!$res){
- return $this->error('不存在此uuid',2);
- }
- return $this->success('操作成功',$res);
- }
- public function fetch()
- {
- global $_GPC;
- $param = $this->request->param();
- $platform = $this->request->header('platform','H5');
- $id = $this->request->param('id','');
- $uuid = $this->request->param('uuid','');
- if(!$id){
- return $this->error('请上传请求id',2);
- }
- $res = InsServiceFacade::sd_result($id);
- if(empty($res)){
- return $this->error('请求错误');
- }
- $res2 =json_decode($res,true);
- // print_r($res2);
- // return;
- if(empty($res2) || (!empty($res2['code']) && $res2['code'] ==403) || !empty($res2['code']) && $res2['code'] ==400){
- return $this->error('参数错误.'.$res2['msg']);
- }
- if($res2['data']['status'] == 'success'){
- if($platform == 'wxMiniProgram'){
- $app = MiniappServiceFacade::option();
- $filePath = $this->_put_file_from_url_content($res2['data']['output'][0]);
- $check = $app->content_security->checkImage(IA_ROOT_WK.'/public/static/storage/' . $filePath['saveName']);
- if($check['errcode'] !=0){
- $info = $this->model->where('uuid','=',$uuid)->find();
- if(!empty($info)){
- $this->model->destroy($info['id']);
- return $this->error('抱歉,图片内容安全监测不通过',2);
- }
- }
- }elseif($platform == 'wxOfficialAccount' || $platform == 'H5'){
- $check = ImageCensorServiceFacade::imageCensorUserDefined($res2['data']['output'][0]);
- // print_r($check);
- if(isset($check['error_code'])){
- $info = $this->model->where('uuid','=',$uuid)->find();
- if(!empty($info)){
- $this->model->destroy($info['id']);
- return $this->error('抱歉,图片内容安全检测不通过:'.$check['error_msg'],2);
- }
- }
- if(isset($check['conclusion']) && $check['conclusion']=='不合规'){
- $info = $this->model->where('uuid','=',$uuid)->find();
- if(!empty($info)){
- $this->model->destroy($info['id']);
- return $this->error('抱歉,图片内容安全监测不通过',2);
- }
- }
- }
- }
- return $this->success('操作成功',$res2['data']);
- }
- public function chat(){
- global $_GPC;
- $loginUserInfo = UserServiceFacade::getUserInfo();
- $modelQuestion = new \app\model\Question();
- $prompt = $this->request->post('prompt','');
- $guide = $this->request->post('guide','');
- $answer = $this->request->post('answer','');
- $platform = $this->request->header('platform','');
- $conf = ConfServiceFacade::groupGet('system.config', 0);
- $type = $this->request->post('type','gpt35');
- $numberAlias = !empty($conf['number_alias'])?$conf['number_alias']:'点数';
- if(!$prompt){
- return $this->error('请填写问题');
- }
- // $clearPrompt = $this->wordFilter($prompt);
- // $proxy = $this->getUrl($type,$conf);
- $guide='分别以"中文:"和"英文:"格式返回返回,将用户的话生成高质量的描述词';
- $text='我希望你能担任英语翻译、拼写校对和修辞改进的角色。我会将翻译的结果用于如stable diffusion、midjourney等绘画场景生成图片,语言要求尽量优美。我会用任何语言和你交流,你会识别语言,将其翻译为英语并仅回答翻译的最终结果,不要写解释。我的第一句话是:'.$prompt.'。请立刻翻译,不要回复其它内容。';
- $res = ChatgptServiceFacade::chat($prompt,$guide,'',$platform);
- if(!$res){
- return $this->error('请求错误,请稍后重试',3);
- }
- // if($platform == 'wxMiniProgram'){
- // $check = MiniappServiceFacade::msgSecCheck($res,$loginUserInfo['openid_miniapp']);
- // if(!$check){
- // return $this->error('失败,文字内容安全检测不通过!');
- // }
- // }elseif($platform == 'wxOfficialAccount' || $platform == 'H5'){
- // if(!empty($conf) && !empty($conf['is_h5_pic_filter']) && $conf['is_h5_pic_filter'] == 1){
- // $check = ImageCensorServiceFacade::textCensorUserDefined($res);
- // if(!empty($check['conclusion']) && $check['conclusion'] == '不合规' ){
- // return $this->error('失败,文字内容安全检测不通过!');
- // }
- // }
- // }
- // if(!empty($conf['time_member']) && $conf['time_member'] ==1){
- // if(empty($loginUserInfo['vip_time'])){
- // if($loginUserInfo['coin'] <=0){
- // return $this->error($numberAlias.'不足',2);
- // }else{
- // \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',1)->update();
- // BillServiceFacade::record(0,1,'chatgpt','问答消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
- // }
- // }else{
- // if($loginUserInfo['vip_time'] < time() && $loginUserInfo['vip_time'] !=9999){
- // if($loginUserInfo['coin'] <=0){
- // return $this->error($numberAlias.'不足',2);
- // }else{
- // \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',1)->update();
- // BillServiceFacade::record(0,1,'chatgpt','问答消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
- // }
- // }
- // }
- // }else{
- // if($loginUserInfo['coin'] <=0){
- // return $this->error($numberAlias.'不足',2);
- // }else{
- // \app\model\User::where(['id'=>$loginUserInfo['id']])->dec('coin',1)->update();
- // BillServiceFacade::record(0,1,'chatgpt','问答消耗'.$numberAlias,$loginUserInfo['id'],$_GPC['uniacid']);
- // }
- // }
- // $question = $modelQuestion->save(['uid'=>$loginUserInfo['id'],'question'=>$prompt,'answer'=>$res,'uniacid'=>$_GPC['uniacid'] ]);
- return $this->success('操作成功',$res);
- }
- public function getWords()
- {
- global $_GPC;
- $where = ['uniacid' =>$_GPC['uniacid']];
- $order = ['sort' => 'DESC','id'=>'DESC'];
- $words = \app\model\Words::where($where)->orderRaw("rand() , id desc")->limit(20)->select();
- if(!$words){
- return $this->error('暂无描述提示词',2);
- }
- return $this->success('数据获取成功', $words);
- }
- public function setOpen()
- {
- global $_GPC;
- $id = $this->request->post('id',0);
- if(!$id){
- return $this->error('未获取到要修改的信息id',2);
- }
- $info = $this->model->where('id','=',$id)->update(['is_open'=>1]);
- return $this->success('获取成功', $info);
- }
- public function getModels()
- {
- global $_GPC;
- $engine = $this->request->param('engine','sd');
- // $where = ['engine'=>$engine];
- // $order = ['sort' => 'DESC','id'=>'DESC'];
- // $data =\app\model\gallery\Models::where($where)->order($order)->sheepFilter()->paginate(request()->param('pageSize', 10));
- $res = InsServiceFacade::model($engine);
- $res = @json_decode($res,true);
- return $this->success('获取成功', $res['data']);
- }
- // 列表
- public function list(){
- global $_GPC;
- $loginUserInfo = UserServiceFacade::getUserInfo();
- $param = $this->request->param();
- // $where[] = ['status','=',1];
- $where[] = ['uniacid','=',$_GPC['uniacid']];
- $where[] = ['uid','=',$loginUserInfo['id']];
- // $where[] = ['img','<>',NULL];
- $order = ['id' => 'desc'];
- $limit = $this->request->param('limit', 10);
- $data = $this->model->order($order)->where($where)->with(['user_info'])->paginate($limit)->toArray();
- if(!$data){
- return $this->error('数据获取失败');
- }
- return $this->success('数据获取成功', $data);
- }
- // 列表
- public function all(){
- global $_GPC;
- // $loginUserInfo = UserServiceFacade::getUserInfo();
- $param = $this->request->param();
- $engine = $this->request->param('engine','');
- if(!empty($engine)){
- $where[] = ['engine','=',$engine];
- }
- $where[] = ['uniacid','=',$_GPC['uniacid']];
- $where[] = ['is_open','=',1];
- $order = ['id' => 'desc'];
- $limit = $this->request->param('limit', 10);
- $data = $this->model->order($order)->where($where)->with(['user_info','models_info'])->paginate($limit)->toArray();
- if(!$data){
- return $this->error('数据获取失败');
- }
- return $this->success('数据获取成功', $data);
- }
- /**
- * 详情
- * @param $file
- * @return array|boolean
- */
- public function detail()
- {
- $return = [];
- $id = $this->request->param('id');
- $info = $this->model->with(['user_info','models_info'])->find($id);
- if(!$info){
- return $this->error('详情获取失败');
- }
- // $this->model->where('id','=',$id)->inc('browse', 1)->update();
- return $this->success('获取成功', $info);
- }
- public function del(){
- global $_GPC;
- $loginUserInfo = UserServiceFacade::getUserInfo();
- $id = $this->request->post('id');
- if (!$id) {
- return $this->error('参数id不能为空',2);
- }
- $jobUid = $this->model->where('id',$id)->value('uid');
- if($jobUid != $loginUserInfo['id']){
- return $this->error('您没有删除的权限');
- }
- try{
- $delRes = $this->model->destroy($id);
- if (!$delRes) {
- return $this->error('删除失败');
- }
- return $this->success('删除成功');
- }catch (\Exception $e){
- return $this->exceptionError($e);
- }
- }
- public function notify(){
- global $_GPC;
- $sdProxy = 'sd.iduomi.cc';
- $reg = '/(https):\/\/([^\/]+)/i';
- $post = $this->request->post();
- // file_put_contents(__DIR__ . '/notify.json', json_encode($post));
- if(!empty($post) && isset($post['webhook_type']) && $post['webhook_type'] =='success'){
- $info = $this->model->where('uuid','=',$post['track_id'])->find();
- if(!$info || $info['done'] == 1){
- return true; //已经处理过
- }
- $_GPC['uniacid'] =$info['uniacid'];
- $count = count($post['output']);
- $arr=[];$originArr=[];
- if($count > 0){
- foreach($post['output'] as $k=>$v) {
- preg_match($reg, $v,$res);
- $replace = preg_replace($reg, 'https://'.$sdProxy, $v);
- // file_put_contents(__DIR__ . '/replace.json', json_encode($replace));
- $originArr[$k] = $replace;
- $uploadImageInfo = $this->uploadImage($v,$info['uniacid']);
- array_push($arr,$uploadImageInfo['id']);
- }
- }
- $data['imgs'] =implode(',',$arr);
- $data['origin_url']=implode(',',$originArr);
- $data['done'] =1;
- $data['real_prompt'] =$post['meta']['prompt'];
- $res = $this->model->where('uuid',$post['track_id'])->update($data);
- // file_put_contents(__DIR__ . '/res.json', json_encode($res));
- }
- }
- public function mjNotify(){
- global $_GPC;
- $mjCdn = 'mjcdn.iduomi.cc';
- $reg = '/(https):\/\/([^\/]+)/i';
- $post = $this->request->post();
- // file_put_contents(__DIR__ . '/mjNotify.json', json_encode($post));
- // file_put_contents(__DIR__ . '/get.json', json_encode($_GET));
- if(empty($_GET['uuid'])){
- echo false;
- return;
- }
- $info = $this->model->where('uuid','=',$_GET['uuid'])->find();
- if(empty($info) || $info['done'] == 1){
- echo true; //已经处理过
- return;
- }
- // file_put_contents(__DIR__ . '/info.json', json_encode($info));
- $_GPC['uniacid'] =$info['uniacid'];
- // if($info['mj_action'] =='imagine' || $info['mj_action'] =='imagine'){
- if($info['mj_api_engine'] =='ins'){
- $data['mj_id'] = $post['id'];
- $data['mj_hash'] = $post['hash'];
- $data['origin_url'] = $post['url'];
- $uploadImageInfo = $this->uploadImage($post['url'],$info['uniacid']);
- }elseif($info['mj_api_engine'] =='zsy'){
- // preg_match($reg, $post['image_url'],$res);
- $replace = preg_replace($reg, 'https://'.$mjCdn, $post['image_url']);
- $data['mj_id'] = $post['image_id'];
- $data['origin_url'] = $replace;
- $uploadImageInfo = $this->uploadImage($post['image_url'],$info['uniacid']);
- }
- if(!empty($uploadImageInfo)){
- $data['imgs'] = $uploadImageInfo['id'];
- }
- $data['done'] = 1;
- $data['status'] = 1;
- $res = $this->model->where('uuid',$_GET['uuid'])->update($data);
- // }
- }
- public function test(){
- $uploadImageInfo = $this->uploadImage('',2);
- if(!empty($uploadImageInfo)){
- $arr['imgs'] = $uploadImageInfo['id'];
- }
- }
- public function download_remote_file($file_url)
- {
- $name =md5(uniqid(mt_rand())) . ".png";
- $saveName = date("Ymd") . "/" . $name;
- $path = IA_ROOT_WK.'/public/static/storage/';
- $filename = $path . $saveName;
- if(!file_exists($path. date("Ymd")))mkdir($path. date("Ymd"), 0755,TRUE);//如果文件夹不存在,则创建
- $opts = [
- "ssl" => [
- // "allow_self_signed" => true,
- "verify_peer"=>false,
- "verify_peer_name"=>false,
- ]
- ];
- $content = file_get_contents($file_url,false, stream_context_create($opts));
- $res = file_put_contents($filename, $content);
- // print_r('file_get'.$res);
- if($res){
- $data['filename'] = $filename;
- $data['url'] = request()->domain() . STATIC_PATH . '/storage/' . $saveName;
- $data['saveName'] = $saveName;
- $data['name'] = $name;
- return $data;
- }else{
- return false;
- }
- }
- public function uploadImage($url,$uniacid=0){
- global $_GPC;
- if($uniacid){
- $_GPC['uniacid'] = $uniacid;
- }
- try {
- $file_url =$url;
- $filePath = $this->download_remote_file($file_url);
- if(!$filePath){
- return false;
- }
- $pathname = $filePath['filename'];
- $object = $filePath['name'];
- $uploadType= ConfServiceFacade::get('system.upload.defaultType', 'local');
- $inputValue = "";
- //上传至七牛云
- if ($uploadType == 'qiniu-kodo') {
- if(ConfServiceFacade::get('plugin.qiniu_kodo.switch') != 1){
- return $this->error('未开启七牛云KODO存储,请到七牛云KODO配置中开启');
- }
- $kodoConf = [
- 'accessKey' => ConfServiceFacade::get('plugin.qiniu_kodo.accessKeyID'),
- 'secretKey' => ConfServiceFacade::get('plugin.qiniu_kodo.secretKey'),
- 'bucket' => ConfServiceFacade::get('plugin.qiniu_kodo.bucket'),
- 'domain' => ConfServiceFacade::get('plugin.qiniu_kodo.domain'),
- ];
- $kodo = Kodo::instance();
- $kodoRes = $kodo->upload($pathname, $object, $kodoConf);
- if ($kodoRes) {
- $inputValue = $kodoRes;
- $pathname = $object;
- } else {
- return $this->error($kodo->getError());
- }
- }
- //上传至阿里云
- if ($uploadType == 'ali-oss') {
- if (ConfServiceFacade::get('plugin.ali_oss.switch') != 1) {
- return $this->error('未开启阿里云OSS存储,请到阿里云OSS配置中开启');
- }
- $ossConf = [
- 'accessKeyID' => ConfServiceFacade::get('plugin.ali_oss.accessKeyID'),
- 'accessKeySecret' => ConfServiceFacade::get('plugin.ali_oss.accessKeySecret'),
- 'bucket' => ConfServiceFacade::get('plugin.ali_oss.bucket'),
- 'endpoint' => ConfServiceFacade::get('plugin.ali_oss.endpoint'),
- 'domain' => ConfServiceFacade::get('plugin.ali_oss.domain'),
- ];
- $oss = Oss::instance();
- $ossUploadRes = $oss->upload($pathname, $object, $ossConf);
- if ($ossUploadRes) {
- $inputValue = $ossUploadRes;
- $pathname = $object;
- } else {
- return $this->error($oss->getError());
- }
- }
- //本地上传
- if ($uploadType == 'local') {
- $inputValue = request()->domain() . STATIC_PATH . '/storage/' . $filePath['saveName'];
- $pathname = $filePath['saveName'];
- }
- //将inputValue存入lt_files表中
- $filesModel = new \app\model\Files();
- $fileId = $filesModel->insertGetId([
- 'category_id' => (int)$this->request->param('file_category_id', 0),
- 'name' => $object,
- 'file_type' => 'image',
- 'path' => $pathname,
- 'upload_type' => $uploadType,
- 'size' => 0,
- 'ext' => 'png',
- 'create_admin_user_id' => 0,
- 'update_admin_user_id' => 0,
- 'create_time' => date('Y-m-d H:i:s'),
- 'update_time' => date('Y-m-d H:i:s'),
- 'uniacid' =>$_GPC['uniacid']
- ]);
- return ['id' => $fileId,'path' => $inputValue,
- 'name' => $filePath['saveName']];
- }catch (\Exception $e) {
- return $this->exceptionError($e);
- }
- }
- private function _put_file_from_url_content($url) {
- $name =md5(uniqid(mt_rand())) . ".png";
- $saveName = date("Ymd") . "/" . $name;
- $path = IA_ROOT_WK.'/public/static/storage/';
- // 设置运行时间为无限制
- set_time_limit ( 0 );
- $url = trim ( $url );
- $curl = curl_init ();
- // 设置你需要抓取的URL
- curl_setopt ( $curl, CURLOPT_URL, $url );
- // 设置header
- curl_setopt ( $curl, CURLOPT_HEADER, 0 );
- // 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
- curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, 1 );
- // 运行cURL,请求网页
- $file = curl_exec ( $curl );
- // 关闭URL请求
- curl_close ( $curl );
- // 将文件写入获得的数据
- $filename = $path . $saveName;
- // 打开用来保存数据的文件,如果没有则会创建
- if (!file_exists($path.date("Ymd"))) {
- mkdir($path.date("Ymd"), 0755,true);
- }
- $write = @fopen ( $filename, "w" );
- if ($write == false) {
- return false;
- }
- // 将CURL请求返回的数据写入文件
- if (fwrite ( $write, $file ) == false) {
- return false;
- }
- // 关闭文档流
- if (fclose ( $write ) == false) {
- return false;
- }
- // file_put_contents($imgName, file_get_contents($imgFile));
- $data['filename'] = $filename;
- $data['url'] = request()->domain() . STATIC_PATH . '/storage/' . $saveName;
- $data['saveName'] = $saveName;
- $data['name'] = $name;
- return $data;
- }
- //小知识:php中使用最多的是通过curl来模拟get和post请求
- public function http_get_data($url) //框架放common.php
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_URL, $url);
- ob_start();
- curl_exec($ch);
- $return_content = ob_get_contents();
- ob_end_clean();
- $return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- return $return_content;
- }
- public function saveTheFile($url,$savePath){
- $file_content = $this->http_get_data($url);
- $fp = @fopen($savePath,'a');
- return fwrite($fp,$file_content);
- }
- }
|