Newer
Older
operation_web / src / util / item.js
// 设置本地存储
import { Message } 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"
    });
  }
};