index.vue 7.6 KB

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