index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <template>
  2. <view class="outerBox">
  3. <navBar title="首页" flex="cen" :navImg="navImg" :back="false" color="#333333" background="#FFFFFF" />
  4. <view class="upload">
  5. <view class="currModel" @click="show = true">
  6. <view class="left" v-if="currModalCon">
  7. 当前模型:{{currModalCon.type+'-'+currModalCon.name}}
  8. </view>
  9. <view class="right" v-if="currModalCon">
  10. </view>
  11. </view>
  12. <view class="tipTxt">
  13. 请按要求点击上传您的照片(至少5张)
  14. </view>
  15. <u-upload style="margin-top: 20px;" :fileList="fileList1" @afterRead="afterRead" @delete="deletePic"
  16. name="1" multiple :maxCount="20"></u-upload>
  17. </view>
  18. <view class="content">
  19. <u-divider text="照片示例"></u-divider>
  20. <view class="examplePic">
  21. <view class="item" v-for="(item,index) in exampleList" @click="previewImg(exampleList, index)">
  22. <image class="imagesRow" :src="item.url" mode="widthFix"></image>
  23. <u-icon :name="index==0?'checkmark':'close'" :label="item.text" labelPos="top" labelSize="12px"
  24. style="" color="#febb2b" size="18"></u-icon>
  25. </view>
  26. </view>
  27. <view class="botTipTxtBox">
  28. <u--text color="#aeaeae" size="12px" align="center" text="温馨提醒:目前婴儿、老人、眼镜等特征效果有不稳定性,我们正在努力优化"></u--text>
  29. </view>
  30. <view class="btnBox" v-if="isGen" style="display: flex;justify-content: space-between;margin-top: 20px;">
  31. <view class="btn b1" v-if="fileListUp.length!==0&&showGenBtn" @click="createRenwu()">
  32. 立即生成
  33. </view>
  34. <view class="btn b2" v-else @click="reCreate">
  35. 重新上传
  36. </view>
  37. </view>
  38. </view>
  39. <u-popup :show="show" @close="close" @open="open" :round="10">
  40. <view class="modelBox">
  41. <view class="mName">
  42. <view class="left">
  43. 模型选择:
  44. </view>
  45. <u-icon @click="show=false" name="close" color="black" size="20" />
  46. </view>
  47. <view class="top1">
  48. <u-subsection fontSize="16" activeColor="orange" :list="manList" :current="current"
  49. @change="sectionChange"></u-subsection>
  50. </view>
  51. <view class="bot1" v-if="current==0">
  52. <view class="item1" :class="[manCurrIndex==index?'at':'']" v-for="(item,index) in styles.man"
  53. @click="selM(0,index,item)">
  54. {{item.name}}
  55. </view>
  56. <!-- <view class="item1" :class="[manCurrIndex==index?'at':'']" v-for="(item,index) in 20"
  57. @click="selM(0,index,item)">
  58. {{item}}
  59. </view> -->
  60. <view class="pos">
  61. </view>
  62. </view>
  63. <view class="bot1" v-else>
  64. <view class="item1" :class="[womanCurrIndex==index?'at':'']" v-for="(item,index) in styles.woman"
  65. @click="selM(1,index,item)">
  66. {{item.name}}
  67. </view>
  68. <view class="pos">
  69. </view>
  70. </view>
  71. </view>
  72. </u-popup>
  73. </view>
  74. </template>
  75. <script>
  76. import {
  77. buildPic,
  78. getImage,
  79. getAiModel,
  80. // pushFile
  81. createTask,
  82. } from '@/api/genPic/index.js'
  83. import genAvatarCom from '@/components/genAvatarComp/index.vue'
  84. export default {
  85. components: {
  86. genAvatarCom
  87. },
  88. data() {
  89. return {
  90. exampleList: [{
  91. text: '五官清晰',
  92. url: require('@/static/images/WX20231024-110939.png')
  93. },
  94. {
  95. text: '不是正面',
  96. url: require('@/static/images/20231024-110955.png')
  97. },
  98. {
  99. text: '有遮挡',
  100. url: require('@/static/images/20231024-111008.png')
  101. },
  102. {
  103. text: '过于模糊',
  104. url: require('@/static/images/20231024-111021.png')
  105. },
  106. ],
  107. show: false,
  108. userInfo: uni.getStorageSync('userInfo'),
  109. value: 0,
  110. is_show: false,
  111. fileList1: [],
  112. stepUrl: '',
  113. isGen: false,
  114. showGenBtn: true,
  115. fileListUp: [],
  116. aiPath: '',
  117. styles: {},
  118. manList: [],
  119. current: 0,
  120. manCurrIndex: 0,
  121. womanCurrIndex: -1,
  122. currModalCon: null
  123. }
  124. },
  125. async onLoad() {
  126. this.getAiPath()
  127. let res = await getAiModel({
  128. type: 2
  129. })
  130. if (res.code == 0) {
  131. this.styles = res.data.styles
  132. this.manList = Object.keys(res.data.styles)
  133. this.currModalCon = {
  134. type: this.manList[0],
  135. name: res.data.styles[this.manList[0]][0].name,
  136. index: res.data.styles[this.manList[0]][0].index
  137. }
  138. console.log('ai模型列表返回值:', res, this.styles, this.currModalCon);
  139. } else {
  140. uni.showToast({
  141. title: res.message,
  142. icon: "none"
  143. })
  144. }
  145. },
  146. onShareAppMessage(params) {
  147. if (params.from === 'button' || params.from === 'menu') {
  148. return {
  149. title: '思维定制相机-首页',
  150. imageUrl: '',
  151. path: '/pages/index/index',
  152. mpId: this.$appId,
  153. type: this.$shareType,
  154. }
  155. }
  156. },
  157. watch: {
  158. fileListUp(n, o) {
  159. let len = n.filter((item, index) => {
  160. return item == 'success'
  161. })
  162. // console.log('len----', n, len.length);
  163. if (len.length >= 5) {
  164. this.isGen = true
  165. this.showGenBtn = true
  166. } else {
  167. this.isGen = false
  168. this.showGenBtn = false
  169. }
  170. }
  171. },
  172. methods: {
  173. //点击预览图片
  174. previewImg(list, index) {
  175. let listr = []
  176. list.forEach((item, index) => {
  177. listr.push(item.url)
  178. })
  179. console.log('listr', listr);
  180. uni.previewImage({
  181. current: index,
  182. urls: listr, //存放图片的数组
  183. // urls: ["/static/images/WX20231024-110939.png", "/static/images/20231024-110955.png"],
  184. loop: true,
  185. indicator: 'default'
  186. })
  187. },
  188. selM(type, index, item) {
  189. if (type == 0) {
  190. this.manCurrIndex = index
  191. this.womanCurrIndex = -1
  192. } else {
  193. this.womanCurrIndex = index
  194. this.manCurrIndex = -1
  195. }
  196. this.currModalCon = {
  197. type: type ? 'woman' : 'man',
  198. name: item.name,
  199. index: index
  200. }
  201. console.log('当前选择的模型', type, index, item, this.currModalCon);
  202. },
  203. sectionChange(index) {
  204. this.current = index;
  205. },
  206. open() {
  207. },
  208. close() {
  209. this.show = false
  210. },
  211. testUp() {
  212. uni.switchTab({
  213. url: '/pages/main/index'
  214. })
  215. },
  216. async getAiPath() {
  217. let res = await getAiModel({
  218. type: 3
  219. })
  220. if (res.code == 0) {
  221. console.log('ai模型返回值:', res.data.data_path);
  222. this.aiPath = res.data.data_path
  223. } else {
  224. uni.showToast({
  225. title: '远程文件夹创建失败',
  226. icon: "none"
  227. })
  228. }
  229. },
  230. toGenPic() {
  231. uni.switchTab({
  232. url: '/pages/selAndGenPage/index'
  233. })
  234. },
  235. toMyPage() {
  236. uni.switchTab({
  237. url: '/pages/main/index'
  238. })
  239. },
  240. // 删除图片
  241. deletePic(event) {
  242. console.log('del', event);
  243. this[`fileList${event.name}`].splice(event.index, 1)
  244. this.fileListUp.splice(event.index, 1)
  245. console.log('1213', this.fileListUp);
  246. },
  247. // 新增图片
  248. async afterRead(event) {
  249. if (!uni.getStorageSync('token')) {
  250. this.$toast('请登录后上传')
  251. setTimeout(() => {
  252. uni.reLaunch({
  253. url: '/pages/main/index'
  254. })
  255. }, 1500)
  256. return
  257. }
  258. console.log('afterRead------:', event);
  259. if (this.aiPath) {
  260. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  261. let lists = [].concat(event.file)
  262. let fileListLen = this[`fileList${event.name}`].length
  263. lists.map((item) => {
  264. this[`fileList${event.name}`].push({
  265. ...item,
  266. status: 'uploading',
  267. message: '上传中'
  268. })
  269. })
  270. for (let i = 0; i < lists.length; i++) {
  271. const result = await this.uploadFilePromise(lists[i].url)
  272. console.log('result------', result);
  273. let item = this[`fileList${event.name}`][fileListLen]
  274. this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
  275. // status: 'success',
  276. status: result.type,
  277. message: '',
  278. url: result.data,
  279. errTxt: result.errTxt ? result.errTxt : ""
  280. }))
  281. fileListLen++
  282. }
  283. }
  284. },
  285. async uploadFilePromise(url) {
  286. let _this = this
  287. return new Promise((resolve, reject) => {
  288. let a = uni.uploadFile({
  289. url: _this.$baseUrl + '/api/checkAndUploadImg', // 仅为示例,非真实的接口地址
  290. filePath: url,
  291. name: 'file',
  292. header: {
  293. Authorization: uni.getStorageSync('token'),
  294. },
  295. formData: {
  296. user: 'test',
  297. 'tag': 'lamp',
  298. pathId: _this.aiPath,
  299. },
  300. success: (res) => {
  301. let result = JSON.parse(res.data)
  302. console.log('上传并校验图片返回值:', res);
  303. // console.log('res.data.data1:', res.data.data);
  304. if (result.code == 0) {
  305. if (!result.data.img_path) {
  306. // _this.fileList1.pop()
  307. _this.$toast('图片不符合标准,请检查后重新上传')
  308. _this.fileListUp.push('failed')
  309. setTimeout(() => {
  310. resolve({
  311. data: res.data.data,
  312. type: 'failed',
  313. errTxt: result.data.data[0].msg
  314. })
  315. }, 1000)
  316. } else {
  317. _this.fileListUp.push('success')
  318. setTimeout(() => {
  319. resolve({
  320. data: res.data.data,
  321. type: 'success'
  322. })
  323. }, 1000)
  324. }
  325. }
  326. }
  327. });
  328. })
  329. },
  330. async createRenwu() {
  331. if (this.fileListUp.length < 5) {
  332. return uni.showToast({
  333. title: '请上传5张图片以上',
  334. icon: 'none'
  335. })
  336. }
  337. uni.showLoading({
  338. title: '任务创建中...'
  339. })
  340. let _this = this
  341. console.log('创建任务参数:', {
  342. pathId: this.aiPath,
  343. cate: this.currModalCon.type,
  344. model_index: this.currModalCon.index,
  345. // -2返回20张
  346. output_level: '-2'
  347. });
  348. let res = await createTask({
  349. pathId: this.aiPath,
  350. // cate: 'man',
  351. // model_index: 0,
  352. cate: this.currModalCon.type,
  353. model_index: this.currModalCon.index,
  354. // -2返回20张
  355. output_level: '-2'
  356. })
  357. console.log('创建任务返回值:', res);
  358. if (res.code == 0 && res.data.task_id) {
  359. this.$toast('任务创建成功')
  360. this.showGenBtn = false
  361. setTimeout(() => {
  362. uni.switchTab({
  363. url: '/pages/my/index'
  364. })
  365. }, 1500)
  366. }
  367. },
  368. async reCreate() {
  369. this.fileListUp = []
  370. this.fileList1 = []
  371. this.isGen = false
  372. this.getAiPath()
  373. }
  374. }
  375. }
  376. </script>
  377. <style lang="scss" scoped>
  378. @import "./index.scss";
  379. </style>