index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <template>
  2. <div>
  3. <el-upload
  4. name="file"
  5. :class="['uploader', uid]"
  6. :action="oss_url"
  7. :data="dataObj"
  8. :multiple="multiple"
  9. :accept="accept"
  10. :show-file-list="false"
  11. :on-success="uploadSuccess"
  12. :before-upload="beforeUpload"
  13. />
  14. <div class="images" v-if="modType === 'view'">
  15. <div v-for="(item, index) in files" :key="index" class="item">
  16. <div v-if="item.url" class="img">
  17. <el-image
  18. v-if="checkFileType(item.url) == 'image'"
  19. ref="img"
  20. :src="imageURL + item.url"
  21. :preview-src-list="previewImages"
  22. style="width: 120px; height: 120px"
  23. fit="cover"
  24. />
  25. <el-image v-else ref="img" :src="imageURL + item.url" style="width: 120px; height: 120px" fit="cover">
  26. <div slot="error" class="image-slot">
  27. <img v-if="checkFileType(item.url) == 'word'" class="file" src="@/assets/common/word.png" />
  28. <img v-if="checkFileType(item.url) == 'excel'" class="file" src="@/assets/common/excel.png" />
  29. <img v-if="checkFileType(item.url) == 'ppt'" class="file" src="@/assets/common/ppt.png" />
  30. <img v-if="checkFileType(item.url) == 'pdf'" class="file" src="@/assets/common/pdf.png" />
  31. <img v-if="checkFileType(item.url) == 'file'" class="file" src="@/assets/common/zip.jpeg" />
  32. </div>
  33. </el-image>
  34. <div v-if="isEdit || checkFileType(item.url) == 'image'" class="mask">
  35. <i v-if="checkFileType(item.url) == 'image'" class="el-icon-zoom-in" @click="previewImage(item.url)" />
  36. <i v-if="isEdit" class="el-icon-upload2" @click="uploadImage(item.url)" />
  37. <i v-if="isEdit" class="el-icon-delete" @click="deleteImage(item.url)" />
  38. </div>
  39. </div>
  40. </div>
  41. <template v-if="isEdit">
  42. <div v-if="limit">
  43. <div v-if="limit !== files.length" class="add" @click="uploadImage()">
  44. <i class="el-icon-plus" />
  45. </div>
  46. </div>
  47. <div v-else>
  48. <div v-if="multiple || (!multiple && files.length < 1)" class="add" @click="uploadImage()">
  49. <i class="el-icon-plus" />
  50. </div>
  51. </div>
  52. </template>
  53. </div>
  54. <div v-if="modType === 'btn'">
  55. <el-button size="mini" type="primary" @click="uploadImage()">上传</el-button>
  56. </div>
  57. </div>
  58. </template>
  59. <script>
  60. import { getOssConfig } from '@/api/common'
  61. import { findElem } from '@/utils/util'
  62. export default {
  63. name: 'FileUpload',
  64. props: {
  65. modType: {
  66. type: String,
  67. default: 'view'
  68. },
  69. uid: {
  70. type: String,
  71. default: 'uidImgfile'
  72. },
  73. // 最大上传数量
  74. limit: {
  75. type: Number,
  76. default: 1
  77. },
  78. // 接受上传的文件列表
  79. fileList: {
  80. type: Array,
  81. default: () => []
  82. },
  83. // 接受上传的文件类型
  84. fileType: {
  85. type: Array,
  86. default: () => ['image', 'word', 'excel', 'ppt', 'pdf', 'file']
  87. },
  88. // 是否支持多选文件
  89. multiple: {
  90. type: Boolean,
  91. default: false
  92. },
  93. startRestricting: {
  94. type: Boolean,
  95. default: false
  96. },
  97. restrictFilename: {
  98. type: Array,
  99. default: () => {
  100. return []
  101. }
  102. },
  103. isEdit: {
  104. type: Boolean,
  105. default: true
  106. }
  107. },
  108. data() {
  109. return {
  110. imageURL: this.$imageUrl,
  111. oss_url: '',
  112. dataObj: {},
  113. uploadImageUrl: '',
  114. waitUploadList: [],
  115. fileName: '',
  116. files: this.fileList
  117. }
  118. },
  119. computed: {
  120. isShowFileList: {
  121. get: function () {
  122. if (this.files.length > 0 && this.files[0].url) {
  123. return true
  124. } else {
  125. return false
  126. }
  127. },
  128. set: function (newValue) {}
  129. },
  130. accept() {
  131. const imageList = ['.jpg', '.jpeg', '.png']
  132. const videoList = ['.mp4']
  133. const wordList = ['.doc', '.docx', '.dot', '.wps', '.wpt']
  134. const excelList = ['.xls', '.xlsx', '.xlt', '.et', '.ett']
  135. const pptList = ['.ppt', '.pptx', '.dps', '.dpt', '.pot', '.pps']
  136. const pdfList = ['.pdf']
  137. const fileList = ['.zip', '.rar', '.gz', '.apk']
  138. let whiteList = []
  139. this.fileType.includes('image') && (whiteList = whiteList.concat(imageList))
  140. this.fileType.includes('video') && (whiteList = whiteList.concat(videoList))
  141. this.fileType.includes('word') && (whiteList = whiteList.concat(wordList))
  142. this.fileType.includes('excel') && (whiteList = whiteList.concat(excelList))
  143. this.fileType.includes('ppt') && (whiteList = whiteList.concat(pptList))
  144. this.fileType.includes('pdf') && (whiteList = whiteList.concat(pdfList))
  145. this.fileType.includes('file') && (whiteList = whiteList.concat(fileList))
  146. return whiteList.join(',')
  147. },
  148. previewImages() {
  149. const fileList = []
  150. if (this.files && this.files.length > 0) {
  151. this.files.forEach(item => {
  152. if (this.checkFileType(item.url) == 'image') {
  153. fileList.push(this.imageURL + item.url)
  154. }
  155. })
  156. }
  157. return fileList
  158. }
  159. },
  160. watch: {
  161. files() {
  162. if (this.isEdit) {
  163. this.$emit('getFiles', this.files)
  164. }
  165. },
  166. fileList() {
  167. this.files = this.fileList
  168. }
  169. },
  170. created() {
  171. getOssConfig().then(res => {
  172. this.oss_url = res.data.host
  173. })
  174. },
  175. methods: {
  176. getUUID() {
  177. return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
  178. return (c === 'x' ? (Math.random() * 16) | 0 : 'r&0x3' | '0x8').toString(16)
  179. })
  180. },
  181. createName(name) {
  182. const date = Date.now()
  183. const uuid = this.getUUID()
  184. const fileSuffix = name.substring(name.lastIndexOf('.') + 1)
  185. return `${date}${uuid}.${fileSuffix}`
  186. },
  187. // 检查文件类型
  188. checkFileType(url) {
  189. if (!url) return ''
  190. const fileSuffix = url.substring(url.lastIndexOf('.') + 1)
  191. if (['jpg', 'jpeg', 'png'].includes(fileSuffix)) {
  192. return 'image'
  193. } else if (['doc', 'docx', 'dot', 'wps', 'wpt'].includes(fileSuffix)) {
  194. return 'word'
  195. } else if (['xls', 'xlsx', 'xlt', 'et', 'ett'].includes(fileSuffix)) {
  196. return 'excel'
  197. } else if (['ppt', 'pptx', 'dps', 'dpt', 'pot', 'pps'].includes(fileSuffix)) {
  198. return 'ppt'
  199. } else if (['pdf'].includes(fileSuffix)) {
  200. return 'pdf'
  201. } else if (['zip', 'rar', 'gz', 'apk'].includes(fileSuffix)) {
  202. return 'file'
  203. }
  204. },
  205. // 获取oss配置
  206. async getOssConfig(fileName) {
  207. const result = await new Promise((resolve, reject) => {
  208. getOssConfig()
  209. .then(res => {
  210. const fileKey = this.createName(fileName)
  211. res.data.name = fileName
  212. res.data.key = res.data.dir + fileKey
  213. resolve(res.data)
  214. })
  215. .catch(res => {
  216. resolve({})
  217. })
  218. })
  219. return result
  220. },
  221. // 预览图片
  222. previewImage(url) {
  223. const index = findElem(this.files, 'url', url)
  224. this.$refs.img[index].showViewer = true
  225. },
  226. // 删除图片
  227. deleteImage(url) {
  228. const index = findElem(this.files, 'url', url)
  229. this.files.splice(index, 1)
  230. },
  231. // 点击上传
  232. uploadImage(url) {
  233. this.uploadImageUrl = url
  234. document.querySelector(`.${this.uid}` + ' input').click()
  235. },
  236. // 上传文件之前
  237. async beforeUpload(file) {
  238. const loading = this.$loading({
  239. lock: true,
  240. text: 'Loading',
  241. spinner: 'el-icon-loading',
  242. background: 'rgba(0, 0, 0, 0.7)'
  243. })
  244. this.getFileName(file.name)
  245. this.$emit('handleIsFileName', this.fileName)
  246. this.dataObj = await this.getOssConfig(this.fileName)
  247. this.waitUploadList.push(this.dataObj)
  248. },
  249. // 文件上传成功
  250. uploadSuccess(res, file) {
  251. const loading = this.$loading({
  252. lock: true,
  253. text: 'Loading',
  254. spinner: 'el-icon-loading',
  255. background: 'rgba(0, 0, 0, 0.7)'
  256. })
  257. if (this.uploadImageUrl) {
  258. const index = findElem(this.files, 'url', this.uploadImageUrl)
  259. this.$set(this.files, index, {
  260. name: this.dataObj.name,
  261. url: this.dataObj.key
  262. })
  263. this.waitUploadList = []
  264. } else {
  265. this.getFileName(file.name)
  266. const index = findElem(this.waitUploadList, 'name', this.fileName)
  267. this.files.push({
  268. name: this.waitUploadList[index].name,
  269. url: this.waitUploadList[index].key
  270. })
  271. this.waitUploadList.splice(index, 1)
  272. }
  273. this.showFileList = true
  274. loading.close()
  275. },
  276. getFileName(name) {
  277. const fileName = name.substring(0, name.lastIndexOf('.'))
  278. let suffix = name.match(/.[^.]+$/)[0]
  279. // 押金申请上传限制
  280. this.fileName = name
  281. if (this.startRestricting) {
  282. // 检查是否存在相应文字,否filterKeywords.length = 0
  283. const filterKeywords = this.restrictFilename.filter(k => fileName.includes(k))
  284. // filterKeywords = 0 || 'zip', 'rar', 'gz', 'apk' 归为 其他文件
  285. if (!filterKeywords.length || suffix.includes('zip', 'rar', 'gz', 'apk')) {
  286. this.fileName = `其他文件-${fileName}${suffix}`
  287. }
  288. // 限制照片/相片名称统一为照片 , restrictFilename[restrictFilename.length-1|restrictFilename.length-1]为照片、相片
  289. if (fileName.includes('相片') || fileName.includes('照片')) {
  290. this.fileName = `照片-${fileName}${suffix}`
  291. }
  292. }
  293. }
  294. }
  295. }
  296. </script>
  297. <style scoped lang="scss">
  298. .images {
  299. display: flex;
  300. flex-wrap: wrap;
  301. .item {
  302. margin-right: 20px;
  303. margin-bottom: 20px;
  304. .img {
  305. width: 120px;
  306. height: 120px;
  307. border-radius: 5px;
  308. overflow: hidden;
  309. position: relative;
  310. border: 1px dashed #eaeaea;
  311. display: flex;
  312. .el-image {
  313. display: block;
  314. }
  315. .file {
  316. width: 120px;
  317. height: 120px;
  318. display: block;
  319. padding: 30px;
  320. }
  321. .mask {
  322. position: absolute;
  323. left: 0;
  324. top: 0;
  325. width: 120px;
  326. height: 120px;
  327. background: rgba($color: #000000, $alpha: 0.3);
  328. display: none;
  329. align-items: center;
  330. justify-content: center;
  331. i {
  332. font-size: 20px;
  333. color: #ffffff;
  334. cursor: pointer;
  335. margin: 0 8px;
  336. }
  337. }
  338. &:hover .mask {
  339. display: flex;
  340. }
  341. }
  342. }
  343. .add {
  344. width: 120px;
  345. height: 120px;
  346. border: 1px dashed #eaeaea;
  347. border-radius: 5px;
  348. cursor: pointer;
  349. display: flex;
  350. align-items: center;
  351. justify-content: center;
  352. i {
  353. font-size: 30px;
  354. color: #999;
  355. }
  356. }
  357. }
  358. .uploader {
  359. height: 0;
  360. }
  361. </style>