useAutoLogin.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import QueryString from 'qs';
  2. import { useUserStore } from '/@/store/modules/user';
  3. import { RouteLocationNormalized } from 'vue-router';
  4. import { useMessage } from '../web/useMessage';
  5. import { AUTO_LOGIN_URL_QUERY } from '/@/router/constant';
  6. import { AxiosError } from 'axios';
  7. import { tokenLogin, getUserInfo } from '/@/api/sys/user';
  8. import { isArray } from '/@/utils/is';
  9. import { RoleEnum } from '/@/enums/roleEnum';
  10. /** 自动登录功能的Hook,该Hook是为了部署在同一局域网内的多套系统之间能够无缝切换 */
  11. export function useAutoLogin() {
  12. const userStore = useUserStore();
  13. /** 获取自动登录的url */
  14. function getUrl(baseUrl: string, extraQuery: Record<string, string> = {}) {
  15. const qs = QueryString.stringify({
  16. [AUTO_LOGIN_URL_QUERY.key]: AUTO_LOGIN_URL_QUERY.val,
  17. realname: userStore.getUserInfo.realname,
  18. workNo: userStore.getUserInfo.workNo,
  19. ...extraQuery,
  20. });
  21. return `${baseUrl}?${qs}`;
  22. }
  23. /** 启用自动登录功能来跳转新的页面 */
  24. function open(url: string, target?: string) {
  25. window.open(getUrl(url), target);
  26. }
  27. /** 判断当前路由是否需要自动登录,本方法是同步方法,可以用于事先判断 */
  28. function validateRoute(route: RouteLocationNormalized) {
  29. if (route.query && route.query[AUTO_LOGIN_URL_QUERY.key] === AUTO_LOGIN_URL_QUERY.val) {
  30. return true;
  31. }
  32. return false;
  33. }
  34. /** 根据路由是否携带token,判断是否走根据token获取人员信息的接口试下假登录 */
  35. function tokenValidateRoute(route: RouteLocationNormalized) {
  36. if (route.query && Object.keys(route.query).length == 1 && route.query['token']) {
  37. return true;
  38. }
  39. return false;
  40. }
  41. /**
  42. * 用在路由守卫里,执行自动登录的逻辑,如果存在符合自动登录标准的query则执行自动登录,返回是否自动登录
  43. *
  44. * 该方法需要修改query
  45. * */
  46. async function doAutoLogin(route: RouteLocationNormalized): Promise<void> {
  47. if (!validateRoute(route)) return;
  48. const { realname, workNo } = route.query;
  49. if (!realname || !workNo) return;
  50. const params = {
  51. username: realname as string,
  52. workNo: workNo as string,
  53. checkKey: new Date().getTime(),
  54. };
  55. const userStore = useUserStore();
  56. try {
  57. await userStore.autoLogin({
  58. ...params,
  59. goHome: false,
  60. });
  61. delete route.query[AUTO_LOGIN_URL_QUERY.key];
  62. delete route.query['realname'];
  63. delete route.query['workNo'];
  64. return;
  65. } catch (e) {
  66. const message = useMessage().createMessage;
  67. message.error((e as AxiosError).message);
  68. throw e;
  69. }
  70. }
  71. // token 登录
  72. async function doTokenLogin(token: string) {
  73. const res = await tokenLogin({ token });
  74. userStore.setUserInfo(res['userInfo']);
  75. userStore.setAllDictItems(res['sysAllDictItems']);
  76. userStore.setToken(res['token']);
  77. }
  78. return {
  79. /** 启用单点登录功能来跳转新的页面,参数与window.open一致 */
  80. open,
  81. /** 用在跳转到的页面上,执行单点登录的逻辑 */
  82. doAutoLogin,
  83. /** 获取自动登录的url */
  84. getUrl,
  85. /** 判断当前路由是否需要自动登录,本方法是同步方法,可以用于事先判断 */
  86. validateRoute,
  87. tokenValidateRoute,
  88. doTokenLogin,
  89. };
  90. }