index.vue 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <!--
  2. * @Author: yangpeiqin
  3. * @Date: 2023-12-15 16:57:35
  4. * @LastEditors: yangpeiqin
  5. * @LastEditTime: 2023-12-15 17:30:29
  6. * @FilePath: \gdtq_admin_app移动端\components\uploadImg\index.vue
  7. -->
  8. <template>
  9. <view>
  10. <u-upload
  11. ref="uploadFile"
  12. :name="name"
  13. :multiple="true"
  14. :maxCount="maxCount"
  15. :fileList="fileList1"
  16. :sizeType="['compressed']"
  17. :width="width"
  18. :height="height"
  19. :capture="capture"
  20. :uploadText="uploadText"
  21. @afterRead="afterRead"
  22. @delete="deletePic"></u-upload>
  23. </view>
  24. </template>
  25. <script>
  26. import { isEmpty } from 'lodash';
  27. import * as imageConversion from 'image-conversion';
  28. import { getToken } from '@/utils/auth'
  29. export default {
  30. data() {
  31. return {
  32. fileList1: [],
  33. }
  34. },
  35. methods: {
  36. // 删除图片
  37. deletePic(event) {
  38. this[`fileList${event.name}`].splice(event.index, 1)
  39. },
  40. // 新增图片
  41. async afterRead(event) {
  42. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  43. let lists = [].concat(event.file)
  44. let fileListLen = this[`fileList${event.name}`].length
  45. lists.map((item) => {
  46. this[`fileList${event.name}`].push({
  47. ...item,
  48. status: 'uploading',
  49. message: '上传中'
  50. })
  51. })
  52. for (let i = 0; i < lists.length; i++) {
  53. const result = await this.uploadFilePromise(lists[i].url)
  54. let item = this[`fileList${event.name}`][fileListLen]
  55. this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
  56. status: 'success',
  57. message: '',
  58. url: result
  59. }))
  60. fileListLen++
  61. }
  62. },
  63. uploadFilePromise(url) {
  64. return new Promise((resolve, reject) => {
  65. let a = uni.uploadFile({
  66. url: 'http://192.168.2.21:7001/upload', // 仅为示例,非真实的接口地址
  67. filePath: url,
  68. name: 'file',
  69. formData: {
  70. user: 'test'
  71. },
  72. success: (res) => {
  73. setTimeout(() => {
  74. resolve(res.data.data)
  75. }, 1000)
  76. }
  77. });
  78. })
  79. },
  80. }
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. </style>