index.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. :headers="headers"
  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. <video v-if="fileType=='video/mp4'" controls="controls" style="width: 100%" :src="previewImage" />
  26. <img v-else alt="example" style="width: 100%" :src="previewImage" />
  27. </a-modal>
  28. </div>
  29. </template>
  30. <script>
  31. export default {
  32. name: 'Upload',
  33. props: {
  34. maxNums: {
  35. type: Number,
  36. default: 1
  37. },
  38. fileType: {
  39. type: String,
  40. default: 'image/jpeg,image/png'
  41. },
  42. fileSize: {
  43. type: Number,
  44. default: 5
  45. },
  46. previewFile: {
  47. type: Function,
  48. default: function () {}
  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. headers: {
  98. 'access-token': this.$store.getters.token
  99. }
  100. }
  101. },
  102. methods: {
  103. setFileList (a) {
  104. const temp = []
  105. if (a && a.length > 0) {
  106. if (this.uploadType == 'img' || this.uploadType == 'import') { // 图片/导入
  107. a.split(',').map((item, index) => {
  108. if (item) {
  109. temp.push({
  110. uid: index,
  111. name: item,
  112. status: 'done',
  113. url: item,
  114. response: {
  115. data: item
  116. }
  117. })
  118. }
  119. })
  120. } else if (this.uploadType == 'attach') { // 附件
  121. a.map((item, index) => {
  122. if (item) {
  123. const arr = []
  124. arr.push(item) // 包裹成与新上传文件数据返回结构一致
  125. temp.push({
  126. uid: index,
  127. name: item.fileName,
  128. status: 'done',
  129. url: item.filePath,
  130. response: {
  131. data: arr
  132. }
  133. })
  134. }
  135. })
  136. }
  137. }
  138. this.fileList = temp
  139. },
  140. handleCancel () {
  141. this.previewVisible = false
  142. },
  143. handlePreview (file) {
  144. if (this.listType == 'text') {
  145. return
  146. }
  147. this.previewImage = file.url || file.thumbUrl
  148. this.previewVisible = true
  149. },
  150. handleRemove (file) {
  151. this.$emit('remove')
  152. },
  153. handleChange ({ file, fileList }) {
  154. // console.log(file, fileList, '-----change')
  155. if (file.response && (file.response.status == 500 || file.response.status == 900)) {
  156. if (file.status == 'error' || file.status == 'done') {
  157. this.$message.error(file.response.message || '上传失败,请重试!')
  158. this.$emit('remove')
  159. return
  160. }
  161. }
  162. this.fileList = this.validaFile(file, fileList)
  163. this.$emit('change', this.formatFile())
  164. this.$emit('input', this.formatFile())
  165. },
  166. // 格式化文件列表数据,给表单
  167. formatFile () {
  168. const _this = this
  169. const a = this.fileList
  170. let temp = []
  171. let obj = null
  172. if (a && a.length) {
  173. if (this.uploadType == 'img' || this.uploadType == 'import') { // 图片/导入 将接口的返回图片地址逗号拼接后返回页面
  174. a.map(item => {
  175. if (item.response && item.status == 'done') {
  176. temp.push(item.response.data)
  177. }
  178. })
  179. obj = temp.join(',')
  180. } else if (this.uploadType == 'attach') { // 附件 将接口的返回数据返回页面
  181. a.map(item => {
  182. if (item.response && item.status == 'done') {
  183. if (_this.maxNums == 1) {
  184. temp = [item.response.data]
  185. } else {
  186. temp = [...temp, ...item.response.data]
  187. }
  188. }
  189. })
  190. obj = JSON.stringify(temp)
  191. }
  192. }
  193. return obj
  194. },
  195. // 文件校验
  196. validaFile (file, filesList) {
  197. if (filesList.length == 0) {
  198. return []
  199. }
  200. // console.log(file, filesList, this.beforeUpload(file, 0))
  201. if (!this.beforeUpload(file, 0) && file.status != 'removed') {
  202. const index = filesList.findIndex(item => item.uid == file.uid)
  203. filesList.splice(index, 1)
  204. }
  205. return filesList || []
  206. },
  207. // 上传前校验
  208. beforeUpload (file, tip) {
  209. // 不限制
  210. if (this.fileType == '*') {
  211. // 文件大小
  212. const isLt2M = file.size / 1024 / 1024 < this.fileSize
  213. if (!isLt2M) {
  214. if (tip != 0) {
  215. this.$message.error('上传文件不能超过 ' + this.fileSize + 'MB!')
  216. }
  217. return isLt2M
  218. }
  219. return true
  220. }
  221. const isType = this.fileType.indexOf(file.type) >= 0 && file.type != ''
  222. // 文件格式限制为.xls、.xlsx时,上传.csv文件的type为application/vnd.ms-excel,与.xls一致,无法限制,因此更改为:
  223. if (!isType || file.name.indexOf('.csv') >= 0) {
  224. if (tip != 0) {
  225. this.$message.error('请上传正确的格式!')
  226. }
  227. return isType && !(file.name.indexOf('.csv') >= 0)
  228. }
  229. // if (!isType) {
  230. // if (tip != 0) {
  231. // this.$message.error('请上传正确的格式!')
  232. // }
  233. // return isType
  234. // }
  235. // 文件大小
  236. const isLt2M = file.size / 1024 / 1024 < this.fileSize
  237. if (!isLt2M) {
  238. if (tip != 0) {
  239. if (this.fileType != 'video/mp4') {
  240. this.$message.error('图片不能超过 ' + this.fileSize + 'MB!')
  241. } else {
  242. this.$message.error('视频不能超过 ' + this.fileSize + 'MB!')
  243. }
  244. }
  245. return isLt2M
  246. }
  247. return true
  248. }
  249. }
  250. }
  251. </script>
  252. <style lang="less">
  253. .upload-file{
  254. float: left;
  255. overflow: hidden;
  256. height: 80px;
  257. /* you can make up upload button and sample style by using stylesheets */
  258. .ant-upload-select-picture-card i {
  259. font-size: 32px;
  260. color: #999;
  261. margin-top: 9px;
  262. }
  263. .ant-upload-select-picture-card .ant-upload-text {
  264. margin-top: 8px;
  265. color: #666;
  266. }
  267. .ant-upload-btn{
  268. height: 100%;
  269. >div{
  270. padding: 4px 0;
  271. }
  272. }
  273. .ant-upload-list-picture-card-container{
  274. height: 80px;
  275. width: 80px;
  276. }
  277. .ant-upload.ant-upload-select-picture-card,
  278. .ant-upload-list-picture-card .ant-upload-list-item{
  279. width:80px;
  280. height:80px;
  281. padding: 4px;
  282. margin: 0;
  283. }
  284. .ant-upload-list-item-info{
  285. padding: 0 22px 0 4px;
  286. }
  287. .ant-upload-list-item .anticon-close{
  288. color: red;
  289. }
  290. .ant-upload-list-item-name{
  291. word-break: break-all;
  292. white-space: normal;
  293. }
  294. }
  295. </style>