123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- import Cookies from 'js-cookie'
- const TokenKey = 'Admin-Token'
- const useUrl = 'Back-Url'
- const useType = 'use-Type'
- const expTime = 'exp-time'
- const webStieId = 'website-id'
- const TokenKeyTest = 'test-token'
- const loginStatus = 'login-status'
- //1.设置token
- export function getToken() {
- return Cookies.get(TokenKey)
- }
- export function getTokenTest() {
- return Cookies.get(TokenKeyTest)
- }
- export function setToken(token,exp) {
- const expdays = convertSecondsToDays(exp)
- return Cookies.set(TokenKey, token, { expires: expdays })
- }
- export function removeToken() {
- return Cookies.remove(TokenKey)
- }
- //2.设置所属网站
- export function setUserUrl(url,exp) {
- const expdays = convertSecondsToDays(exp)
- return Cookies.set(useUrl, url, { expires: expdays })
- }
- export function getUserUrl() {
- return Cookies.get(useUrl)
- }
- export function removUserUrl() {
- return Cookies.remove(useUrl)
- }
- //3.储存用户等级
- export function setUseType(url,exp) {
- const expdays = convertSecondsToDays(exp)
- return Cookies.set(useType, url, { expires: expdays })
- }
- export function getUseType() {
- return Cookies.get(useType)
- }
- export function removUseType() {
- return Cookies.remove(useType)
- }
- //4.设置过期时间
- export function getExp() {
- return Cookies.get(expTime)
- }
- export function setExp(exp) {
- const expdays = convertSecondsToDays(exp)
- return Cookies.set(expTime, exp, { expires: expdays })
- }
- export function removeExp() {
- return Cookies.remove(expTime)
- }
- //网站id
- export function setWebSiteId(id,exp) {
- const expdays = convertSecondsToDays(exp)
- return Cookies.set(webStieId, id, { expires: expdays })
- }
- export function getWebSiteId() {
- return Cookies.get(webStieId)
- }
- export function removeWebSiteId() {
- return Cookies.remove(webStieId)
- }
- //5.把秒转换成天
- function convertSecondsToDays(seconds) {
- return seconds/(60*60*24); //1天=60秒*60分钟*24小时
- }
- //6.设置用户登录状态 - 单点登录 用于判断用户是否用权限进入系统内部
- export function setLoginStatus(status,exp) {
- const expdays = convertSecondsToDays(exp)
- return Cookies.set(loginStatus, status, { expires: expdays })
- }
- //6.1 获取用户登录状态
- export function getLoginStatus() {
- return Cookies.get(loginStatus)
- }
- //6.2 删除用户登录状态
- export function removeLoginStatus() {
- return Cookies.remove(loginStatus)
- }
- //6.3 获取登录类型
- export function getLoginType(url) {
- const match = url.match(/[?&](backurl|userurl)=/);
- return match ? match[1] : null;
- }
- //6.4 获取backurl的值
- export function getBackUrlValue(url) {
- const regex = /[?&](?:backurl|userurl)=([^&#]+)/;
- const match = url.match(regex);
- return match ? decodeURIComponent(match[1]) : null;
- }
- // Function to parse hash parameters from the URL
- export function hashParams() {
- const urlString = window.location.href;
- const url = new URL(urlString);
- const hash = url.hash;
- const hashParams = new URLSearchParams(hash.split('?')[1]);
- const userurl = hashParams.get('userurl');
- const backurl = hashParams.get('backurl');
- if (userurl) {
- // Create a URL object to extract the domain
- const userUrlObject = new URL(userurl);
- return userUrlObject.hostname; // Return only the domain
- }
- if (backurl) {
- // Create a URL object to extract the domain
- const backUrlObject = new URL(backurl);
- return backUrlObject.hostname; // Return only the domain
- }
- return null; // Return null if userurl is not present
- }
|