app.js 1.65 KB

import {  util } from 'utils'
import { Http } from 'glsx-common'
import { Notify } from 'vant'
const state = {
  showWindow: false
}
import router from '@/router'

const mutations = {
  SET_PAGE_META (state, meta) {
    state.pageMeta = meta
  },
}
const actions = {
  setPageMeta ({ commit }, meta) {
    document.title = meta.title || '企微通知模板'
    commit('SET_PAGE_META', meta)
  },
  registerHttp ({ }, sessionId = '') {
    window.$HTTP = new Http({ ...window.$fetchConfig, alert: Notify, headers: { token: sessionId } })
  },
  login ({ dispatch }) {
    const { corpId, agentId } = util.getLocalStorage('ww_storage_info'),
      { loginUrl } = window.$fetchConfig
    window.location.href = `${loginUrl}?redirect_uri=${encodeURIComponent(window.location.href)}&corpId=${corpId}&agentId=${agentId}`
  },
  navigation ({ dispatch }, to) {
    let { corpId, agentId } = getParams()
    let userInfo = util.getLocalStorage('ww_userInfo')
    dispatch('registerHttp')
    if (corpId) {
      let ww_storage_info = {}
      ww_storage_info.corpId = corpId
      if (agentId && agentId != 'undefined') ww_storage_info.agentId = agentId
      util.setLocalStorage('ww_storage_info', ww_storage_info)
    }
    if (userInfo) {
      state.showWindow = true
    } else {
      util.showLoading()
      dispatch('login')
    }
  },
}
const getParams = name => {
  let [url, paramsList] = window.location.href.split('?')
  if (!paramsList) return name ? null : {}
  let param = paramsList.split('&')
  let obj = {}
  param.map(e => {
    let [key, value] = e.split('=')
    obj[key] = value
  })
  return name ? obj[name] : obj
}
export default {
  state,
  mutations,
  actions
}