Upload.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <!-- -->
  2. <template>
  3. <div class="upload">
  4. <el-upload
  5. :accept="targetAccept"
  6. class="upload-demo"
  7. :show-file-list="type === '批量上传' ? false : true"
  8. :action="url"
  9. :on-preview="handlePreview"
  10. :on-remove="handleRemove"
  11. :multiple="true"
  12. :on-exceed="handleExceed"
  13. :on-success="handleSuccess"
  14. :file-list="targetFileList"
  15. :before-remove="beforeRemove"
  16. :limit="filleNumber"
  17. :before-upload="handlebeforeUplaod"
  18. >
  19. <el-button v-if="type == '批量上传'" type="primary">{{ $t('Key158') }}</el-button>
  20. <el-button v-else>{{ $t('Key152') }}</el-button>
  21. </el-upload>
  22. </div>
  23. </template>
  24. <script>
  25. import { getToken, removeToken } from '../utils/auth';
  26. export default {
  27. name: 'Upload',
  28. props: {
  29. fileList: {
  30. type: Array,
  31. default: () => [],
  32. },
  33. changeFillId: {
  34. type: Function,
  35. default: () => {},
  36. },
  37. type: {
  38. type: String,
  39. default: '',
  40. },
  41. accept: {
  42. type: String,
  43. default: '',
  44. },
  45. filleNumber: {
  46. type: Number,
  47. default: 1,
  48. },
  49. },
  50. data() {
  51. return {
  52. targetFileList: null,
  53. targetAccept: null,
  54. fileTypeName: '',
  55. loading: null,
  56. };
  57. },
  58. computed: {
  59. url() {
  60. let userInfor = getToken()
  61. ? JSON.parse(getToken())
  62. : sessionStorage.getItem('GCLS_Token_Tc')
  63. ? JSON.parse(sessionStorage.getItem('GCLS_Token_Tc'))
  64. : null;
  65. let SessionID = '';
  66. let UserCode = '';
  67. let UserType = '';
  68. if (userInfor) {
  69. UserCode = userInfor.user_code;
  70. UserType = userInfor.user_type;
  71. SessionID = userInfor.session_id;
  72. }
  73. return `${process.env.VUE_APP_BASE_API}/GCLSFileServer/WebFileUpload?UserCode=${UserCode}&UserType=${UserType}&SessionID=${SessionID}&SecurityLevel=Mid"`;
  74. },
  75. },
  76. watch: {
  77. fileList(newval) {
  78. this.targetFileList = newval || [];
  79. },
  80. },
  81. created() {
  82. this.targetFileList = this.fileList || [];
  83. this.targetAccept = this.accept;
  84. },
  85. // 方法集合
  86. methods: {
  87. handlebeforeUplaod(file) {
  88. if (file.size > 20 * 1024 * 1024) {
  89. this.$message.warning('上传文件大小不能超过20M');
  90. return false; // 必须返回false
  91. }
  92. this.loading = this.$loading({
  93. lock: true,
  94. text: 'Loading',
  95. spinner: 'el-icon-loading',
  96. background: 'rgba(0, 0, 0, 0.7)',
  97. });
  98. },
  99. handleSuccess(response, file, fileList) {
  100. if (response.status === 1) {
  101. this.$message.success(this.$t('Key498'));
  102. this.loading.close();
  103. this.changeFillId(response.file_info_list[0], fileList, this.type);
  104. } else if (response.status === -1) {
  105. this.loading.close();
  106. removeToken();
  107. this.$message.warning(response.error);
  108. this.$router.push('/login');
  109. } else {
  110. this.targetFileList = [];
  111. this.loading.close();
  112. // this.$message.warning(response.msg);
  113. }
  114. },
  115. handleRemove(file, fileList) {
  116. this.changeFillId(file.response, fileList, this.type);
  117. },
  118. handlePreview(file) {
  119. console.log(file);
  120. },
  121. handleExceed(files, fileList) {
  122. this.$message.warning(
  123. `当前限制选择 ${this.filleNumber} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
  124. files.length + fileList.length
  125. } 个文件`,
  126. );
  127. },
  128. beforeRemove(file, filleList) {},
  129. },
  130. };
  131. </script>
  132. <style lang="scss" scoped>
  133. .uploadImage {
  134. display: flex;
  135. align-items: center;
  136. justify-content: center;
  137. width: 200px;
  138. height: 100px;
  139. background: #e5e5e5;
  140. }
  141. </style>