华西海圻ELN前端工程
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

260 lines
7.2 KiB

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