1. 程式人生 > 實用技巧 >對本地儲存的進一步處理

對本地儲存的進一步處理

  1 import {
  2     validatenull
  3 } from '@/util/validate';
  4 import website from '@/config/website'
  5 
  6 const keyName = website.key + '-';
  7 /**
  8  * 儲存localStorage
  9  */
 10 export const setStore = (params = {}) => {
 11     let {
 12         name,
 13         content,
 14         type,
15 } = params; 16 name = keyName + name 17 let obj = { 18 dataType: typeof (content), 19 content: content, 20 type: type, 21 datetime: new Date().getTime() 22 } 23 if (type) window.sessionStorage.setItem(name, JSON.stringify(obj)); 24 else
window.localStorage.setItem(name, JSON.stringify(obj)); 25 } 26 /** 27 * 獲取localStorage 28 */ 29 30 export const getStore = (params = {}) => { 31 let { 32 name, 33 debug 34 } = params; 35 name = keyName + name 36 let obj = {}, 37 content; 38 obj = window.sessionStorage.getItem(name);
39 if (validatenull(obj)) obj = window.localStorage.getItem(name); 40 if (validatenull(obj)) return; 41 try { 42 obj = JSON.parse(obj); 43 } catch{ 44 return obj; 45 } 46 if (debug) { 47 return obj; 48 } 49 if (obj.dataType == 'string') { 50 content = obj.content; 51 } else if (obj.dataType == 'number') { 52 content = Number(obj.content); 53 } else if (obj.dataType == 'boolean') { 54 content = eval(obj.content); 55 } else if (obj.dataType == 'object') { 56 content = obj.content; 57 } 58 return content; 59 } 60 /** 61 * 刪除localStorage 62 */ 63 export const removeStore = (params = {}) => { 64 let { 65 name, 66 type 67 } = params; 68 name = keyName + name 69 if (type) { 70 window.sessionStorage.removeItem(name); 71 } else { 72 window.localStorage.removeItem(name); 73 } 74 75 } 76 77 /** 78 * 獲取全部localStorage 79 */ 80 export const getAllStore = (params = {}) => { 81 let list = []; 82 let { 83 type 84 } = params; 85 if (type) { 86 for (let i = 0; i <= window.sessionStorage.length; i++) { 87 list.push({ 88 name: window.sessionStorage.key(i), 89 content: getStore({ 90 name: window.sessionStorage.key(i), 91 type: 'session' 92 }) 93 }) 94 } 95 } else { 96 for (let i = 0; i <= window.localStorage.length; i++) { 97 list.push({ 98 name: window.localStorage.key(i), 99 content: getStore({ 100 name: window.localStorage.key(i), 101 }) 102 }) 103 104 } 105 } 106 return list; 107 108 } 109 110 /** 111 * 清空全部localStorage 112 */ 113 export const clearStore = (params = {}) => { 114 let { type } = params; 115 if (type) { 116 window.sessionStorage.clear(); 117 } else { 118 window.localStorage.clear() 119 } 120 121 }

1,匯入的validatenull函式是用來判斷是否為空

 1 /**
 2  * 判斷是否為空
 3  */
 4 export function validatenull(val) {
 5     if (typeof val == 'boolean') {
 6         return false;
 7     }
 8     if (typeof val == 'number') {
 9         return false;
10     }
11     if (val instanceof Array) {
12         if (val.length == 0) return true;
13     } else if (val instanceof Object) {
14         if (JSON.stringify(val) === '{}') return true;
15     } else {
16         if (val == 'null' || val == null || val == 'undefined' || val == undefined || val == '') return true;
17         return false;
18     }
19     return false;
20 }

2,匯入的website中配置的key

website.js中的配置的key:'peacock',//配置主鍵,目前用於儲存