example.ts
1.07 KB
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
47
48
49
50
51
52
53
54
55
/**
* API 使用示例
* 展示如何在子应用中使用 GSC Request 发起请求
*/
import { gscPost, gscGet } from '@/utils/gscRequest';
/**
* 示例:用户登录
*/
export async function login(username: string, password: string) {
try {
const response = await gscPost('/login', {
username,
password,
});
return response;
} catch (error) {
console.error('登录失败:', error);
throw error;
}
}
/**
* 示例:获取用户信息
*/
export async function getUserInfo() {
try {
const response = await gscGet('/user/info');
return response;
} catch (error) {
console.error('获取用户信息失败:', error);
throw error;
}
}
/**
* 示例:带配置的 POST 请求
*/
export async function createData(data: any) {
try {
const response = await gscPost(
'/api/data',
data,
{
returnAll: true, // 返回完整响应
timeout: 10000, // 超时时间
},
);
return response;
} catch (error) {
console.error('创建数据失败:', error);
throw error;
}
}