viewMask.vue 28 KB

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