Преглед на файлове

增加环境变量配置门户跳转

dengshaobo преди 4 седмици
родител
ревизия
17fff9c780
променени са 7 файла, в които са добавени 74 реда и са изтрити 20 реда
  1. 3 1
      .env.development
  2. 3 1
      .env.production
  3. 3 1
      .env.test
  4. 46 0
      src/utils/env.ts
  5. 3 12
      src/views/school/index.vue
  6. 3 5
      src/views/school/teacherList.vue
  7. 13 0
      src/vite-env.d.ts

+ 3 - 1
.env.development

@@ -1 +1,3 @@
-VITE_API_BASE_URL = ""
+VITE_API_BASE_URL = ""
+# 门户跳转域名(开发环境使用测试门户)
+VITE_PORTAL_BASE_URL = "https://index.k12100.net"

+ 3 - 1
.env.production

@@ -1 +1,3 @@
-VITE_API_BASE_URL = "https://www.huijiaoyan.com/admin/"
+VITE_API_BASE_URL = "https://www.huijiaoyan.com/admin/"
+# 门户跳转域名(生产环境)
+VITE_PORTAL_BASE_URL = "https://www.k12100.com"

+ 3 - 1
.env.test

@@ -1,4 +1,6 @@
 # 测试环境 API 基础路径
 VITE_API_BASE_URL="https://admin.k12100.net/admin/"
 # 环境标识
-VITE_APP_ENV=test
+VITE_APP_ENV=test
+# 门户跳转域名(测试环境)
+VITE_PORTAL_BASE_URL="https://index.k12100.net"

+ 46 - 0
src/utils/env.ts

@@ -0,0 +1,46 @@
+// 门户跳转域名(生产:官网域名;测试/开发:测试域名)
+// 域名切换只需修改 .env.* 中的 VITE_PORTAL_BASE_URL,无需改动业务代码
+export const PORTAL_BASE_URL: string = import.meta.env.VITE_PORTAL_BASE_URL || ''
+
+// 是否为生产环境(使用 Vite 内置 MODE,与 npm run build / build:test 的 --mode 保持一致)
+export const IS_PROD: boolean = import.meta.env.MODE === 'production'
+
+/**
+ * 构建门户跳转 URL
+ * @param hashPath hash 路由部分,如 '/otherLogin'
+ * @param query    查询参数对象,如 { type: 1, schoolId: 'xx', ticket: 'xx' }
+ * @returns        完整 URL,如 https://www.k12100.com/#/otherLogin?type=1&schoolId=xx&ticket=xx
+ */
+export const buildPortalUrl = (
+  hashPath: string,
+  query: Record<string, string | number | boolean | undefined | null> = {}
+): string => {
+  const qs = Object.entries(query)
+    .filter(([, v]) => v !== undefined && v !== null && v !== '')
+    .map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`)
+    .join('&')
+  const path = hashPath.startsWith('/') ? hashPath : `/${hashPath}`
+  return `${PORTAL_BASE_URL}/#${path}${qs ? `?${qs}` : ''}`
+}
+
+/**
+ * 打开门户 otherLogin 页面(教师/学校统一入口)
+ * @param type     0=学校入口,1=教师入口
+ * @param schoolId 学校 ID
+ * @param ticket   一次性票据
+ * @param userId   用户 ID(type=1 时必传)
+ */
+export const openOtherLogin = (
+  type: 0 | 1,
+  schoolId: string | number,
+  ticket: string,
+  userId?: string | number
+) => {
+  const url = buildPortalUrl('/otherLogin', {
+    type,
+    schoolId,
+    id: userId,
+    ticket,
+  })
+  window.open(url)
+}

+ 3 - 12
src/views/school/index.vue

@@ -225,6 +225,7 @@ import { ref, reactive, provide, onMounted, nextTick, onUnmounted } from 'vue'
 import { useRouter } from 'vue-router'
 import { getSchoolNumber, getSchoolList, changeStatus ,getTicket, getSearchList} from '@/api/school'
 import { throttle } from 'lodash';
+import { openOtherLogin } from '@/utils/env'
 
 const router = useRouter()
 
@@ -639,18 +640,8 @@ const GoTeach = (row: any) => {
   }
   
   getTicket(params).then((res: any) => {
-    const ticket = res.data;
-    let url = `https://www.k12100.com/#/otherLogin?type=0&schoolId=${row.id}&ticket=${ticket}`;
-
-    if(import.meta.env.VITE_API_BASE_URL == 'https://www.huijiaoyan.com/admin/')
-    {
-      url = `https://www.k12100.com/#/otherLogin?type=0&schoolId=${row.id}&ticket=${ticket}`;
-    }
-    else
-    {
-      url = `https://index.k12100.net/#/otherLogin?type=0&schoolId=${row.id}&ticket=${ticket}`;
-    }
-    window.open(url);
+    // 门户域名由 .env.* 中的 VITE_PORTAL_BASE_URL 统一配置,切换环境/域名无需改代码
+    openOtherLogin(0, row.id, res.data)
   }).catch(() => {
    
   })

+ 3 - 5
src/views/school/teacherList.vue

@@ -218,6 +218,7 @@ import { ElMessage } from 'element-plus'
 import { throttle } from 'lodash'
 import * as TabSearch from '@/baseComponents/TabSearch.vue'
 import { getAllTeacher, getTicket, updateTeacherTextbook } from '@/api/school'
+import { openOtherLogin } from '@/utils/env'
 
 type EditField = 'textbookVersions' | 'textbook'
 
@@ -518,11 +519,8 @@ const GoEnter = (row: Teacher) => {
   }
 
   getTicket(params).then((res: any) => {
-    const ticket = res.data
-    const isProd = import.meta.env.VITE_API_BASE_URL === 'https://www.huijiaoyan.com/admin/'
-    const baseUrl = isProd ? 'https://www.k12100.com' : 'https://index.k12100.net'
-    const url = `${baseUrl}/#/otherLogin?type=1&schoolId=${schoolId.value}&id=${row.userId}&ticket=${ticket}`
-    window.open(url)
+    // 门户域名由 .env.* 中的 VITE_PORTAL_BASE_URL 统一配置,切换环境/域名无需改代码
+    openOtherLogin(1, schoolId.value, res.data, row.userId)
   }).catch(() => {})
 }
 

+ 13 - 0
src/vite-env.d.ts

@@ -1,5 +1,18 @@
 /// <reference types="vite/client" />
 
+interface ImportMetaEnv {
+  /** 后台接口基础地址 */
+  readonly VITE_API_BASE_URL: string
+  /** 门户(C 端)跳转基础地址 */
+  readonly VITE_PORTAL_BASE_URL: string
+  /** 环境标识:test / production 等 */
+  readonly VITE_APP_ENV?: string
+}
+
+interface ImportMeta {
+  readonly env: ImportMetaEnv
+}
+
 interface Window {
   $global: { [key: string]: (...args: any[]) => any };
 }