app.js 5.57 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 = {
  // 设置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({ dispatch }) {
    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 }) => {
        // const { baseURL } = window.$fetchConfig.request
        const [baseURL] = redirectUrl.split('/dj-scrm-wx-work-main/')
        let url = `${baseURL}/dj-scrm-wx-work-main/#/`
        let route = state.lastPath == 'coupon' ? '' : state.lastPath || ''
        route = route.replace('.', '/')
        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}`)
              console.log(data)
                // 先注释 
              window.location.replace(`${url}common/accountList?redirect_uri=${redirect_uri}`)
                //以下是 本地调试多账号
                // let userInfo = data.loginList[1]
                // let { merchantId, accountName } = userInfo
                // let loginAccount = accountName
                // ssoApi.selectAccount({
                //   loginAccount,
                //   merchantId,
                //   loginToken: data.loginToken,
                //   loginSource: 'WW',
                //   loginType: 3
                // }).then(res => {
                //   util.removeLocalStorage('dj_scrm_loginToken')
                //   util.removeLocalStorage('dj_scrm_accountList')
                //   util.setLocalStorage('dj_scrm_userInfo', res.data)
                //   util.setSessionId(res.data.sessionId)
                //   dispatch('registerHttp', res.data.sessionId)
                //   window.location.replace(`${url}${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}`)
              window.location.replace(`${redirectUrl}${route}`)
            }
          } else {
            /**没有账号 */
            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 || 'ww7ee69c648ae1b7e6'
        // 
      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`
    }
  },
  navigation({ dispatch }) {
    const sessionId = getParams('sessionId')
    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
}