app.js 6.38 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 { 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)
  },
  pushKeep ({ commit }, name) {
    commit('PUSH_KEEP', name)
  },
  removeKeep ({ commit }, names) {
    commit('REMOVE_KEEP', names)
  },
  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) {
      let storage_params = util.getLocalStorage('wx_scrm_link_params') || {}
      storage_params = {
        ...storage_params,
        ...params
      }
      util.setLocalStorage('wx_scrm_link_params', storage_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 ({ }, sessionId = '') {
    window.$HTTP = new Http({ ...window.$fetchConfig, alert: Notify, headers: { token: sessionId } })
  }
}
export default {
  state,
  mutations,
  actions
}