zhuoyuan.wang
2024-06-19 15ebe96f28cadec6a726c5324593a40bbf56205f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import {request} from '@/plugins';
import axios from 'axios';
 
import {auth} from '@/plugins';
import config from '/config';
 
 
const getFileText = async (id) => {
  const {data} = await request({
    url: `/root/file/download?id=${id}`,
    method: 'get',
    responseType: 'blob'
  })
 
  const reader = new FileReader();
  reader.readAsText(data, 'UTF-8');
  return new Promise((resolve) => {
    reader.onload = ({target}) => {
      resolve(target.result);
    };
  });
 
}
 
const downloadFile = async (id) => {
  const {data} = await request({
    url: `/root/file/download?id=${id}&&isSrc=true`,
  })
  return data.path.replace('root',`${config.baseUrl}source`);
}
 
const uploadFile = async (formData) => {
  const {data} = await axios.post(`${config.baseUrl}worder/file/exec?userId=${auth.getToken()}`, formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  });
  return data.data.ids;
}
 
 
export default {
  getFileText,
  uploadFile,
  downloadFile
}