app.js 4.03 KB
/*
 * @Descripttion:
 * @version:
 * @Author: limin
 * @Date: 2021-06-07 14:12:32
 * @LastEditors: limin
 * @LastEditTime: 2021-07-06 12:02:21
 */
import { ssoApi } from '@/api'
import { store } from 'utils'
import util from '@/utils/util'
const state = {
  pageMeta: '',
  keepAlives: [],
}
const mutations = {
  SET_PAGE_META(state, meta) {
    state.pageMeta = meta
  },
  PUSH_KEEP(state, name) {
    if (!state.keepAlives.includes(name)) {
      state.keepAlives.push(name)
    }
  },
  REMOVE_KEEP(state, names) {
    state.keepAlives = state.keepAlives.reduce((arr, name) => {
      if (!names.includes(name)) {
        return arr.push(name)
      }
      return arr
    }, [])
  },

}
const actions = {
  // 设置name
  setUserInfo({ commit }, user) {
    commit('SET_USER_INFO', user)
    store.set(store.consts.USER_KEY, user)
  },
  setPageMeta({ commit }, meta) {
    document.title = meta.title
    commit('SET_PAGE_META', meta)
  },
  setLoginToken({ commit }, val) {
    commit('SET_LOGIN_TOKEN', val)
  },
  setAccountList({ commit }, list) {
    commit('SET_ACCOUNT_LIST', list)
  },
  setSessionId({ commit }, id) {
    commit('SET_SESSION_ID', id)
  },
  pushKeep({ commit }, name) {
    commit('PUSH_KEEP', name)
  },
  removeKeep({ commit }, names) {
    commit('REMOVE_KEEP', names)
  },
  login() {
    let [redirectUrl] = window.location.href.split('?')
    const code = getParams('code')
    if (code) {
      const corpId = util.getLocalStorage('dj_scrm_corpId')
      ssoApi.login({
        code,
        corpId
      }).then(({ code, data, message }) => {
        let url = `${window.$baseUrl}/dj-scrm-wx-work-main/#/`
        let route = state.lastPath == 'home' ? '' : state.lastPath
        state.showWindow = true
        if (code == '0') {
          if (data.loginList.length) {
            if (data.loginList.length > 1) {
              util.setLocalStorage('dj_scrm_loginToken', data.loginToken)
              util.setLocalStorage('dj_scrm_accountList', data.loginList)
              let redirect_uri = encodeURI(`${redirectUrl}${route}`)
              window.location.replace(`${url}common/accountList?redirect_uri=${redirect_uri}`)
            } else {
              let [userInfo] = data.loginList
              util.setLocalStorage('dj_scrm_userInfo', userInfo)
              util.setLocalStorage('dj_scrm_session_id', userInfo.sessionId)
                // 只有一个账号 history.back() 
              console.log(`${redirectUrl}${route}`)
              window.location.replace(`${redirectUrl}${route}`)
            }
          } else {
            /**没有账号 */
            window.location.replace(`${url}common/nothing`)
          }
        } else {
          /**账号过期 (暂时没有字段判断是否过期) */
          window.vm.showToast(message)
          window.location.replace(`${url}common/overdue`)
        }
      })
    } else {
      util.showLoading()
      let corpId = getParams('corpId') //从链接获取
      if (!corpId) corpId = 'ww7ee69c648ae1b7e6'
      util.setLocalStorage('dj_scrm_corpId', corpId)
        // 'ww657a2d4ae89a4424'
      const wxUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize'
      window.location.replace(`${wxUrl}?appid=${corpId}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`)
    }
  },
  navigation({ dispatch }) {
    util.showLoading()
    const sessionId = getParams('sessionId')
    const storage_sessionId = util.getLocalStorage('dj_scrm_session_id')
    if (sessionId || storage_sessionId) {
      state.showWindow = true
      util.setLocalStorage('dj_scrm_session_id', sessionId || storage_sessionId)
    } else {
      dispatch('login')
      return
    }
    state.showWindow = true
  }
}
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
}