index.vue 2.4 KB

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