index.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 if (file.type) {
  180. if (file.response && file.response.status && file.response.status == 200) {
  181. var src = file.response.data
  182. if (Array.isArray(src)) {
  183. src = file.response.data[0].filePath
  184. }
  185. window.location.href = src.replace('http:', 'https:')
  186. }
  187. } else {
  188. this.previewImage = file.url
  189. this.previewVisible = true
  190. }
  191. },
  192. handleRemove (file) {
  193. this.$emit('remove')
  194. },
  195. handleChange ({ file, fileList }) {
  196. console.log(file, fileList, '-----change')
  197. // if (file.status == 'error' || (file.response && (file.response.status == 500 || file.response.status == 900))) {
  198. // this.$message.error('上传失败,请重试!')
  199. // this.$emit('remove')
  200. // return
  201. // }
  202. if (file.response && (file.response.status == 500 || file.response.status == 900)) {
  203. this.$message.error(file.response.message || '上传失败,请重试!')
  204. this.setFileList('')
  205. this.$emit('remove')
  206. return
  207. }
  208. this.fileList = this.validaFile(file, fileList)
  209. this.$emit('change', this.formatFile())
  210. this.$emit('input', this.formatFile())
  211. },
  212. // 格式化文件列表数据,给表单
  213. formatFile () {
  214. const a = this.fileList
  215. let temp = []
  216. let obj = null
  217. if (a && a.length) {
  218. if (this.uploadType == 'img' || this.uploadType == 'import') { // 图片/导入 将接口的返回图片地址逗号拼接后返回页面
  219. a.map(item => {
  220. if (item.response && item.status == 'done') {
  221. temp.push(item.response.data)
  222. }
  223. })
  224. obj = temp.join(',')
  225. } else if (this.uploadType == 'attach') { // 附件 将接口的返回数据返回页面
  226. a.map(item => {
  227. if (item.response && item.status == 'done') {
  228. if (item.response.data instanceof Array) {
  229. temp = [...temp, ...item.response.data]
  230. } else {
  231. temp = [...temp, ...[item.response.data]]
  232. }
  233. }
  234. })
  235. obj = JSON.stringify(temp)
  236. }
  237. }
  238. return obj
  239. },
  240. // 文件校验
  241. validaFile (file, filesList) {
  242. if (filesList.length == 0) {
  243. return []
  244. }
  245. // console.log(file, filesList, this.beforeUpload(file, 0))
  246. if (!this.beforeUpload(file, 0) && file.status != 'removed' || file.status == 'error') {
  247. const index = filesList.findIndex(item => item.uid == file.uid)
  248. filesList.splice(index, 1)
  249. }
  250. return filesList || []
  251. },
  252. // 上传前校验
  253. beforeUpload (file, tip) {
  254. // 不限制
  255. if (this.fileType == '*') {
  256. // 文件大小
  257. const isLt2M = file.size / 1024 / 1024 < this.fileSize
  258. if (!isLt2M) {
  259. if (tip != 0) {
  260. this.$message.error('上传文件不能超过 ' + this.fileSize + 'MB!')
  261. }
  262. return isLt2M
  263. }
  264. return true
  265. }
  266. console.log(file, this.fileType)
  267. const isType = this.fileType.indexOf(file.type) >= 0 && file.type != ''
  268. if (!isType) {
  269. if (tip != 0) {
  270. this.$message.error('请上传正确的格式!')
  271. }
  272. return isType
  273. }
  274. // 文件大小
  275. const isLt2M = file.size / 1024 / 1024 < this.fileSize
  276. if (!isLt2M) {
  277. if (tip != 0) {
  278. if (this.fileType != 'video/mp4') {
  279. this.$message.error('图片不能超过 ' + this.fileSize + 'MB!')
  280. } else {
  281. this.$message.error('视频不能超过 ' + this.fileSize + 'MB!')
  282. }
  283. }
  284. return isLt2M
  285. }
  286. return true
  287. }
  288. }
  289. }
  290. </script>
  291. <style lang="less" scoped>
  292. .upload-file{
  293. float: left;
  294. overflow: hidden;
  295. /* you can make up upload button and sample style by using stylesheets */
  296. .ant-upload-select-picture-card i {
  297. font-size: 32px;
  298. color: #999;
  299. }
  300. .ant-upload-select-picture-card .ant-upload-text {
  301. color: #666;
  302. }
  303. .ant-upload-btn{
  304. height: 100%;
  305. >div{
  306. padding: 4px 0;
  307. }
  308. }
  309. .ant-upload-list-picture-card-container{
  310. height: 80px;
  311. width: 80px;
  312. }
  313. .ant-upload.ant-upload-select-picture-card,
  314. .ant-upload-list-picture-card .ant-upload-list-item{
  315. width:80px;
  316. height:80px;
  317. padding: 4px;
  318. margin: 0;
  319. }
  320. .ant-upload-list-item-info{
  321. padding: 0 22px 0 4px;
  322. }
  323. .ant-upload-list-item .anticon-close{
  324. color: red;
  325. }
  326. .ant-upload-list-item-name{
  327. word-break: break-all;
  328. white-space: normal;
  329. }
  330. }
  331. /deep/.ant-modal-body{
  332. max-height:860px;
  333. text-align:center;
  334. }
  335. /deep/.ant-modal-close-x{
  336. height: 35px;
  337. line-height: 35px;
  338. width: 35px;
  339. }
  340. /deep/.ant-modal-body video{
  341. width: 90% !important;
  342. }
  343. </style>