app.js 5.29 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'
import store from '@/store'

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

import router from '@/router'

const EVECTION_AUTH_PATHS = ['/department', '/personage', '/company']
const APPROVAL_AUTH_PATHS = ['/orderApproval', '/accountApproval']

function saveUrlParams() {
  const { corpId, agentId, phone, name, id, orderCode, uuid } = util.getQuery()
  if (phone) util.setLocalStorage('ww_params_phone', phone)
  if (name) util.setLocalStorage('ww_params_name', name)
  if (id) util.setLocalStorage('ww_params_id', id)
  if (uuid) util.setLocalStorage('ww_params_uuid', uuid)
  if (orderCode) util.setLocalStorage('ww_orderCode', orderCode)
  if (corpId) {
    const ww_storage_info = util.getLocalStorage('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)
  }
}

function needWwAuth(href) {
  const pushId = util.getQuery('pushId')
  const isEvectionPath = EVECTION_AUTH_PATHS.some(keyword => href.includes(keyword))
  const isApprovalPath = APPROVAL_AUTH_PATHS.some(keyword => href.includes(keyword))
  if (isEvectionPath && pushId && pushId * 1 > 744) return true
  if (isApprovalPath) return true
  return false
}

async function handleWwLogin({ commit }) {
  const { corpId, agentId } = util.getQuery()
  const wwCode = util.getQuery('code')
  const { loginUrl } = window.$fetchConfig
  let userInfo = util.getLocalStorage('ww_userInfo') || ''

  if (userInfo && userInfo.timestamp) {
    if (Date.now() - userInfo.timestamp <= 1000 * 60 * 1440) {
      if (userInfo.phone) util.setLocalStorage('ww_params_phone', userInfo.phone)
      commit('SET_AUTHORIZE_PAGE', true)
      commit('SET_SHOW_WINDOW', true)
      return
    }
  }

  if (wwCode) {
    const res = await ssoApi.login({ code: wwCode, corpId, getUserInfoFlag: false })
    const { code, data, message } = res
    if (code == '0') {
      const hrefUrl = window.location.href
      const newUrl = hrefUrl.replace(/([?&])code=[^&]*/, (match, prefix) => (prefix === '?' ? '?' : ''))
      window.history.replaceState({}, '', newUrl)
      if (data.loginList && data.loginList.length) {
        const [item] = data.loginList
        if (item.loginList && item.loginList.length) {
          const [loginUserInfo] = item.loginList
          if (loginUserInfo.phone) util.setLocalStorage('ww_params_phone', loginUserInfo.phone)
          loginUserInfo.timestamp = Date.now()
          util.setLocalStorage('ww_userInfo', loginUserInfo)
          commit('SET_AUTHORIZE_PAGE', true)
          commit('SET_SHOW_WINDOW', true)
          return
        }
      }
      throw new Error('登录失败或无权限')
    }
    Notify({ message })
    commit('SET_SHOW_WINDOW', false)
    commit('SET_AUTHORIZE_PAGE', false)
    return
  }

  commit('SET_AUTHORIZE_PAGE', false)
  const ua = navigator.userAgent.toLowerCase()
  if (ua.match(/MicroMessenger/i) && ua.match(/wxwork/i)) {
    window.location.href = `${loginUrl}?redirect_uri=${encodeURIComponent(window.location.href)}&corpId=${corpId}&agentId=${agentId}&backLoginCode=BACKCODE`
  } else {
    Notify({ message: '请使用企业微信打开', duration: 72000000 })
  }
}

const mutations = {
  SET_PAGE_META(state, meta) {
    state.pageMeta = meta
  },
  SET_LAST_PATH(state, path) {
    state.lastPath = path
  },
  SET_SHOW_WINDOW(state, showWindow) {
    state.showWindow = showWindow
  },
  SET_AUTHORIZE_PAGE(state, authorizePage) {
    state.authorizePage = authorizePage
  }
}

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({ commit }) {
    // 缓存 corpId/agentId 等公共参数,再进入 OAuth
    saveUrlParams()
    const currentPath = window.location.href
    if (needWwAuth(currentPath)) {
      handleWwLogin({ commit }).catch(error => {
        const message = error && error.message ? error.message : '登录失败或无权限'
        Notify(message)
      })
    } else {
      commit('SET_AUTHORIZE_PAGE', true)
      commit('SET_SHOW_WINDOW', true)
    }
  },
  navigation({ dispatch, commit }) {
    saveUrlParams()
    const userInfo = util.getLocalStorage('ww_userInfo')
    dispatch('registerHttp')
    if (userInfo) {
      commit('SET_SHOW_WINDOW', true)
    } else {
      util.showLoading()
      dispatch('login')
    }
  }
}
export default {
  state,
  mutations,
  actions
}