index.vue 7.0 KB

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