移动端
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 

161 行
3.6 KiB

  1. <!-- 通用上传组件 zhao -->
  2. <template>
  3. <van-uploader
  4. v-model="fileList"
  5. :multiple="multiple"
  6. :after-read="afterRead"
  7. :show-upload="showUpload"
  8. :deletable="deletable"
  9. @delete="deleteFile"
  10. :accept="accept || null"
  11. />
  12. </template>
  13. <script>
  14. import {commonUpload} from "@/api/sunVillage_info/fixedAssets";
  15. export default {
  16. name: "commonUpload",
  17. props: {
  18. name: String,
  19. value: { // 绑定值 字符串 ,分隔 可监听
  20. type: String,
  21. default: null,
  22. },
  23. accept: { // 上传类型限制: 默认图片, * = 任意类型
  24. type: String,
  25. },
  26. multiple: { // 多文件上传
  27. type: Boolean,
  28. default: false,
  29. },
  30. deletable: { // 允许删除
  31. type: Boolean,
  32. default: true,
  33. },
  34. showUpload: { // 显示上传按钮
  35. type: Boolean,
  36. default: true,
  37. },
  38. formData: { // 额外请求参数
  39. type: Object,
  40. default: function() {
  41. return {};
  42. },
  43. },
  44. file: { // 上传文件字段名
  45. type: String,
  46. default: 'file',
  47. },
  48. host: {
  49. type: String, // 文件地址前缀
  50. default: '/api',
  51. },
  52. },
  53. watch: {
  54. value: function (newVal, oldVal) {
  55. if(newVal != this.internalValue)
  56. this.setInternalValue(newVal);
  57. },
  58. },
  59. created() {
  60. this.parseValue(this.value);
  61. },
  62. data() {
  63. return {
  64. internalValue: this.value,
  65. fileList: [],
  66. pathList: [],
  67. };
  68. },
  69. methods: {
  70. setInternalValue(newVal) {
  71. this.parseValue(newVal);
  72. this.internalValue = newVal;
  73. },
  74. parseValue(data) {
  75. if(data)
  76. {
  77. this.pathList = data.split(',');
  78. }
  79. else
  80. {
  81. this.pathList = [];
  82. }
  83. this.fileList = this.pathList.map((x) => {
  84. return {
  85. url: this.host + x,
  86. };
  87. });
  88. },
  89. makeFormData() {
  90. let fd = new FormData();
  91. if(this.formData)
  92. {
  93. for(let k of Object.keys(this.formData))
  94. {
  95. fd.set(k, this.formData[k]);
  96. }
  97. }
  98. return fd;
  99. },
  100. upload(file) {
  101. let params1 = this.makeFormData();
  102. params1.append(this.file, file.file);
  103. return commonUpload(params1).then((resp) => {
  104. this.pathList.push(resp.fileName);
  105. this.updateInternalValue();
  106. this.$emit('upload', resp.fileName);
  107. });
  108. },
  109. afterRead(file) {
  110. this.$toast.loading({
  111. message: "上传中...",
  112. forbidClick: true,
  113. duration: 0,
  114. });
  115. // 此时可以自行将文件上传至服务器
  116. if (file instanceof Array) {//判断是否为数组,单张图片为array,多张为数组,数组返回true否则为false
  117. if(file.length > 0)
  118. {
  119. let index = 0;
  120. const f = () => {
  121. if(index >= file.length)
  122. return;
  123. let up = file[index];
  124. //console.log(up);
  125. console.log(`上传文件: ${index} -> ${up.file.name}`);
  126. this.upload(up).then(() => {
  127. index++;
  128. if(index < file.length)
  129. f();
  130. });
  131. };
  132. f();
  133. }
  134. } else {
  135. this.upload(file);
  136. }
  137. },
  138. deleteFile(detail){
  139. this.pathList.splice(detail.index,1);
  140. this.updateInternalValue();
  141. this.$emit('remove', detail.index);
  142. },
  143. updateInternalValue() {
  144. let files = this.pathList.join(',');
  145. console.log(files);
  146. this.internalValue = files;
  147. if(this.internalValue != this.value)
  148. this.$emit('input', this.internalValue);
  149. },
  150. },
  151. }
  152. </script>
  153. <style scoped>
  154. </style>