auth.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import request from '@/utils/request';
  2. import config from '@/config'
  3. const TokenKey = 'App-Token'
  4. export const isLoginSso = config.VITE_SSO_LOGIN
  5. export function getToken() {
  6. return uni.getStorageSync(TokenKey)
  7. }
  8. export function setToken(token) {
  9. return uni.setStorageSync(TokenKey, token)
  10. }
  11. export function removeToken() {
  12. return uni.removeStorageSync(TokenKey)
  13. }
  14. const SYS_CODE = 'XXLSSO';
  15. // 单点登录Token
  16. const SsoTokenKey = 'Sso-Admin-Token';
  17. // 获取单点登录Token
  18. export function getSsoAdminToken() {
  19. return uni.getStorageSync(SsoTokenKey);
  20. };
  21. // 设置单点登录Token
  22. export function setSsoAdminToken(token) {
  23. return uni.setStorageSync(SsoTokenKey, token);
  24. };
  25. // 删除单点登录Token
  26. export function removeSsoAdminToken() {
  27. return uni.removeStorageSync(SsoTokenKey);
  28. };
  29. // 当前环境
  30. // const VITE_APP_ENV = import.meta.env.VITE_APP_ENV;
  31. const VITE_APP_ENV = 'beta'
  32. function getEnv() {
  33. // let jumpEnv = 'http://localhost:88';
  34. // let curEnv = 'http://localhost:81/pages';
  35. // let suffix = 'localhost';
  36. // if (VITE_APP_ENV == 'production') {
  37. // // 生产环境
  38. let jumpEnv = 'https://sso.huaxiazhizao.com';
  39. let curEnv = 'https://tools-m.huaxiazhizao.com/pages';
  40. let suffix = '.huaxiazhizao.com';
  41. // } else if (VITE_APP_ENV == 'beta') {
  42. // // 测试环境
  43. // jumpEnv = 'https://sso.huabaosmart.cn';
  44. // curEnv = 'https://sundry.huabaosmart.cn';
  45. // suffix = '.huabaosmart.cn';
  46. // };
  47. return { jumpEnv, curEnv, suffix };
  48. };
  49. /**
  50. * 获取当前用户 Token
  51. * @param {*} SSO_CODE
  52. * @param {*} noMessage
  53. * @returns
  54. */
  55. export function getOpenToken() {
  56. return request({
  57. url: '/open/ex_login',
  58. method: 'get',
  59. params: { sysCode: SYS_CODE, token: getSsoAdminToken() },
  60. noMessage: false
  61. });
  62. };
  63. export function ssoLogout() {
  64. removeEnv();
  65. document.cookie = "ssoLogout=true;path=/;domain=" + getEnv().suffix;
  66. };
  67. // 打开单点登录页面
  68. export function openWindowSso() {
  69. // 清除单点登录Token
  70. removeSsoAdminToken();
  71. // 清除Token
  72. removeToken();
  73. setEnv();
  74. // 登录失效跳转到单点登录页面
  75. window.open(getEnv().jumpEnv, '_self');
  76. };
  77. // 系统退出
  78. export function toolsLogout() {
  79. Cookies.set('tools-logout', true);
  80. };
  81. // 设置跳转 url
  82. export function setEnv() {
  83. document.cookie = "ssoLogin=" + getEnv().curEnv + ";path=/;domain=" + getEnv().suffix;
  84. };
  85. // 删除定点跳转cookie
  86. export function removeEnv() {
  87. var date = new Date();
  88. date.setTime(date.getTime() - 500000);
  89. document.cookie = "ssoLogin=' ';expires=" + date.toUTCString() + "path=/;domain=" + getEnv().suffix;
  90. };
  91. // 请求头
  92. // let search = window.location.search;
  93. // if (search && search.length > 1) {
  94. // let arr = search.split('=')
  95. // if (arr[0] === '?token') {
  96. // let token = arr[1];
  97. // Cookies.set('TokenKey', token);
  98. // // setToken(token) // 将token值按照子系统的方式保存下来,cookie或者localStorage均可
  99. // window.location.href = window.location.origin
  100. // }
  101. // }