dengshaobo il y a 1 an
Parent
commit
9f2085b6bd
10 fichiers modifiés avec 114 ajouts et 38 suppressions
  1. 1 1
      .env.development
  2. 11 17
      src/api/login.ts
  3. 3 1
      src/main.ts
  4. 10 1
      src/router/index.ts
  5. 5 0
      src/types/vue.d.ts
  6. 5 5
      src/utils/request.ts
  7. 33 0
      src/views/layout/index.vue
  8. 26 9
      src/views/login/login.vue
  9. 10 1
      tsconfig.json
  10. 10 3
      vite.config.ts

+ 1 - 1
.env.development

@@ -1 +1 @@
-VITE_API_BASE_URL = "/api"
+VITE_API_BASE_URL = ""

+ 11 - 17
src/api/login.ts

@@ -1,30 +1,24 @@
 // src/api/login.ts
-import request from '@/utils/request'
+import request from '../utils/request.ts'
+
+
 
-export interface LoginParams {
-  username: string
-  password: string
-}
 
-export interface LoginResponse {
-  token: string
-  userId: number
-}
 
 // 登录接口
-export function login(data: LoginParams) {
-  return request<LoginResponse>({
-    url: '/user/login',
+export function login(data:any) {
+  return request({
+    url: '/api/v1/teach_login/sign_up',
     method: 'post',
     data
   })
 }
 
 // 获取用户信息
-export function getUserInfo(token: string) {
+export function getUserInfo() {
   return request({
-    url: '/user/info',
-    method: 'get',
-    params: { token }
+    url: '/api/v1/pc_personal/find_user_info',
+    method: 'get'
   })
-}
+}
+

+ 3 - 1
src/main.ts

@@ -2,6 +2,7 @@ import { createApp } from 'vue'
 import './style.css'
 import App from './App.vue'
 import ElementPlus from 'element-plus'
+
 import 'element-plus/dist/index.css'
 import * as ElIcons from '@element-plus/icons-vue'
 import router from './router' // 引入路由配置
@@ -9,7 +10,8 @@ import { setupStore } from './store' // 引入 store 初始化方法
 const app = createApp(App)
 Object.keys(ElIcons).forEach((key) => {
   app.component(key, ElIcons[key])
-})
+})//ele icon
+
 app.use(ElementPlus)
 app.use(router) // 注册路由
 setupStore(app) // 注册 Vuex Store

+ 10 - 1
src/router/index.ts

@@ -3,11 +3,20 @@ import type { RouteRecordRaw } from 'vue-router'
 
 // 示例路由配置
 const routes: Array<RouteRecordRaw> = [
-
+  {
+    path: '/',
+    name: 'Login',
+    component: () => import('../views/login/login.vue')
+  },
   {
     path: '/login',
     name: 'Login',
     component: () => import('../views/login/login.vue')
+  },
+  {
+    path: '/main',
+    name: 'main',
+    component: () => import('../views/layout/index.vue')
   }
 ]
 

+ 5 - 0
src/types/vue.d.ts

@@ -0,0 +1,5 @@
+declare module '*.vue' {
+  import type { DefineComponent } from 'vue'
+  const component: DefineComponent<{}, {}, any>
+  export default component
+}

+ 5 - 5
src/utils/request.ts

@@ -5,18 +5,18 @@ import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios'
 
 // 创建 axios 实例
 const service: AxiosInstance = axios.create({
-  baseURL: import.meta.env.VITE_API_BASE_URL || '/api', // 从环境变量中读取
+  baseURL: import.meta.env.VITE_API_BASE_URL || '', // 从环境变量中读取
   timeout: 10000, // 超时时间
   withCredentials: true // 是否允许携带 cookie
 })
 
 // 请求拦截器
 service.interceptors.request.use(
-  (config: AxiosRequestConfig) => {
+  (config) => {
     // 可在此添加 token 到 headers 中
     const token = localStorage.getItem('token')
     if (token) {
-      config.headers!['Authorization'] = `Bearer ${token}`
+      config.headers!['Authorization'] = `${token}`
     }
     return config
   },
@@ -42,7 +42,7 @@ service.interceptors.response.use(
       // 特殊状态码处理,如 token 失效等
       if (res.code === 401) {
         // 跳转登录页
-        window.location.href = '/login'
+        // window.location.href = '/login'
       }
 
       return Promise.reject(res.message || '请求失败')
@@ -61,4 +61,4 @@ service.interceptors.response.use(
   }
 )
 
-export default service
+export default service

+ 33 - 0
src/views/layout/index.vue

@@ -0,0 +1,33 @@
+<template>
+  <div>
+    <!-- 页面内容 -->
+  </div>
+</template>
+
+<script setup>
+import { onMounted, onBeforeUnmount } from 'vue'
+
+// 组件注册(如有子组件)
+// import SomeComponent from '@/components/SomeComponent.vue'
+
+// 响应式数据(替代 data)
+// const someData = ref('')
+
+// 生命周期钩子(替代 mounted / beforeDestroy)
+onMounted(() => {
+  // 初始化逻辑
+})
+
+onBeforeUnmount(() => {
+  // 清理逻辑
+})
+
+// 方法定义(替代 methods)
+// function someMethod() {
+//   // ...
+// }
+</script>
+
+<style lang="scss" scoped>
+
+</style>

+ 26 - 9
src/views/login/login.vue

@@ -109,11 +109,12 @@
 <script setup>
 import { ref, onMounted } from 'vue'
 // import particles from 'particles.js'
-import { login } from '@/api/login'
-
-
+import { login,getUserInfo } from '@/api/login'
+import { ElMessage } from 'element-plus' // 
+import { useRouter } from 'vue-router'
 
 
+const router = useRouter() // 获取路由实例
 // 响应式数据
 const userName = ref('')
 const passWord = ref('')
@@ -256,17 +257,33 @@ const SubmitLogin = () => {
     }
 
     loadingLogin.value = true
-
     login(params).then((res) => {
         loadingLogin.value = false
-        if (res.code === 200) {
-            localStorage.setItem('token', res.data.token)
-            router.push('/')
+        if (res.code === 200) 
+        {
+        
+            
+            console.log("登录成功返回",res)
+            localStorage.setItem('token', res.data.tokenValue)
+            // 登录成功后调用获取用户信息接口
+            return getUserInfo(res.data.token)
+            
         }
-    }).catch((err) => {
+    }).then((userInfoRes)=>{
+      console.log("获取用户信息返回",userInfoRes)
+       if (userInfoRes && userInfoRes.code === 200) 
+       {
+        // 存储用户信息到 localStorage 或 Pinia
+        localStorage.setItem('userInfo', JSON.stringify(userInfoRes.data))
+        ElMessage.success('登录成功');
+        // 跳转到主页
+        router.push('/main')
+      }
+    })
+    .catch((err) => {
         
         loadingLogin.value = false
-        ElMessage.error('登录失败,请检查账号密码')
+        ElMessage.error('登录失败!'+err);
         console.error(err)
     })
 

+ 10 - 1
tsconfig.json

@@ -3,5 +3,14 @@
   "references": [
     { "path": "./tsconfig.app.json" },
     { "path": "./tsconfig.node.json" }
-  ]
+  ],
+  "compilerOptions": {
+    "moduleResolution": "node",
+    "extensions": [".ts", ".js", ".vue"],
+    "baseUrl": "./",
+    "paths": {
+      "@/*": ["src/*"]
+    }
+  }
+
 }

+ 10 - 3
vite.config.ts

@@ -11,11 +11,18 @@ export default defineConfig({
   },
   server: {
     proxy:{
-      '/adminapi':{
+      '/api':{
+        // target:'https://dev.k12100.net/admin/adminApi',
+        target:'https://dev.k12100.net/teaching/api/',
+        changeOrigin:true,
+        rewrite:path => path.replace(/^\/api/, '')
+      },
+      '/adminApi':{
         target:'https://dev.k12100.net/admin/adminApi',
         changeOrigin:true,
-        rewrite:path => path.replace(/^\/adminapi/, '')
+        rewrite:path => path.replace(/^\/adminApi/, '')
       }
-    }
+    },
+    
   }//配置代理
 })