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
/**
 * 企业微信配置文件
 * 需要根据实际应用进行修改
 */
 
export const WECOM_CONFIG = {
    // 企业ID - 从企业微信管理后台获取
    corpId: 'ww55d5f3230808ad80',
    
    // 应用ID - 从应用详情获取
    agentId: '1000019',
    
    // 回调地址 - 用户授权后会跳转到该地址
    // 必须在企业微信应用配置中添加为白名单域名
    redirectUri: `${window.location.origin}/`,
    
    // API 基础 URL(如果有后端服务)
    apiBaseUrl: '/api'
};
 
/**
 * 获取企业微信授权链接
 * 使用方式:
 * 1. 在企业微信管理后台配置应用
 * 2. 填入上述配置信息
 * 3. 将用户重定向到 getWeChatAuthUrl() 返回的链接
 * 4. 企业微信会将用户重定向回 redirectUri,并携带 code 参数
 * 5. 在回调页面中使用 code 调用后端 API 获取用户信息
 */
export function getWeChatAuthUrl(config) {
    const params = new URLSearchParams({
        appid: config.corpId,
        redirect_uri: config.redirectUri,
        response_type: 'code',
        scope: 'snsapi_userinfo',
        state: generateRandomState()
    });
    return `https://open.weixin.qq.com/connect/oauth2/authorize?${params.toString()}#wechat_redirect`;
}
 
/**
 * 生成随机 state 参数防止 CSRF 攻击
 */
function generateRandomState() {
    const state = Math.random().toString(36).substring(2, 15);
    sessionStorage.setItem('oauth_state', state);
    return state;
}
 
export default WECOM_CONFIG;