index.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <template>
  2. <u-upload :action="action" :fileList="fileListData"
  3. v-bind="$attrs"
  4. @on-remove="onRemove"
  5. @on-success="afterRead"
  6. @on-list-change="onListChange">
  7. <slot></slot>
  8. </u-upload>
  9. </template>
  10. <script>
  11. import configService from '@/common/service/config.service';
  12. export default {
  13. name: "",
  14. props: {
  15. api: {
  16. type: String,
  17. default: '/sys/common/upload'
  18. },
  19. fileList: {
  20. type: Array,
  21. default: () => []
  22. },
  23. },
  24. data() {
  25. return {
  26. fileListData: [],
  27. };
  28. },
  29. watch: {
  30. fileList: {
  31. immediate: true,
  32. handler(n) {
  33. if (n.length > 0) {
  34. this.fileListData = n;
  35. }
  36. }
  37. }
  38. },
  39. computed: {
  40. action() {
  41. return configService.apiUrl + this.api
  42. }
  43. },
  44. mounted() {
  45. },
  46. beforeDestroy() {
  47. },
  48. methods: {
  49. afterRead(event, index, list) {
  50. // url是组件的固定字段
  51. this.fileListData[index] = {
  52. ...event,
  53. url: list[index].url
  54. };
  55. this.$emit('update:fileList', this.fileListData);
  56. this.$emit('complete', this.fileListData);
  57. },
  58. onRemove(index, lists) {
  59. this.fileListData.splice(index, 1);
  60. this.$emit('update:fileList', this.fileListData);
  61. this.$emit('complete', this.fileListData);
  62. },
  63. // onListChange早于onSuccess
  64. onListChange(lists) {
  65. // this.$emit('complete', lists);
  66. },
  67. // 手动上传
  68. uploadFilePromise(url) {
  69. return new Promise((resolve, reject) => {
  70. let a = uni.uploadFile({
  71. url: this.action,
  72. name: "file",
  73. header:{
  74. 'AppType': '5',
  75. 'X-Access-Token': uni.getStorageSync('token'),
  76. },
  77. filePath: url,
  78. // formData: {
  79. // user: "test",
  80. // },
  81. success: (res) => {
  82. uni.hideLoading();
  83. let data = JSON.parse(res.data);
  84. if (data.success) {
  85. resolve(data);
  86. } else {
  87. uni.showToast({
  88. icon: 'none',
  89. title: data.message || '文件导入失败',
  90. });
  91. }
  92. },
  93. });
  94. });
  95. },
  96. }
  97. }
  98. </script>
  99. <style lang="scss" scoped>
  100. </style>