
import store from '@/store/index.js'

// 解析地址栏参数
function getQueryVariable(variable) {
  if (!window) {
    return undefined;
  }
  // 从?开始获取后面的所有数据
  var query = window.location.search.substring(1);
  // 从字符串&开始分隔成数组split
  var vars = query.split('&');
  // 遍历该数组
  for (var i = 0; i < vars.length; i++) {
    // 从等号部分分割成字符
    var pair = vars[i].split('=');
    // 如果第一个元素等于 传进来的参的话 就输出第二个元素
    if (pair[0] == variable) {
      return (pair[1]);
    }
  }
  return undefined;
}

export const setStorage = (name, data) => {
  var appid = store?.state?.user?.appId || getQueryVariable("appid") || getQueryVariable("appId")
  if(!appid){
    return ""
  }
  return uni.setStorageSync(`APPID${appid}_${name}`, data);
}

export const getStorage = (name) => {
  var appid = store?.state?.user?.appId || getQueryVariable("appid") || getQueryVariable("appId")
  if(!appid){
    return ""
  }
  return uni.getStorageSync(`APPID${appid}_${name}`);
}

export const removeStorage = (name) => {
  var appid = store?.state?.user?.appId || getQueryVariable("appid") || getQueryVariable("appId")
  if(!appid){
    return ""
  }
  return uni.removeStorageSync(`APPID${appid}_${name}`);
}

export default {
  setStorage,
  getStorage,
  removeStorage
}
