import actions from './actions'
import mutations from './mutations'
import { setStorage, getStorage, removeStorage } from '@/common/utils/storage.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
}

const state = {
  openId: getQueryVariable('openId') || getStorage('openId') || '',
  token: getStorage('token') || getQueryVariable('x-token') || '',
  name: getQueryVariable('username') || getStorage('name') || '',
  avatar: '',
  userId: getQueryVariable('userId') || getStorage('userId') || '',
  mobile: getQueryVariable('mobile') || getStorage('mobile') || '',
  appId: getQueryVariable('appid') || getQueryVariable('appId') || '',
  miniOpenId: getQueryVariable('miniOpenId') || getStorage('miniOpenId') || '',
  sharerOpenId: getQueryVariable('sharerOpenId') || getStorage('sharerOpenId') || '',
  code2: getQueryVariable('code') || getStorage('code') || '',
  crossPagePam: ''
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
