app.js 4.07 KB
import { Http } from 'glsx-common'
import { Notify } from 'vant'
import { ssoApi } from '@/api'
import { util } from 'utils'

const state = {
  showWindow: false,
  lastPath: ''
}

const mutations = {
  SET_PAGE_META (state, meta) {
    state.pageMeta = meta
  },
  SET_LAST_PATH (state, path) {
    state.lastPath = path
  }
}
const actions = {
  setPageMeta ({ commit }, meta) {
    document.title = meta.title || '嘀加智慧云'
    commit('SET_PAGE_META', meta)
  },
  registerHttp ({ }, sessionId = '') {
    let { corpId } = getParams()
    setCorpInfo(corpId)
    let { scrmRequest, djRequest } = window.$fetchConfig
    window.$SCRM_HTTP = new Http({ ...scrmRequest, alert: Notify })
    window.$DJ_HTTP = new Http({ ...djRequest, alert: Notify, headers: { token: sessionId } })
  },
  toNoAuth ({ commit }) {
    let [jumpUrl] = window.location.href.split('/#/')
    window.location.href = `${jumpUrl}/#/mobile/noAuth`
  },
  async login ({ dispatch }) {
    try {
      const wwCode = getParams('code'),
        { corpId } = util.getLocalStorage('ww_storage_info') || {},
        { loginUrl } = window.$fetchConfig
      if (wwCode) {
        const { code, data, message } = await ssoApi.login({
          code: wwCode,
          corpId,
          getUserInfoFlag: false
        })
        if (code == 0) {
          if (data.loginList && data.loginList.length) {
            let [item] = data.loginList
            if (item.loginList && item.loginList.length) {
              let [userInfo] = item.loginList
              if (userInfo) {
                let { phone } = userInfo
                if (phone) {
                  const res = await ssoApi.loginByPhone({
                    phone,
                  })
                  if (res.code == 0) {
                    let route = state.lastPath || '',
                      [jumpUrl, query] = window.location.href.split('/#/')
                    query = query.replace(/&?code=[^&]*/, '')
                    route = route.replace('.', '/')
                    util.setLocalStorage('ww_userInfo', res.data)
                    util.setSessionId(res.data.sessionId)
                    dispatch('registerHttp', res.data.sessionId)
                    state.showWindow = true
                    window.location.replace(`${jumpUrl}/#/${query}`)
                  } else dispatch('toNoAuth')
                } else dispatch('toNoAuth')
              } else dispatch('toNoAuth')
            } else dispatch('toNoAuth')
          } else dispatch('toNoAuth')
        } else throw message
      } else {
        const corpInfo = await ssoApi.getAgentIdByAppid({
          corpId
        })
        if (corpInfo) {
          const { agentId } = corpInfo
          window.location.href = `${loginUrl}?redirect_uri=${encodeURIComponent(window.location.href)}&corpId=${corpId}&agentId=${agentId}&backLoginCode=BACKCODE`
        } else throw '获取企业信息失败'
        console.log(corpInfo)
      }
    } catch (error) {
      if (!error) error = '登录失败或无权限'
      if (error) Notify(typeof error === 'string' ? error : error.toString())
    }
  },
  navigation ({ dispatch }, isReLogin) {
    let { corpId, sessionId } = getParams(),
      userInfo = util.getLocalStorage('ww_userInfo')
    dispatch('registerHttp', sessionId || '')
    setCorpInfo(corpId)
    if (isReLogin) {
      dispatch('login')
      return
    }
    if (userInfo || sessionId) {
      state.showWindow = true
    } else {
      dispatch('login')
    }
  }
}
const setCorpInfo = corpId => {
  if (corpId) {
    let ww_storage_info = {}
    ww_storage_info.corpId = corpId
    util.setLocalStorage('ww_storage_info', ww_storage_info)
  }
}
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
}