app.js 5.53 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: '',
  showWindow: false
}
import router from '@/router'

// 检测是否为移动端
function isMobileDevice () {
  return /(phone|pad|pod|iphone|ipod|ios|ipad|android|mobile)/i.test(navigator.userAgent);
}
// 综合环境判断
function detectEnvironment () {
  const ua = navigator.userAgent.toLowerCase();
  const isWxWork = ua.includes('wxwork');
  const isWechat = ua.includes('micromessenger');
  const isMobile = isMobileDevice();

  if (isWxWork) {
    return 1
  } else {
    return isMobile ? 2 : 3
  }
}
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)
  },
  // 设置name
  setUserInfo ({ commit }, user) {
    commit('SET_USER_INFO', user)
    store.set(store.consts.USER_KEY, user)
  },
  login ({ dispatch }) {
    let [redirectUrl] = window.location.href.split('?')
    const wwcode = getParams('code')
    if (wwcode) {
      const { corpId, agentId } = util.getLocalStorage('ww_storage_info')
      ssoApi
        .login({
          code: wwcode,
          corpId,
          agentId
        })
        .then(res => {
          let redirect_uri = util.getLocalStorage('ww_redirect_uri'),
            url = `${redirectUrl.split('#')[0]}#/`
          if (res.errCode == 0 && res.userId) {
            util.setLocalStorage('ww_userInfo', {
              userId: res.userId,
              entmicroId: corpId
            })
            if (redirect_uri) {
              let [URL, query] = decodeURIComponent(redirect_uri).split('?')
              URL = `${URL}?userId=${res.userId}`
              if (query) URL = `${URL}&${query}`
              window.location.href = URL
            }
            else window.location.href = url
          } else {
            Notify(res.errMsg)
          }
        })
    } else {
      util.showLoading()
      let appid = getParams('corpId') //从链接获取
      let agentid = getParams('agentId') //从链接获取
      let storage_info = util.getLocalStorage('ww_storage_info')
      const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV)
      if (!appid) {
        if (storage_info) {
          appid = storage_info.corpId
          agentid = storage_info.agentId
        } else {
          if (IS_PROD) {
            Notify({
              message: '缺少corpId!',
              duration: 15000
            })
            return
          } else {
            appid = 'wwa55ffac1225eb25e'
            agentid = 1000015
          }
        }
      }
      let ww_storage_info = { corpId: appid },
        env = detectEnvironment()
      if (agentid && agentid != 'undefined') ww_storage_info.agentId = agentid
      util.setLocalStorage('ww_storage_info', ww_storage_info)
      if (env == 1) {
        let wxUrl = 'https://open.weixin.qq.com/connect/oauth2/authorize'
        wxUrl = `${wxUrl}?appid=${appid}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code`
        if (agentid && agentid != 'undefined') {
          wxUrl = `${wxUrl}&scope=snsapi_privateinfo&agentid=${agentid}`
          ww_storage_info.agentId = agentid
        }
        else wxUrl = `${wxUrl}&scope=snsapi_base`

        window.location.href = `${wxUrl}&state=STATE#wechat_redirect`
      } else if (env == 2) {
        Notify({
          message: '请使用企业微信打开',
          duration: 999999999
        })
      } else {
        //扫码
        router.replace({ name: 'common.wwLoginPanel' })
      }

    }
  },
  navigation ({ dispatch }, to) {
    let { redirect_uri, corpId, agentId } = getParams()
    let userInfo = util.getLocalStorage('ww_userInfo')
    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 (redirect_uri) util.setLocalStorage('ww_redirect_uri', redirect_uri)
    let storage_info = util.getLocalStorage('ww_storage_info')
    if (storage_info && userInfo) {
      if (storage_info.corpId != userInfo.entmicroId) {
        util.showLoading()
        removeStoreage()
      }
    }
    login({
      dispatch,
      to
    })
  },
}

const login = ({ dispatch }) => {
  let userInfo = util.getLocalStorage('ww_userInfo')
  if (!userInfo) {
    state.showWindow = false
    util.showLoading()
    dispatch('login')
    return
  }
  state.showWindow = true

}
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
}
const removeStoreage = () => {
  util.removeLocalStorage('ww_userInfo')
}
export default {
  state,
  mutations,
  actions
}