-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
numsg
committed
Oct 22, 2019
1 parent
f740616
commit 848003e
Showing
4 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
export default { | ||
|
||
/** | ||
* 添加或修改 | ||
* | ||
* @param {*} key | ||
* @returns | ||
*/ | ||
set(key: any, data: any) { | ||
if (data !== null && (typeof data === 'object')) { | ||
data = JSON.stringify(data); | ||
} | ||
localStorage.setItem(key, data); | ||
}, | ||
|
||
/** | ||
* get查询 | ||
*/ | ||
get(key: any) { | ||
const val = localStorage.getItem(key); | ||
if (val === null || val === 'null') { | ||
return null; | ||
} else if (val === 'undefined') { | ||
return undefined; | ||
} else { | ||
try { | ||
return JSON.parse(val); // 解析成功说明是JSON | ||
} catch (e) { | ||
return val; | ||
} | ||
} | ||
}, | ||
|
||
/** | ||
* 修改 key 对应对象 中的字段 | ||
* 如果对象不存在则创建 | ||
*/ | ||
modify(key: any, fields: any) { | ||
let obj = this.get(key); | ||
if (obj === null) { | ||
obj = {}; | ||
} | ||
if (obj instanceof Object && fields) { | ||
for (const name in fields) { | ||
if (name !== null) { | ||
obj[name] = fields[name]; | ||
} | ||
} | ||
this.set(key, obj); | ||
} | ||
}, | ||
|
||
/** | ||
* 删除 | ||
*/ | ||
remove(key: any) { | ||
if (key instanceof Array) { | ||
for (const k of key) { | ||
if (k !== null) { | ||
localStorage.removeItem(k); | ||
} | ||
} | ||
} else { | ||
localStorage.removeItem(key); | ||
} | ||
} | ||
}; | ||
|