12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- export default {
- set: function(key, value, expired) {
- window.localStorage.setItem(key, JSON.stringify({
- value: JSON.stringify(value),
- expire: Date.now() + 1000*60*60*24 * expired
- }))
- },
- get: function(key) {
- let obj = window.localStorage.getItem(key) || '';
- if(obj) {
- const { value,expire } = JSON.parse(obj);
- if ( Date.now() >= expire ) {
- window.localStorage.removeItem(key);
- return [];
- }
- return JSON.parse(value);
- }
- },
- setCookie: function(key,value,hours=8) {
- let expires = '';
- if (hours) {
- let date = new Date();
- date.setTime(date.getTime() + (hours * 60 * 60 * 1000));
- expires = ';expires=' + date.toUTCString();
- }
- document.cookie = key + '=' + value + expires + (';path=/');
- },
- getCookie: function(name) {
- var cookieArr = document.cookie.split(';');
- for (var i = 0; i < cookieArr.length; i++) {
- var cookiePair = cookieArr[i].split('=');
-
- var cookieName = cookiePair[0].trim();
- var cookieValue = cookiePair[1];
- if (cookieName === name) {
- return cookieValue;
- }
- }
-
- return null;
- },
- }
|