AI-draw.vue 32 KB

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