auth.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import Cookies from 'js-cookie'
  2. const TokenKey = 'dy_admin_token'
  3. // const CrossTokenKey = 'Admin-Token'
  4. const CrossTokenKey = 'Admin-Token'
  5. function getCookieDomain() {
  6. let host = location.hostname
  7. const ip = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
  8. if (ip.test(host) === true || host === 'localhost') return host
  9. const regex = /([^]*).*/
  10. const match = host.match(regex)
  11. if (typeof match !== 'undefined' && match !== null) host = match[1]
  12. if (typeof host !== 'undefined' && host !== null) {
  13. const strAry = host.split('.')
  14. if (strAry.length > 1) {
  15. host = strAry[strAry.length - 2] + '.' + strAry[strAry.length - 1]
  16. }
  17. }
  18. return '.' + host
  19. }
  20. export function getToken () {
  21. return Cookies.get(TokenKey)
  22. }
  23. export function setToken (token) {
  24. return Cookies.set(TokenKey, token)
  25. }
  26. export function removeToken () {
  27. return Cookies.remove(TokenKey)
  28. }
  29. export function setCrossToken (token) {
  30. return Cookies.set(CrossTokenKey, token, { domain: getCookieDomain() })
  31. }
  32. export function removeCrossToken () {
  33. return Cookies.remove(CrossTokenKey, { domain: getCookieDomain() })
  34. }