handle.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. import Status from "$mixins/status/const/status";
  2. import filePath from '$utils/tool/file';
  3. import compress from "$utils/tool/compress";
  4. import $request from '$utils/request';
  5. export default <LibMixins>{
  6. data(){
  7. return {
  8. vSrc:'',
  9. status_fetch_key:'upload-next',
  10. Status
  11. }
  12. },
  13. created() {
  14. this.setSrc(this.src);
  15. },
  16. methods:{
  17. triggerFile(type:string){
  18. if (this.status === Status.loading) return ;
  19. // 如果点击类型为图片且 不允许点击图片上传触发
  20. if (type === 'image' && !this.triggerImage) return;
  21. return this.$refs.file && this.$refs.file.open();
  22. },
  23. setSrc(src){
  24. if (src !== this.vSrc) {
  25. this.vSrc = src;
  26. }
  27. },
  28. uploadFile(file,retry=false) {
  29. if (this.status === Status.loading) return ;
  30. // 设置当前状态
  31. this.setStatus(Status.loading);
  32. // 获取文件
  33. file = file[0];
  34. // 暂存文件
  35. this.storageFile = file;
  36. // 清空路径
  37. this.uploadItem = null;
  38. if (retry) {
  39. // 如果设置图片压缩触发
  40. return this.triggerUpload(file);
  41. } else {
  42. filePath.createdPath(file).then((src)=>{
  43. // 设置图片路径
  44. this.setSrc(src);
  45. // 如果设置图片压缩触发
  46. if (this.compress) {
  47. return this.nextCompress(file,src);
  48. } else {
  49. return this.triggerUpload(file);
  50. }
  51. }).catch(()=>{
  52. this.setSrc('');
  53. return this.triggerUpload(file)
  54. });
  55. }
  56. },
  57. // 执行压缩操作
  58. nextCompress(file,src){
  59. compress.compress(file,src).then((params:Record<string, any>)=>{
  60. this.storageFile = params.file;
  61. return this.triggerUpload(params.file);
  62. }).catch(()=> this.triggerUpload(file));
  63. },
  64. // 执行上传操作
  65. triggerUpload(file){
  66. $request.uploadFile({
  67. url:'hxupload/img_upload',
  68. data:{
  69. file
  70. },
  71. token:true
  72. }).then((data)=>{
  73. if (data.isSuccess) {
  74. this.triggerUploadSuccess(data);
  75. } else {
  76. return this.setStatus(Status.fail)
  77. }
  78. }).catch(()=> this.setStatus(Status.fail));
  79. },
  80. triggerUploadSuccess(data){
  81. this.uploadItem = data;
  82. !this.vSrc && this.setSrc(this.uploadItem.data);
  83. console.log(this);
  84. if (this.$attrs.onUploadNext) {
  85. return this.statusFetch(true,data.data);
  86. } else {
  87. this.$emit('upload-success',data.data);
  88. return this.setStatus(Status.success);
  89. }
  90. },
  91. triggerRetry(){
  92. if (this.status === Status.loading) return;
  93. if (this.uploadItem) {
  94. this.setStatus(Status.loading);
  95. return this.triggerUploadSuccess(this.uploadItem);
  96. } else {
  97. return this.uploadFile([this.storageFile],true);
  98. }
  99. },
  100. triggerChangeStatus(status) {
  101. if (status === Status.success) {
  102. this.storageFile = null;
  103. }
  104. }
  105. }
  106. }