// 设置本地存储 import { Message } from "element-ui"; import { Notification } from 'element-ui'; import state from "./state"; var loginSet = null; export const setItem = (name, val) => { localStorage.setItem(name, JSON.stringify(val)); }; export const isJSON = str => { if (typeof str === "string") { try { var obj = JSON.parse(str); if (typeof obj === "object" && obj) { return true; } else { return false; } } catch (e) { return false; } } }; export const getItem = name => { return localStorage.getItem(name); }; export const removeItem = name => { localStorage.removeItem(name); }; export const setSession = (name, val) => { sessionStorage.setItem(name, JSON.stringify(val)); }; export const getSession = name => { return sessionStorage.getItem(name); }; export const removeSession = name => { sessionStorage.removeItem(name); }; export const loginTime = () => { // 登录计时,6小时超过登录期限 loginSet = setTimeout(function() { removeSession("logins"); loginTimeout(); }, 21600000); // 21600000 }; export const loginTimeout = () => { // 退出,取消计时 if (!loginSet) { clearTimeout(loginSet); } }; export const message = res => { let code = res.data.code; let message = ""; if (typeof res.data.msg === "string") { message = res.data.msg; } else { message = state(code); } if (res.data.code === 1 || res.data.code == 200) { Message({ message: message, type: "success" }); } else { Message({ message: message, type: "error" }); } }; export const notify = (res) => { // 请求消息提示 let code = res.data ? (res.data.code ? res.data.code : res.code) : res.code; let message = ''; let getMessage = res.data ? (res.data.message ? res.data.message : res.message) : res.message; if (typeof getMessage === 'string') { message = getMessage; } else { message = state(code); } if (code === 1) { Notification({ 'message': getMessage, 'type': 'warning' }); return; } if (code === 200) { Notification({ 'message': message, 'type': 'success' }); } else { Notification({ 'message': message, 'type': 'error' }); } };