index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <u-upload :name="name"
  3. :width="width" :height="height"
  4. :accept="accept" :fileList="fileList"
  5. :multiple="multiple" :maxCount="maxCount"
  6. @afterRead="afterRead" @delete="onDelete">
  7. <slot name="content" :data="fileList"></slot>
  8. </u-upload>
  9. </template>
  10. <script>
  11. import { baseUrl } from '@/common/config.js';
  12. export default {
  13. name: "",
  14. props: {
  15. api: {
  16. type: String,
  17. default: '/common/upload'
  18. },
  19. name: {
  20. type: String,
  21. default: uni.$u.props.upload.name
  22. },
  23. accept: {
  24. type: String,
  25. default: uni.$u.props.upload.accept
  26. },
  27. width: {
  28. type: [String, Number],
  29. default: uni.$u.props.upload.width
  30. },
  31. height: {
  32. type: [String, Number],
  33. default: uni.$u.props.upload.height
  34. },
  35. // 文件大小限制,单位为byte
  36. maxSize: {
  37. type: [String, Number],
  38. default: uni.$u.props.upload.maxSize
  39. },
  40. maxCount: {
  41. type: Number,
  42. default: 5
  43. },
  44. multiple: {
  45. type: Boolean,
  46. default: true
  47. }
  48. },
  49. data() {
  50. return {
  51. fileList: [],
  52. };
  53. },
  54. computed: {
  55. },
  56. mounted() {
  57. },
  58. beforeDestroy() {
  59. },
  60. methods: {
  61. // 上传后的处理函数
  62. async afterRead(event) {
  63. // 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
  64. let lists = [].concat(event.file);
  65. let fileListLen = this[`fileList`].length;
  66. lists.map((item) => {
  67. this[`fileList`].push({
  68. ...item,
  69. status: "uploading",
  70. message: "上传中",
  71. });
  72. });
  73. // 无预览时,使用toast
  74. if (!this.$attrs.previewImage) {
  75. uni.showLoading({
  76. title: '上传中...'
  77. });
  78. }
  79. for (let i = 0; i < lists.length; i++) {
  80. const result = await this.uploadFilePromise(lists[i].url);
  81. let item = this[`fileList`][fileListLen];
  82. this[`fileList`].splice(
  83. fileListLen,
  84. 1,
  85. Object.assign(item, {
  86. status: "success",
  87. message: "",
  88. url: baseUrl + result,
  89. })
  90. );
  91. fileListLen++;
  92. // 无预览时,使用toast
  93. if (!this.$attrs.previewImage) {
  94. uni.hideLoading();
  95. uni.showToast({
  96. icon: 'none',
  97. title: result.message || '上传完成',
  98. duration: 2000
  99. });
  100. this.$emit('complete');
  101. }
  102. }
  103. this.$emit('fileList', this.fileList);
  104. },
  105. uploadFilePromise(url) {
  106. return new Promise((resolve, reject) => {
  107. uni.uploadFile({
  108. url: baseUrl + this.api,
  109. name: "file",
  110. header: {
  111. 'Authorization': `tf: ${uni.getStorageSync('access-token')}`
  112. },
  113. formData: {
  114. 'biz': 'temp',
  115. },
  116. filePath: url,
  117. success: (res) => {
  118. let data = JSON.parse(res.data);
  119. if (data.code === 200) {
  120. resolve(data.fileName);
  121. } else {
  122. uni.$u.toast('上传失败');
  123. reject();
  124. }
  125. },
  126. });
  127. });
  128. },
  129. onDelete(event) {
  130. uni.showModal({
  131. title: '提示',
  132. content: '确定要删除?',
  133. cancelText: '取消',
  134. confirmText: '确定',
  135. success: res => {
  136. if (res.confirm) {
  137. this[`fileList`].splice(event.index, 1);
  138. this.$emit('fileList', this.fileList);
  139. }
  140. }
  141. })
  142. },
  143. }
  144. }
  145. </script>
  146. <style lang="scss" scoped></style>