app.js 4.21 KB
/*
 * @Author: 'wangzhihui' '1733904062@qq.com'
 * @Date: 2026-01-26 12:02:37
 * @LastEditors: 'wangzhihui' '1733904062@qq.com'
 * @LastEditTime: 2026-03-20 16:50:12
 * @FilePath: \wx-notification\src\store\modules\app.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import {  util } from 'utils'
import { Http } from 'glsx-common'
import { Notify } from 'vant'
import { ssoApi } from '@/api'

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

import router from '@/router'

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 { scrmRequest, djRequest } = window.$fetchConfig
    window.$SCRM_HTTP = new Http({ ...scrmRequest, alert: Notify })
   window.$HTTP = new Http({ ...djRequest, alert: Notify, headers: { token: sessionId } })
    // window.$HTTP = new Http({ ...window.$fetchConfig, alert: Notify, headers: { token: sessionId } })
  },


  login({ dispatch, state }) {
    //获取当前路由路径
    const currentPath = window.location.href;
    const pathKeywords = ['/department', '/personage', '/company'];
    const isTargetPath = pathKeywords.some(keyword => currentPath.includes(keyword));
    if (isTargetPath) {
      try {
        let { corpId, agentId } = getParams();
        const wwCode = getParams('code');
        let { loginUrl } = window.$fetchConfig;
        let userInfo = util.getLocalStorage('ww_userInfo');
        if(userInfo) return state.showWindow = true;
        if (wwCode) {
          ssoApi.login({ code: wwCode, corpId, getUserInfoFlag: false }).then(res => {
            const { code, data, message } = res;
            state.showWindow = false
            if (code == 0) {
              state.showWindow = true;
              if (data.loginList && data.loginList.length) {
                let [item] = data.loginList
                if (item.loginList && item.loginList.length) {
                  let [userInfo] = item.loginList,
                    { phone } = userInfo
                  if (phone) util.setLocalStorage('ww_params_phone', phone)
                  util.setLocalStorage('ww_userInfo', userInfo)
                } else throw ''
              } else throw ''
            } else throw message
          })
        } else {
          window.location.href = `${loginUrl}?redirect_uri=${encodeURIComponent(window.location.href)}&corpId=${corpId}&agentId=${agentId}&backLoginCode=BACKCODE`
        }
      } catch (error) {
        if (!error) error = '登录失败或无权限'
        if (error) Notify(typeof error === 'string' ? error : error.toString())
      }
    } else {
      state.showWindow = true
      const { corpId, agentId } = util.getLocalStorage('ww_storage_info'),
        { loginUrl } = window.$fetchConfig;
      window.location.href = `${loginUrl}?redirect_uri=${encodeURIComponent(window.location.href)}&corpId=${corpId}&agentId=${agentId}`
    }
  },

  navigation({ dispatch }, to) {
    let { corpId, agentId, phone, name,id } = getParams(),
      userInfo = util.getLocalStorage('ww_userInfo')
    if (phone) util.setLocalStorage('ww_params_phone', phone)
    if (name) util.setLocalStorage('ww_params_name', name)
    if (id) util.setLocalStorage('ww_params_id', id)
    dispatch('registerHttp')
    if (corpId) {
      let ww_storage_info = {}
      ww_storage_info.corpId = corpId
      if (agentId && agentId != 'undefined') ww_storage_info.agentId = agentId
      util.setLocalStorage('ww_storage_info', ww_storage_info)
    }
    if (userInfo) {
      state.showWindow = true
    } else {
      util.showLoading()
      dispatch('login')
    }
  }
}
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
}