Przeglądaj źródła

免登录页面

丸子 7 miesięcy temu
rodzic
commit
2a71c369f0
3 zmienionych plików z 192 dodań i 7 usunięć
  1. 12 0
      src/router/index.js
  2. 33 7
      src/views/Login.vue
  3. 147 0
      src/views/QuickLogin.vue

+ 12 - 0
src/router/index.js

@@ -6,6 +6,8 @@ const routes = [
 
   { path: '/', component: () => import('../views/Login.vue') },
   { path: '/login', component: () => import('../views/Login.vue') },
+  // 免登录S
+  { path: '/quick-login', component: () => import('../views/QuickLogin.vue') },
   // 首页
   {
     path: '/home',
@@ -46,6 +48,16 @@ const routes = [
     path: '/ai-painting',
     component: () => import('../views/AIPainting.vue')
   },
+  // 图生图
+  {
+    path: '/ai-image',
+    component: () => import('../views/AIImageToImage.vue')
+  },
+  // 图生视频
+  {
+    path: '/ai-video',
+    component: () => import('../views/AIImageToVideo.vue')
+  },
   // 智能问答
   {
     path: '/ai-questions',

+ 33 - 7
src/views/Login.vue

@@ -9,7 +9,7 @@
     <!-- 登录输入框 -->
     <div class="login-wrapper">
       <div class="login-input">
-         <span>AI课程</span>
+         <span>人工智能通识课平台</span>
         <el-form
           ref="loginFormRef"
           :model="loginData.loginForm"
@@ -62,7 +62,7 @@
 </template>
 
 <script setup>
-import { ref, onMounted, computed } from 'vue'
+import { ref, onMounted, computed ,onUnmounted} from 'vue'
 import { useRouter } from 'vue-router'
 import { HomeFilled, Avatar, Lock } from '@element-plus/icons-vue'
 import { getTenantIdByName, login } from '@/api/login/login.js'
@@ -103,16 +103,19 @@ const getTenantId = async () => {
     if (res && res.data) {
       //记录租户id
       tenantId.value = res.data
+      return true; // 租户验证成功
     } else {
-      ElMessage.error('获取租户 ID 失败,请检查租户名称')
-      return
+      ElMessage.error('租户填写错误!')
+      return false; // 租户验证失败
     }
   } catch (error) {
-    ElMessage.error('获取租户 ID 出错,请重试')
+    ElMessage.error('租户填写错误!')
     console.error('获取租户 ID 错误:', error)
-    return
+    return false; // 租户验证失败
   }
 }
+
+
 // 登录
 const handleLogin = async params => {
   if (!loginFormRef.value) return
@@ -120,7 +123,13 @@ const handleLogin = async params => {
     if (valid) {
       loginLoading.value = true
       try {
-        await getTenantId()
+        // 先验证租户是否存在
+        const tenantValid = await getTenantId()
+        if (!tenantValid) {
+          // 租户验证失败,不执行登录
+          loginLoading.value = false
+          return
+        }
         const loginDataLoginForm = { ...loginData.value.loginForm }
         // 构建包含 headers 的请求参数
         const res = await login(
@@ -183,6 +192,8 @@ const handleLogin = async params => {
   })
 }
 
+
+
 // 在组件挂载时检查登录状态和恢复登录信息
 onMounted(() => {
   const storedStatus = localStorage.getItem('isLoggedIn') // isLoggedIn
@@ -207,6 +218,21 @@ onMounted(() => {
     isLoggedIn.value = true
     router.push('/home')
   }
+
+   const handleKeyPress = (event) => {
+    // 检查是否按下回车键(keyCode 13)
+    if (event.key === 'Enter' || event.keyCode === 13) {
+      handleLogin()
+    }
+  }
+  
+  document.addEventListener('keydown', handleKeyPress)
+  
+  // 在组件卸载时移除事件监听
+  onUnmounted(() => {
+    document.removeEventListener('keydown', handleKeyPress)
+  })
+
 })
 </script>
 

+ 147 - 0
src/views/QuickLogin.vue

@@ -0,0 +1,147 @@
+<template>
+  <div class="quick-login-container">
+    <div class="loading-overlay">
+      <div class="loading-content">
+        <el-loading-spinner class="loading-spinner"></el-loading-spinner>
+        <p class="loading-text">登录中...</p>
+      </div>
+    </div>
+  </div>
+</template>
+
+<script setup>
+import { onMounted } from 'vue'
+import { useRouter } from 'vue-router'
+import { getTenantIdByName, login } from '@/api/login/login.js'
+import { ElLoading, ElMessage } from 'element-plus'
+
+const router = useRouter()
+let loading = null
+
+// 测试账号信息
+const testAccount = {
+  tenantName: '人工智能通识课平台',
+  username: 'aiTest',
+  password: 'aiTest' 
+}
+
+// 获取租户 ID
+const getTenantId = async (tenantName) => {
+  try {
+    const res = await getTenantIdByName(tenantName)
+    if (res && res.data) {
+      return res.data
+    } else {
+      ElMessage.error('租户验证失败!')
+      return null
+    }
+  } catch (error) {
+    ElMessage.error('获取租户信息失败!')
+    console.error('获取租户 ID 错误:', error)
+    return null
+  }
+}
+
+// 自动登录函数
+const autoLogin = async () => {
+  try {
+    // 显示全局加载状态
+    loading = ElLoading.service({
+      lock: true,
+      text: '登录中...',
+      background: 'rgba(0, 0, 0, 0.7)'
+    })
+
+    // 获取租户ID
+    const tenantId = await getTenantId(testAccount.tenantName)
+    if (!tenantId) {
+      // 租户验证失败
+      return
+    }
+
+    // 执行登录
+    const res = await login(
+      { 'Tenant-Id': tenantId },
+      testAccount
+    )
+
+    if (res && res.code === 0) {
+      // 登录成功,保存登录状态
+      localStorage.setItem('isLoggedIn', 'true')
+      localStorage.setItem('token', res.data.accessToken)
+      localStorage.setItem('userName', testAccount.username)
+      localStorage.setItem('tenantName', testAccount.tenantName)
+      localStorage.setItem('password', testAccount.password)
+      localStorage.setItem('rememberMe', 'true')
+
+      // 根据账号类型设置可查看的课程小节数
+      if (testAccount.username === 'aiTest') {
+        localStorage.setItem('maxCourseSections', '5')
+      } else if (testAccount.username === 'aiAdmin') {
+        localStorage.setItem('maxCourseSections', 'all')
+      }
+
+      ElMessage.success('自动登录成功')
+      // 跳转到首页
+      router.push('/ai-general-course')
+    } else {
+      ElMessage.error(res?.message || '自动登录失败')
+      // 如果登录失败,跳转到正常登录页面
+      router.push('/login')
+    }
+  } catch (error) {
+    ElMessage.error('自动登录过程中发生错误')
+    console.error('自动登录错误:', error)
+    // 错误时跳转到登录页面
+    router.push('/login')
+  } finally {
+    // 关闭加载状态
+    if (loading) {
+      loading.close()
+    }
+  }
+}
+
+// 组件挂载时立即执行自动登录
+onMounted(() => {
+  autoLogin()
+})
+</script>
+
+<style scoped>
+.quick-login-container {
+  position: fixed;
+  top: 0;
+  left: 0;
+  right: 0;
+  bottom: 0;
+  display: flex;
+  justify-content: center;
+  align-items: center;
+  background: linear-gradient(to bottom, #001169, #8a78d0);
+}
+
+.loading-overlay {
+  display: flex;
+  justify-content: center;
+  align-items: center;
+  width: 100%;
+  height: 100%;
+}
+
+.loading-content {
+  text-align: center;
+  color: white;
+}
+
+.loading-spinner {
+  width: 60px;
+  height: 60px;
+  margin-bottom: 20px;
+}
+
+.loading-text {
+  font-size: 18px;
+  font-weight: 500;
+}
+</style>