index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <view class="content">
  3. <view class="upload">
  4. <view class="tipTxt">
  5. 请按要求点击上传您的照片
  6. </view>
  7. <u-upload :deletable="showDelBtn" style="margin-top: 20px;" :fileList="fileList1" @afterRead="afterRead"
  8. @delete="deletePic" name="1" :maxCount="1" width="290" height="290"></u-upload>
  9. </view>
  10. <u-divider text="照片示例"></u-divider>
  11. <view class="examplePic">
  12. <view class="item" v-for="(item,index) in exampleList" @click="previewImg(exampleList, index)">
  13. <image class="imagesRow" :src="item.url" mode="widthFix"></image>
  14. <u-icon :name="index==0?'checkmark':'close'" :label="item.text" labelPos="top" labelSize="12px" style=""
  15. color="#febb2b" size="18"></u-icon>
  16. </view>
  17. </view>
  18. <view class="tipTxtBox">
  19. <u--text color="#aeaeae" size="12px" align="center" text="温馨提醒:目前婴儿、老人、眼镜等特征效果有不稳定性,我们正在努力优化"></u--text>
  20. </view>
  21. <!-- <view v-if="is_show" style="margin-top: 40px;"><u-button class="buttonCalss" type="warning" shape="circle"
  22. size="large" text="制作数字人分身" @click="buildPicTrue()"></u-button></view> -->
  23. <!-- <view class="" style="display: flex;justify-content: space-between;margin-top: 20px;">
  24. <button type="warn" style="" @click="reset">重新上传</button>
  25. <button type="primary" style="" @click="buildPicTrue()">生成图片</button>
  26. </view> -->
  27. <view class="btnBox" v-if="isGen" style="display: flex;justify-content: space-between;margin-top: 20px;">
  28. <view class="btn b1" v-if="stepUrl!==''&&showGenBtn" @click="buildPicTrue()">
  29. 立即生成
  30. </view>
  31. <view class="btn b2" v-else @click="reset">
  32. 重新上传
  33. </view>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. export default {
  39. data() {
  40. return {
  41. userInfo: uni.getStorageSync('userInfo'),
  42. value: 0,
  43. is_show: false,
  44. fileList1: [],
  45. stepUrl: '',
  46. isGen: false,
  47. showGenBtn: true,
  48. fileListUp: [],
  49. aiPath: '',
  50. showDelBtn: true,
  51. exampleList: [{
  52. text: '五官清晰',
  53. url: require('@/static/images/WX20231024-110939.png')
  54. },
  55. {
  56. text: '不是正面',
  57. url: require('@/static/images/20231024-110955.png')
  58. },
  59. {
  60. text: '有遮挡',
  61. url: require('@/static/images/20231024-111008.png')
  62. },
  63. {
  64. text: '过于模糊',
  65. url: require('@/static/images/20231024-111021.png')
  66. },
  67. ],
  68. }
  69. },
  70. onLoad() {
  71. },
  72. watch: {
  73. stepUrl(n, o) {
  74. if (n != '') {
  75. this.isGen = true
  76. this.showGenBtn = true
  77. } else {
  78. this.isGen = false
  79. this.showGenBtn = false
  80. }
  81. }
  82. },
  83. methods: {
  84. //点击预览图片
  85. previewImg(list, index) {
  86. let listr = []
  87. list.forEach((item, index) => {
  88. listr.push(item.url)
  89. })
  90. console.log('listr', listr);
  91. uni.previewImage({
  92. current: index,
  93. urls: listr, //存放图片的数组
  94. // urls: ["/static/images/WX20231024-110939.png", "/static/images/20231024-110955.png"],
  95. loop: true,
  96. indicator: 'default'
  97. })
  98. },
  99. reset() {
  100. this.stepUrl = ''
  101. this.fileList1 = []
  102. this.isGen = false
  103. this.showDelBtn = true
  104. },
  105. change() {
  106. },
  107. click() {
  108. },
  109. // 删除图片
  110. deletePic(event) {
  111. this[`fileList${event.name}`].splice(event.index, 1)
  112. this.stepUrl = ''
  113. // this.fileList1 = []
  114. console.log('event', event);
  115. },
  116. // 新增图片
  117. async afterRead(event) {
  118. if (!uni.getStorageSync('token')) {
  119. this.$toast('请登录后上传')
  120. setTimeout(() => {
  121. uni.reLaunch({
  122. url: '/pages/main/index'
  123. })
  124. }, 1500)
  125. return
  126. }
  127. console.log('afterRead------:', event);
  128. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  129. let lists = [].concat(event.file)
  130. let fileListLen = this[`fileList${event.name}`].length
  131. lists.map((item) => {
  132. this[`fileList${event.name}`].push({
  133. ...item,
  134. status: 'uploading',
  135. message: '上传中'
  136. })
  137. })
  138. for (let i = 0; i < lists.length; i++) {
  139. const result = await this.uploadFilePromise(lists[i].url)
  140. // const result = await this.uploadFilePromise(lists[i])
  141. let item = this[`fileList${event.name}`][fileListLen]
  142. this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
  143. status: 'success',
  144. message: '',
  145. url: result
  146. }))
  147. fileListLen++
  148. }
  149. },
  150. async uploadFilePromise(url) {
  151. let _this = this
  152. return new Promise((resolve, reject) => {
  153. let a = uni.uploadFile({
  154. url: _this.$baseUrl + '/api/attachments', // 仅为示例,非真实的接口地址
  155. filePath: url,
  156. name: 'file',
  157. header: {
  158. Authorization: uni.getStorageSync('token'),
  159. },
  160. formData: {
  161. // user: 'test',
  162. 'tag': 'camera',
  163. // pathId: _this.aiPath,
  164. },
  165. success: (res) => {
  166. let result = JSON.parse(res.data)
  167. if (result.code == 0) {
  168. // _this.fileListUp.push(result.data.data)
  169. _this.stepUrl = result.data.file
  170. console.log('上传并校验图片返回值--头像:', _this.stepUrl);
  171. setTimeout(() => {
  172. resolve(res.data.data)
  173. }, 1000)
  174. }
  175. }
  176. });
  177. })
  178. },
  179. buildPicTrue() {
  180. if (!this.stepUrl) {
  181. return uni.showToast({
  182. title: '请上传图片',
  183. icon: 'none'
  184. })
  185. }
  186. uni.showLoading({
  187. title: '生成中...'
  188. })
  189. let _this = this
  190. uni.request({
  191. url: _this.$baseUrl + '/api/buildPicTrue', // 接口地址
  192. method: 'POST', // 请求方法,可以是 GET、POST 等
  193. data: {
  194. pathPictrue: this.stepUrl,
  195. },
  196. header: {
  197. Authorization: uni.getStorageSync('token'),
  198. },
  199. success: function(res) {
  200. uni.request({
  201. url: _this.$baseUrl + "/api/getImage",
  202. method: 'POST',
  203. data: {
  204. file_name: res.data.data.result,
  205. },
  206. header: {
  207. Authorization: uni.getStorageSync('token'),
  208. },
  209. success: function(res) {
  210. _this.showDelBtn = false
  211. // _this.stepUrl = ''
  212. _this.showGenBtn = false
  213. console.log(res)
  214. console.log('制作商城的图片地址:', res.data.data.url)
  215. _this.fileList1 = [{
  216. // url: 'http://mall.wangkuncheng.cn/upload/lamp/20231031/d8c96316c879464cae5f677824e0ac4d.jpg'
  217. url: res.data.data.url
  218. }]
  219. uni.hideLoading()
  220. }
  221. })
  222. // 请求成功时的回调函数
  223. console.log('请求成功', res.data);
  224. // 在这里处理接口返回的数据
  225. },
  226. fail: function(res) {
  227. // 请求失败时的回调函数
  228. console.log('请求失败', res);
  229. // 在这里处理请求失败的情况
  230. },
  231. complete: function() {
  232. // 请求完成时的回调函数,无论成功或失败都会执行
  233. console.log('请求完成');
  234. }
  235. });
  236. }
  237. }
  238. }
  239. </script>
  240. <style lang="scss" scoped>
  241. @import "./index.scss";
  242. </style>