chenyc
2025-12-09 b38f8abd8a9865148792f4bc996c461211b88561
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * 后端 API 集成模块
 * 用于与后端服务通信
 */
 
export class ApiClient {
    constructor(baseUrl = '/api') {
        this.baseUrl = baseUrl;
    }
 
    /**
     * 通用 GET 请求
     */
    async get(endpoint) {
        try {
            const response = await fetch(`${this.baseUrl}${endpoint}`, {
                method: 'GET',
                headers: {
                    'Content-Type': 'application/json'
                }
            });
            return await this.handleResponse(response);
        } catch (error) {
            console.error('GET 请求失败:', error);
            throw error;
        }
    }
 
    /**
     * 通用 POST 请求
     */
    async post(endpoint, data) {
        try {
            const response = await fetch(`${this.baseUrl}${endpoint}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            });
            return await this.handleResponse(response);
        } catch (error) {
            console.error('POST 请求失败:', error);
            throw error;
        }
    }
 
    /**
     * 处理响应
     */
    async handleResponse(response) {
        if (!response.ok) {
            throw new Error(`HTTP error! status: ${response.status}`);
        }
        return await response.json();
    }
 
    /**
     * 使用授权码获取用户信息
     * @param {string} code - 企业微信授权码
     */
    async getUserInfoByCode(code) {
        return this.post('/wecom/getUserInfo', { code });
    }
 
    /**
     * 获取企业微信用户详情
     * @param {string} userId - 用户ID
     */
    async getWeChatUserDetail(userId) {
        return this.get(`/wecom/userDetail/${userId}`);
    }
 
    /**
     * 验证用户是否具有权限
     * @param {string} userId - 用户ID
     */
    async verifyUserPermission(userId) {
        return this.get(`/wecom/verify/${userId}`);
    }
}
 
export default ApiClient;