custom-wxwork-auth-guard.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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('✅ 获取用户信息成功:', {
  85. name: userInfo?.name,
  86. userid: userInfo?.userid,
  87. cid
  88. });
  89. } catch (err) {
  90. console.log('⚠️ 需要授权,跳转到激活页面');
  91. // 需要授权,跳转到激活页面
  92. await router.navigate(['/wxwork', cid, 'activation']);
  93. return false;
  94. }
  95. if (!userInfo) {
  96. console.log('⚠️ 无用户信息,跳转到激活页面');
  97. await router.navigate(['/wxwork', cid, 'activation']);
  98. return false;
  99. }
  100. // 检查用户是否已激活
  101. const profile = await wxAuth.currentProfile();
  102. console.log('🔎 currentProfile 查询结果:', {
  103. id: profile?.id,
  104. isActivated: profile?.get?.('isActivated')
  105. });
  106. if (!profile || !profile.get('isActivated')) {
  107. // 回退:直接按 userid 查询 Profile,避免因未登录或缓存导致的取数失败
  108. try {
  109. const Parse = FmodeParse.with('nova');
  110. const q = new Parse.Query('Profile');
  111. q.equalTo('userid', userInfo.userid);
  112. const p2 = await q.first();
  113. console.log('🔎 回退 Profile 查询结果:', {
  114. id: p2?.id,
  115. isActivated: p2?.get?.('isActivated')
  116. });
  117. if (p2 && p2.get('isActivated')) {
  118. // 已激活:执行自动登录并放行
  119. await wxAuth.autoLogin(userInfo);
  120. console.log('✅ 回退校验通过(已激活),允许访问');
  121. return true;
  122. }
  123. } catch (e) {
  124. console.warn('⚠️ 回退 Profile 查询异常:', e);
  125. }
  126. console.log('⚠️ 用户未激活,跳转到激活页面');
  127. await router.navigate(['/wxwork', cid, 'activation']);
  128. return false;
  129. }
  130. // 用户已激活,自动登录
  131. await wxAuth.autoLogin(userInfo);
  132. console.log('✅ 认证通过');
  133. return true;
  134. } catch (error) {
  135. console.error('❌ 认证守卫错误:', error);
  136. return false;
  137. }
  138. };