index.vue 7.9 KB

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