app.js 6.77 KB
/*
 * @Descripttion:
 * @version:
 * @Author: limin
 * @Date: 2021-06-07 14:12:32
 * @LastEditors: limin
 * @LastEditTime: 2022-10-25 09:31:24
 */
import { ssoApi } from '@/api'
import { util } from 'utils'
import { Http } from 'glsx-common'
import { Notify } from 'vant'
const state = {
  pageMeta: '',
  keepAlives: [],
  lastPath: '',
  activityInfo: {},
  updataCarInfo: Date.now(),
  loginStatus: false,
  isLiveFull: false
}
const mutations = {
  SET_ACTIVITY_INFO (state, value) {
    state.activityInfo = value
    state.updataCarInfo = Date.now()
  },
  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
  },
  SET_LOGIN_STATUS (state, data) {
    state.loginStatus = data
  },
  SET_LIVE_FULL (state, data) {
    state.isLiveFull = data
  }
}
const actions = {
  setPageMeta ({ commit }, meta) {
    document.title = meta.title
    commit('SET_PAGE_META', meta)
  },
  pushKeep ({ commit }, name) {
    commit('PUSH_KEEP', name)
  },
  removeKeep ({ commit }, names) {
    commit('REMOVE_KEEP', names)
  },
  setActivityInfo ({ commit }, value) {
    commit('SET_ACTIVITY_INFO', value)
  },
  setLoginStatus ({ commit }, value) {
    commit('SET_LOGIN_STATUS', value)
  },
  setIsLiveFull ({ commit }, value) {
    commit('SET_LIVE_FULL', value)
  },
  wxLogin ({ dispatch }, { code, redirectUrl, appId }) {
    ssoApi
      .login({
        code,
        appId: window.$fetchConfig.appid,
        state: 'snsapi_userinfo'
      })
      .then(({ code, data, message }) => {
        let route = state.lastPath == 'crowdfunding' ? '' : state.lastPath || ''
        route = route.replace('.', '/')
        if (code == '0') {
          util.setUserInfo(data)
          util.setSessionId(data.sessionId)
          dispatch('registerHttp', data.sessionId)
          state.showWindow = true
          let query = window.location.href.split('/split/')[1]
          let pageQuery = query ? decodeURIComponent(query) : ''
          pageQuery = pageQuery.replace(/\|/g, '&')
          let url = `${redirectUrl}#/${route}`
          if (pageQuery) url += `?${pageQuery}`
          window.location.replace(url)
        } else {
          Notify(message)
        }
      })
  },
  getWxCode ({ dispatch }) {
    let [redirectUrl] = window.location.href.split('?')
    let params = util.getPageQuery()
    let arr = []
    Object.keys(params).map(key => {
      arr.push(`${key}=${params[key]}`)
    })
    if (arr.length) redirectUrl += `/split/${arr.join('|')}`
    const isWxWork = util.getLocalStorage('is_wxwork')
    const STORAGE_PARAMS = util.getLocalStorage('wx_scrm_link_params')
    util.showLoading()
    let { appid } = window.$fetchConfig
    if (isWxWork && STORAGE_PARAMS && STORAGE_PARAMS.corpId) appid = STORAGE_PARAMS.corpId
    let wxUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code&scope=snsapi_userinfo`
    if (isWxWork && STORAGE_PARAMS && STORAGE_PARAMS.agentId) wxUrl = `${wxUrl}&agentid=${STORAGE_PARAMS.agentId}`
    util.setAppId(appid)
    window.location.href = `${wxUrl}&state=STATE#wechat_redirect`
  },
  login ({ dispatch }) {
    let [redirectUrl] = window.location.href.split('?')
    const isWxWork = util.getLocalStorage('is_wxwork')
    const code = util.getPageQuery('code')
    let appId = util.getAppId()
    util.showLoading()
    if (process.env.NODE_ENV == 'development') {
      //本地开发
      if (code) {
        const target = isWxWork ? 'wwLogin' : 'login'
        const PARAMS = isWxWork
          ? {
            code,
            corpId: appId,
            getUserInfoFlag: false
          }
          : {
            code,
            appId: window.$fetchConfig.appid,
            state: 'snsapi_userinfo'
          }
        ssoApi[target](PARAMS).then(({ code, data, message }) => {
          let route = state.lastPath == 'crowdfunding' ? '' : state.lastPath || ''
          route = route.replace('.', '/')
          if (code == '0') {
            let sessionId = '',
              userInfo = {}
            if (isWxWork) {
              userInfo = data.loginList[0]
              sessionId = userInfo.sessionId
            } else {
              userInfo = data
              sessionId = data.sessionId
            }
            util.setUserInfo(userInfo)
            util.setSessionId(sessionId)
            dispatch('registerHttp', sessionId)
            state.showWindow = true
            let query = window.location.href.split('/split/')[1]
            let pageQuery = query ? decodeURIComponent(query) : ''
            pageQuery = pageQuery.replace(/\|/g, '&')
            let url = `${redirectUrl}#/${route}`
            if (pageQuery) url += `?${pageQuery}`
             window.location.replace(url)
          } else Notify(message)
        })
      } else dispatch('getWxCode')
    } else {
      if (isWxWork) {
        let storage_params = util.getLocalStorage('wx_scrm_link_params') || {}
        let url = `/dj-scrm-wx-work-main/#/?redirect_uri=${encodeURIComponent(redirectUrl)}&corpId=${storage_params.corpId}`
        if (storage_params.agentId) url = `${url}&agentId=${storage_params.agentId}`
        window.location.href = url
      } else {
        if (code) dispatch('wxLogin', { redirectUrl, code, appId })
        else dispatch('getWxCode')
      }
    }
  },
  navigation ({ dispatch }) {
    const isWxWork = util.getLocalStorage('is_wxwork')
    let params = util.getPageQuery() //从链接获取
    if (Object.keys(params).length && params.id) util.setLocalStorage('wx_scrm_link_params', params)
    dispatch('registerHttp')
    const storage_sessionId = util.getSessionId()
    if (isWxWork) {
      const ww_sessionId = window.localStorage.getItem('dj_scrm_session_id')
      if (params.sessionId || ww_sessionId || storage_sessionId) {
        state.showWindow = true
        let userInfo = util.getLocalStorage('dj_scrm_userInfo')
        if (userInfo) util.setUserInfo(userInfo)
        util.setSessionId(params.sessionId || ww_sessionId || storage_sessionId)
        dispatch('registerHttp', params.sessionId || ww_sessionId || storage_sessionId)
      } else dispatch('login')
      return
    } else if (storage_sessionId) {
      state.showWindow = true
      dispatch('registerHttp', storage_sessionId)
      return
    } else dispatch('login')
  },
  registerHttp ({ dispatch }, sessionId = '') {
    window.$HTTP = new Http({ ...window.$fetchConfig, alert: Notify, headers: { token: sessionId } })
  }
}
export default {
  state,
  mutations,
  actions
}