JUpload.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <template>
  2. <a-upload
  3. name="file"
  4. :multiple="true"
  5. :action="uploadAction"
  6. :headers="headers"
  7. :data="{'isup':1,'bizPath':bizPath}"
  8. :fileList="fileList"
  9. :beforeUpload="beforeUpload"
  10. @change="handleChange">
  11. <a-button>
  12. <a-icon type="upload" />{{ text }}
  13. </a-button>
  14. </a-upload>
  15. </template>
  16. <script>
  17. import Vue from 'vue'
  18. import { ACCESS_TOKEN } from "@/store/mutation-types"
  19. const FILE_TYPE_ALL = "all"
  20. const FILE_TYPE_IMG = "image"
  21. const FILE_TYPE_TXT = "file"
  22. const uidGenerator=()=>{
  23. return '-'+parseInt(Math.random()*10000+1,10);
  24. }
  25. const getFileName=(path)=>{
  26. if(path.lastIndexOf("\\")>=0){
  27. let reg=new RegExp("\\\\","g");
  28. path = path.replace(reg,"/");
  29. }
  30. return path.substring(path.lastIndexOf("/")+1);
  31. }
  32. export default {
  33. name: 'JUpload',
  34. data(){
  35. return {
  36. uploadAction:window._CONFIG['domianURL']+"/sys/common/upload",
  37. urlDownload:window._CONFIG['domianURL'] + "/sys/common/download/",
  38. headers:{},
  39. fileList: []
  40. }
  41. },
  42. props:{
  43. text:{
  44. type:String,
  45. required:false,
  46. default:"点击上传"
  47. },
  48. fileType:{
  49. type:String,
  50. required:false,
  51. default:FILE_TYPE_ALL
  52. },
  53. /*这个属性用于控制文件上传的业务路径*/
  54. bizPath:{
  55. type:String,
  56. required:false,
  57. default:"temp"
  58. },
  59. value:{
  60. type:String,
  61. required:false
  62. },
  63. //此属性被废弃了
  64. triggerChange:{
  65. type: Boolean,
  66. required: false,
  67. default: false
  68. },
  69. },
  70. watch:{
  71. value(val){
  72. this.initFileList(val)
  73. }
  74. },
  75. created(){
  76. const token = Vue.ls.get(ACCESS_TOKEN);
  77. this.headers = {"X-Access-Token":token}
  78. },
  79. methods:{
  80. initFileList(paths){
  81. if(!paths || paths.length==0){
  82. //return [];
  83. // update-begin- --- author:os_chengtgen ------ date:20190729 ---- for:issues:326,Jupload组件初始化bug
  84. this.fileList = [];
  85. return;
  86. // update-end- --- author:os_chengtgen ------ date:20190729 ---- for:issues:326,Jupload组件初始化bug
  87. }
  88. let fileList = [];
  89. let arr = paths.split(",")
  90. for(var a=0;a<arr.length;a++){
  91. fileList.push({
  92. uid:uidGenerator(),
  93. name:getFileName(arr[a]),
  94. status: 'done',
  95. url: this.urlDownload+arr[a],
  96. response:{
  97. status:"history",
  98. message:arr[a]
  99. }
  100. })
  101. }
  102. this.fileList = fileList
  103. },
  104. handlePathChange(){
  105. let uploadFiles = this.fileList
  106. let path = ''
  107. if(!uploadFiles || uploadFiles.length==0){
  108. path = ''
  109. }
  110. let arr = [];
  111. for(var a=0;a<uploadFiles.length;a++){
  112. arr.push(uploadFiles[a].response.message)
  113. }
  114. if(arr.length>0){
  115. path = arr.join(",")
  116. }
  117. this.$emit('change', path);
  118. },
  119. beforeUpload(file){
  120. var fileType = file.type;
  121. if(fileType===FILE_TYPE_IMG){
  122. if(fileType.indexOf('image')<0){
  123. this.$message.warning('请上传图片');
  124. return false;
  125. }
  126. }else if(fileType===FILE_TYPE_TXT){
  127. if(fileType.indexOf('image')>=0){
  128. this.$message.warning('请上传文件');
  129. return false;
  130. }
  131. }
  132. //TODO 扩展功能验证文件大小
  133. return true
  134. },
  135. handleChange(info) {
  136. console.log("--文件列表改变--")
  137. let fileList = info.fileList
  138. if(info.file.status==='done'){
  139. if(info.file.response.success){
  140. fileList = fileList.map((file) => {
  141. if (file.response) {
  142. file.url = this.urlDownload+file.response.message;
  143. }
  144. return file;
  145. });
  146. }
  147. this.$message.success(`${info.file.name} 上传成功!`);
  148. }else if (info.file.status === 'error') {
  149. this.$message.error(`${info.file.name} 上传失败.`);
  150. }else if(info.file.status === 'removed'){
  151. this.handleDelete(info.file)
  152. }
  153. this.fileList = fileList
  154. if(info.file.status==='done' || info.file.status === 'removed'){
  155. this.handlePathChange()
  156. }
  157. },
  158. handleDelete(file){
  159. //如有需要新增 删除逻辑
  160. console.log(file)
  161. },
  162. },
  163. model: {
  164. prop: 'value',
  165. event: 'change'
  166. }
  167. }
  168. </script>
  169. <style scoped>
  170. </style>