index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <u-upload v-bind="$attrs" :fileList="fileList" @afterRead="afterRead" @delete="deletePic" :notFile="notFile"
  3. :disabled="disabled">
  4. </u-upload>
  5. <view class="tips" v-if="tips">{{tips}}</view>
  6. </template>
  7. <script setup name="cyUpload">
  8. import {
  9. watch,
  10. ref
  11. } from 'vue';
  12. import baseUrl from '@/config/request.js';
  13. const emit = defineEmits(['update:filename', 'update:keyValue', 'success','update:loading'])
  14. const props = defineProps({
  15. filename: {
  16. type: [Array, String],
  17. default: () => []
  18. },
  19. url: {
  20. type: String
  21. },
  22. notFile: {
  23. type: Boolean,
  24. default: false
  25. },
  26. tips: {
  27. type: String
  28. },
  29. disabled: {
  30. type: Boolean,
  31. default: false
  32. },
  33. isUpdate: {
  34. type: Boolean,
  35. default: false
  36. },
  37. loading:{
  38. type:Boolean,
  39. default:false
  40. }
  41. })
  42. const fileList = ref([])
  43. watch(() => props.filename, (filename) => {
  44. if (!filename) return fileList.value = []
  45. if (typeof filename == 'string') {
  46. fileList.value = filename.split(',').map(v => ({
  47. url: baseUrl + v,
  48. }))
  49. } else {
  50. fileList.value = filename && filename.length > 0 ? filename.map(v => ({
  51. url: v.url || v,
  52. })) : []
  53. }
  54. }, {
  55. immediate: true,
  56. deep: true
  57. })
  58. const afterRead = async (e) => {
  59. const {
  60. file
  61. } = e
  62. fileList.value.push({
  63. ...file,
  64. status: 'uploading',
  65. message: '上传中...',
  66. });
  67. emit('update:loading',true)
  68. const result = await uploadFilePromise(file.url);
  69. console.log(result, 'result')
  70. fileList.value.forEach((v,index) => {
  71. if(index == fileList.value.length - 1){
  72. v.url = result.url
  73. v.status = 'success'
  74. v.message = ''
  75. }
  76. })
  77. console.log(fileList.value, 'fileList.value')
  78. //更新外部参数
  79. emit('update:filename', fileList.value.map(v => v.url))
  80. emit('success', result)
  81. emit('update:loading',false)
  82. }
  83. const deletePic = (event) => {
  84. fileList.value.splice(event.index, 1);
  85. }
  86. const uploadFilePromise = (url, baseStr = '/app/common/upload') => {
  87. let token = uni.getStorageSync('token')
  88. return new Promise((resolve, reject) => {
  89. let a = uni.uploadFile({
  90. url: baseUrl + baseStr, // 图片上传地址
  91. filePath: url,
  92. name: 'file',
  93. header: {
  94. "Authorization": token
  95. },
  96. success: (res) => {
  97. let respic = JSON.parse(res.data)
  98. resolve(respic)
  99. },
  100. });
  101. });
  102. };
  103. </script>
  104. <style lang="scss" scoped>
  105. .tips {
  106. font-size: 24rpx;
  107. font-family: PingFangSC, PingFang SC;
  108. font-weight: 400;
  109. color: #969696;
  110. line-height: 48rpx;
  111. margin-top: 10rpx;
  112. }
  113. ::v-deep .align-right .u-upload__wrap {
  114. justify-content: flex-end;
  115. }
  116. </style>