AI-draw.vue 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. <template>
  2. <view class="container" :style="{'top': viewTop + 'px', 'height': viewHeight + 'px'}">
  3. <view class="content">
  4. <scroll-view scroll-y="true" :show-scrollbar="false" :scroll-top="scroll_top" style="top: 0%;width: 80vw; height:65%;position: absolute;">
  5. <view class="scroll-view-text">
  6. <view style="margin-left: 13%; display: flex; align-items: center;justify-content: center;width: 100%; user-select: text;" v-for="(msgIndex,index) in recvMsgQueue " :key="index">
  7. <!-- ai 的消息结构体 -->
  8. <view v-if="isAItell(recvMsgQueue[index].who_msg)" style="width: 100%;height: 100%;margin-bottom: 1%;">
  9. <text style=" font-size: 10rpx;color: #B34FFF;font-size: 10rpx;margin-left: 2%;background: linear-gradient(90deg, #24BF74 10%, #FAEE07 90%);-webkit-background-clip: text;-webkit-text-fill-color: transparent;">
  10. {{ai_name}}
  11. </text>
  12. <view style="margin-bottom: 3%; margin-top: 1%;margin-left: 1%;padding: 1%; width: 70%;background: #2A2832;border-radius: 10rpx 10rpx 10rpx 10rpx;">
  13. <view @tap="handleClick()" style="width: 100%;background-color: #2A2832;color: white;" class="htmlContent" ref="htmlContent" v-html="returnText(recvMsgQueue[index].msg_text)" >
  14. </view>
  15. <image v-if="getImg(index)!=''" :src="getImg(index)" @click="onclickAiGetImg(index)"></image>
  16. <view @click="stopRecv()" v-if="recvStatus&&recvMsgQueue[index].isFinish==false" style="display: flex;width: 100%;align-items: center;justify-content: center;">
  17. <image src="../../static/closed.png" style="width: 50upx;height: 50upx;align-self: center;"></image>
  18. </view>
  19. </view>
  20. <view v-if="false" style="margin-left: 65%;">
  21. <image src="../../static/msg-opt-3.png" style="margin-left: 10upx;width: 30upx;height: 30upx;"></image>
  22. <image src="../../static/msg-opt-4.png" style="margin-left: 10upx;width: 30upx;height: 30upx;"></image>
  23. </view>
  24. <view style=" display: flex;flex-wrap: wrap;width: 50%;">
  25. <view style="width: 23%;justify-content: center;align-items: center;display: flex;margin-top: 2%; margin-right: 2%;" v-for="(item,i) in recvMsgQueue[index].actions" key="key">
  26. <view @click="clickitem(recvMsgQueue[index].actions[i],recvMsgQueue[index].image_id)" style="background-color: #32B3AA;display: flex; box-sizing: border-box; border-radius: 5px;align-items: center;justify-content: center;width: 100%; padding: 2%; font-size: 1.7vw; word-break: break-all; text-overflow: ellipsis; word-wrap: break-word; white-space: pre-wrap; text-align: center;">{{recvMsgQueue[index].actions[i]}}</view>
  27. </view>
  28. </view>
  29. </view>
  30. <!-- 我发送 的消息结构体 -->
  31. <view v-if="!isAItell(recvMsgQueue[index].who_msg)" style="width: 100%;height: 100%;margin-top: 1%;">
  32. <view style=" font-size: 10rpx;color: #B34FFF;font-size: 10rpx;margin-left: 2%;">
  33. {{user_name}}
  34. </view>
  35. <view style="margin-bottom: 3%; margin-top: 1%;margin-left: 1%;padding: 1%; width: 70%;background: #2A2832;border-radius: 10rpx 10rpx 10rpx 10rpx;">
  36. <view style="width: 100%;background-color: #2A2832;color: white;" class="htmlContent" ref="htmlContent" >
  37. {{recvMsgQueue[index].msg_text}}
  38. </view>
  39. <image v-if="recvMsgQueue[index].des_img!=''" :src="recvMsgQueue[index].des_img" style="background-color: #111111; margin-top: 1%;" mode="aspectFit" @click="onclickSendImg(index)"></image>
  40. </view>
  41. <view v-if="false" style="margin-left: 65%;">
  42. <image src="../../static/msg-opt-1.png" style="margin-left: 10upx; width: 30upx;height: 30upx;"></image>
  43. <image src="../../static/msg-opt-2.png" style="margin-left: 10upx;width: 30upx;height: 30upx;"></image>
  44. <image src="../../static/msg-opt-3.png" style="margin-left: 10upx;width: 30upx;height: 30upx;"></image>
  45. <image src="../../static/msg-opt-4.png" style="margin-left: 10upx;width: 30upx;height: 30upx;"></image>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. </scroll-view>
  51. <view class="inputbox" style="display: flex;width: 70vw;height: 90upx;position: absolute;bottom:500upx;">
  52. <view style="display: flex;align-items: center;justify-content: center;width: 5%;position: relative;">
  53. <image @click="onclickchooseImg()" src="../../static/img_upload.png" style="width: 65upx;height: 65upx;"></image>
  54. </view>
  55. <view style="display: flex;width: 90%; margin-left: 5px; border-radius: 25px 25px 25px 25px;opacity: 1;border: 1px solid #32B3AA;" class="textarea-box">
  56. <view style="width: 90%; height: 100%; display: flex;align-items: center;justify-content: center" >
  57. <textarea v-model="inputValue" :cursor-spacing="15" class="textarea" auto-height="true"
  58. @keydown.enter="onKeydown('enter')" :disabled="disabled"
  59. @input="onKeyInput"
  60. placeholder="请输入消息内容(使用 Enter 发送)" :maxlength="-1"
  61. placeholder-class="input-placeholder"></textarea>
  62. </view>
  63. <view v-if="recvStatus==false" style="width: 10%; height: 100%; display: flex; color: aliceblue;align-items: center;justify-content: center" @click="sendMsg">
  64. <image src="../../static/img_send.png" style="width: 29.57upx;height: 37.67upx;" ></image>
  65. </view>
  66. </view>
  67. <view style=" display: flex;align-items: center;justify-content: center;width: 5%;position: relative;">
  68. <image @click="onclickdelete()" src="../../static/img_delete.png" style="width: 50upx;height: 70upx;"></image>
  69. </view>
  70. </view>
  71. </view>
  72. <view style="position: absolute;top: 10%;right: 5%;width: 200upx;display: flex;align-items: center;justify-content: center;">
  73. <view style="color: white;margin-right: 10upx;">开启快速</view>
  74. <image @click="is_open_relax=false" v-show="is_open_relax==true" src="../../static/img-on.png" style="width: 60upx;height: 30upx;"></image>
  75. <image @click="is_open_relax=true" v-show="is_open_relax==false" src="../../static/img-off.png" style="width: 60upx;height: 30upx;"></image>
  76. </view>
  77. <view v-if="false" @click="is_show_import_img_view=true" style="position: absolute;top: 20%;right: 5%;width: 200upx;display: flex;align-items: center;justify-content: center;">
  78. <view style="color: white;margin-right: 10upx;">导入图片</view>
  79. <image src="../../static/import-img.png" style="width: 60upx;height: 60upx;"></image>
  80. </view>
  81. // v-if="is_show_import_img_view==true"
  82. <view v-if="false" style=" display: flex; height: 100vh; position: absolute;top: 0;width: 500upx;background-color:#24BF74;">
  83. <view style="width: 100%;;">
  84. <view @click="onFileInput" style="display: flex;align-items: center;justify-content: center;margin-top: 5%;center;margin-bottom: 5%;">
  85. <image :src="cur_select_img" style="width: 100upx;height: 100upx;"></image>
  86. </view>
  87. <view style="display: flex;align-items: center;justify-content: center;margin-top: 5%;center;margin-bottom: 5%;">
  88. <textarea style="display: flex;align-items: center;justify-content: center;" v-model="diy_inputValue" :cursor-spacing="15" auto-height="true"
  89. @input="onKeyInput"
  90. placeholder="请输入描述" :maxlength="-1"
  91. placeholder-class="input-placeholder"></textarea>
  92. </view>
  93. <view @click="send_diy_img()" style="display: flex;align-items: center;justify-content: center;margin-top: 5%;center;margin-bottom: 5%;">
  94. <view style="display: flex;align-items: center;justify-content: center;background-color: aqua;;border-radius: 10rpx 10rpx 10rpx 10rpx;width: 50%;">发送</view>
  95. </view>
  96. <view @click="is_show_import_img_view=false" style="display: flex;align-items: center;justify-content: center;margin-top: 5%;center;margin-bottom: 5%;">
  97. <view style="display: flex;align-items: center;justify-content: center;background-color: aqua;;border-radius: 10rpx 10rpx 10rpx 10rpx;width: 50%;">关闭</view>
  98. </view>
  99. <scroll-view scroll-y="true" style="height:500upx;">
  100. <view @click="selectImg(import_list[j])" style="display: flex;align-items: center;justify-content: center;" v-for="(itemx,j) in import_list" :key="j">
  101. <image style="width: 150upx;height: 150upx;margin-top: 5%;" :src="import_list[j]"></image>
  102. </view>
  103. </scroll-view>
  104. </view>
  105. </view>
  106. <view v-if="choose_img_tempFilePath != ''" style="position: fixed; top: 500px; left: 20%; width: 130px; height: 90px;">
  107. <view style="position: absolute; top: 10px; width: 120px; height: 80px; background-color: #111111;">
  108. <image style="width: 100%; height: 100%;" :src="choose_img_tempFilePath" mode="aspectFill"></image>
  109. </view>
  110. <view style="position: absolute; right: 0; width: 30px; height: 30px;" @click="onclickDeleteChooseImg">
  111. <image style="margin-left: 10px; width: 20px; height: 20px;" src="../../static/img_jian.png" mode="aspectFit"></image>
  112. </view>
  113. </view>
  114. <view v-if="is_up_img_file" style="align-items: center;justify-content: center;display: flex; color: aliceblue; position: absolute; width: 100vw;height:100vh;background-color: black;opacity: 70%;">
  115. 正在上传图片...
  116. </view>
  117. </view>
  118. </template>
  119. <script>
  120. import marked from '../marked/marked.min.js';
  121. import config from '../index/config.js';
  122. import hljs from "../highlight.js/lib/common.js";
  123. import interfaces from '../../utils/interfaces.js'
  124. import "../highlight.js/styles/atom-one-dark.css";
  125. // import '../v-copy.js';
  126. // 键盘的shift键是否被按下
  127. let shiftKeyPressed = false
  128. export default {
  129. components:{
  130. marked
  131. },
  132. data() {
  133. return {
  134. diy_inputValue: '',
  135. is_up_img_file:false,
  136. import_list:[],
  137. is_show_import_img_view:false,
  138. is_open_relax:true,
  139. code:'',
  140. title: 'Hello',
  141. disabled: false,
  142. socketOpen:false,
  143. socketMsgQueue:[],
  144. recvMsgQueue:[],
  145. sendMsgQueue:[],
  146. inputValue: '',
  147. inputOldValue: '',
  148. _socketTask:null,
  149. scroll_text_:"\n",
  150. sendStatus:false,
  151. recvStatus:false,
  152. all_src_list:[],
  153. src_index:0,
  154. scroll_top:0,
  155. user_name:'ZYWZ', //用户名字
  156. ai_name:"SmartAssistant", //ai名字
  157. user_mobile:"18612220000",//用户手机号
  158. ani:'',
  159. ani_style:{
  160. width: "100upx",height: "100upx",rotate: 0,left:"25%",right:"25%",position:"absolute"
  161. },
  162. angle:359,
  163. imageUrl:'',
  164. fileData:null,
  165. cur_select_img:'../../static/import-img.png',
  166. choose_img_tempFilePath: '',
  167. }
  168. },
  169. mounted(){
  170. this.user_mobile = this.tools.get_user_info().mobile
  171. marked.setOptions({
  172. renderer: new marked.Renderer(),
  173. gfm: true,
  174. tables: true,
  175. breaks: false,
  176. pedantic: false,
  177. sanitize: false,
  178. smartLists: true,
  179. smartypants: false,
  180. highlight: function (code) {
  181. return hljs.highlightAuto(code).value;
  182. },
  183. langPrefix:"hljs language-"
  184. });
  185. },
  186. props: {
  187. viewTop: {
  188. type: Number,
  189. default: 0
  190. },
  191. viewHeight: {
  192. type: Number,
  193. default: 0
  194. }
  195. },
  196. methods: {
  197. send_diy_img(){
  198. if(this.cur_select_img==="../../static/import-img.png"){
  199. uni.showToast({
  200. title:"请选择图片"
  201. })
  202. return
  203. }
  204. if(this.diy_inputValue.length>0){
  205. this.is_show_import_img_view = false;
  206. let des_img = this.cur_select_img;
  207. this.sendSocketMessage(this.diy_inputValue,this.cur_select_img+" "+this.diy_inputValue,des_img)
  208. this.diy_inputValue = '';
  209. }else{
  210. uni.showToast({
  211. title:"请添加描述,只支持英文哦"
  212. })
  213. }
  214. },
  215. selectImg(path){
  216. let self = this;
  217. self.cur_select_img = path;
  218. },
  219. onFileInput(event) {
  220. let self = this;
  221. uni.chooseImage({
  222. success: (chooseImageRes) => {
  223. const tempFilePaths = chooseImageRes.tempFilePaths;
  224. self.is_up_img_file = true;
  225. uni.uploadFile({
  226. url: 'https://mlapi.hainanmlwl.com/file/upload_aiimg', //仅为示例,非真实的接口地址
  227. filePath: tempFilePaths[0],
  228. name: 'aiimg_url',
  229. formData: {
  230. // 'aiimg_url': self.cur_select_img
  231. },
  232. success: (uploadFileRes) => {
  233. self.is_up_img_file = false;
  234. let obj = JSON.parse(uploadFileRes.data)
  235. uni.showModal({
  236. content:obj.message,
  237. showCancel:false
  238. })
  239. console.log("self.cur_select_img",self.cur_select_img)
  240. if(obj.code==10000){
  241. let img_url = obj.content.surl
  242. self.import_list.push(img_url)
  243. self.cur_select_img = img_url;
  244. }
  245. console.log("uploadFile",obj);
  246. },
  247. fail() {
  248. uni.showModal({
  249. content:"上传失败",
  250. showCancel:false
  251. })
  252. self.is_up_img_file = false;
  253. }
  254. });
  255. }
  256. });
  257. // console.log("onFileInput",event)
  258. // const file = event.target.files[0] // 获取选择的文件
  259. // const reader = new FileReader() // 创建FileReader对象
  260. // // 当文件被读取时,将其转换为DataURL格式的字符串,并将其作为imageUrl值赋给组件的data属性中
  261. // reader.onload = (e) => {
  262. // this.imageUrl = e.target.result
  263. // // 保存文件对象到data属性中
  264. // this.fileData = file
  265. // }
  266. // reader.readAsDataURL(file) // 读取文件并启动onload回调函数
  267. },
  268. clickitem(action,image_id){
  269. switch(action){
  270. case "upsample1":
  271. break;
  272. case "upsample2":
  273. break;
  274. case "upsample3":
  275. break;
  276. case "upsample4":
  277. break;
  278. case "variation1":
  279. break;
  280. case "variation2":
  281. break;
  282. case "variation3":
  283. break;
  284. case "variation4":
  285. break;
  286. }
  287. console.log("clickitem",this.recvStatus,action,image_id)
  288. if(!this.recvStatus){
  289. this.sendSocketMessage(action,'','',action,image_id)
  290. }
  291. },
  292. getImg(index){
  293. return this.recvMsgQueue[index].msg_img
  294. },
  295. onclickdelete(){
  296. let self = this;
  297. if(self.recvMsgQueue.length>0&&self.recvMsgQueue[self.recvMsgQueue.length-1].isFinish){
  298. uni.showModal({
  299. showCancel:true,
  300. content:"是否删除记录",
  301. success:(res)=>{
  302. if(res.confirm){
  303. self.recvMsgQueue = []
  304. }
  305. }
  306. })
  307. }
  308. },
  309. // #ifdef H5 && VUE2
  310. onKeydown(keyname) {
  311. let self = this;
  312. if(self.inputOldValue!=self.inputValue){
  313. self.sendMsg();
  314. }else{
  315. }
  316. },
  317. onKeyup(keyname) {
  318. let self = this;
  319. self.inputOldValue=self.inputValue;
  320. },
  321. // #endif
  322. setCopy(content){
  323. // 使用#ifdef H5和#endif控制各端调用情况
  324. // 该方法不支持h5
  325. //#ifndef H5
  326. uni.setClipboardData({
  327. data: String(content), // 必须字符串
  328. success: function () {
  329. console.log('success');
  330. }
  331. });
  332. //#endif
  333. // h5端赋值方法,使用创建节点
  334. // #ifdef H5
  335. if (!document.queryCommandSupported('copy')) { // 兼容某些浏览器的判断
  336. console.log('该浏览器不支持')
  337. }
  338. let textarea = document.createElement("textarea")
  339. textarea.value = content
  340. textarea.readOnly = "readOnly"
  341. document.body.appendChild(textarea)
  342. textarea.select() // 选择对象
  343. textarea.setSelectionRange(0, content.length) // 核心
  344. let result = document.execCommand("copy") // 执行浏览器复制命令
  345. if (result) {
  346. uni.showToast({
  347. title: '复制成功',
  348. duration: 2000
  349. });
  350. }
  351. textarea.remove()
  352. // #endif
  353. },
  354. updateSrcList(){
  355. let self = this;
  356. let list = this.getIndexList(self.scroll_text_);
  357. for (var i = 0; i < list.length; i+=2) {
  358. let start = list[i];
  359. let end = list[i+1];
  360. self.all_src_list.push(self.scroll_text_.substring(start,end))
  361. }
  362. console.log("updateSrcList",self.all_src_list,self.scroll_text_)
  363. },
  364. stopRecv(){
  365. let self =this;
  366. self._socketTask.close()
  367. // self.initTcp(true);
  368. self.finish_recv();
  369. },
  370. handleClick(e) {
  371. e = e || window.event;
  372. let target = e.target || e.srcElement;
  373. let name = target.tagName.toLowerCase();
  374. let self =this;
  375. if(name.substring(0,3)=="abc"){
  376. let name_list = name.split('_');
  377. if(name_list.length>0){
  378. let index = parseInt(name_list[1])-1;
  379. console.log("handleClick",index,self.all_src_list)
  380. self.setCopy(self.all_src_list[index])
  381. }
  382. console.log("handleClick",self.all_src_list[index])
  383. console.log(target.tagName.toLowerCase())
  384. }
  385. console.log(name.substring(0,3))
  386. },
  387. copyUpdates(){
  388. let self = this;
  389. let str = self.recvMsgQueue[self.recvMsgQueue.length-1].msg_text
  390. let key = "</code></pre>";
  391. let index = self.all_src_list.length;
  392. while(str.indexOf(key) != -1){
  393. index++;
  394. let s_tag = "</code><abc_"+index+" class=\"copy-button\">Copy</abc_"+index+"></pre>"
  395. console.log("s_tag",s_tag)
  396. str=str.replace("</code></pre>", s_tag)
  397. }
  398. // str=str.replaceAll("</code></pre>", "</code><abc class=\"copy-button\">Copy</abc></pre>")
  399. self.recvMsgQueue[self.recvMsgQueue.length-1].msg_text = str
  400. // console.log("copyUpdates",str)
  401. self.$forceUpdate();
  402. },
  403. getIndexList(box_str){
  404. let box = box_str;
  405. let boxarr=[];
  406. let pos = box.indexOf('```');
  407. while(pos>-1){
  408. boxarr.push(pos);
  409. pos= box.indexOf('```',pos+1);
  410. }
  411. return boxarr
  412. },
  413. returnText(text){
  414. return "<style> body { background-color: black; color: white; } pre {position: relative;}.copy-button { position: absolute; top: 0;right: 0;padding: 4px 8px;background-color: #333;color: #fff;cursor: pointer;user-select: none;} p {line-height: 2;} li{line-height: 1.5;} table {border-collapse: collapse;width: 100%;font-family: Arial, sans-serif;font-size: 14px;}table th,table td {border: 1px solid #ddd;padding: 8px;text-align: left;}</style>" +text
  415. },
  416. // 滚动窗口以显示最新的一条消息
  417. showLastMsg() {
  418. let self = this;
  419. let container = uni.createSelectorQuery().in(this).select(".scroll-view-text");
  420. // 利用uniapp提供的接口获取可视区域的高度和滚动高度
  421. // let query=uni.createSelectorQuery()
  422. // let container=query.select('.box');
  423. container.fields({
  424. // rect:true, //是否返回节点布局位置信息{left,top,right,bottom}
  425. size:true, //是否返回节点尺寸信息{width,height}
  426. scrollOffset:true //是否返回节点滚动信息{scrollLeft,scrollTop}
  427. },(res)=>{
  428. self.scroll_top = res.height;
  429. // console.log(res)
  430. }).exec()
  431. },
  432. isAItell(type){
  433. if(type===config.type_ai){
  434. return true;
  435. }
  436. return false;
  437. },
  438. make_avatar(type){
  439. if(type===config.type_ai){
  440. return "../../static/PubImgs_avatar_avatar7.png";
  441. }
  442. return "../../static/PubImgs_avatar_avatar8.png";
  443. },
  444. isShow(text){
  445. return text!="";
  446. },
  447. initAni(){
  448. let self = this;
  449. if(!self.disabled){
  450. clearTimeout(self.initAni)
  451. return;
  452. }
  453. let loading_title = self.recvMsgQueue[self.recvMsgQueue.length-1].loading_title;
  454. let loading_title_str = ""
  455. for (var i = 0; i < loading_title; i++) {
  456. loading_title_str+="."
  457. }
  458. // console.log("loading_title_str",loading_title_str)
  459. self.recvMsgQueue[self.recvMsgQueue.length-1].msg_text ="正在生成" +loading_title_str;
  460. loading_title = loading_title>=3?0: (loading_title+1)
  461. self.recvMsgQueue[self.recvMsgQueue.length-1].loading_title = loading_title;
  462. setTimeout(self.initAni,1000)
  463. },
  464. async finish_recv(){
  465. let self = this;
  466. self.recvStatus = false;
  467. self.disabled = false;
  468. clearTimeout(self.initAni)
  469. self.scroll_text_+="\n"
  470. console.log("self.recvMsgQueue",self.scroll_text_)
  471. self.stopUpdateView();
  472. self.copyUpdates();
  473. self.updateSrcList();
  474. self.showLastMsg();
  475. self.recvMsgQueue[self.recvMsgQueue.length-1].isFinish = true;
  476. },
  477. async sendMsg(){
  478. if(this.choose_img_tempFilePath != '') {
  479. this.sendImageAndTextOperation()
  480. return
  481. }
  482. // 如果内容为空
  483. if (!this.inputValue) {
  484. // 弹出提示框
  485. return uni.showToast({
  486. // 提示内容
  487. title: '内容不能为空',
  488. // 不显示图标
  489. icon: 'none'
  490. });
  491. }
  492. this.sendSocketMessage(this.inputValue)
  493. },
  494. onKeyInput(event) {
  495. // console.log("onKeyInput",event.target.value)
  496. let self = this;
  497. // self.inputOldValue=self.inputValue
  498. },
  499. startUpdateView(){
  500. let self = this;
  501. self.$forceUpdate();
  502. setTimeout(self.startUpdateView,500)
  503. },
  504. stopUpdateView(){
  505. let self = this;
  506. clearTimeout(self.startUpdateView)
  507. },
  508. sendSocketMessage(msg,prompt=msg,des_img='',action='generate',image_id='') {
  509. console.log("sendSocketMessage send",des_img)
  510. let self = this;
  511. let isRelax = self.is_open_relax;
  512. let token = isRelax?"7d995cf958f54d2c9c3264e645a9c4d9":"bdb957b8003e4a46b6b26a7174be5789";
  513. let relax = isRelax?"/relax" :"";
  514. let url = 'https://api.zhishuyun.com/midjourney/imagine' +relax +'?token='+token
  515. self.sendStatus = true;
  516. self.disabled = true;
  517. self.ani_style.rotate = 0;
  518. self.inputValue = '';
  519. self.recvMsgQueue.push({who_msg:config.type_self,msg_type:0,msg_text:msg,isFinish:true,'des_img':des_img})
  520. self.recvMsgQueue.push({who_msg:config.type_ai,msg_type:0,msg_text:"正在生成...",msg_img:"",isFinish:false,actions:[],image_id:"",task_id:"",loading_title:1})
  521. // self.recvMsgQueue[self.recvMsgQueue.length-1].actions=['upsample1', 'upsample2', 'upsample3', 'upsample4', 'variation1', 'variation2', 'variation3', 'variation4']
  522. self.initAni();
  523. uni.request({
  524. url:url,
  525. timeout:600000,
  526. header: {'Content-Type':'application/json','accept':'application/json'},
  527. method:'POST',
  528. data:{'prompt':prompt,'action':action,'image_id':image_id},
  529. success:(res)=>{
  530. self.finish_recv()
  531. if(res.statusCode===200){
  532. let html_img = '<!DOCTYPE html> <html><head></head><body> <img src='+res.data.image_url+ '></body></html>'
  533. self.recvMsgQueue[self.recvMsgQueue.length-1].msg_img = res.data.image_url;
  534. self.recvMsgQueue[self.recvMsgQueue.length-1].msg_text = "";
  535. self.recvMsgQueue[self.recvMsgQueue.length-1].image_id = res.data.image_id;
  536. console.log("res.data.actions",res.data.actions)
  537. self.recvMsgQueue[self.recvMsgQueue.length-1].actions = [];
  538. let list = [];
  539. list = res.data.actions;
  540. for (var i = 0; i < list.length; i++) {
  541. self.recvMsgQueue[self.recvMsgQueue.length-1].actions.push(list[i])
  542. }
  543. }else{
  544. uni.showModal({
  545. content:res.data.detail+":"+res.statusCode,
  546. showCancel:false
  547. })
  548. self.recvMsgQueue[self.recvMsgQueue.length-1].msg_text = res.data.detail;
  549. }
  550. console.log("Ai-view",res)
  551. },
  552. fail: (e) => {
  553. console.log('fail', e)
  554. self.finish_recv()
  555. self.recvMsgQueue[self.recvMsgQueue.length-1].msg_text ="生成失败";
  556. }
  557. })
  558. },
  559. onclickSendImg(index){
  560. let img_url = this.recvMsgQueue[index].des_img
  561. if(img_url) {
  562. uni.previewImage({
  563. urls:[img_url]
  564. })
  565. } else {
  566. uni.showToast({
  567. icon:'error',
  568. title:'预览图片失败',
  569. duration:2000
  570. })
  571. }
  572. },
  573. onclickAiGetImg(index){
  574. let img_url = this.getImg(index)
  575. if(img_url) {
  576. uni.previewImage({
  577. urls:[img_url]
  578. })
  579. } else {
  580. uni.showToast({
  581. icon:'error',
  582. title:'预览图片失败',
  583. duration:2000
  584. })
  585. }
  586. },
  587. onclickDeleteChooseImg() {
  588. let self = this
  589. uni.showModal({
  590. showCancel:true,
  591. content:"是否删除",
  592. success:(res)=>{
  593. if(res.confirm){
  594. self.choose_img_tempFilePath = ''
  595. }
  596. }
  597. })
  598. },
  599. onclickchooseImg(event){
  600. let self = this;
  601. uni.chooseImage({
  602. success: (chooseImageRes) => {
  603. const tempFilePaths = chooseImageRes.tempFilePaths;
  604. this.choose_img_tempFilePath = tempFilePaths[0]
  605. if(tempFilePaths.length > 0) {
  606. self.choose_img_tempFilePath = tempFilePaths[0]
  607. } else {
  608. uni.showToast({
  609. icon:'error',
  610. title:'图片选择失败,请重新选择',
  611. duration:2000
  612. })
  613. }
  614. }
  615. });
  616. },
  617. sendImageAndTextOperation() {
  618. if (this.inputValue.length == 0) {
  619. return uni.showToast({
  620. title: '请添加描述哦,只支持英文',
  621. icon: 'none'
  622. });
  623. }
  624. let self = this
  625. self.is_up_img_file = true
  626. // 上传图片+发送图片链接和文本
  627. // 上传图片
  628. uni.uploadFile({
  629. url: 'https://mlapi.hainanmlwl.com/file/upload_aiimg',
  630. filePath: self.choose_img_tempFilePath,
  631. name: 'aiimg_url',
  632. formData: {
  633. // 'aiimg_url': self.cur_select_img
  634. },
  635. success: (uploadFileRes) => {
  636. console.log("uploadFile",uploadFileRes);
  637. self.is_up_img_file = false;
  638. let obj = JSON.parse(uploadFileRes.data)
  639. uni.showToast({
  640. title:obj.message,
  641. duration:1500
  642. })
  643. if(obj.code == 10000) {
  644. let img_url = obj.content.surl
  645. if(img_url == '') {
  646. uni.showModal({
  647. content:"图片链接为空",
  648. showCancel:false
  649. })
  650. return
  651. }
  652. if(self.choose_img_tempFilePath != '') {
  653. self.choose_img_tempFilePath = ''
  654. }
  655. // 发送
  656. self.sendSocketMessage(self.inputValue, img_url+" "+self.inputValue ,img_url)
  657. } else {
  658. uni.showModal({
  659. showCancel:false,
  660. content:obj.msg
  661. })
  662. }
  663. },
  664. fail(err) {
  665. console.log('上传失败err=',err)
  666. uni.showModal({
  667. content:"上传失败",
  668. showCancel:false
  669. })
  670. self.is_up_img_file = false;
  671. }
  672. });
  673. // if(this.cur_select_img==="../../static/import-img.png"){
  674. // uni.showToast({
  675. // title:"请选择图片"
  676. // })
  677. // return
  678. // }
  679. // if(this.diy_inputValue.length>0){
  680. // this.is_show_import_img_view = false;
  681. // let des_img = this.cur_select_img;
  682. // this.sendSocketMessage(this.diy_inputValue,this.cur_select_img+" "+this.diy_inputValue,des_img)
  683. // this.diy_inputValue = '';
  684. // }else{
  685. // uni.showToast({
  686. // title:"请添加描述,只支持英文哦"
  687. // })
  688. // }
  689. }
  690. }
  691. }
  692. </script>
  693. <style>
  694. .container{
  695. display: flex;
  696. box-sizing: border-box;
  697. flex-direction: column;
  698. position: fixed;
  699. /* position: relative; */
  700. /* background-color: #2A2832; */
  701. left: 0;
  702. width: 100%;
  703. justify-content: center;
  704. }
  705. .content {
  706. height: 100%;
  707. display: flex;
  708. flex-direction: column;
  709. align-items: center;
  710. justify-content: center;
  711. position: relative;
  712. background-color: #2A2832;
  713. }
  714. .text-area {
  715. display: flex;
  716. justify-content: center;
  717. }
  718. .title {
  719. font-size: 36rpx;
  720. color: #8f8f94;
  721. }
  722. .textarea-box,
  723. .textarea {
  724. width: 100%;
  725. height: 100%;
  726. margin-left: 3%;
  727. },
  728. textarea-box {
  729. width: 100% !important;
  730. }
  731. .textarea-box,
  732. .textarea,
  733. textarea,
  734. textarea-box {
  735. /* height: 120px; */
  736. }
  737. .textarea-box {
  738. background-color: #FFF;
  739. }
  740. uni-scroll-view .uni-scroll-view::-webkit-scrollbar {
  741. display: none;
  742. width: 0 !important;
  743. height: 0 !important;
  744. -webkit-appearance: none;
  745. background: transparent;
  746. color: transparent;
  747. }
  748. </style>