index.vue 7.5 KB

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