index.vue 12 KB

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