const storage = {};

/**
 * @description 存储 storage 值
 * @param {String} name storage name
 * @param {String} value storage value
 */
storage.set = function (name = 'default', value = '') {
  if (typeof value === 'object') {
    value = JSON.stringify(value);
  }
  sessionStorage.setItem(
    `erp-main-${process.env.VUE_APP_VERSION}-${name}`,
    value
  );
};

/**
 * @description 拿到 storage 值
 * @param {String} name storage name
 */
storage.get = function (name = 'default') {
  let value = sessionStorage.getItem(
    `erp-main-${process.env.VUE_APP_VERSION}-${name}`
  );
  // console.log('value', value)
  // value = JSON.parse(value)
  if (isJSONString(value)) {
    value = JSON.parse(value);
  }
  return value;
};

/**
 * @description 删除 cookie
 * @param {String} name cookie name
 */
storage.remove = function (name = 'default') {
  return sessionStorage.removeItem(
    `erp-main-${process.env.VUE_APP_VERSION}-${name}`
  );
};

function isJSONString(str) {
  if (typeof str === 'string') {
      try {
          var obj = JSON.parse(str);
          // console.log('转换成功：'+obj);
          return true;
      } catch (e) {
          // console.log('error：'+str+'!!!'+e);
          return false;
      }
  }
  // console.log('It is not a string!')
}
export default storage;
