viewMask.vue 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. <template src="./viewMask.html">
  2. </template>
  3. <script>
  4. const util = require('@/utils/util.js').default;
  5. import router from "@/router";
  6. import { MessageBox } from 'mint-ui';
  7. import spaceTypes from '@/static/spaceTypesIE.js';
  8. // const config = require('@/services/url$config.js');
  9. import touchHandle from '@/mixins/touchHandle.js';
  10. import {
  11. setStorage,
  12. getStorage,
  13. } from '@/utils/localStorage';
  14. // import requestConfig from '@/static/lib/requestConfig';
  15. // const app = getApp(); //获取应用实例
  16. export default {
  17. mixins: [touchHandle],
  18. data: function() {
  19. return {
  20. currentIndex:0, //当前看到的图片序号
  21. showAIImage: false,//是否显示当前AI结果集合 默认不显示,因为没有
  22. aiImagesList:[
  23. ],//AI生成的图片列表
  24. styleList: [], //风格列表数据
  25. curStyleIndex: 0, //当前选中的风格序号
  26. aiFlag:false,
  27. aiImage: "",
  28. inputBase64Url: "",
  29. aiPicId: "", // 图生图任务ID
  30. randomTimer:null,
  31. timer: null,
  32. outTimer: null,
  33. imageWidth: 750,
  34. imageHeight: 448,
  35. showAIFlag:false,//继续生成状态切换标志
  36. checked:false,
  37. shottingImg:'',
  38. count:0,
  39. random:1,
  40. // disableAble:false,
  41. myloading:false,
  42. loadingMsg:'',
  43. screenWidth:0,
  44. screenHeight:0,
  45. modelType:2, //当前模型类型,1指创意设计 2指精准设计 默认2
  46. themeIndex:0,
  47. themeList:[
  48. {
  49. id:1,
  50. text:'默认',
  51. prot:'',
  52. },
  53. {
  54. id:2,
  55. leftColor:'#F4EBDE',
  56. rightColor:'#C1C9B4',
  57. text:'莫兰素雅',
  58. prot:'Soft grays, warm earth tones, subtle blues, soft pinks,',
  59. },
  60. {
  61. id:3,
  62. leftColor:'#1F1F1F',
  63. rightColor:'#FFFFFF',
  64. text:'经典黑白',
  65. prot:'Classic black and pure white,',
  66. },
  67. {
  68. id:4,
  69. leftColor:'#6FA5BE',
  70. rightColor:'#E2CCC5',
  71. text:'海岸珊瑚',
  72. prot:'Deep ocean blue paired with vibrant coral red,',
  73. },
  74. {
  75. id:5,
  76. leftColor:'#849059',
  77. rightColor:'#E6CEB4',
  78. text:'森林璀璨',
  79. prot:'Deep forest green paired with luxurious gold,'
  80. },
  81. {
  82. id:6,
  83. leftColor:'#A1BACB',
  84. rightColor:'#E6DFD8',
  85. text:'梦幻静谧',
  86. prot:'Dreamy powder blue paired with neutral gray,'
  87. },
  88. {
  89. id:7,
  90. leftColor:'#303B2B',
  91. rightColor:'#D8C8A6',
  92. text:'自然和谐',
  93. prot:'Natural olive green paired with warm beige,'
  94. },
  95. {
  96. id:8,
  97. leftColor:'#652C30',
  98. rightColor:'#D5C3B0',
  99. text:'醇酒乳香',
  100. prot:'Rich burgundy paired with soft cream,'
  101. },
  102. {
  103. id:9,
  104. leftColor:'#E3C7C5',
  105. rightColor:'#EDE4DB',
  106. text:'雅致浪漫',
  107. prot:'Understated taupe paired with romantic pink,'
  108. },
  109. {
  110. id:10,
  111. leftColor:'#2C3C6E',
  112. rightColor:'#F0DBBD',
  113. text:'夜空璀璨',
  114. prot:'Elegant navy blue paired with glamorous gold,'
  115. },
  116. {
  117. id:11,
  118. leftColor:'#ACC9A1',
  119. rightColor:'#E6DCC6',
  120. text:'复古翠绿',
  121. prot:'Vintage tan paired with vibrant green,'
  122. },
  123. {
  124. id:12,
  125. leftColor:'#AAA8A7',
  126. rightColor:'#D8A176',
  127. text:'暖阳灰影',
  128. prot:'Warm orange paired with cool gray,'
  129. },
  130. {
  131. id:13,
  132. leftColor:'#413D41',
  133. rightColor:'#D1B687',
  134. text:'耀眼黑金',
  135. prot:'black and gold,'
  136. },
  137. ],
  138. timeOut:20000, //超时时间
  139. subType:0,
  140. currentImg:null,
  141. reqList:[
  142. {
  143. model:'batouresearch/sdxl-controlnet-lora',
  144. level:'5',
  145. aiPicId:'',
  146. resultImg:'',
  147. }
  148. ],
  149. reqListReset:[
  150. {
  151. model:'deployments/elabgroup/elab-sdxl-controlnet-lora',
  152. level:'3',
  153. aiPicId:'',
  154. resultImg:'',
  155. },{
  156. model:'batouresearch/sdxl-controlnet-lora',
  157. level:'5',
  158. aiPicId:'',
  159. resultImg:'',
  160. }
  161. ],
  162. // dialogVisible: false,
  163. // dialogAIVisible: false,
  164. datalist:[],
  165. connectUsImg:'',
  166. curSpaceId:'',
  167. }
  168. },
  169. props:{
  170. spaceObj: {
  171. type: Object,
  172. default: null,
  173. },
  174. spaceList: {
  175. type: [Array ,Object],
  176. default: [],
  177. },
  178. },
  179. watch: {
  180. spaceObj(newVal, oldVal) {
  181. if (newVal == null) {
  182. return;
  183. }
  184. console.log("当前空间数据view-mark-watch:", newVal);
  185. this.getAiBeautyFamily();
  186. // this.curSpaceArea = parseFloat(
  187. // (newVal.spaceWidth * newVal.spaceHeight) / 10000
  188. // ).toFixed(2);
  189. // this.getOverallArrangementDetailsList();
  190. },
  191. spaceList(newVal, oldVal) {
  192. if (newVal == null) {
  193. return;
  194. }
  195. console.log("当前空间数据spaceList-watch:", newVal);
  196. if(newVal && newVal.length>0){
  197. this.datalist = [];
  198. newVal.forEach(it=>{
  199. if(it.spaceName && !it.isSizeLock){
  200. this.datalist.push(it);
  201. }
  202. })
  203. }
  204. // this.curSpaceArea = parseFloat(
  205. // (newVal.spaceWidth * newVal.spaceHeight) / 10000
  206. // ).toFixed(2);
  207. // this.getOverallArrangementDetailsList();
  208. },
  209. },
  210. filters:{
  211. spaceTypeFilter(type){
  212. let name = spaceTypes[type - 1];
  213. return name || '-'
  214. },
  215. },
  216. mounted() {//组件挂载时事件
  217. // console.warn("***mounted-nav***",this.seedItem)
  218. // if(this.seedItem){
  219. // this.title = this.seedItem.seedText;
  220. // }
  221. // wx.checkJsApi({
  222. // jsApiList: ['previewImage'], // 需要检测的JS接口列表,所有JS接口列表见附录2,
  223. // success: function(res) {
  224. // // 以键值对的形式返回,可用的api值true,不可用为false
  225. // // 如:{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
  226. // console.warn("checkJsApi",res)
  227. // // alert('su'+ JSON.stringify(res))
  228. // },
  229. // fail:(err)=>{
  230. // console.warn("checkJsApi-err",err)
  231. // // alert('err'+ JSON.stringify(err) )
  232. // }
  233. // })
  234. this.screenWidth = window.screen.width;
  235. this.screenHeight = window.screen.height;
  236. if(window.innerWidth && window.screen.width){
  237. this.screenWidth = Math.min(window.innerWidth,window.screen.width)
  238. }
  239. if(window.innerHeight && window.screen.height){
  240. this.screenHeight = Math.min(window.innerHeight,window.screen.height)
  241. }
  242. let firstData = JSON.parse(getStorage('firstData'));
  243. this.connectUsImg = firstData[0].selectOptionImageUrl;
  244. },
  245. beforeDestroy:function(){
  246. console.warn("***beforeDestroy***");//更新到页面上的数据
  247. this.stopInterval();
  248. },
  249. computed: {
  250. aiData() {
  251. return this.$store.state.aiData;
  252. },
  253. curHouseObj() {
  254. return this.$store.state.curHouseObj;
  255. },
  256. },
  257. methods:{
  258. swiperChangeImg(e){
  259. this.currentIndex = e.detail?e.detail.current:e;
  260. this.checked = this.aiImagesList[this.currentIndex].checked;
  261. console.warn("***swiperChangeImg***",this.checked);//更新到页面上的数据
  262. },
  263. //切换空间
  264. changeSpace(item){
  265. this.$parent.changeSpace(item.spaceId);
  266. let param = {
  267. type: 'CLK', //埋点类型
  268. clkId: 'clk_2cmina_23080417', //点击ID
  269. clkName: 'changeangle_clk', //点击前往的页面名称
  270. clkParams: {
  271. locusName: "视角切换",
  272. spaceId:item.spaceId,
  273. type:this.styleList[this.curStyleIndex].styleName
  274. }
  275. };
  276. util.trackRequest(param);
  277. },
  278. //视角切换
  279. switchActor(){
  280. // this.$parent.clearHandle()
  281. this.$emit('switchActor');
  282. this.showAIImage = false;//隐藏AI结果集合-执行切换视角
  283. let index = this.$parent.currentActor.userIndex;//当前视角的序号
  284. let nextIndex = (index + 1) % this.$parent.actors.length;
  285. let param = {
  286. type: 'CLK', //埋点类型
  287. clkId: 'clk_2cmina_23080417', //点击ID
  288. clkName: 'changeangle_clk', //点击前往的页面名称
  289. clkParams: {
  290. locusName: "视角切换",
  291. type:this.$parent.actors[nextIndex].actorEum
  292. }
  293. };
  294. util.trackRequest(param);
  295. },
  296. showOrHideWebGl(){
  297. // this.$parent.clearHandle()
  298. this.showAIImage = !this.showAIImage;
  299. let param = {
  300. type: 'CLK', //埋点类型
  301. clkId: 'clk_2cmina_23080415', //点击ID
  302. clkName: 'contrast_clk', //点击前往的页面名称
  303. clkParams: {
  304. locusName: "对比",
  305. }
  306. };
  307. util.trackRequest(param);
  308. },
  309. //空间或者风格切换
  310. selectAction(selItem,type,index) {
  311. console.log('点击动作111:', selItem,type,index)
  312. var self = this;
  313. if(this.themeIndex == index){
  314. return false
  315. }
  316. this.themeIndex = index;
  317. let param = {
  318. type: 'CLK', //埋点类型
  319. clkId: 'clk_2cmina_23112701', //点击ID
  320. clkName: 'AIstyle_theme_clk', //点击前往的页面名称
  321. clkParams: {
  322. locusName: "AI 主题",
  323. type:this.themeList[this.themeIndex].text
  324. }
  325. };
  326. util.trackRequest(param);
  327. },
  328. //切换模式
  329. changeModel(){
  330. this.modelType = (this.modelType + 1) >2 ? 1 : 2;
  331. },
  332. //保存到相册
  333. save() {
  334. //表示canvas正在绘制,不能进行保存
  335. if (!this.aiImagesList || this.aiImagesList.length==0) {
  336. return false;
  337. }
  338. var _resultImg = this.aiImagesList[this.currentIndex].image;
  339. //正在选择照片,不能生效
  340. if (!_resultImg || _resultImg.length==0) {
  341. this.showToast("请选中图片后再试!");
  342. return false;
  343. }
  344. var para = {
  345. type: 'CLK', //埋点类型
  346. clkId: 'clk_2cmina_56', //点击ID,固定
  347. clkName: "share-savepic", //点击名称
  348. expand: {
  349. "resultImg": (_resultImg || ""),
  350. },
  351. }
  352. util.trackRequest(para);
  353. if(navigator.userAgent.toLocaleLowerCase().includes('micromessenger')){
  354. this.$message("请长按图片保存!");
  355. }else{
  356. this.saveImageHandle(_resultImg);
  357. }
  358. },
  359. downloadIamge(imgsrc) { //下载图片地址和图片名
  360. var fileName = "4DImage" + util.formatDate(new Date(), "yyyyMMddhhmmss") + '.jpg';
  361. const image = new Image();
  362. // 解决跨域 Canvas 污染问题
  363. image.setAttribute("crossOrigin", "anonymous");
  364. image.onload = ()=> {
  365. // let canvas = document.createElement("canvas");
  366. // canvas.width = image.width;
  367. // canvas.height = image.height;
  368. // const context = canvas.getContext("2d");
  369. // context.drawImage(image, 0, 0, image.width, image.height);
  370. // const url = canvas.toDataURL("image/jpg"); //得到图片的base64编码数据
  371. const a = document.createElement("a"); // 生成一个a元素
  372. const event = new MouseEvent("click"); // 创建一个单击事件
  373. a.download = fileName || "photo"; // 设置图片名称
  374. a.href = imgsrc; // 将生成的URL设置为a.href属性
  375. a.dispatchEvent(event); // 触发a的单击事件
  376. this.showToast("保存成功!");
  377. };
  378. image.src = imgsrc;
  379. },
  380. saveImageHandle(_resultImg){
  381. let param = {
  382. type: 'CLK', //埋点类型
  383. clkId: 'clk_2cmina_23080414', //点击ID
  384. clkName: 'download_clk', //点击前往的页面名称
  385. clkParams: {
  386. locusName: "下载",
  387. img:_resultImg
  388. }
  389. };
  390. util.trackRequest(param);
  391. this.downloadIamge(_resultImg);
  392. },
  393. //点赞喜欢
  394. changeAIImg() {
  395. // let lastPage = getCurrentPages()[getCurrentPages().length - 2] ? getCurrentPages()[getCurrentPages().length - 2].$vm : null;
  396. this.checked = !this.checked;//变更选项
  397. this.aiImagesList[this.currentIndex].checked = this.checked;
  398. console.warn("***changeAIImg***",this.checked,this.aiData)
  399. // if(this.aiData){//给上一个页面回传生成的数据
  400. // let space = this.aiData.find(it=>{
  401. // return it.spaceId == this.spaceObj.spaceId
  402. // })
  403. // if(space){
  404. // space.aiImagesList[this.currentIndex].checked = this.checked;
  405. // }
  406. // }
  407. if(this.checked){
  408. let param = {
  409. type: 'CLK', //埋点类型
  410. clkId: 'clk_2cmina_23080418', //点击ID
  411. clkName: 'chooseprogramme_clk', //点击前往的页面名称
  412. clkParams: {
  413. locusName: "选定风格/视角",
  414. style:this.styleList[this.curStyleIndex].styleName,
  415. img:this.aiImagesList[this.currentIndex].image,
  416. }
  417. };
  418. util.trackRequest(param);
  419. }
  420. },
  421. rightScroll(){//右滑
  422. if(this.currentIndex <= (this.aiImagesList.length - 1) && this.currentIndex > 0){
  423. // this.currentIndex --;
  424. this.$refs.carousel.prev();
  425. }
  426. },
  427. leftScroll(){//继续生成
  428. if(this.currentIndex != this.aiImagesList.length - 1){
  429. if(this.currentIndex<this.aiImagesList.length - 1){
  430. // this.currentIndex ++;
  431. this.$refs.carousel.next();
  432. }
  433. return false;
  434. }
  435. console.log("***leftScroll***",this.currentIndex,this.aiImagesList.length)
  436. this.aiSubmit(2);//继续生成下一张
  437. },
  438. //预览图片-使用微信自带的预览功能,即H5中不可用
  439. previewAction() {
  440. if(wx && typeof(wx.previewImage)=='function'){
  441. let imglist = this.aiImagesList.map(it=>it.image);
  442. wx.previewImage({
  443. current: this.currentIndex,
  444. urls: imglist,
  445. success: (res) => {
  446. console.log('图片预览:', res);
  447. },
  448. fail:(err)=>{
  449. console.warn('图片预览-err:', res);
  450. }
  451. })
  452. }
  453. },
  454. mynavigateFuc(e) {
  455. if (e) {
  456. // this.$parent.clearHandle();
  457. let param = {
  458. type: 'CLK', //埋点类型
  459. clkId: 'clk_2cmina_23080408', //点击ID
  460. clkName: 'WeCom_clk', //点击前往的页面名称
  461. clkParams: {
  462. locusName: "联系定制",
  463. }
  464. };
  465. util.trackRequest(param);
  466. if(window.__wxjs_environment === 'miniprogram'){
  467. wx.miniProgram.navigateTo({url: '/pages/transfer/transfer?event=openCustomerServiceChat&houseId='+this.$store.state.houseId})
  468. }else{
  469. this.$message.warning("敬请期待");
  470. }
  471. }
  472. },
  473. catchTapEvent:function(){
  474. return false;
  475. },
  476. //获取AI风格列表
  477. async getAiBeautyFamily() {
  478. // const spaceName = this.spaceTypes[this.spaceObj.spaceType - 1].title;
  479. let res = await requestConfig("getHardboundEffects", {
  480. "houseId": this.$route.query.houseId || this.$store.state.houseId,
  481. "spaceType": this.spaceObj.spaceType,
  482. "spaceName": this.spaceObj.spaceName,
  483. });
  484. this.styleList = [];
  485. // alert("***getAiBeautyFamily-res***"+JSON.stringify(res))
  486. if (res.success) {
  487. let list = res.list;
  488. this.styleList = list;
  489. }
  490. if(!this.styleList || this.styleList.length==0){
  491. // this.disableAble = true;//不能点击-没有风格
  492. let unit = this.screenWidth / 750;//单位rpm 对应 px 的值
  493. this.$parent.canvasHeight = this.screenHeight - (208 * unit);
  494. this.$parent.camera.aspect = this.screenWidth / this.$parent.canvasHeight;
  495. this.$parent.camera.updateProjectionMatrix();
  496. this.$parent.renderer.setSize(this.screenWidth, this.$parent.canvasHeight);
  497. }else{
  498. // this.disableAble = false;//可以点击
  499. }
  500. },
  501. //风格选择
  502. selectStyle(idx){
  503. if(this.curStyleIndex == idx){
  504. return false
  505. }
  506. this.curStyleIndex = idx;
  507. let param = {
  508. type: 'CLK', //埋点类型
  509. clkId: 'clk_2cmina_23080416', //点击ID
  510. clkName: 'AIstyle_ret_clk', //点击前往的页面名称
  511. clkParams: {
  512. locusName: "AI 风格",
  513. type:this.styleList[this.curStyleIndex].styleName
  514. }
  515. };
  516. util.trackRequest(param);
  517. },
  518. // AI渲染
  519. async aiSubmit(type) {
  520. if(!this.styleList || this.styleList.length==0 || this.curStyleIndex==-1
  521. || !this.styleList[this.curStyleIndex].prompt || !this.styleList[this.curStyleIndex].negativePrompt){
  522. return false;
  523. }
  524. // 防止连续点击处理
  525. if (this.aiFlag) {
  526. return
  527. }
  528. this.aiFlag = true;
  529. this.aiImage = "";//清空AI结果图
  530. this.stopInterval();
  531. // if(!this.showAIImage){
  532. this.myloading = true;
  533. this.loadingMsg = "启动中";
  534. // }
  535. // this.inputBase64Url = await this.shottingAction(2);//开始截图-返回的是base64的数据
  536. // this.startServer();
  537. // let base64 = await this.$parent.shottingAction(2);//开始截图-返回的是base64
  538. let shottingImg = await this.$parent.shottingAction();//开始截图-返回的是图片地址
  539. if(!shottingImg){
  540. this.showToast("渲染失败,请重试");
  541. return false;
  542. }
  543. // ?x-oss-process=image/auto-orient,1/quality,Q_46/format,jpg //阿里OSS
  544. // "?imageMogr2/auto-orient/format/webp/blur/1x0/quality/75";//七牛云压缩图片
  545. shottingImg += "?x-oss-process=image/auto-orient,1/quality,Q_46/format,jpg";//压缩图片
  546. this.shottingImg = shottingImg;
  547. this.changeImg2Base64(this.shottingImg, false);
  548. console.warn("***shottingImg***",this.shottingImg)
  549. if(type==1){
  550. let param = {
  551. type: 'CLK', //埋点类型
  552. clkId: 'clk_2cmina_23080413', //点击ID
  553. clkName: 'AIcreate_clk', //点击前往的页面名称
  554. clkParams: {
  555. locusName: "AI生成",
  556. userparamter:{
  557. shottingImg:this.shottingImg,
  558. style:this.styleList[this.curStyleIndex].styleName
  559. }
  560. }
  561. };
  562. util.trackRequest(param);
  563. }else{
  564. let param = {
  565. type: 'CLK', //埋点类型
  566. clkId: 'clk_2cmina_23080419', //点击ID
  567. clkName: 'continueAIcreate_clk', //点击前往的页面名称
  568. clkParams: {
  569. locusName: "继续生成",
  570. userparamter:{
  571. shottingImg:this.shottingImg,
  572. style:this.styleList[this.curStyleIndex].styleName
  573. }
  574. }
  575. };
  576. util.trackRequest(param);
  577. }
  578. },
  579. image2Base64(imgUrl) {//导入的图片路径
  580. var toBase64= new Promise(function(resolve, reject){
  581. window.URL = window.URL || window.webkitURL;
  582. var xhr = new XMLHttpRequest();
  583. xhr.open("get", imgUrl, true);
  584. // 至关重要
  585. xhr.responseType = "blob";//文件流
  586. xhr.onload = function (res) {
  587. if (res.currentTarget.status == 200) {
  588. //得到一个blob对象
  589. var blob = res.currentTarget.response;
  590. // 至关重要
  591. let oFileReader = new FileReader();
  592. oFileReader.onloadend = function (e) {
  593. let base64 = e.target.result;//base64
  594. resolve(base64)
  595. };
  596. oFileReader.readAsDataURL(blob);
  597. }
  598. }
  599. xhr.send();
  600. });
  601. return toBase64;
  602. },
  603. changeImg2Base64(url, isRepeat) {
  604. var self = this;
  605. if(isRepeat && self.inputBase64Url){//重复使用
  606. self.startServer();
  607. }else{
  608. self.startServer();
  609. }
  610. },
  611. //前置逻辑
  612. async prevHandle(parmas){
  613. //不是首次请求,则无需前置判断 用户上传的也不需要前置处理
  614. if(this.subType!=0){
  615. return '';
  616. }
  617. return new Promise(async (resolve, reject) => {
  618. let _data = JSON.parse(JSON.stringify(parmas))
  619. delete _data.webhook;
  620. let res = await requestConfig("img2img_local", _data);
  621. if (res.success) {
  622. if (res.success && res.single) {
  623. let resultImg = res.single;
  624. resolve(resultImg);
  625. }else{
  626. resolve('');
  627. }
  628. }else{
  629. resolve('');
  630. }
  631. })
  632. },
  633. //开始图生图流程
  634. async startServer() {
  635. if(!this.styleList || this.curStyleIndex < 0 || !this.styleList[this.curStyleIndex].imgUrl){
  636. return false
  637. }
  638. if(this.$parent.pvCurPageName!="room_show"){//说明用户切换页面了
  639. console.warn("***用户已经退出页面***")
  640. return false;
  641. }
  642. let imgUrl = this.styleList[this.curStyleIndex].imgUrl;
  643. let subType = this.subType;
  644. let session_hash = Date.now();
  645. let theme = this.themeList[this.themeIndex];//主题数据
  646. let prompt = this.styleList[this.curStyleIndex].prompt + theme.prot;
  647. let noPromot = this.styleList[this.curStyleIndex].negativePrompt;
  648. let unit = 768 / this.screenWidth;
  649. this.imageWidth = parseInt((this.screenWidth * unit).toFixed());
  650. this.imageHeight = parseInt((this.$parent.canvasHeight * unit).toFixed());
  651. this.timeOut = this.modelType==1? 20000: 50000;
  652. var parmas = {
  653. negativePrompt: noPromot,
  654. prompt: prompt,
  655. "batchSize": 1,
  656. brandId: $config.brandId,
  657. height: this.imageHeight,
  658. width: this.imageWidth,
  659. "moduleType": "AI_Biography",
  660. "steps":20,
  661. "sampler":"DDIM",
  662. "controlNetSessionHash":session_hash,
  663. "cfgScale":12,
  664. "denoising":0.9,
  665. image:this.shottingImg,
  666. styleImage:imgUrl,
  667. keyword: "replicate",
  668. model: this.modelType==1? "lucataco/ssd-1b" : 'catio-apps/controlnet-interior-design',
  669. };
  670. if(this.modelType==2){//精准设计
  671. if(subType == 0){//首次
  672. parmas.model = this.reqList[0].model;
  673. }else{//重试
  674. parmas.model = this.reqListReset[0].model;
  675. }
  676. }else{//创意设计
  677. if(subType == 0){//首次
  678. parmas.model = "lucataco/ssd-1b";
  679. }else{//重试
  680. parmas.model = "deployments/elabgroup/elab-ssd-1b";
  681. }
  682. }
  683. this.curSpaceId = this.spaceObj.spaceId;//生成时的空间id
  684. let result = await this.prevHandle(parmas);
  685. console.warn("***prevHandle***",result)
  686. if(result && result.length>0){
  687. this.randomHandle(result)
  688. return false;
  689. }else{
  690. if(this.modelType==2){//精准设计
  691. this.otherHandle(parmas);//发送其他AI请求
  692. }
  693. let res = await requestConfig("generateTaskImgToImgForAliyun", parmas);
  694. console.log("图生图结果:", res);
  695. let that = this;
  696. if (res.success && res.single) {
  697. this.aiPicId = res.single;
  698. if(this.modelType==2){//精准设计
  699. if(subType == 0){//首次
  700. this.reqList[0].aiPicId = this.aiPicId;
  701. }else{//重试
  702. this.reqListReset[0].aiPicId = this.aiPicId;
  703. }
  704. }
  705. if (this.aiPicId) {
  706. this.startInterval();//开始轮询AI生成图的结果
  707. }else{
  708. this.stopInterval()
  709. }
  710. }else{
  711. this.stopInterval()
  712. this.showToast("渲染失败,请重试")
  713. }
  714. }
  715. },
  716. stopInterval() {
  717. if (this.randomTimer) {
  718. clearInterval(this.randomTimer);
  719. this.randomTimer = null;
  720. }
  721. if (this.timer) {
  722. // clearInterval(this.timer);
  723. this.timer = null;
  724. }
  725. if (this.outTimer) {
  726. clearTimeout(this.outTimer)
  727. this.outTimer = null
  728. }
  729. this.subType = 0;
  730. this.reqList.forEach(it=>{
  731. it.aiPicId = '';
  732. it.resultImg = '';
  733. })
  734. this.reqListReset.forEach(it=>{
  735. it.aiPicId = '';
  736. it.resultImg = '';
  737. })
  738. // this.random = 1;
  739. this.myloading = false;
  740. this.aiFlag = false;
  741. },
  742. //随机处理
  743. randomHandle(resultImg){
  744. if(this.timer || !resultImg){
  745. return false;
  746. }
  747. let self = this;
  748. var count = 1;
  749. var process = 0;//进度
  750. var randomNum = Math.floor(Math.random() * 4 + 5);//5-8随机数
  751. this.randomTimer = setInterval(function() {
  752. process = parseInt(count * 100/(randomNum));
  753. if(process>=100){
  754. process = 99;
  755. }
  756. if (count < randomNum) {//没有到上限
  757. // if(!self.showAIImage){
  758. self.myloading = true;
  759. self.loadingMsg = '生成中…' + process + '%';
  760. // }
  761. }else{
  762. self.myloading = false;
  763. self.resultHandle(resultImg);
  764. }
  765. count = count + 1;
  766. }, 1000);
  767. },
  768. otherHandle(parmas){
  769. let reqList = [];
  770. if(this.subType == 0){//首次
  771. reqList = this.reqList;
  772. }else{//重试
  773. reqList = this.reqListReset;
  774. }
  775. reqList.forEach(async (it,index)=>{
  776. let _data = JSON.parse(JSON.stringify(parmas));
  777. if(index>0){
  778. _data.model = it.model;
  779. let res = await requestConfig("generateTaskImgToImgForAliyun", _data);
  780. if (res.success) {
  781. console.log('生成结果123:', res);
  782. it.aiPicId = res.single || '';
  783. }
  784. }
  785. })
  786. },
  787. //开始生成AI图的轮询,每隔1s轮询一次
  788. startInterval() {
  789. if(this.timer){
  790. return false;
  791. }
  792. let self = this;
  793. this.count = 1;//轮询次数
  794. var random = 0;
  795. this.currentImg = false;//当次生成图还没有结果
  796. this.timer = 1;
  797. this.getOutPicture();//不在轮询,而是等结果
  798. this.setOutTimer();//设置超时逻辑
  799. },
  800. //设置一个超时逻辑,到底指定时间后停止轮询,当前是90s
  801. setOutTimer() {
  802. if (this.outTimer) {
  803. clearTimeout(this.outTimer)
  804. this.outTimer = null
  805. }
  806. var self = this;
  807. this.outTimer = setTimeout(function() {
  808. if (self.timer) {
  809. let hasResult = false;
  810. if(self.modelType==2){//精准设计
  811. let reqList = [];
  812. if(self.subType == 0){//首次
  813. reqList = self.reqList;
  814. }else{//重试
  815. reqList = self.reqListReset;
  816. }
  817. reqList.some((item,index) => {
  818. if(item.resultImg){
  819. hasResult = true;
  820. self.resultHandle(item.resultImg)
  821. }
  822. });
  823. }
  824. console.warn("***hasResult***",hasResult)
  825. if(!hasResult){//没有结果
  826. self.stopInterval();//停止轮询
  827. // self.$message.warning("AI开了个小差,请稍后再试");
  828. // self.dialogVisible = true;//弹出超时提示
  829. MessageBox.confirm('',{
  830. title: '提示',
  831. message: '当前AI使用火爆,请继续尝试?',
  832. showCancelButton: true,
  833. confirmButtonText:'继续尝试',
  834. cancelButtonText:'取消等待',
  835. }).then(action => {
  836. console.warn("***MessageBox-action***",action)
  837. if(action == 'confirm'){
  838. self.confirmHandle(1);
  839. }
  840. }).catch(err=>{
  841. console.warn("***MessageBox-err***",err)
  842. if(err == 'cancel'){
  843. self.cancelHandle();
  844. }
  845. });
  846. // MessageBox.confirm('确定执行此操作?')
  847. }
  848. }
  849. clearTimeout(self.outTimer);
  850. self.outTimer = null
  851. }, this.timeOut);
  852. },
  853. confirmHandle(type){
  854. console.log('用户点击确定')
  855. this.subType = type || 0;
  856. this.startServer();
  857. },
  858. cancelHandle(){
  859. console.log('用户点击取消')
  860. this.subType = 0;
  861. },
  862. //获取生成图结果
  863. getOutPicture() {
  864. if(this.timer==null){
  865. console.warn("***当前轮询已经结束了1***")
  866. return false;
  867. }
  868. if(this.modelType==2){//精准设计
  869. let reqList = [];
  870. if(this.subType == 0){//首次
  871. reqList = this.reqList;
  872. }else{//重试
  873. reqList = this.reqListReset;
  874. }
  875. reqList.forEach((item,index) => {
  876. this.singleHandle(item)
  877. });
  878. }
  879. else{
  880. this.singleHandle({aiPicId:this.aiPicId})
  881. }
  882. },
  883. //发出获取结果请求获取AI生成结果
  884. async singleHandle(model){
  885. if(!model || !model.aiPicId){
  886. return false;
  887. }
  888. var parmas = {
  889. id: model.aiPicId,
  890. };
  891. let res = await requestConfig("getPredictions", parmas);
  892. if (res.success && res.single) {
  893. if(this.currentImg){//当前已经有生成图了
  894. console.warn("***当前已经有最高级生成图了***")
  895. return false;
  896. }
  897. if(this.timer==null){
  898. console.warn("***当前轮询已经结束了***")
  899. return false;
  900. }
  901. if (res.single.status == 'succeeded' && res.single.output) {
  902. if(this.modelType==2){//精准设计
  903. model.resultImg = res.single.output;
  904. if(model.level=='5'){//最高级了
  905. this.currentImg = true;
  906. setTimeout(()=>{
  907. this.resultHandle(res.single.output)
  908. },1500)
  909. }
  910. }else{
  911. this.currentImg = true;
  912. setTimeout(()=>{
  913. this.resultHandle(res.single.output)
  914. },1500)
  915. }
  916. console.warn("***有生成图了***",model)
  917. }
  918. if(this.modelType==2){//最高优先级返回没有图片,则处理进度条事宜
  919. if(model.level=='5'){
  920. this.processHandle(res.single);
  921. }
  922. }else{//普通设计
  923. this.processHandle(res.single);
  924. }
  925. } else if (!res.success) {
  926. this.stopInterval();//停止轮询
  927. this.showToast("渲染失败,请重试")
  928. }
  929. },
  930. //进度处理
  931. processHandle(single){
  932. console.warn("***single***",single.status,single.progress,this.count,single);
  933. let self = this;
  934. if(single.status=='starting'){//启动中的逻辑
  935. if(this.count>=20){
  936. this.stopInterval();//停止轮询
  937. // this.dialogAIVisible = true;
  938. MessageBox.confirm('',{
  939. title: '提示',
  940. message: 'AI开了小差,是否重新生成?',
  941. showCancelButton: true,
  942. confirmButtonText:'继续生成',
  943. cancelButtonText:'放弃生成',
  944. }).then(action => {
  945. console.warn("***MessageBox-action***",action)
  946. if(action == 'confirm'){
  947. this.confirmHandle(0);
  948. }
  949. }).catch(err=>{
  950. if(err == 'cancel'){
  951. this.cancelHandle();
  952. }
  953. });
  954. }else{
  955. this.myloading = true;
  956. this.loadingMsg = '启动中';
  957. }
  958. this.count = this.count + 1;
  959. this.getOutPicture();
  960. }else if(single.status=='processing'){
  961. let random = single.progress || 0;
  962. if(random >= 100){
  963. random = 99;
  964. }
  965. this.myloading = true;
  966. this.loadingMsg = '生成中…' + parseInt(random) + '%';
  967. this.getOutPicture();
  968. }else if(single.status=='succeeded'){
  969. this.myloading = true;
  970. this.loadingMsg = '生成中…100%';
  971. }
  972. },
  973. //返回结果处理
  974. resultHandle(resultImg){
  975. this.currentImg = true;
  976. // this.random = 100;
  977. this.aiImage = resultImg;
  978. this.showAIFlag = true;
  979. let newImage = resultImg;
  980. let aiStyleName = this.styleList[this.curStyleIndex].styleName;
  981. let _data = {
  982. image:newImage,
  983. checked:false,
  984. list:[newImage],
  985. spaceId:this.curSpaceId,//记录下当前生成的AI图是哪个空间的,因为会切换空间
  986. }
  987. this.aiImagesList.push(_data);
  988. this.showAIImage = true;//显示AI结果集合-因为生成了AI图片
  989. if(this.$parent && typeof(this.$parent.clearHandle)=="function" ){
  990. this.$parent.clearHandle();
  991. }
  992. this.stopInterval();
  993. if(this.aiImagesList.length>1){
  994. setTimeout(()=>{
  995. this.$refs.carousel.setActiveItem(this.aiImagesList.length-1);//切换到最后一张
  996. }, 1000);
  997. }
  998. },
  999. //空间数据整理
  1000. spaceAIHandle(){
  1001. let curHouseObj = this.$parent.curHouseObj;//当前户型数据对象
  1002. let aiImagesList = this.aiImagesList;//AI生成图数据对象
  1003. let wallList = this.$parent.wallList;//当前户型墙体数据列表对象
  1004. let spaceList = JSON.parse(curHouseObj.houseJson);
  1005. //遍历当前户型下的每一个空间
  1006. spaceList.forEach((item)=>{
  1007. let space = item;
  1008. let list = aiImagesList.filter(it=>it.spaceId==space.spaceId);//找到AI生成图中的当前空间
  1009. if(list && list.length>0){
  1010. list = list.filter(it=>it.checked==true);//过滤选中的
  1011. list = list.map(it=>{
  1012. return it.image
  1013. })
  1014. space.hardboundEffect = list;
  1015. console.warn("submitHouse: ", list,space);
  1016. }
  1017. let wall = wallList.find(it=>it.id==space.wallId);
  1018. if(wall){//找到当前空间的墙体数据
  1019. space.wallList = JSON.stringify(wall);
  1020. }
  1021. })
  1022. setStorage('spaceList', spaceList);//把空间选择的数据存入本地缓存里面,方便后续使用
  1023. console.warn("**spaceList**",spaceList)
  1024. },
  1025. //进入下一步
  1026. gonext(){
  1027. this.spaceAIHandle()
  1028. router.push({
  1029. name: "webgl_rxdz_env",
  1030. query:{
  1031. houseId:this.$route.query.houseId || this.$store.state.houseId,
  1032. }
  1033. });
  1034. },
  1035. showToast(title,time=3000){
  1036. this.myloading = false;
  1037. this.$store.state.loading = true;
  1038. this.$store.state.loadingMsg = title || "";
  1039. setTimeout(()=>{
  1040. this.$store.state.loading = false;
  1041. }, time);
  1042. }
  1043. }
  1044. }
  1045. </script>
  1046. <style lang="scss" scoped>
  1047. @import "./viewMask.scss";
  1048. </style>
  1049. <style lang="css" scoped>
  1050. /* @import "@/common/css/common.css"; */
  1051. </style>