JImageUpload.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <template>
  2. <a-upload
  3. name="file"
  4. listType="picture-card"
  5. :multiple="isMultiple"
  6. :action="uploadAction"
  7. :headers="headers"
  8. :data="{ biz: bizPath }"
  9. :fileList="fileList"
  10. :beforeUpload="beforeUpload"
  11. :disabled="disabled"
  12. :isMultiple="isMultiple"
  13. :showUploadList="isMultiple"
  14. @change="handleChange"
  15. @preview="handlePreview"
  16. >
  17. <img v-if="!isMultiple && picUrl" :src="getAvatarView()" style="height: 104px; max-width: 300px" />
  18. <div v-else>
  19. <a-icon :type="uploadLoading ? 'loading' : 'plus'" />
  20. <div class="ant-upload-text">{{ text }}</div>
  21. </div>
  22. <a-modal :visible="previewVisible" :width="1000" :footer="null" @cancel="handleCancel()">
  23. <img alt="example" style="width: 100%" :src="previewImage" />
  24. </a-modal>
  25. </a-upload>
  26. </template>
  27. <script>
  28. import Vue from 'vue'
  29. import { ACCESS_TOKEN } from '@/store/mutation-types'
  30. import { getFileAccessHttpUrl } from '@/api/manage'
  31. import { fileSizeLimit } from '@/api/api'
  32. const uidGenerator = () => {
  33. return '-' + parseInt(Math.random() * 10000 + 1, 10)
  34. }
  35. const getFileName = (path) => {
  36. if (path.lastIndexOf('\\') >= 0) {
  37. let reg = new RegExp('\\\\', 'g')
  38. path = path.replace(reg, '/')
  39. }
  40. return path.substring(path.lastIndexOf('/') + 1)
  41. }
  42. export default {
  43. name: 'JImageUpload',
  44. data() {
  45. return {
  46. uploadAction: window._CONFIG['domianURL'] + '/systemConfig/upload',
  47. uploadLoading: false,
  48. picUrl: false,
  49. headers: {},
  50. fileList: [],
  51. previewImage: '',
  52. previewVisible: false,
  53. sizeLimit: 0,
  54. uploadGoOn: true,
  55. }
  56. },
  57. props: {
  58. text: {
  59. type: String,
  60. required: false,
  61. default: '上传',
  62. },
  63. /*这个属性用于控制文件上传的业务路径*/
  64. bizPath: {
  65. type: String,
  66. required: false,
  67. default: 'temp',
  68. },
  69. value: {
  70. type: [String, Array],
  71. required: false,
  72. },
  73. disabled: {
  74. type: Boolean,
  75. required: false,
  76. default: false,
  77. },
  78. isMultiple: {
  79. type: Boolean,
  80. required: false,
  81. default: false,
  82. },
  83. returnUrl: {
  84. type: Boolean,
  85. required: false,
  86. default: true,
  87. },
  88. },
  89. watch: {
  90. value: {
  91. immediate: true,
  92. handler() {
  93. let val = this.value
  94. if (val instanceof Array) {
  95. if (this.returnUrl) {
  96. this.initFileList(val.join(','))
  97. } else {
  98. this.initFileListArr(val)
  99. }
  100. } else {
  101. this.initFileList(val)
  102. }
  103. },
  104. },
  105. },
  106. created() {
  107. this.initFileSizeLimit()
  108. const token = Vue.ls.get(ACCESS_TOKEN)
  109. this.headers = { 'X-Access-Token': token }
  110. },
  111. methods: {
  112. initFileSizeLimit() {
  113. fileSizeLimit().then((res) => {
  114. if (res.code === 200) {
  115. this.sizeLimit = res.data
  116. }
  117. })
  118. },
  119. initFileListArr(val) {
  120. if (!val || val.length == 0) {
  121. this.fileList = []
  122. return
  123. }
  124. let fileList = []
  125. for (var a = 0; a < val.length; a++) {
  126. let url = getFileAccessHttpUrl(val[a].filePath)
  127. fileList.push({
  128. uid: uidGenerator(),
  129. name: val[a].fileName,
  130. status: 'done',
  131. url: url,
  132. response: {
  133. code: 'history',
  134. data: val[a].filePath,
  135. },
  136. })
  137. }
  138. this.fileList = fileList
  139. },
  140. initFileList(paths) {
  141. if (!paths || paths.length == 0) {
  142. this.fileList = []
  143. this.picUrl = false
  144. return
  145. }
  146. this.picUrl = true
  147. let fileList = []
  148. let arr = paths.split(',')
  149. for (var a = 0; a < arr.length; a++) {
  150. let url = getFileAccessHttpUrl('' + arr[a])
  151. fileList.push({
  152. uid: uidGenerator(),
  153. name: getFileName(arr[a]),
  154. status: 'done',
  155. url: url,
  156. response: {
  157. code: 'history',
  158. data: arr[a],
  159. },
  160. })
  161. }
  162. this.fileList = fileList
  163. console.log('------------------21312', this.fileList)
  164. },
  165. beforeUpload: function (file) {
  166. this.uploadGoOn = true
  167. let fileType = file.type
  168. let fileSize = file.size
  169. if (fileType.indexOf('image') < 0) {
  170. this.$message.warning('请上传图片')
  171. this.uploadGoOn = false
  172. return false
  173. }
  174. //验证文件大小
  175. if (fileSize > this.sizeLimit / 10) {
  176. let parseSizeLimit = (this.sizeLimit / 1024 / 1024 / 10).toFixed(2)
  177. this.$message.warning('抱歉,图片大小不能超过' + parseSizeLimit + 'M')
  178. this.uploadGoOn = false
  179. return false
  180. }
  181. return true
  182. },
  183. handleChange(info) {
  184. console.log('--文件列表改变--')
  185. if (!info.file.status && this.uploadGoOn === false) {
  186. info.fileList.pop()
  187. }
  188. this.picUrl = false
  189. let fileList = info.fileList
  190. if (info.file.status === 'done') {
  191. if (info.file.response.code === 200) {
  192. this.picUrl = true
  193. fileList = fileList.map((file) => {
  194. if (file.response) {
  195. file.url = file.response.data
  196. }
  197. return file
  198. })
  199. }
  200. //this.$message.success(`${info.file.name} 上传成功!`);
  201. } else if (info.file.status === 'error') {
  202. this.$message.error(`${info.file.name} 上传失败.`)
  203. } else if (info.file.status === 'removed') {
  204. this.handleDelete(info.file)
  205. }
  206. this.fileList = fileList
  207. if (info.file.status === 'done' || info.file.status === 'removed') {
  208. this.handlePathChange()
  209. }
  210. },
  211. // 预览
  212. handlePreview(file) {
  213. this.previewImage = file.url || file.thumbUrl
  214. this.previewVisible = true
  215. },
  216. getAvatarView() {
  217. if (this.fileList.length > 0) {
  218. let url = this.fileList[0].url
  219. return url
  220. }
  221. },
  222. handlePathChange() {
  223. let uploadFiles = this.fileList
  224. let path = ''
  225. if (!uploadFiles || uploadFiles.length == 0) {
  226. path = ''
  227. }
  228. let arr = []
  229. if (!this.isMultiple) {
  230. arr.push(uploadFiles[uploadFiles.length - 1].response.data)
  231. } else {
  232. for (var a = 0; a < uploadFiles.length; a++) {
  233. arr.push(uploadFiles[a].response.data)
  234. }
  235. }
  236. if (arr.length > 0) {
  237. path = arr.join(',')
  238. }
  239. this.$emit('change', path)
  240. },
  241. handleDelete(file) {
  242. //如有需要新增 删除逻辑
  243. console.log(file)
  244. },
  245. handleCancel() {
  246. this.close()
  247. this.previewVisible = false
  248. },
  249. close() {},
  250. },
  251. model: {
  252. prop: 'value',
  253. event: 'change',
  254. },
  255. }
  256. </script>
  257. <style scoped></style>