index.vue 9.3 KB

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