app.js 5.84 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, util } from 'utils'
import { Http } from 'glsx-common'
import { Notify } from 'vant'
const state = {
  pageMeta: '',
  keepAlives: [],
  lastPath: ''
}
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
    }, [])
  },
  SET_LAST_PATH (state, path) {
    state.lastPath = path
  }
}
const actions = {
  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)
  },
  pushKeep ({ commit }, name) {
    commit('PUSH_KEEP', name)
  },
  removeKeep ({ commit }, names) {
    commit('REMOVE_KEEP', names)
  },
  login ({ dispatch }) {
    let [redirectUrl] = window.location.href.split('?')
    // 以下是 本地调试
    if (process.env.NODE_ENV == 'development') {
      const code = getParams('code')
      if (code) {
        const { corpId } = util.getLocalStorage('dj_scrm_corpId')
        ssoApi
          .login({
            code,
            corpId,
            getUserInfoFlag: false
          })
          .then(({ code, data, message }) => {
            let route = state.lastPath == 'coupon' ? '' : state.lastPath || ''
            route = route.replace('.', '/')
            if (code == '0') {
              if (data.loginList.length) {
                if (data.loginList.length > 1) {
                  let userInfo = data.loginList[0]
                  let { merchantId, accountName } = userInfo
                  let loginAccount = accountName
                  ssoApi
                    .selectAccount({
                      loginAccount,
                      merchantId,
                      loginToken: data.loginToken,
                      loginSource: 'WW',
                      loginType: 3
                    })
                    .then(res => {
                      util.setLocalStorage('dj_scrm_userInfo', res.data)
                      util.setSessionId(res.data.sessionId)
                      dispatch('registerHttp', res.data.sessionId)
                      state.showWindow = true
                      window.location.replace(`${redirectUrl}#/${route}`)
                    })
                } else {
                  let [userInfo] = data.loginList
                  util.setLocalStorage('dj_scrm_userInfo', userInfo)
                  util.setSessionId(userInfo.sessionId)
                  dispatch('registerHttp', userInfo.sessionId)
                  // 只有一个账号 history.back()
                  console.log(`${redirectUrl}${route}`)
                  state.showWindow = true
                  window.location.replace(`${redirectUrl}#/${route}`)
                }
              } else {
                /**没有账号 */
                Notify('没有账号')
                // window.location.href = `${url}common/nothing`
              }
            } else {
              /**账号过期 (暂时没有字段判断是否过期) */
              Notify(message)
              // window.location.href = `${url}common/overdue`
            }
          })
      } else {
        util.showLoading()
        let appid = getParams('corpId') //从链接获取
        let dj_scrm = util.getLocalStorage('dj_scrm_corpId')
        if (!appid) appid = (dj_scrm && dj_scrm.corpId) || 'wwc33bc9a3c72d45f3'
        util.setLocalStorage('dj_scrm_corpId', { corpId: appid })
        // 'ww657a2d4ae89a4424'
        const wxUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize'
        window.location.href = `${wxUrl}?appid=${appid}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect`
      }
    } else {
      const { corpId, agentId } = util.getLocalStorage('dj_scrm_corpId')
      const { baseURL } = window.$fetchConfig.request
      let url = `${baseURL}/dj-scrm-wx-work-main/#/`
      window.location.href = `${url}?redirect_uri=${encodeURIComponent(redirectUrl)}&corpId=${corpId}&agentId=${agentId}`
    }
  },
  navigation ({ dispatch }) {
    let { sessionId, corpId, agentId } = getParams()
    const storage_corpId = util.getLocalStorage('dj_scrm_corpId')
    if (!corpId) corpId = storage_corpId && storage_corpId.corpId
    if (corpId && storage_corpId) {
      if (corpId != storage_corpId.corpId) {
        util.removeLocalStorage('dj_scrm_userInfo')
        util.clearSessionId()
      }
    }
    let dj_scrm_storage = { corpId }
    if (agentId) dj_scrm_storage.agentId = agentId
    util.setLocalStorage('dj_scrm_corpId', dj_scrm_storage)
    const storage_sessionId = util.getSessionId()
    if (sessionId || storage_sessionId) {
      state.showWindow = true
      util.setSessionId(sessionId || storage_sessionId)
      dispatch('registerHttp', sessionId || storage_sessionId)
    } else {
      util.showLoading()
      dispatch('registerHttp')
      dispatch('login')
      return
    }
    state.showWindow = true
  },
  registerHttp ({ }, sessionId = '') {
    window.$HTTP = new Http({ ...window.$fetchConfig, alert: Notify, headers: { token: sessionId } })
  }
}
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
}