index.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <template>
  2. <div class="upload-file" :style="{width: maxNums>1?'100%':'auto'}">
  3. <a-upload
  4. :action="action"
  5. :listType="listType"
  6. :fileList="fileList"
  7. :disabled="disabled"
  8. @preview="handlePreview"
  9. @change="handleChange"
  10. :headers="headers"
  11. :remove="handleRemove"
  12. :beforeUpload="beforeUpload"
  13. :data="params"
  14. :value="comVal"
  15. >
  16. <div v-if="fileList.length < maxNums && listType=='picture-card'">
  17. <a-icon type="plus" />
  18. <div class="ant-upload-text"> {{ upText }}</div>
  19. </div>
  20. <div v-if="fileList.length < maxNums && listType!='picture-card'">
  21. <a-button> <a-icon type="upload" /> {{ upText }} </a-button>
  22. </div>
  23. </a-upload>
  24. <a-modal :visible="previewVisible" :footer="null" centered :maskClosable="false" @cancel="handleCancel">
  25. <div style="height: 30px;">预览</div>
  26. <video v-if="fileType=='video/mp4'" controls="controls" style="width: 100%" :src="previewImage" />
  27. <img v-else alt="example" style="width: 100%" :src="previewImage" />
  28. </a-modal>
  29. </div>
  30. </template>
  31. <script>
  32. import Sortable from 'sortablejs'
  33. import { moveElement } from '@/libs/tools'
  34. export default {
  35. name: 'Upload',
  36. props: {
  37. maxNums: {
  38. type: Number,
  39. default: 1
  40. },
  41. fileType: {
  42. type: String,
  43. default: 'image/jpeg,image/png'
  44. },
  45. fileSize: {
  46. type: Number,
  47. default: 5
  48. },
  49. previewFile: {
  50. type: Function,
  51. default: function () {
  52. }
  53. },
  54. action: {
  55. type: String,
  56. default: process.env.VUE_APP_API_BASE_URL + '/upload'
  57. },
  58. listType: {
  59. type: String,
  60. default: 'text'
  61. },
  62. upText: {
  63. type: String,
  64. default: '上传文件'
  65. },
  66. value: {
  67. type: String
  68. },
  69. uploadParams: {
  70. type: Object,
  71. default: function () {
  72. return {
  73. savePathType: 'ali' // 存储类型 local 服务器本地,ali 阿里云, web
  74. }
  75. }
  76. },
  77. disabled: {
  78. type: Boolean,
  79. default: false
  80. },
  81. uploadType: { // 上传类型 img图片;attach附件;import导入
  82. type: String,
  83. default: 'img'
  84. }
  85. },
  86. watch: {
  87. value (a, b) {
  88. this.comVal = a || ''
  89. if (a == undefined) {
  90. this.fileList = []
  91. }
  92. }
  93. },
  94. data () {
  95. return {
  96. previewVisible: false,
  97. previewImage: '',
  98. fileList: [],
  99. comVal: this.value,
  100. params: this.uploadParams,
  101. headers: {
  102. 'access-token': this.$store.state.user.token
  103. }
  104. }
  105. },
  106. created(){
  107. let _this = this
  108. this.$nextTick(()=>{
  109. let el = document.querySelector('.ant-upload-list-picture-card');
  110. let sortable = Sortable.create(el,{
  111. onEnd:function(evt){
  112. console.log(evt.oldIndex,evt.newIndex)
  113. const a =_this.formatFile()
  114. console.log(a.split(','))
  115. const ret = moveElement(a.split(','),evt.oldIndex,evt.newIndex)
  116. console.log(ret)
  117. _this.$emit('change', ret.join(','))
  118. _this.$emit('input', ret.join(','))
  119. }
  120. });
  121. })
  122. },
  123. methods: {
  124. setFileList (a) {
  125. const temp = []
  126. if (a && a.length > 0) {
  127. if (this.uploadType == 'img' || this.uploadType == 'import') { // 图片/导入
  128. a.split(',').map((item, index) => {
  129. if (item) {
  130. temp.push({
  131. uid: index,
  132. name: item,
  133. status: 'done',
  134. url: item,
  135. response: {
  136. data: item
  137. }
  138. })
  139. }
  140. })
  141. } else if (this.uploadType == 'attach') { // 附件
  142. a.map((item, index) => {
  143. if (item) {
  144. const arr = []
  145. arr.push(item) // 包裹成与新上传文件数据返回结构一致
  146. temp.push({
  147. uid: index,
  148. name: item.fileName,
  149. status: 'done',
  150. url: item.filePath,
  151. response: {
  152. data: arr
  153. }
  154. })
  155. }
  156. })
  157. }
  158. }
  159. this.fileList = temp
  160. },
  161. handleCancel () {
  162. this.previewVisible = false
  163. },
  164. handlePreview (file) {
  165. console.log(file)
  166. if (file.type && (file.type.indexOf('image') >= 0 || file.type.indexOf('video') >= 0)) {
  167. if (file.response && file.response.status && file.response.status == 200) {
  168. const src = file.response.data
  169. this.previewImage = src && Array.isArray(src) ? src[0] && src[0].filePath : src
  170. } else {
  171. this.previewImage = file.url || file.thumbUrl
  172. }
  173. this.previewVisible = true
  174. }else{
  175. const src = file.response.data
  176. if(!Array.isArray(src)){
  177. this.previewImage = file.url || file.thumbUrl
  178. this.previewVisible = true
  179. }
  180. }
  181. },
  182. handleRemove (file) {
  183. this.$emit('remove')
  184. },
  185. handleChange ({ file, fileList }) {
  186. // console.log(file, fileList, '-----change')
  187. // if (file.status == 'error' || (file.response && (file.response.status == 500 || file.response.status == 900))) {
  188. // this.$message.error('上传失败,请重试!')
  189. // this.$emit('remove')
  190. // return
  191. // }
  192. if (file.response && (file.response.status == 500 || file.response.status == 900)) {
  193. if (file.status == 'error' || file.status == 'done') {
  194. this.$message.error(file.response.message || '上传失败,请重试!')
  195. this.$emit('remove')
  196. // return
  197. }
  198. }
  199. this.fileList = this.validaFile(file, fileList)
  200. this.$emit('change', this.formatFile())
  201. this.$emit('input', this.formatFile())
  202. },
  203. // 格式化文件列表数据,给表单
  204. formatFile () {
  205. const a = this.fileList
  206. let temp = []
  207. let obj = null
  208. if (a && a.length) {
  209. if (this.uploadType == 'img' || this.uploadType == 'import') { // 图片/导入 将接口的返回图片地址逗号拼接后返回页面
  210. a.map(item => {
  211. if (item.response && item.status == 'done') {
  212. temp.push(item.response.data)
  213. }
  214. })
  215. obj = temp.join(',')
  216. } else if (this.uploadType == 'attach') { // 附件 将接口的返回数据返回页面
  217. a.map(item => {
  218. if (item.response && item.status == 'done') {
  219. if (item.response.data instanceof Array) {
  220. temp = [...temp, ...item.response.data]
  221. } else {
  222. temp = [...temp, ...[item.response.data]]
  223. }
  224. }
  225. })
  226. // console.log(temp)
  227. obj = JSON.stringify(temp)
  228. }
  229. }
  230. return obj
  231. },
  232. // 文件校验
  233. validaFile (file, filesList) {
  234. if (filesList.length == 0) {
  235. return []
  236. }
  237. // console.log(file, filesList, this.beforeUpload(file, 0))
  238. if (!this.beforeUpload(file, 0) && file.status != 'removed' || file.status == 'error') {
  239. const index = filesList.findIndex(item => item.uid == file.uid)
  240. filesList.splice(index, 1)
  241. }
  242. return filesList || []
  243. },
  244. // 上传前校验
  245. beforeUpload (file, tip) {
  246. // 不限制
  247. if (this.fileType == '*') {
  248. // 文件大小
  249. const isLt2M = file.size / 1024 / 1024 < this.fileSize
  250. if (!isLt2M) {
  251. if (tip != 0) {
  252. this.$message.error('上传文件不能超过 ' + this.fileSize + 'MB!')
  253. }
  254. return isLt2M
  255. }
  256. return true
  257. }
  258. const isType = this.fileType.indexOf(file.type) >= 0 && file.type != ''
  259. if (!isType) {
  260. if (tip != 0) {
  261. this.$message.error('请上传正确的格式!')
  262. }
  263. return isType
  264. }
  265. // 文件大小
  266. const isLt2M = file.size / 1024 / 1024 < this.fileSize
  267. if (!isLt2M) {
  268. if (tip != 0) {
  269. if (this.fileType != 'video/mp4') {
  270. this.$message.error('图片不能超过 ' + this.fileSize + 'MB!')
  271. } else {
  272. this.$message.error('视频不能超过 ' + this.fileSize + 'MB!')
  273. }
  274. }
  275. return isLt2M
  276. }
  277. return true
  278. }
  279. }
  280. }
  281. </script>
  282. <style lang="less" scoped>
  283. .upload-file{
  284. float: left;
  285. overflow: hidden;
  286. /* you can make up upload button and sample style by using stylesheets */
  287. .ant-upload-select-picture-card i {
  288. font-size: 32px;
  289. color: #999;
  290. }
  291. .ant-upload-select-picture-card .ant-upload-text {
  292. color: #666;
  293. }
  294. .ant-upload-btn{
  295. height: 100%;
  296. >div{
  297. padding: 4px 0;
  298. }
  299. }
  300. .ant-upload-list-picture-card-container{
  301. height: 80px;
  302. width: 80px;
  303. }
  304. .ant-upload.ant-upload-select-picture-card,
  305. .ant-upload-list-picture-card .ant-upload-list-item{
  306. width:80px;
  307. height:80px;
  308. padding: 4px;
  309. margin: 0;
  310. }
  311. .ant-upload-list-item-info{
  312. padding: 0 22px 0 4px;
  313. }
  314. .ant-upload-list-item .anticon-close{
  315. color: red;
  316. }
  317. .ant-upload-list-item-name{
  318. word-break: break-all;
  319. white-space: normal;
  320. }
  321. }
  322. /deep/.ant-modal-body{
  323. max-height:860px;
  324. text-align:center;
  325. }
  326. /deep/.ant-modal-close-x{
  327. height: 35px;
  328. line-height: 35px;
  329. width: 35px;
  330. }
  331. /deep/.ant-modal-body video{
  332. width: 90% !important;
  333. }
  334. </style>