auth.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import Cookies from 'js-cookie'
  2. const TokenKey = 'Admin-Token'
  3. const useUrl = 'Back-Url'
  4. const useType = 'use-Type'
  5. const expTime = 'exp-time'
  6. const webStieId = 'website-id'
  7. const TokenKeyTest = 'test-token'
  8. const loginStatus = 'login-status'
  9. //1.设置token
  10. export function getToken() {
  11. return Cookies.get(TokenKey)
  12. }
  13. export function getTokenTest() {
  14. return Cookies.get(TokenKeyTest)
  15. }
  16. export function setToken(token,exp) {
  17. const expdays = convertSecondsToDays(exp)
  18. return Cookies.set(TokenKey, token, { expires: expdays })
  19. }
  20. export function removeToken() {
  21. return Cookies.remove(TokenKey)
  22. }
  23. //2.设置所属网站
  24. export function setUserUrl(url,exp) {
  25. const expdays = convertSecondsToDays(exp)
  26. return Cookies.set(useUrl, url, { expires: expdays })
  27. }
  28. export function getUserUrl() {
  29. return Cookies.get(useUrl)
  30. }
  31. export function removUserUrl() {
  32. return Cookies.remove(useUrl)
  33. }
  34. //3.储存用户等级
  35. export function setUseType(url,exp) {
  36. const expdays = convertSecondsToDays(exp)
  37. return Cookies.set(useType, url, { expires: expdays })
  38. }
  39. export function getUseType() {
  40. return Cookies.get(useType)
  41. }
  42. export function removUseType() {
  43. return Cookies.remove(useType)
  44. }
  45. //4.设置过期时间
  46. export function getExp() {
  47. return Cookies.get(expTime)
  48. }
  49. export function setExp(exp) {
  50. const expdays = convertSecondsToDays(exp)
  51. return Cookies.set(expTime, exp, { expires: expdays })
  52. }
  53. export function removeExp() {
  54. return Cookies.remove(expTime)
  55. }
  56. //网站id
  57. export function setWebSiteId(id,exp) {
  58. const expdays = convertSecondsToDays(exp)
  59. return Cookies.set(webStieId, id, { expires: expdays })
  60. }
  61. export function getWebSiteId() {
  62. return Cookies.get(webStieId)
  63. }
  64. export function removeWebSiteId() {
  65. return Cookies.remove(webStieId)
  66. }
  67. //5.把秒转换成天
  68. function convertSecondsToDays(seconds) {
  69. return seconds/(60*60*24); //1天=60秒*60分钟*24小时
  70. }
  71. //6.设置用户登录状态 - 单点登录 用于判断用户是否用权限进入系统内部
  72. export function setLoginStatus(status,exp) {
  73. const expdays = convertSecondsToDays(exp)
  74. return Cookies.set(loginStatus, status, { expires: expdays })
  75. }
  76. //6.1 获取用户登录状态
  77. export function getLoginStatus() {
  78. return Cookies.get(loginStatus)
  79. }
  80. //6.2 删除用户登录状态
  81. export function removeLoginStatus() {
  82. return Cookies.remove(loginStatus)
  83. }
  84. //6.3 获取登录类型
  85. export function getLoginType(url) {
  86. const match = url.match(/[?&](backurl|userurl)=/);
  87. return match ? match[1] : null;
  88. }
  89. //6.4 获取backurl的值
  90. export function getBackUrlValue(url) {
  91. const regex = /[?&](?:backurl|userurl)=([^&#]+)/;
  92. const match = url.match(regex);
  93. return match ? decodeURIComponent(match[1]) : null;
  94. }
  95. // Function to parse hash parameters from the URL
  96. export function hashParams() {
  97. const urlString = window.location.href;
  98. const url = new URL(urlString);
  99. const hash = url.hash;
  100. const hashParams = new URLSearchParams(hash.split('?')[1]);
  101. const userurl = hashParams.get('userurl');
  102. const backurl = hashParams.get('backurl');
  103. if (userurl) {
  104. // Create a URL object to extract the domain
  105. const userUrlObject = new URL(userurl);
  106. return userUrlObject.hostname; // Return only the domain
  107. }
  108. if (backurl) {
  109. // Create a URL object to extract the domain
  110. const backUrlObject = new URL(backurl);
  111. return backUrlObject.hostname; // Return only the domain
  112. }
  113. return null; // Return null if userurl is not present
  114. }