custom-wxwork-auth-guard.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // 🔥 保存原始URL,激活后跳转回来
  72. localStorage.setItem('returnUrl', state.url);
  73. await router.navigate(['/wxwork', cid, 'activation']);
  74. return false;
  75. }
  76. console.log('✅ 测试模式认证通过');
  77. return true;
  78. }
  79. // 生产模式:真实企微认证
  80. // 初始化 WxworkAuth
  81. const wxAuth = new WxworkAuth({ cid, appId: 'crm' });
  82. // 尝试获取用户信息
  83. let userInfo;
  84. try {
  85. userInfo = await wxAuth.getUserInfo();
  86. console.log('✅ 获取用户信息成功:', {
  87. name: userInfo?.name,
  88. userid: userInfo?.userid,
  89. cid
  90. });
  91. } catch (err) {
  92. console.log('⚠️ 需要授权,跳转到激活页面');
  93. // 🔥 保存原始URL,激活后跳转回来
  94. localStorage.setItem('returnUrl', state.url);
  95. // 需要授权,跳转到激活页面
  96. await router.navigate(['/wxwork', cid, 'activation']);
  97. return false;
  98. }
  99. if (!userInfo) {
  100. console.log('⚠️ 无用户信息,跳转到激活页面');
  101. // 🔥 保存原始URL,激活后跳转回来
  102. localStorage.setItem('returnUrl', state.url);
  103. await router.navigate(['/wxwork', cid, 'activation']);
  104. return false;
  105. }
  106. // 检查用户是否已激活
  107. const profile = await wxAuth.currentProfile();
  108. console.log('🔎 currentProfile 查询结果:', {
  109. id: profile?.id,
  110. name: profile?.get?.('name'),
  111. realname: profile?.get?.('realname'),
  112. userid: profile?.get?.('userid'),
  113. isActivated: profile?.get?.('isActivated'),
  114. activatedAt: profile?.get?.('activatedAt')
  115. });
  116. if (!profile || !profile.get('isActivated')) {
  117. // 回退:直接按 userid 查询 Profile,避免因未登录或缓存导致的取数失败
  118. try {
  119. const Parse = FmodeParse.with('nova');
  120. const q = new Parse.Query('Profile');
  121. q.equalTo('userid', userInfo.userid);
  122. // 🔥 强制从服务器获取最新数据,不使用缓存
  123. const p2 = await q.first();
  124. console.log('🔎 回退 Profile 查询结果:', {
  125. userid查询: userInfo.userid,
  126. 找到Profile: !!p2,
  127. id: p2?.id,
  128. name: p2?.get?.('name'),
  129. realname: p2?.get?.('realname'),
  130. userid: p2?.get?.('userid'),
  131. isActivated: p2?.get?.('isActivated'),
  132. activatedAt: p2?.get?.('activatedAt'),
  133. createdAt: p2?.get?.('createdAt'),
  134. updatedAt: p2?.get?.('updatedAt')
  135. });
  136. if (p2 && p2.get('isActivated')) {
  137. // 已激活:执行自动登录并放行
  138. await wxAuth.autoLogin(userInfo);
  139. console.log('✅ 回退校验通过(已激活),允许访问');
  140. return true;
  141. }
  142. // 🔥 新增:如果Profile存在但isActivated为false/undefined,输出详细信息
  143. if (p2) {
  144. console.error('❌ Profile存在但未激活:', {
  145. profileId: p2.id,
  146. isActivated: p2.get('isActivated'),
  147. '所有字段': {
  148. name: p2.get('name'),
  149. realname: p2.get('realname'),
  150. userid: p2.get('userid'),
  151. roleName: p2.get('roleName'),
  152. mobile: p2.get('mobile'),
  153. isActivated: p2.get('isActivated'),
  154. isDisabled: p2.get('isDisabled'),
  155. isDeleted: p2.get('isDeleted')
  156. }
  157. });
  158. } else {
  159. console.error('❌ 未找到Profile记录, userid:', userInfo.userid);
  160. }
  161. } catch (e) {
  162. console.error('⚠️ 回退 Profile 查询异常:', e);
  163. }
  164. console.log('⚠️ 用户未激活,跳转到激活页面');
  165. // 🔥 保存原始URL,激活后跳转回来
  166. localStorage.setItem('returnUrl', state.url);
  167. await router.navigate(['/wxwork', cid, 'activation']);
  168. return false;
  169. }
  170. // 用户已激活,自动登录
  171. await wxAuth.autoLogin(userInfo);
  172. console.log('✅ 认证通过');
  173. return true;
  174. } catch (error) {
  175. console.error('❌ 认证守卫错误:', error);
  176. return false;
  177. }
  178. };