index.vue 8.1 KB

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