增加基本项目配置
This commit is contained in:
86
utils/store/cookie.js
Normal file
86
utils/store/cookie.js
Normal file
@ -0,0 +1,86 @@
|
||||
import { trim, isType } from "@/utils";
|
||||
|
||||
const doc = null;
|
||||
// const doc = window.document;
|
||||
|
||||
function get(key) {
|
||||
if (!key || !_has(key)) {
|
||||
return null;
|
||||
}
|
||||
return wx.getStorageSync(key)
|
||||
// let regexpStr =
|
||||
// "(?:^|.*;\\s*)" +
|
||||
// escape(key).replace(/[-.+*]/g, "\\$&") +
|
||||
// "\\s*\\=\\s*((?:[^;](?!;))*[^;]?).*";
|
||||
// return JSON.parse(unescape(doc.cookie.replace(new RegExp(regexpStr), "$1")));
|
||||
|
||||
}
|
||||
|
||||
function all() {
|
||||
return wx.getStorageInfoSync()
|
||||
// let cookies = doc.cookie.split(/; ?/g),
|
||||
// data = {};
|
||||
// for (let i = cookies.length - 1; i >= 0; i--) {
|
||||
// if (!trim(cookies[i])) {
|
||||
// continue;
|
||||
// }
|
||||
// let kvp = cookies[i].split("=");
|
||||
// let key = unescape(kvp[0]);
|
||||
// data[key] = unescape(kvp[1]);
|
||||
// }
|
||||
// return data;
|
||||
}
|
||||
|
||||
function set(key, data, time) {
|
||||
if (!key) {
|
||||
return;
|
||||
}
|
||||
// let expires = "Tue, 19 Jan 2038 03:14:07 GMT";
|
||||
// if (time) {
|
||||
// let date;
|
||||
// if (isType(time, "Date")) {
|
||||
// date = time;
|
||||
// } else {
|
||||
// date = new Date();
|
||||
// date.setTime(date.getTime() + time * 60000);
|
||||
// }
|
||||
// expires = date.toGMTString();
|
||||
// }
|
||||
|
||||
// data = JSON.stringify(data);
|
||||
// doc.cookie =
|
||||
// escape(key) + "=" + escape(data) + "; expires=" + expires + "; path=/";
|
||||
|
||||
wx.setStorageSync(key, data)
|
||||
}
|
||||
|
||||
function remove(key) {
|
||||
if (!key || !_has(key)) {
|
||||
return;
|
||||
}
|
||||
wx.removeStorageSync(key)
|
||||
}
|
||||
|
||||
function clearAll() {
|
||||
wx.clearStorage()
|
||||
}
|
||||
|
||||
function _has(key) {
|
||||
if (!key) {
|
||||
return
|
||||
}
|
||||
let value = wx.getStorageSync(key)
|
||||
if (value) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
export default {
|
||||
get,
|
||||
all,
|
||||
set,
|
||||
remove,
|
||||
clearAll,
|
||||
has: _has
|
||||
};
|
7
utils/store/index.js
Normal file
7
utils/store/index.js
Normal file
@ -0,0 +1,7 @@
|
||||
import cookie from "./cookie";
|
||||
import localStorage from "./localStorage";
|
||||
|
||||
export default {
|
||||
cookie,
|
||||
localStorage
|
||||
};
|
42
utils/store/localStorage.js
Normal file
42
utils/store/localStorage.js
Normal file
@ -0,0 +1,42 @@
|
||||
function localStorage() {
|
||||
return window.localStorage;
|
||||
}
|
||||
|
||||
function get(key) {
|
||||
return JSON.parse(localStorage().getItem(key));
|
||||
}
|
||||
|
||||
function set(key, data) {
|
||||
return localStorage().setItem(key, JSON.stringify(data));
|
||||
}
|
||||
|
||||
function all() {
|
||||
const data = {};
|
||||
for (var i = localStorage().length - 1; i >= 0; i--) {
|
||||
var key = localStorage().key(i);
|
||||
data[key] = get(key);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
function remove(key) {
|
||||
return localStorage().removeItem(key);
|
||||
}
|
||||
|
||||
function clearAll() {
|
||||
return localStorage().clear();
|
||||
}
|
||||
|
||||
function has(key) {
|
||||
return localStorage().getItem(key) !== null;
|
||||
}
|
||||
|
||||
export default {
|
||||
get,
|
||||
set,
|
||||
all,
|
||||
remove,
|
||||
clearAll,
|
||||
has
|
||||
};
|
Reference in New Issue
Block a user