JUpload.vue 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <a-upload
  3. name="file"
  4. :multiple="true"
  5. :action="uploadAction"
  6. :headers="headers"
  7. :data="{'biz':bizPath}"
  8. :fileList="fileList"
  9. :beforeUpload="beforeUpload"
  10. @change="handleChange"
  11. :disabled="disabled"
  12. :returnUrl="returnUrl">
  13. <a-button>
  14. <a-icon type="upload" />{{ text }}
  15. </a-button>
  16. </a-upload>
  17. </template>
  18. <script>
  19. import Vue from 'vue'
  20. import { ACCESS_TOKEN } from "@/store/mutation-types"
  21. import { getFileAccessHttpUrl } from '@/api/manage';
  22. const FILE_TYPE_ALL = "all"
  23. const FILE_TYPE_IMG = "image"
  24. const FILE_TYPE_TXT = "file"
  25. const uidGenerator=()=>{
  26. return '-'+parseInt(Math.random()*10000+1,10);
  27. }
  28. const getFileName=(path)=>{
  29. if(path.lastIndexOf("\\")>=0){
  30. let reg=new RegExp("\\\\","g");
  31. path = path.replace(reg,"/");
  32. }
  33. return path.substring(path.lastIndexOf("/")+1);
  34. }
  35. export default {
  36. name: 'JUpload',
  37. data(){
  38. return {
  39. uploadAction:window._CONFIG['domianURL']+"/sys/common/upload",
  40. urlDownload:window._CONFIG['staticDomainURL'],
  41. headers:{},
  42. fileList: [],
  43. newFileList: [],
  44. }
  45. },
  46. props:{
  47. text:{
  48. type:String,
  49. required:false,
  50. default:"点击上传"
  51. },
  52. fileType:{
  53. type:String,
  54. required:false,
  55. default:FILE_TYPE_ALL
  56. },
  57. /*这个属性用于控制文件上传的业务路径*/
  58. bizPath:{
  59. type:String,
  60. required:false,
  61. default:"temp"
  62. },
  63. value:{
  64. type:[String,Array],
  65. required:false
  66. },
  67. // update-begin- --- author:wangshuai ------ date:20190929 ---- for:Jupload组件增加是否能够点击
  68. disabled:{
  69. type:Boolean,
  70. required:false,
  71. default: false
  72. },
  73. // update-end- --- author:wangshuai ------ date:20190929 ---- for:Jupload组件增加是否能够点击
  74. //此属性被废弃了
  75. triggerChange:{
  76. type: Boolean,
  77. required: false,
  78. default: false
  79. },
  80. /**
  81. * update -- author:lvdandan -- date:20190219 -- for:Jupload组件增加是否返回url,
  82. * true:仅返回url
  83. * false:返回fileName filePath fileSize
  84. */
  85. returnUrl:{
  86. type:Boolean,
  87. required:false,
  88. default: true
  89. },
  90. },
  91. watch:{
  92. value(val){
  93. if (val instanceof Array) {
  94. if(this.returnUrl){
  95. this.initFileList(val.join(','))
  96. }else{
  97. this.initFileListArr(val);
  98. }
  99. } else {
  100. this.initFileList(val)
  101. }
  102. }
  103. },
  104. created(){
  105. const token = Vue.ls.get(ACCESS_TOKEN);
  106. this.headers = {"X-Access-Token":token}
  107. },
  108. methods:{
  109. initFileListArr(val){
  110. if(!val || val.length==0){
  111. this.fileList = [];
  112. return;
  113. }
  114. let fileList = [];
  115. for(var a=0;a<val.length;a++){
  116. fileList.push({
  117. uid:uidGenerator(),
  118. name:val[a].fileName,
  119. status: 'done',
  120. url: val[a].filePath,
  121. response:{
  122. status:"history",
  123. message:val[a].filePath
  124. }
  125. })
  126. }
  127. this.fileList = fileList
  128. },
  129. initFileList(paths){
  130. if(!paths || paths.length==0){
  131. //return [];
  132. // update-begin- --- author:os_chengtgen ------ date:20190729 ---- for:issues:326,Jupload组件初始化bug
  133. this.fileList = [];
  134. return;
  135. // update-end- --- author:os_chengtgen ------ date:20190729 ---- for:issues:326,Jupload组件初始化bug
  136. }
  137. let fileList = [];
  138. let arr = paths.split(",")
  139. for(var a=0;a<arr.length;a++){
  140. let url = getFileAccessHttpUrl(arr[a],this.urlDownload,"http");
  141. fileList.push({
  142. uid:uidGenerator(),
  143. name:getFileName(arr[a]),
  144. status: 'done',
  145. url: url,
  146. response:{
  147. status:"history",
  148. message:arr[a]
  149. }
  150. })
  151. }
  152. this.fileList = fileList
  153. },
  154. handlePathChange(){
  155. let uploadFiles = this.fileList
  156. let path = ''
  157. if(!uploadFiles || uploadFiles.length==0){
  158. path = ''
  159. }
  160. let arr = [];
  161. for(var a=0;a<uploadFiles.length;a++){
  162. arr.push(uploadFiles[a].response.message)
  163. }
  164. if(arr.length>0){
  165. path = arr.join(",")
  166. }
  167. this.$emit('change', path);
  168. },
  169. beforeUpload(file){
  170. var type = file.type;
  171. if(this.fileType===FILE_TYPE_IMG){
  172. if(type.indexOf('image')<0){
  173. this.$message.warning('请上传图片');
  174. return false;
  175. }
  176. }else if(this.fileType===FILE_TYPE_TXT){
  177. if(type.indexOf('image')>=0){
  178. this.$message.warning('请上传文件');
  179. return false;
  180. }
  181. }
  182. //TODO 扩展功能验证文件大小
  183. return true
  184. },
  185. handleChange(info) {
  186. console.log("--文件列表改变--")
  187. let fileList = info.fileList
  188. if(info.file.status==='done'){
  189. if(info.file.response.success){
  190. fileList = fileList.map((file) => {
  191. if (file.response) {
  192. let reUrl = file.response.message;
  193. file.url = getFileAccessHttpUrl(reUrl,this.urlDownload,"http");
  194. }
  195. return file;
  196. });
  197. }
  198. //this.$message.success(`${info.file.name} 上传成功!`);
  199. }else if (info.file.status === 'error') {
  200. this.$message.error(`${info.file.name} 上传失败.`);
  201. }else if(info.file.status === 'removed'){
  202. this.handleDelete(info.file)
  203. }
  204. this.fileList = fileList
  205. if(info.file.status==='done' || info.file.status === 'removed'){
  206. //returnUrl为true时仅返回文件路径
  207. if(this.returnUrl){
  208. this.handlePathChange()
  209. }else{
  210. //returnUrl为false时返回文件名称、文件路径及文件大小
  211. fileList = fileList.filter((file) => {
  212. if (file.response) {
  213. return file.response.success === true;
  214. }
  215. return false;
  216. }).map((file) => {
  217. var fileJson = {
  218. fileName:file.name,
  219. filePath:file.response.message,
  220. fileSize:file.size
  221. };
  222. this.newFileList.push(fileJson);
  223. this.$emit('change', this.newFileList);
  224. });
  225. }
  226. }
  227. },
  228. handleDelete(file){
  229. //如有需要新增 删除逻辑
  230. console.log(file)
  231. },
  232. },
  233. model: {
  234. prop: 'value',
  235. event: 'change'
  236. }
  237. }
  238. </script>
  239. <style scoped>
  240. </style>