index.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <template>
  2. <div class="upload-file">
  3. <el-upload
  4. multiple
  5. :action="uploadFileUrl"
  6. :before-upload="handleBeforeUpload"
  7. :file-list="fileList"
  8. :limit="limit"
  9. :on-error="handleUploadError"
  10. :on-exceed="handleExceed"
  11. :on-success="handleUploadSuccess"
  12. :show-file-list="false"
  13. :headers="headers"
  14. class="upload-file-uploader"
  15. ref="fileUpload"
  16. >
  17. <!-- 上传按钮 -->
  18. <el-button size="mini" type="primary">选取文件</el-button>
  19. <!-- <i class="el-icon-upload"></i>
  20. <div class="el-upload__text">
  21. 将文件拖到此处,或
  22. <em>点击上传</em>
  23. </div> -->
  24. <!-- 上传提示 -->
  25. <div class="el-upload__tip" slot="tip" v-if="showTip">
  26. 请上传
  27. <template v-if="fileSize">
  28. 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
  29. </template>
  30. <template v-if="fileType">
  31. 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
  32. </template>
  33. 的文件
  34. </div>
  35. </el-upload>
  36. <!-- 文件列表 -->
  37. <transition-group
  38. class="upload-file-list el-upload-list el-upload-list--text"
  39. name="el-fade-in-linear"
  40. tag="ul"
  41. >
  42. <li
  43. :key="file.url"
  44. class="el-upload-list__item ele-upload-list__item-content"
  45. v-for="(file, index) in fileList"
  46. >
  47. <el-link
  48. :href="`${baseUrl}?id=${file.id}`"
  49. :underline="false"
  50. target="_blank"
  51. >
  52. <!-- <span class="el-icon-document"> {{ getFileName(file.name) }} </span> -->
  53. <span class="el-icon-document"> {{ file.name }} </span>
  54. </el-link>
  55. <div class="ele-upload-list__item-content-action">
  56. <el-link :underline="false" @click="handleDelete(index)" type="danger"
  57. >删除</el-link
  58. >
  59. </div>
  60. </li>
  61. </transition-group>
  62. </div>
  63. </template>
  64. <script>
  65. import { getToken } from "@/utils/auth";
  66. export default {
  67. name: "FileUpload",
  68. props: {
  69. // 值
  70. value: [String, Object, Array],
  71. // 数量限制
  72. limit: {
  73. type: Number,
  74. default: 5,
  75. },
  76. // 大小限制(MB)
  77. fileSize: {
  78. type: Number,
  79. default: 5,
  80. },
  81. // 文件类型, 例如['png', 'jpg', 'jpeg']
  82. fileType: {
  83. type: Array,
  84. default: () => ["doc", "xls", "ppt", "txt", "pdf"],
  85. },
  86. // 是否显示提示
  87. isShowTip: {
  88. type: Boolean,
  89. default: true,
  90. },
  91. },
  92. data() {
  93. return {
  94. number: 0,
  95. separator: ";",
  96. uploadList: [],
  97. baseUrl: "https://sy.derom.com/document-center/fastdfs/download",
  98. uploadFileUrl:
  99. process.env.NODE_ENV == "development"
  100. ? "/drp-file/document-center/fastdfs/upload"
  101. : "/document-center/fastdfs/upload",
  102. headers: {
  103. Authorization: "Bearer " + getToken(),
  104. },
  105. fileList: [],
  106. };
  107. },
  108. watch: {
  109. value: {
  110. handler(val) {
  111. if (val) {
  112. let temp = 1;
  113. // 首先将值转为数组
  114. const list = Array.isArray(val)
  115. ? val
  116. : this.value.split(this.separator);
  117. this.fileList = list.map((item) => {
  118. item = JSON.parse(item);
  119. return {
  120. ...item,
  121. uid: item.uid || new Date().getTime() + temp++,
  122. };
  123. });
  124. } else {
  125. this.fileList = [];
  126. return [];
  127. }
  128. },
  129. deep: true,
  130. immediate: true,
  131. },
  132. },
  133. computed: {
  134. // 是否显示提示
  135. showTip() {
  136. return this.isShowTip && (this.fileType || this.fileSize);
  137. },
  138. },
  139. methods: {
  140. // 上传前校检格式和大小
  141. handleBeforeUpload(file) {
  142. // 校检文件类型
  143. if (this.fileType) {
  144. const fileName = file.name.split(".");
  145. const fileExt = fileName[fileName.length - 1];
  146. const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
  147. if (!isTypeOk) {
  148. this.$modal.msgError(
  149. `文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`
  150. );
  151. return false;
  152. }
  153. }
  154. // 校检文件大小
  155. if (this.fileSize) {
  156. const isLt = file.size / 1024 / 1024 < this.fileSize;
  157. if (!isLt) {
  158. this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
  159. return false;
  160. }
  161. }
  162. this.$modal.loading("正在上传文件,请稍候...");
  163. this.number++;
  164. return true;
  165. },
  166. // 文件个数超出
  167. handleExceed() {
  168. this.$modal.msgError(`上传文件数量不能超过 ${this.limit} 个!`);
  169. },
  170. // 上传失败
  171. handleUploadError(err) {
  172. this.$modal.msgError("上传文件失败,请重试");
  173. this.$modal.closeLoading();
  174. },
  175. // 上传成功回调
  176. handleUploadSuccess(res, file) {
  177. if (res.code * 1 === 200) {
  178. this.uploadList.push({
  179. id: res.id,
  180. url: res.filepath,
  181. name: res.filename,
  182. });
  183. this.uploadedSuccessfully();
  184. } else {
  185. this.number--;
  186. this.$modal.closeLoading();
  187. this.$modal.msgError(res.msg);
  188. this.$refs.fileUpload.handleRemove(file);
  189. this.uploadedSuccessfully();
  190. }
  191. },
  192. // 删除文件
  193. handleDelete(index) {
  194. this.fileList.splice(index, 1);
  195. this.$emit("input", this.listToString(this.fileList, this.separator));
  196. },
  197. // 上传结束处理
  198. uploadedSuccessfully() {
  199. if (this.number > 0 && this.uploadList.length === this.number) {
  200. this.fileList = this.fileList.concat(this.uploadList);
  201. this.uploadList = [];
  202. this.number = 0;
  203. this.$emit("input", this.listToString(this.fileList, this.separator));
  204. this.$modal.closeLoading();
  205. }
  206. },
  207. // 获取文件名称
  208. getFileName(name) {
  209. if (name.lastIndexOf("/") > -1) {
  210. return name.slice(name.lastIndexOf("/") + 1);
  211. } else {
  212. return "";
  213. }
  214. },
  215. // 对象转成指定字符串分隔
  216. listToString(list, separator) {
  217. list = list.map((file) =>
  218. JSON.stringify({
  219. id: file.id,
  220. url: file.url,
  221. name: file.name,
  222. })
  223. );
  224. let strs = "";
  225. separator = separator || ",";
  226. for (let i in list) {
  227. strs += list[i] + separator;
  228. }
  229. return strs != "" ? strs.substr(0, strs.length - 1) : "";
  230. },
  231. },
  232. };
  233. </script>
  234. <style lang="scss">
  235. .el-form-item.is-error .el-upload-dragger {
  236. border: 1px dashed #ff4949;
  237. }
  238. </style>
  239. <style scoped lang="scss">
  240. .upload-file-uploader {
  241. margin-bottom: 5px;
  242. }
  243. .upload-file-list .el-upload-list__item {
  244. border: 1px solid #e4e7ed;
  245. line-height: 2;
  246. margin-bottom: 10px;
  247. position: relative;
  248. }
  249. .upload-file-list .ele-upload-list__item-content {
  250. display: flex;
  251. justify-content: space-between;
  252. align-items: center;
  253. color: inherit;
  254. }
  255. .ele-upload-list__item-content-action .el-link {
  256. margin-right: 10px;
  257. }
  258. </style>