chenlei пре 1 дан
родитељ
комит
abf9b8e8ec
1 измењених фајлова са 54 додато и 36 уклоњено
  1. 54 36
      packages/admin/src/views/login.vue

+ 54 - 36
packages/admin/src/views/login.vue

@@ -75,19 +75,65 @@ export default {
   // 方法集合
   // 方法集合
   methods: {
   methods: {
     async handleSSOLogin (code) {
     async handleSSOLogin (code) {
-      const ssoCode =
-        code || (this.$route && this.$route.query && this.$route.query.code) ||
-        new URLSearchParams(window.location.search).get('code')
+      let codes = []
+      if (code) codes = codes.concat(Array.isArray(code) ? code : [code])
+
+      try {
+        const url = new URL(window.location.href)
+        codes = codes.concat(url.searchParams.getAll('code'))
+        if (url.hash) {
+          const hashNo = url.hash.slice(1)
+          const [, hashQuery] = hashNo.split('?')
+          if (hashQuery !== undefined) {
+            const hp = new URLSearchParams(hashQuery)
+            codes = codes.concat(hp.getAll('code'))
+          }
+        }
+      } catch (e) {
+        try {
+          const sp = new URLSearchParams(window.location.search || '')
+          codes = codes.concat(sp.getAll('code'))
+          const hashNo = (window.location.hash || '').replace(/^#/, '')
+          const qIdx = hashNo.indexOf('?')
+          if (qIdx !== -1) {
+            const hashQuery = hashNo.slice(qIdx + 1)
+            const hp = new URLSearchParams(hashQuery)
+            codes = codes.concat(hp.getAll('code'))
+          }
+        } catch (e2) {
+        }
+      }
+
+      if (this.$route && this.$route.query && this.$route.query.code) {
+        const r = this.$route.query.code
+        codes = codes.concat(Array.isArray(r) ? r : [r])
+      }
+
+      const ssoCode = codes.length ? codes[codes.length - 1] : null
+      console.log(ssoCode)
       if (!ssoCode) return
       if (!ssoCode) return
-      const res = await ssoLogin({
-        code: ssoCode
-      })
-      // 清理 URL 中的 code 参数,避免再次使用或泄露
+
       try {
       try {
-        this.removeCodeFromUrl()
+        const url = new URL(window.location.href)
+        url.searchParams.delete('code')
+        if (url.hash) {
+          const hashNo = url.hash.slice(1)
+          const [hashPath, hashQuery] = hashNo.split('?')
+          if (hashQuery !== undefined) {
+            const hp = new URLSearchParams(hashQuery)
+            hp.delete('code')
+            const newHash = hashPath + (hp.toString() ? ('?' + hp.toString()) : '')
+            url.hash = newHash ? ('#' + newHash) : ''
+          } else {
+            url.hash = hashNo ? ('#' + hashNo) : ''
+          }
+        }
+        history.replaceState(null, '', url.toString())
       } catch (e) {
       } catch (e) {
         // ignore
         // ignore
       }
       }
+
+      const res = await ssoLogin({ code: ssoCode })
       if (res.code === 0) {
       if (res.code === 0) {
         localStorage.setItem('JMYZU_token', res.data.token)
         localStorage.setItem('JMYZU_token', res.data.token)
         localStorage.setItem('JMYZU_userInfo', JSON.stringify(res.data.user))
         localStorage.setItem('JMYZU_userInfo', JSON.stringify(res.data.user))
@@ -95,32 +141,6 @@ export default {
         this.$message.success('登录成功')
         this.$message.success('登录成功')
       } else this.$message.warning(res.msg)
       } else this.$message.warning(res.msg)
     },
     },
-
-    removeCodeFromUrl () {
-      // 使用 URL API 删除 query 中的 code,并用 history.replaceState 替换当前地址,保留 hash
-      try {
-        const u = new URL(window.location.href)
-        if (!u.searchParams.has('code')) return
-        u.searchParams.delete('code')
-        const newUrl = u.origin + u.pathname + u.search + u.hash
-        history.replaceState(null, '', newUrl)
-      } catch (err) {
-        const href = window.location.href
-        const parts = href.split('#')
-        const beforeHash = parts[0]
-        const hash = parts[1] ? '#' + parts[1] : ''
-        const qIndex = beforeHash.indexOf('?')
-        if (qIndex === -1) return
-        const base = beforeHash.substring(0, qIndex)
-        const query = beforeHash.substring(qIndex + 1)
-        const params = query
-          .split('&')
-          .filter(p => p.split('=')[0] !== 'code')
-          .join('&')
-        const newBefore = params ? base + '?' + params : base
-        history.replaceState(null, '', newBefore + hash)
-      }
-    },
     async login () {
     async login () {
       try {
       try {
         await this.$refs.ruleForm.validate()
         await this.$refs.ruleForm.validate()
@@ -174,8 +194,6 @@ export default {
       this.handleSSOLogin(code)
       this.handleSSOLogin(code)
     }
     }
   },
   },
-  // 生命周期 - 挂载完成(可以访问DOM元素)
-  mounted () {},
   beforeCreate () {}, // 生命周期 - 创建之前
   beforeCreate () {}, // 生命周期 - 创建之前
   beforeMount () {}, // 生命周期 - 挂载之前
   beforeMount () {}, // 生命周期 - 挂载之前
   beforeUpdate () {}, // 生命周期 - 更新之前
   beforeUpdate () {}, // 生命周期 - 更新之前