123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486 |
- <template>
- <div :id="containerId" style="position: relative">
- <!-- ---------------------------- begin 图片左右换位置 ------------------------------------- -->
- <div
- class="movety-container"
- :style="{ top: top + 'px', left: left + 'px', display: moveDisplay }"
- style="padding: 0 8px; position: absolute; z-index: 91; height: 32px; width: 104px; text-align: center"
- >
- <div
- :id="containerId + '-mover'"
- :class="showMoverTask ? 'uploadty-mover-mask' : 'movety-opt'"
- style="margin-top: 12px"
- >
- <a @click="moveLast" style="margin: 0 5px"><a-icon type="arrow-left" style="color: #fff; font-size: 16px" /></a>
- <a @click="moveNext" style="margin: 0 5px"
- ><a-icon type="arrow-right" style="color: #fff; font-size: 16px"
- /></a>
- </div>
- </div>
- <!-- ---------------------------- end 图片左右换位置 ------------------------------------- -->
- <a-upload
- name="file"
- :multiple="true"
- :action="uploadAction"
- :headers="headers"
- :data="{ biz: bizPath }"
- :fileList="fileList"
- :beforeUpload="beforeUpload"
- @change="handleChange"
- :disabled="disabled"
- :returnUrl="returnUrl"
- :listType="complistType"
- @preview="handlePreview"
- :class="{ 'uploadty-disabled': disabled }"
- >
- <template>
- <div v-if="isImageComp">
- <a-icon type="plus" />
- <div class="ant-upload-text">{{ text }}</div>
- </div>
- <a-button v-else-if="buttonVisible" :type="btnType">
- <a-icon v-if="isIcon" type="upload" />{{ text }}
- </a-button>
- </template>
- </a-upload>
- <a-modal :visible="previewVisible" :width="1000" :footer="null" @cancel="handleCancel">
- <img alt="example" style="width: 100%" :src="previewImage" />
- </a-modal>
- </div>
- </template>
- <script>
- import Vue from 'vue'
- import { ACCESS_TOKEN } from '@/store/mutation-types'
- import { getFileAccessHttpUrl } from '@/api/manage'
- import { fileSizeLimit } from '@/api/api'
- const FILE_TYPE_ALL = 'all'
- const FILE_TYPE_IMG = 'image'
- const FILE_TYPE_TXT = 'file'
- const uidGenerator = () => {
- return '-' + parseInt(Math.random() * 10000 + 1, 10)
- }
- const getFileName = (path) => {
- if (path.lastIndexOf('\\') >= 0) {
- let reg = new RegExp('\\\\', 'g')
- path = path.replace(reg, '/')
- }
- return path.substring(path.lastIndexOf('/') + 1)
- }
- export default {
- name: 'JUpload',
- data() {
- return {
- uploadAction: window._CONFIG['domianURL'] + '/systemConfig/upload',
- headers: {},
- fileList: [],
- newFileList: [],
- uploadGoOn: true,
- previewVisible: false,
- //---------------------------- begin 图片左右换位置 -------------------------------------
- previewImage: '',
- containerId: '',
- top: '',
- left: '',
- moveDisplay: 'none',
- showMoverTask: false,
- moverHold: false,
- currentImg: '',
- //---------------------------- end 图片左右换位置 -------------------------------------
- sizeLimit: 0,
- }
- },
- props: {
- text: {
- type: String,
- required: false,
- default: '点击上传',
- },
- btnType: {
- type: String,
- required: false,
- default: 'default',
- },
- isIcon: {
- type: Boolean,
- required: false,
- default: true,
- },
- fileType: {
- type: String,
- required: false,
- default: FILE_TYPE_ALL,
- },
- /*这个属性用于控制文件上传的业务路径*/
- bizPath: {
- type: String,
- required: false,
- default: 'temp',
- },
- value: {
- type: [String, Array],
- required: false,
- },
- // update-begin- --- author:wangshuai ------ date:20190929 ---- for:Jupload组件增加是否能够点击
- disabled: {
- type: Boolean,
- required: false,
- default: false,
- },
- // update-end- --- author:wangshuai ------ date:20190929 ---- for:Jupload组件增加是否能够点击
- //此属性被废弃了
- triggerChange: {
- type: Boolean,
- required: false,
- default: false,
- },
- /**
- * update -- author:lvdandan -- date:20190219 -- for:Jupload组件增加是否返回url,
- * true:仅返回url
- * false:返回fileName filePath fileSize
- */
- returnUrl: {
- type: Boolean,
- required: false,
- default: true,
- },
- number: {
- type: Number,
- required: false,
- default: 0,
- },
- buttonVisible: {
- type: Boolean,
- required: false,
- default: true,
- },
- },
- watch: {
- value: {
- immediate: true,
- handler() {
- let val = this.value
- if (val instanceof Array) {
- if (this.returnUrl) {
- this.initFileList(val.join(','))
- } else {
- this.initFileListArr(val)
- }
- } else {
- this.initFileList(val)
- }
- },
- },
- },
- computed: {
- isImageComp() {
- return this.fileType === FILE_TYPE_IMG
- },
- complistType() {
- return this.fileType === FILE_TYPE_IMG ? 'picture-card' : 'text'
- },
- },
- created() {
- this.initFileSizeLimit()
- const token = Vue.ls.get(ACCESS_TOKEN)
- //---------------------------- begin 图片左右换位置 -------------------------------------
- this.headers = { 'X-Access-Token': token }
- this.containerId = 'container-ty-' + new Date().getTime()
- //---------------------------- end 图片左右换位置 -------------------------------------
- },
- methods: {
- initFileSizeLimit() {
- fileSizeLimit().then((res) => {
- if (res.code === 200) {
- this.sizeLimit = res.data
- }
- })
- },
- initFileListArr(val) {
- if (!val || val.length == 0) {
- this.fileList = []
- return
- }
- let fileList = []
- for (var a = 0; a < val.length; a++) {
- let url = getFileAccessHttpUrl(val[a].filePath)
- fileList.push({
- uid: uidGenerator(),
- name: val[a].fileName,
- status: 'done',
- url: url,
- response: {
- code: 'history',
- data: val[a].filePath,
- },
- })
- }
- this.fileList = fileList
- },
- initFileList(paths) {
- if (!paths || paths.length == 0) {
- //return [];
- // update-begin- --- author:os_chengtgen ------ date:20190729 ---- for:issues:326,Jupload组件初始化bug
- this.fileList = []
- return
- // update-end- --- author:os_chengtgen ------ date:20190729 ---- for:issues:326,Jupload组件初始化bug
- }
- let fileList = []
- let arr = paths.split(',')
- for (var a = 0; a < arr.length; a++) {
- let url = getFileAccessHttpUrl('systemConfig/static/' + arr[a])
- fileList.push({
- uid: uidGenerator(),
- name: getFileName(arr[a]),
- status: 'done',
- url: url,
- response: {
- code: 'history',
- data: arr[a],
- },
- })
- }
- this.fileList = fileList
- },
- handlePathChange() {
- let uploadFiles = this.fileList
- let path = ''
- if (!uploadFiles || uploadFiles.length == 0) {
- path = ''
- } else {
- let arr = []
- for (let a = 0; a < uploadFiles.length; a++) {
- arr.push(uploadFiles[a].response.data)
- }
- if (arr.length > 0) {
- path = arr.join(',')
- }
- }
- this.$emit('change', path)
- },
- beforeUpload(file) {
- this.uploadGoOn = true
- let fileType = file.type
- let fileSize = file.size
- if (this.fileType === FILE_TYPE_IMG) {
- if (fileType.indexOf('image') < 0) {
- this.$message.warning('请上传图片')
- this.uploadGoOn = false
- return false
- }
- }
- //验证文件大小
- if (fileSize > this.sizeLimit) {
- let parseSizeLimit = (this.sizeLimit / 1024 / 1024).toFixed(2)
- this.$message.warning('抱歉,文件大小不能超过' + parseSizeLimit + 'M')
- this.uploadGoOn = false
- return false
- }
- return true
- },
- handleChange(info) {
- console.log('--文件列表改变--')
- if (!info.file.status && this.uploadGoOn === false) {
- info.fileList.pop()
- }
- let fileList = info.fileList
- if (info.file.status === 'done') {
- if (this.number > 0) {
- fileList = fileList.slice(-this.number)
- }
- if (info.file.response.code === 200) {
- fileList = fileList.map((file) => {
- if (file.response) {
- let reUrl = file.response.data
- file.url = getFileAccessHttpUrl(reUrl)
- }
- return file
- })
- }
- //this.$message.success(`${info.file.name} 上传成功!`);
- } else if (info.file.status === 'error') {
- this.$message.error(`${info.file.name} 上传失败.`)
- } else if (info.file.status === 'removed') {
- this.handleDelete(info.file)
- }
- this.fileList = fileList
- if (info.file.status === 'done' || info.file.status === 'removed') {
- //returnUrl为true时仅返回文件路径
- if (this.returnUrl) {
- setTimeout(() => {
- this.handlePathChange()
- }, 500)
- } else {
- //returnUrl为false时返回文件名称、文件路径及文件大小
- this.newFileList = []
- for (var a = 0; a < fileList.length; a++) {
- var fileJson = {
- fileName: fileList[a].name,
- filePath: fileList[a].response.data,
- fileSize: fileList[a].size,
- }
- this.newFileList.push(fileJson)
- }
- this.$emit('change', this.newFileList)
- }
- }
- },
- handleDelete(file) {
- //如有需要新增 删除逻辑
- console.log(file)
- },
- handlePreview(file) {
- let postfix = file.name.substring(file.name.lastIndexOf('.'))
- if (
- postfix === '.gif' ||
- postfix === '.jpg' ||
- postfix === '.jpeg' ||
- postfix === '.png' ||
- postfix === '.GIF' ||
- postfix === '.JPG' ||
- postfix === '.JPEG' ||
- postfix === '.PNG'
- ) {
- this.previewImage = file.url || file.thumbUrl
- this.previewVisible = true
- } else {
- location.href = file.url
- }
- },
- handleCancel() {
- this.previewVisible = false
- },
- //---------------------------- begin 图片左右换位置 -------------------------------------
- moveLast() {
- //console.log(ev)
- //console.log(this.fileList)
- //console.log(this.currentImg)
- let index = this.getIndexByUrl()
- if (index == 0) {
- this.$message.warn('未知的操作')
- } else {
- let curr = this.fileList[index].url
- let last = this.fileList[index - 1].url
- let arr = []
- for (let i = 0; i < this.fileList.length; i++) {
- if (i == index - 1) {
- arr.push(curr)
- } else if (i == index) {
- arr.push(last)
- } else {
- arr.push(this.fileList[i].url)
- }
- }
- this.currentImg = last
- this.$emit('change', arr.join(','))
- }
- },
- moveNext() {
- let index = this.getIndexByUrl()
- if (index == this.fileList.length - 1) {
- this.$message.warn('已到最后~')
- } else {
- let curr = this.fileList[index].url
- let next = this.fileList[index + 1].url
- let arr = []
- for (let i = 0; i < this.fileList.length; i++) {
- if (i == index + 1) {
- arr.push(curr)
- } else if (i == index) {
- arr.push(next)
- } else {
- arr.push(this.fileList[i].url)
- }
- }
- this.currentImg = next
- this.$emit('change', arr.join(','))
- }
- },
- getIndexByUrl() {
- for (let i = 0; i < this.fileList.length; i++) {
- if (this.fileList[i].url === this.currentImg || encodeURI(this.fileList[i].url) === this.currentImg) {
- return i
- }
- }
- return -1
- },
- },
- mounted() {
- const moverObj = document.getElementById(this.containerId + '-mover')
- moverObj.addEventListener('mouseover', () => {
- this.moverHold = true
- this.moveDisplay = 'block'
- })
- moverObj.addEventListener('mouseout', () => {
- this.moverHold = false
- this.moveDisplay = 'none'
- })
- let picList = document.getElementById(this.containerId)
- ? document.getElementById(this.containerId).getElementsByClassName('ant-upload-list-picture-card')
- : []
- if (picList && picList.length > 0) {
- picList[0].addEventListener('mouseover', (ev) => {
- ev = ev || window.event
- let target = ev.target || ev.srcElement
- if ('ant-upload-list-item-info' == target.className) {
- this.showMoverTask = false
- let item = target.parentElement
- this.left = item.offsetLeft
- this.top = item.offsetTop + item.offsetHeight - 50
- this.moveDisplay = 'block'
- this.currentImg = target.getElementsByTagName('img')[0].src
- }
- })
- picList[0].addEventListener('mouseout', (ev) => {
- ev = ev || window.event
- let target = ev.target || ev.srcElement
- //console.log('移除',target)
- if ('ant-upload-list-item-info' == target.className) {
- this.showMoverTask = true
- setTimeout(() => {
- if (this.moverHold === false) this.moveDisplay = 'none'
- }, 100)
- }
- if (
- 'ant-upload-list-item ant-upload-list-item-done' == target.className ||
- 'ant-upload-list ant-upload-list-picture-card' == target.className
- ) {
- this.moveDisplay = 'none'
- }
- })
- //---------------------------- end 图片左右换位置 -------------------------------------
- }
- },
- model: {
- prop: 'value',
- event: 'change',
- },
- }
- </script>
- <style lang="less">
- .uploadty-disabled {
- .ant-upload-list-item {
- .anticon-close {
- display: none;
- }
- .anticon-delete {
- display: none;
- }
- }
- }
- //---------------------------- begin 图片左右换位置 -------------------------------------
- .uploadty-mover-mask {
- background-color: rgba(0, 0, 0, 0.5);
- opacity: 0.8;
- color: #fff;
- height: 28px;
- line-height: 28px;
- }
- //---------------------------- end 图片左右换位置 -------------------------------------
- </style>
|