<script>
import config from './config'
import store from '@/store'
import { getToken } from '@/utils/auth'

export default {
  onLaunch: function () {
    this.initApp()
  },
  methods: {
    // 初始化应用
    initApp() {
      // 初始化应用配置
      this.initConfig()
      // 检查用户登录状态
      //#ifdef H5
      this.checkLogin()
      //#endif
    },
    initConfig() {
      this.globalData.config = config
      // 运行路径自动开关 CAS:/app-cas 启用,/app 禁用
      try {
        const path = (window.location && window.location.pathname) || ''
        if (/^\/app-cas\/?/.test(path)) {
          this.globalData.config.casEnable = true
        } else if (/^\/app\/?/.test(path)) {
          this.globalData.config.casEnable = false
        }
      } catch (e) { /* noop */ }
    },
    checkLogin() {
      const whiteList = ['/pages/work/sidebar/safetyDeclaratio/index', '/pages/cas/callback']
      const currentPath = this.$route && this.$route.path
      const hasToken = getToken()
      const cfg = this.globalData && this.globalData.config
      const casEnable = cfg && cfg.casEnable
      const casServer = cfg && cfg.casServer
      const casService = cfg && cfg.casService

      // 检查当前路径
      const path = (window.location && window.location.pathname) || ''

      // 1) 如果当前是 CAS 回调地址,根据来源处理
      const isCasCallbackPath = /\/cas\/cas-callback$/.test(path) || /\/app-cas\/cas-callback$/.test(path)
      if (isCasCallbackPath) {
        try {
          const from = sessionStorage.getItem('pasd_sso_from')
          // 如果是从移动端来的请求且当前在PC端回调路径,则重定向到移动端回调路径
          if (from === 'app-cas' && /^\/cas\/cas-callback$/.test(path)) {
            sessionStorage.removeItem('pasd_sso_from')
            window.location.href = `${window.location.origin}/app-cas/cas-callback${window.location.search || ''}`
            return
          }
          // 如果是从PC端来的请求且当前在移动端回调路径,则重定向到PC端回调路径
          if (!from && /^\/app-cas\/cas-callback$/.test(path)) {
            window.location.href = `${window.location.origin}/cas/cas-callback${window.location.search || ''}`
            return
          }
        } catch (e) { /* noop */ }
        try {
          const u = new URL(window.location.href)
          const ticket = u.searchParams.get('ticket')
          const q = ticket ? `?ticket=${encodeURIComponent(ticket)}` : ''
          this.$tab.reLaunch(`/pages/cas/callback${q}`)
          return
        } catch (e) {
          this.$tab.reLaunch('/pages/cas/callback')
          return
        }
      }

      // 仅在开启 CAS 时走统一认证
      if (casEnable && casServer && casService) {
        // 1.1) 如果落在 /login 并携带 redirect=...cas-callback,强制跳到回调地址(避免看到PC登录页)
        try {
          const u2 = new URL(window.location.href)
          const p2 = u2.pathname || ''
          const red = u2.searchParams.get('redirect') || ''
          if (/^\/login$/.test(p2) && /\/(cas|app-cas)\/cas-callback/.test(red)) {
            window.location.href = `${window.location.origin}${red}`
            return
          }
        } catch (e) { /* noop */ }

        // 2) 在 /app-cas/ 路径下,强制跳转到统一认证登录页(无论是否已登录)
        if (/^\/app-cas\//.test(path)) {
          let effectiveService = casService
          // 使用统一的CAS回调地址,但通过sessionStorage记录来源
          try {
            sessionStorage.setItem('pasd_sso_from', 'app-cas')
          } catch (e) { /* noop */ }
          window.location.href = `${casServer}?service=${encodeURIComponent(effectiveService)}`
          return
        }

        // 3) 其它未登录入口,直接跳转到统一认证登录页
        if (!hasToken) {
          let effectiveService = casService
          // 记录PC端访问来源
          try {
            sessionStorage.removeItem('pasd_sso_from')
          } catch (e) { /* noop */ }
          window.location.href = `${casServer}?service=${encodeURIComponent(effectiveService)}`
          return
        }
      }
      // 未启用 CAS 或非 /app-cas/ 路径且未登录的场景,仍回到系统账号登录页
      if (!hasToken && !whiteList.includes(currentPath)) {
        this.$tab.reLaunch('/pages/login')
      }
    }
  }
}
</script>

<style lang="scss">
@import '@/static/scss/index.scss'
</style>
