feat: 提交请求封装

This commit is contained in:
Json_Lee
2025-04-19 12:15:28 +08:00
parent a4c360ee52
commit 294e818487
26 changed files with 1543 additions and 162 deletions
+3 -26
View File
@@ -1,30 +1,7 @@
<script setup lang="ts">
import HelloWorld from './components/HelloWorld.vue'
</script>
<script setup lang="ts"></script>
<template>
<div>
<a href="https://vite.dev" target="_blank">
<img src="/vite.svg" class="logo" alt="Vite logo" />
</a>
<a href="https://vuejs.org/" target="_blank">
<img src="./assets/vue.svg" class="logo vue" alt="Vue logo" />
</a>
</div>
<HelloWorld msg="Vite + Vue" />
<router-view />
</template>
<style scoped>
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vue:hover {
filter: drop-shadow(0 0 2em #42b883aa);
}
</style>
<style scoped></style>
+4
View File
@@ -0,0 +1,4 @@
import type { LoginDTO } from './types';
import { post } from '@/utils/request';
export const login = (data: LoginDTO) => post('/auth/login', data);
+4
View File
@@ -0,0 +1,4 @@
export interface LoginDTO {
username: string;
password: string;
}
+1
View File
@@ -0,0 +1 @@
export * from './auth';
+10 -13
View File
@@ -1,16 +1,14 @@
<script setup lang="ts">
import { ref } from 'vue'
import { ref } from 'vue';
defineProps<{ msg: string }>()
const count = ref(0)
const count = ref(0);
</script>
<template>
<h1>{{ msg }}</h1>
<div class="card">
<button type="button" @click="count++">count is {{ count }}</button>
<button type="button" @click="count++">
count is {{ count }}
</button>
<p>
Edit
<code>components/HelloWorld.vue</code> to test HMR
@@ -19,19 +17,18 @@ const count = ref(0)
<p>
Check out
<a href="https://vuejs.org/guide/quick-start.html#local" target="_blank"
>create-vue</a
>, the official Vue + Vite starter
<a href="https://vuejs.org/guide/quick-start.html#local" target="_blank">create-vue</a>, the official Vue + Vite starter
</p>
<p>
Learn more about IDE Support for Vue in the
<a
href="https://vuejs.org/guide/scaling-up/tooling.html#ide-support"
target="_blank"
>Vue Docs Scaling up Guide</a
>.
>Vue Docs Scaling up Guide</a>.
</p>
<p class="read-the-docs">
Click on the Vite and Vue logos to learn more
</p>
<p class="read-the-docs">Click on the Vite and Vue logos to learn more</p>
</template>
<style scoped>
+9 -4
View File
@@ -1,5 +1,10 @@
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import './styles/index.scss';
import 'virtual:uno.css';
createApp(App).mount('#app')
const app = createApp(App);
app.use(router);
app.mount('#app');
+43
View File
@@ -0,0 +1,43 @@
<script setup lang="ts">
import type { LoginDTO } from '@/api/auth/types';
import type { FormInstance } from 'element-plus';
import { login } from '@/api';
import { reactive, ref } from 'vue';
const formRef = ref<FormInstance>();
const formModel = reactive<LoginDTO>({
username: '',
password: '',
});
async function handleSubmit() {
try {
await formRef.value?.validate();
const _res = await login(formModel);
}
catch (error) {
console.error('请求错误:', error);
}
}
</script>
<template>
<div class="w-screen h-screen justify-center items-center flex bg-black">
<div class="max-w-[500px] w-full p-5 rounded bg-white/60 backdrop-blur-md">
<h1>登录</h1>
<el-form ref="formRef" :model="formModel">
<el-form-item label="用户名" name="username">
<el-input v-model="formModel.username" placeholder="请输入用户名" />
</el-form-item>
<el-form-item label="密码" name="password">
<el-input v-model="formModel.password" placeholder="请输入密码" />
</el-form-item>
<el-form-item>
<el-button type="primary" @click="handleSubmit">
登录
</el-button>
</el-form-item>
</el-form>
</div>
</div>
</template>
+24
View File
@@ -0,0 +1,24 @@
import { createRouter, createWebHashHistory } from 'vue-router';
import { jwtGuard } from './permissions';
const routes = [
{ path: '/', component: () => import('@/components/HelloWorld.vue') },
{
path: '/login',
name: 'login',
component: () => import('@/pages/login/index.vue'),
},
];
const router = createRouter({
history: createWebHashHistory(),
routes,
});
console.group('Routes');
console.log('routes', router.getRoutes());
console.groupEnd();
router.beforeEach(jwtGuard());
export default router;
+7
View File
@@ -0,0 +1,7 @@
import type { NavigationGuard } from 'vue-router';
export function jwtGuard(): NavigationGuard {
return () => {
console.log('进入路由守卫');
};
}
+13
View File
@@ -0,0 +1,13 @@
import type {
_Awaitable,
NavigationGuardNext,
NavigationGuardReturn,
RouteLocationNormalized,
RouteLocationNormalizedLoaded,
} from 'vue-router';
export type NavigationGuard = (
to: RouteLocationNormalized,
from: RouteLocationNormalizedLoaded,
next: NavigationGuardNext,
) => _Awaitable<NavigationGuardReturn>;
-79
View File
@@ -1,79 +0,0 @@
:root {
font-family: system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
.card {
padding: 2em;
}
#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}
+7
View File
@@ -0,0 +1,7 @@
.el-form{
.el-form-item{
&:nth-last-child(1){
margin-bottom: 0px;
}
}
}
+6
View File
@@ -0,0 +1,6 @@
@import 'reset-css';
@import './element-plus.scss';
body{
overflow: hidden;
}
+32
View File
@@ -0,0 +1,32 @@
import hookFetch from 'hook-fetch';
import { sseTextDecoderPlugin } from 'hook-fetch/plugins';
interface BaseResponse {
code: number;
data: null;
message: string;
}
const instance = hookFetch.create<BaseResponse, unknown, 'data'>({
baseURL: 'https://web.pandarobot.chat/api',
headers: {
'Content-Type': 'application/json',
},
plugins: [sseTextDecoderPlugin()],
});
function jwtPlugin() {
return {
name: 'jwt',
};
}
instance.use(jwtPlugin());
export const request = instance.request;
export const post = instance.post;
export const get = instance.get;
export default instance;