index.vue 12 KB

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