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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
 * Node.js + Express 企业微信 OAuth 后端示例
 * 
 * 安装依赖:
 * npm install express axios cors dotenv morgan
 * 
 * 启动服务:
 * node server.js
 */
 
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const axios = require('axios');
const path = require('path');
 
const app = express();
 
// 中间件
app.use(cors());
app.use(morgan('combined'));
app.use(express.json());
app.use(express.static(path.join(__dirname, '../dist')));
 
// 企业微信配置
const WECOM_CONFIG = {
    corpId: process.env.WECOM_CORP_ID,
    appSecret: process.env.WECOM_APP_SECRET,
    agentId: process.env.WECOM_AGENT_ID
};
 
// 缓存 access_token
let accessTokenCache = {
    token: null,
    expireTime: 0
};
 
/**
 * 获取企业微信访问令牌
 */
async function getAccessToken() {
    const now = Date.now();
    
    // 如果 token 未过期,直接返回缓存
    if (accessTokenCache.token && accessTokenCache.expireTime > now) {
        return accessTokenCache.token;
    }
 
    try {
        const response = await axios.get('https://qyapi.weixin.qq.com/cgi-bin/gettoken', {
            params: {
                corpid: WECOM_CONFIG.corpId,
                corpsecret: WECOM_CONFIG.appSecret
            }
        });
 
        if (response.data.errcode === 0) {
            const token = response.data.access_token;
            const expiresIn = response.data.expires_in || 7200;
            
            accessTokenCache = {
                token: token,
                expireTime: now + (expiresIn - 300) * 1000 // 提前5分钟刷新
            };
 
            return token;
        } else {
            throw new Error(`获取 token 失败: ${response.data.errmsg}`);
        }
    } catch (error) {
        console.error('获取 access_token 错误:', error.message);
        throw error;
    }
}
 
/**
 * API: 使用授权码获取用户信息
 */
app.post('/api/wecom/getUserInfo', async (req, res) => {
    const { code } = req.body;
 
    if (!code) {
        return res.status(400).json({
            code: -1,
            message: '缺少授权码'
        });
    }
 
    try {
        const accessToken = await getAccessToken();
 
        // 获取用户信息
        const response = await axios.get('https://qyapi.weixin.qq.com/cgi-bin/auth/getuserdetail', {
            params: {
                access_token: accessToken,
                code: code
            }
        });
 
        if (response.data.errcode === 0) {
            const userData = {
                userid: response.data.userid,
                name: response.data.name,
                mobile: response.data.mobile || '',
                email: response.data.email || '',
                department: (response.data.department && response.data.department[0]) || '',
                position: response.data.position || '',
                gender: response.data.gender || 0,
                avatar: response.data.avatar || '',
                extattr: response.data.extattr || {}
            };
 
            // 可选: 将用户信息存储到数据库
            // await saveUserToDatabase(userData);
 
            res.json({
                code: 0,
                message: 'success',
                data: userData
            });
        } else {
            res.status(401).json({
                code: response.data.errcode,
                message: response.data.errmsg || '获取用户信息失败'
            });
        }
    } catch (error) {
        console.error('获取用户信息错误:', error.message);
        res.status(500).json({
            code: -1,
            message: '获取用户信息失败: ' + error.message
        });
    }
});
 
/**
 * API: 获取用户详情
 */
app.get('/api/wecom/userDetail/:userId', async (req, res) => {
    const { userId } = req.params;
 
    try {
        const accessToken = await getAccessToken();
 
        const response = await axios.get('https://qyapi.weixin.qq.com/cgi-bin/user/get', {
            params: {
                access_token: accessToken,
                userid: userId
            }
        });
 
        if (response.data.errcode === 0) {
            res.json({
                code: 0,
                message: 'success',
                data: response.data
            });
        } else {
            res.status(404).json({
                code: response.data.errcode,
                message: response.data.errmsg
            });
        }
    } catch (error) {
        console.error('获取用户详情错误:', error.message);
        res.status(500).json({
            code: -1,
            message: '获取用户详情失败'
        });
    }
});
 
/**
 * API: 验证用户权限
 */
app.get('/api/wecom/verify/:userId', async (req, res) => {
    const { userId } = req.params;
 
    try {
        // 实现自己的权限验证逻辑
        // 例如: 检查用户是否在允许的部门列表中
        const authorized = await checkUserPermission(userId);
 
        res.json({
            code: 0,
            message: 'success',
            data: {
                authorized: authorized
            }
        });
    } catch (error) {
        console.error('验证用户权限错误:', error.message);
        res.status(500).json({
            code: -1,
            message: '验证失败'
        });
    }
});
 
/**
 * API: 同步用户到本地系统
 */
app.post('/api/user/sync', async (req, res) => {
    const { userid, name, mobile } = req.body;
 
    try {
        // 实现自己的用户同步逻辑
        // 例如: 保存到数据库、更新缓存等
 
        console.log(`同步用户: ${userid}, ${name}, ${mobile}`);
 
        res.json({
            code: 0,
            message: 'success',
            data: {
                synced: true
            }
        });
    } catch (error) {
        console.error('同步用户错误:', error.message);
        res.status(500).json({
            code: -1,
            message: '同步用户失败'
        });
    }
});
 
/**
 * 健康检查
 */
app.get('/api/health', (req, res) => {
    res.json({
        code: 0,
        message: 'OK',
        timestamp: new Date().toISOString()
    });
});
 
/**
 * SPA 路由处理
 */
app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, '../dist/index.html'));
});
 
/**
 * 错误处理中间件
 */
app.use((error, req, res, next) => {
    console.error('未捕获的错误:', error);
    res.status(500).json({
        code: -1,
        message: '服务器内部错误'
    });
});
 
/**
 * 权限检查辅助函数 (示例)
 */
async function checkUserPermission(userId) {
    // 实现你的权限验证逻辑
    // 例如:
    // 1. 检查用户是否在允许列表中
    // 2. 检查用户的部门是否被授权
    // 3. 查询数据库的权限配置
 
    // 这里返回 true 作为示例
    return true;
}
 
/**
 * 数据库保存函数 (示例)
 */
async function saveUserToDatabase(userData) {
    // 实现保存到数据库的逻辑
    // 例如使用 MongoDB:
    // const user = new User(userData);
    // await user.save();
    console.log('保存用户到数据库:', userData);
}
 
// 启动服务器
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`服务器运行在 http://localhost:${PORT}`);
    console.log(`企业微信配置:`);
    console.log(`  CorpID: ${WECOM_CONFIG.corpId}`);
    console.log(`  AgentID: ${WECOM_CONFIG.agentId}`);
});
 
module.exports = app;