index.vue 6.9 KB

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