123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- import Cookies from 'js-cookie';
- const cookieKeys = ['admin_uuid', 'admin_uid'];
- /**
- * 判断是否需要储存为Cookie
- * @param key
- * @returns {boolean}
- */
- function isCookieKey(key) {
- return cookieKeys.indexOf(key) !== -1;
- }
- /**
- * SetItem
- * @param key
- * @param value
- * @param options
- */
- function setItem(key, value, options = {}) {
- console.log(`Setting item: ${key}, value: ${value}, options:`, options); // 添加日志
- if (options.expires === undefined) {
- options.expires = 365 * 10;
- }
- return isCookieKey(key)
- ? setCookieItem(key, value, options)
- : setLocalItem(key, value, options);
- }
- /**
- * GetItem
- * @param key
- * @returns {*|string}
- */
- function getItem(key) {
- return isCookieKey(key)
- ? getCookieItem(key)
- : getLocalItem(key);
- }
- /**
- * RemoveItem
- * @param key
- * @param options
- */
- function removeItem(key, options) {
- return isCookieKey(key)
- ? removeCookieItem(key, options)
- : removeLocalItem(key);
- }
- // ========== LocalStorage ========== //
- /**
- * 设置LocalStorageItem
- * @param key
- * @param value
- * @param options
- */
- function setLocalItem(key, value, options = {}) {
- let { expires } = options;
- if (typeof expires === 'number') {
- expires = new Date().getTime() + expires * 86400 * 1000;
- } else if (typeof expires === 'object') {
- expires = expires.getTime();
- }
- value = JSON.stringify({
- expires,
- data: value
- });
- localStorage.setItem(key, value);
- }
- /**
- * 获取LocalStorageItem
- * @param key
- * @returns {string}
- */
- function getLocalItem(key) {
- let object = localStorage.getItem(key);
- if (!object) return '';
- object = JSON.parse(object);
- const { expires } = object;
- if (typeof expires === 'number' && expires <= Date.now()) {
- localStorage.removeItem(key);
- return '';
- }
- return object.data;
- }
- /**
- * 移除LocalStorageItem
- * @param key
- */
- function removeLocalItem(key) {
- localStorage.removeItem(key);
- }
- // ========== Cookie ========== //
- /**
- * 获取根域名
- * @returns {string}
- */
- function getRootDomain() {
- const parts = document.domain.split('.').reverse();
- if (parts.length >= 2) {
- return parts[1] + '.' + parts[0];
- }
- return document.domain;
- }
- /**
- * 设置CookieItem
- * @param key
- * @param value
- * @param options
- */
- function setCookieItem(key, value, options = {}) {
- const domain = getRootDomain();
- options = { domain, ...options };
- Cookies.set(key, value, options);
- }
- /**
- * 获取CookieItem
- * @param key
- * @returns {*}
- */
- function getCookieItem(key) {
- return Cookies.get(key);
- }
- /**
- * 移除CookieItem
- * @param key
- * @param options
- */
- function removeCookieItem(key, options = {}) {
- const domain = getRootDomain();
- options = { domain, ...options };
- Cookies.remove(key, options);
- }
- export default {
- setItem,
- getItem,
- removeItem
- };
|