Newer
Older
operation_web / src / components / util / item.js
@yuwj yuwj on 27 Jan 2021 2 KB 集成数据滤网模块
/**
 * 设置本地存储
 * 在流程任务过程中,使用的是localStorage,跨页面访问数据保存
 */
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) => {
  if (typeof val === 'string') {
    localStorage.setItem(name, val);
  } else {
    localStorage.setItem(name, JSON.stringify(val));
  }
};

export const getSession = (name) => {
  return localStorage.getItem(name);
};

export const removeSession = (name) => {
  localStorage.removeItem(name);
};

export const loginTime = () => {
  // 登录计时,6小时超过登录期限
  loginSet = setTimeout(function () {
    removeSession('logins');
    loginTimeout();
  }, 21600000);
  // 21600000
};

export const loginTimeout = () => {
  // 退出,取消计时
  if (loginSet) {
    window.clearTimeout(loginSet);
  }
};

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'
    });
  }
};