index.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. uploadParams (val) {
  94. this.params = val
  95. }
  96. },
  97. data () {
  98. return {
  99. previewVisible: false,
  100. previewImage: '',
  101. fileList: [],
  102. comVal: this.value,
  103. params: this.uploadParams,
  104. headers: {
  105. 'access-token': this.$store.state.user.token
  106. }
  107. }
  108. },
  109. created () {
  110. const _this = this
  111. this.$nextTick(() => {
  112. const el = document.querySelector('.ant-upload-list-picture-card')
  113. if (el) {
  114. const sortable = Sortable.create(el, {
  115. onEnd: function (evt) {
  116. console.log(evt.oldIndex, evt.newIndex)
  117. const a = _this.formatFile()
  118. console.log(a.split(','))
  119. const ret = moveElement(a.split(','), evt.oldIndex, evt.newIndex)
  120. console.log(ret)
  121. _this.$emit('change', ret.join(','))
  122. _this.$emit('input', ret.join(','))
  123. }
  124. })
  125. }
  126. })
  127. },
  128. methods: {
  129. setFileList (a) {
  130. const temp = []
  131. if (a && a.length > 0) {
  132. if (this.uploadType == 'img' || this.uploadType == 'import') { // 图片/导入
  133. a.split(',').map((item, index) => {
  134. if (item) {
  135. temp.push({
  136. uid: index,
  137. name: item,
  138. status: 'done',
  139. url: item,
  140. response: {
  141. data: item
  142. }
  143. })
  144. }
  145. })
  146. } else if (this.uploadType == 'attach') { // 附件
  147. a.map((item, index) => {
  148. if (item) {
  149. const arr = []
  150. arr.push(item) // 包裹成与新上传文件数据返回结构一致
  151. temp.push({
  152. uid: index,
  153. name: item.fileName,
  154. status: 'done',
  155. url: item.filePath,
  156. response: {
  157. data: arr
  158. }
  159. })
  160. }
  161. })
  162. }
  163. }
  164. this.fileList = temp
  165. },
  166. handleCancel () {
  167. this.previewVisible = false
  168. },
  169. handlePreview (file) {
  170. console.log(file)
  171. if (file.type && (file.type.indexOf('image') >= 0 || file.type.indexOf('video') >= 0)) {
  172. if (file.response && file.response.status && file.response.status == 200) {
  173. const src = file.response.data
  174. this.previewImage = src && Array.isArray(src) ? src[0] && src[0].filePath : src
  175. } else {
  176. this.previewImage = file.url || file.thumbUrl
  177. }
  178. this.previewVisible = true
  179. } else {
  180. const src = file.response.data
  181. if (!Array.isArray(src)) {
  182. this.previewImage = file.url || file.thumbUrl
  183. this.previewVisible = true
  184. }
  185. }
  186. },
  187. handleRemove (file) {
  188. this.$emit('remove')
  189. },
  190. handleChange ({ file, fileList }) {
  191. // console.log(file, fileList, '-----change')
  192. // if (file.status == 'error' || (file.response && (file.response.status == 500 || file.response.status == 900))) {
  193. // this.$message.error('上传失败,请重试!')
  194. // this.$emit('remove')
  195. // return
  196. // }
  197. if (file.response && (file.response.status == 500 || file.response.status == 900)) {
  198. if (file.status == 'error' || file.status == 'done') {
  199. this.$message.error(file.response.message || '上传失败,请重试!')
  200. this.$emit('remove')
  201. // return
  202. }
  203. }
  204. this.fileList = this.validaFile(file, fileList)
  205. this.$emit('change', this.formatFile())
  206. this.$emit('input', this.formatFile())
  207. },
  208. // 格式化文件列表数据,给表单
  209. formatFile () {
  210. const a = this.fileList
  211. let temp = []
  212. let obj = null
  213. if (a && a.length) {
  214. if (this.uploadType == 'img' || this.uploadType == 'import') { // 图片/导入 将接口的返回图片地址逗号拼接后返回页面
  215. a.map(item => {
  216. if (item.response && item.status == 'done') {
  217. temp.push(item.response.data)
  218. }
  219. })
  220. obj = temp.join(',')
  221. } else if (this.uploadType == 'attach') { // 附件 将接口的返回数据返回页面
  222. a.map(item => {
  223. if (item.response && item.status == 'done') {
  224. if (item.response.data instanceof Array) {
  225. temp = [...temp, ...item.response.data]
  226. } else {
  227. temp = [...temp, ...[item.response.data]]
  228. }
  229. }
  230. })
  231. // console.log(temp)
  232. obj = JSON.stringify(temp)
  233. }
  234. }
  235. return obj
  236. },
  237. // 文件校验
  238. validaFile (file, filesList) {
  239. if (filesList.length == 0) {
  240. return []
  241. }
  242. // console.log(file, filesList, this.beforeUpload(file, 0))
  243. if (!this.beforeUpload(file, 0) && file.status != 'removed' || file.status == 'error') {
  244. const index = filesList.findIndex(item => item.uid == file.uid)
  245. filesList.splice(index, 1)
  246. }
  247. return filesList || []
  248. },
  249. // 上传前校验
  250. beforeUpload (file, tip) {
  251. // 不限制
  252. if (this.fileType == '*') {
  253. // 文件大小
  254. const isLt2M = file.size / 1024 / 1024 < this.fileSize
  255. if (!isLt2M) {
  256. if (tip != 0) {
  257. this.$message.error('上传文件不能超过 ' + this.fileSize + 'MB!')
  258. }
  259. return isLt2M
  260. }
  261. return true
  262. }
  263. const isType = this.fileType.indexOf(file.type) >= 0 && file.type != ''
  264. if (!isType) {
  265. if (tip != 0) {
  266. this.$message.error('请上传正确的格式!')
  267. }
  268. return isType
  269. }
  270. // 文件大小
  271. const isLt2M = file.size / 1024 / 1024 < this.fileSize
  272. if (!isLt2M) {
  273. if (tip != 0) {
  274. if (this.fileType != 'video/mp4') {
  275. this.$message.error('图片不能超过 ' + this.fileSize + 'MB!')
  276. } else {
  277. this.$message.error('视频不能超过 ' + this.fileSize + 'MB!')
  278. }
  279. }
  280. return isLt2M
  281. }
  282. return true
  283. }
  284. }
  285. }
  286. </script>
  287. <style lang="less" scoped>
  288. .upload-file{
  289. float: left;
  290. overflow: hidden;
  291. /* you can make up upload button and sample style by using stylesheets */
  292. .ant-upload-select-picture-card i {
  293. font-size: 32px;
  294. color: #999;
  295. }
  296. .ant-upload-select-picture-card .ant-upload-text {
  297. color: #666;
  298. }
  299. .ant-upload-btn{
  300. height: 100%;
  301. >div{
  302. padding: 4px 0;
  303. }
  304. }
  305. .ant-upload-list-picture-card-container{
  306. height: 80px;
  307. width: 80px;
  308. }
  309. .ant-upload.ant-upload-select-picture-card,
  310. .ant-upload-list-picture-card .ant-upload-list-item{
  311. width:80px;
  312. height:80px;
  313. padding: 4px;
  314. margin: 0;
  315. }
  316. .ant-upload-list-item-info{
  317. padding: 0 22px 0 4px;
  318. }
  319. .ant-upload-list-item .anticon-close{
  320. color: red;
  321. }
  322. .ant-upload-list-item-name{
  323. word-break: break-all;
  324. white-space: normal;
  325. }
  326. }
  327. /deep/.ant-modal-body{
  328. max-height:860px;
  329. text-align:center;
  330. }
  331. /deep/.ant-modal-close-x{
  332. height: 35px;
  333. line-height: 35px;
  334. width: 35px;
  335. }
  336. /deep/.ant-modal-body video{
  337. width: 90% !important;
  338. }
  339. </style>