index.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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" @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: 'web' // 存储类型 local 服务器本地,ali 阿里云, web
  69. }
  70. }
  71. },
  72. disabled: {
  73. type: Boolean,
  74. default: false
  75. }
  76. },
  77. watch: {
  78. value (a, b) {
  79. this.comVal = a || ''
  80. if (a == undefined) {
  81. this.fileList = []
  82. }
  83. }
  84. },
  85. data () {
  86. return {
  87. previewVisible: false,
  88. previewImage: '',
  89. fileList: [],
  90. comVal: this.value,
  91. params: this.uploadParams
  92. }
  93. },
  94. methods: {
  95. setFileList (a) {
  96. const temp = []
  97. if (a && a.length > 0) {
  98. a.split(',').map((item, index) => {
  99. if (item) {
  100. temp.push({
  101. uid: index,
  102. name: item,
  103. status: 'done',
  104. url: item,
  105. response: {
  106. data: item
  107. }
  108. })
  109. }
  110. })
  111. }
  112. this.fileList = temp
  113. },
  114. handleCancel () {
  115. this.previewVisible = false
  116. },
  117. handlePreview (file) {
  118. if (this.listType == 'text') {
  119. return
  120. }
  121. this.previewImage = file.url || file.thumbUrl
  122. this.previewVisible = true
  123. },
  124. handleChange ({ file, fileList }) {
  125. if (file.status == 'error') {
  126. this.$message.error('上传失败,请重试!')
  127. return
  128. }
  129. this.fileList = this.validaFile(file, fileList)
  130. this.$emit('change', this.formatFile())
  131. this.$emit('input', this.formatFile())
  132. },
  133. // 格式化文件列表数据,给表单
  134. formatFile () {
  135. const a = this.fileList
  136. const temp = []
  137. if (a && a.length) {
  138. a.map(item => {
  139. if (item.response && item.status == 'done') {
  140. temp.push(item.response.data)
  141. }
  142. })
  143. }
  144. return temp.join(',')
  145. },
  146. // 文件校验
  147. validaFile (file, filesList) {
  148. if (filesList.length == 0) {
  149. return []
  150. }
  151. console.log(file, filesList, this.beforeUpload(file, 0))
  152. if (!this.beforeUpload(file, 0) && file.status != 'removed') {
  153. const index = filesList.findIndex(item => item.uid == file.uid)
  154. filesList.splice(index, 1)
  155. }
  156. return filesList || []
  157. },
  158. // 上传前校验
  159. beforeUpload (file, tip) {
  160. // 不限制
  161. if (this.fileType == '*') {
  162. return true
  163. }
  164. const isType = this.fileType.indexOf(file.type) >= 0 && file.type != ''
  165. if (!isType) {
  166. if (tip != 0) {
  167. this.$message.error('请上传正确的格式!')
  168. }
  169. return isType
  170. }
  171. // 文件大小
  172. const isLt2M = file.size / 1024 / 1024 < this.fileSize
  173. if (!isLt2M) {
  174. if (tip != 0) {
  175. if (this.fileType != 'video/mp4') {
  176. this.$message.error('图片不能超过 ' + this.fileSize + 'MB!')
  177. } else {
  178. this.$message.error('视频不能超过 ' + this.fileSize + 'MB!')
  179. }
  180. }
  181. return isLt2M
  182. }
  183. return true
  184. }
  185. }
  186. }
  187. </script>
  188. <style lang="less">
  189. .upload-file{
  190. float: left;
  191. overflow: hidden;
  192. height: 80px;
  193. /* you can make up upload button and sample style by using stylesheets */
  194. .ant-upload-select-picture-card i {
  195. font-size: 32px;
  196. color: #999;
  197. margin-top: 9px;
  198. }
  199. .ant-upload-select-picture-card .ant-upload-text {
  200. margin-top: 8px;
  201. color: #666;
  202. }
  203. .ant-upload-btn{
  204. height: 100%;
  205. >div{
  206. padding: 4px 0;
  207. }
  208. }
  209. .ant-upload-list-picture-card-container{
  210. height: 80px;
  211. width: 80px;
  212. }
  213. .ant-upload.ant-upload-select-picture-card,
  214. .ant-upload-list-picture-card .ant-upload-list-item{
  215. width:80px;
  216. height:80px;
  217. padding: 4px;
  218. margin: 0;
  219. }
  220. .ant-upload-list-item-info{
  221. padding: 0 22px 0 4px;
  222. }
  223. .ant-upload-list-item .anticon-close{
  224. color: red;
  225. }
  226. .ant-upload-list-item-name{
  227. word-break: break-all;
  228. white-space: normal;
  229. }
  230. }
  231. </style>