viewMask.vue 29 KB

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