custom-wxwork-auth-guard.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { CanActivateFn, Router } from '@angular/router';
  2. import { inject } from '@angular/core';
  3. import { WxworkAuth, FmodeParse } from 'fmode-ng/core';
  4. /**
  5. * 自定义企微认证守卫
  6. * 🔥 修复:如果是从客服板块跳转,跳过激活检查,直接允许访问
  7. * 如果用户未激活,重定向到身份激活页面
  8. * 如果用户已激活,允许访问
  9. */
  10. export const CustomWxworkAuthGuard: CanActivateFn = async (route, state) => {
  11. const router = inject(Router);
  12. console.log('🔐 CustomWxworkAuthGuard 执行,当前路由:', state.url);
  13. try {
  14. // 🔥 检查是否从客服板块或组长看板进入(跳过激活检查)
  15. const fromCustomerService = localStorage.getItem('enterFromCustomerService');
  16. const customerServiceMode = localStorage.getItem('customerServiceMode');
  17. const teamLeaderMode = localStorage.getItem('teamLeaderMode');
  18. const enterAsTeamLeader = localStorage.getItem('enterAsTeamLeader');
  19. console.log('🔍 检查进入模式标记:', {
  20. enterFromCustomerService: fromCustomerService,
  21. customerServiceMode: customerServiceMode,
  22. teamLeaderMode: teamLeaderMode,
  23. enterAsTeamLeader: enterAsTeamLeader
  24. });
  25. // 如果是从客服板块进入(一次性标记)或处于客服模式(持久化标记),都跳过激活检查
  26. if (fromCustomerService === '1' || customerServiceMode === 'true') {
  27. console.log('✅ 客服模式,跳过激活检查,直接允许访问');
  28. // 清除一次性标记(customerServiceMode保留,用于权限控制)
  29. if (fromCustomerService === '1') {
  30. localStorage.removeItem('enterFromCustomerService');
  31. }
  32. return true;
  33. }
  34. // 🔥 新增:如果是从组长看板进入,也跳过激活检查
  35. if (teamLeaderMode === 'true' || enterAsTeamLeader === '1') {
  36. console.log('✅ 组长模式,跳过激活检查,直接允许访问');
  37. // 清除一次性标记(teamLeaderMode保留,用于权限控制)
  38. if (enterAsTeamLeader === '1') {
  39. localStorage.removeItem('enterAsTeamLeader');
  40. }
  41. return true;
  42. }
  43. // 🔥 新增:如果从 /team-leader 路径进入,也跳过激活检查
  44. if (typeof window !== 'undefined' && (
  45. window.location.pathname.includes('/team-leader/') ||
  46. document.referrer.includes('/team-leader/')
  47. )) {
  48. console.log('✅ 检测到组长路径,跳过激活检查,直接允许访问');
  49. return true;
  50. }
  51. // 获取 cid
  52. let cid = route.paramMap.get('cid') ||
  53. route.queryParamMap.get('cid') ||
  54. localStorage.getItem('company');
  55. if (cid) {
  56. localStorage.setItem('company', cid);
  57. }
  58. if (!cid) {
  59. console.error('❌ 缺少 cid 参数');
  60. return false;
  61. }
  62. // 测试模式:跳过企微认证,直接检查 Profile
  63. if (cid === 'test' || cid === 'demo') {
  64. console.log('🧪 测试模式:跳过企微认证');
  65. const Parse = FmodeParse.with('nova');
  66. const query = new Parse.Query('Profile');
  67. query.equalTo('userid', 'test_user_001');
  68. const profile = await query.first();
  69. if (!profile || !profile.get('isActivated')) {
  70. console.log('⚠️ 测试用户未激活,跳转到激活页面');
  71. await router.navigate(['/wxwork', cid, 'activation']);
  72. return false;
  73. }
  74. console.log('✅ 测试模式认证通过');
  75. return true;
  76. }
  77. // 生产模式:真实企微认证
  78. // 初始化 WxworkAuth
  79. const wxAuth = new WxworkAuth({ cid, appId: 'crm' });
  80. // 尝试获取用户信息
  81. let userInfo;
  82. try {
  83. userInfo = await wxAuth.getUserInfo();
  84. console.log('✅ 获取用户信息成功:', userInfo?.name);
  85. } catch (err) {
  86. console.log('⚠️ 需要授权,跳转到激活页面');
  87. // 需要授权,跳转到激活页面
  88. await router.navigate(['/wxwork', cid, 'activation']);
  89. return false;
  90. }
  91. if (!userInfo) {
  92. console.log('⚠️ 无用户信息,跳转到激活页面');
  93. await router.navigate(['/wxwork', cid, 'activation']);
  94. return false;
  95. }
  96. // 检查用户是否已激活
  97. const profile = await wxAuth.currentProfile();
  98. if (!profile || !profile.get('isActivated')) {
  99. console.log('⚠️ 用户未激活,跳转到激活页面');
  100. await router.navigate(['/wxwork', cid, 'activation']);
  101. return false;
  102. }
  103. // 用户已激活,自动登录
  104. await wxAuth.autoLogin(userInfo);
  105. console.log('✅ 认证通过');
  106. return true;
  107. } catch (error) {
  108. console.error('❌ 认证守卫错误:', error);
  109. return false;
  110. }
  111. };