index.vue 12 KB

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