添加请求内容~

This commit is contained in:
闵宪瑞 2025-01-07 23:15:27 +08:00
parent 4a1cf186dc
commit 95644362ff
8 changed files with 121 additions and 56 deletions

2
.env
View File

@ -1,7 +1,7 @@
# 通用环境变量
# 前端接口
VITE_API_FRONT_BASE_URL = http://localhost:8081
VITE_API_FRONT_BASE_URL = http://localhost:18080
#后端接口
VITE_ADMIN_API_BASE_URL = http://localhost:18080

View File

@ -1,25 +0,0 @@
/**
*
* @param data
*/
export function login(data: any) {
return http.post("/api/login", data)
}
/**
*
* @param data
*/
export function register(data: any) {
return http.post("/api/register", data)
}
/**
*
* @param data
*/
export function userInfo(userId: any) {
return http.get("/api/userInfo", {
params: { userId: userId }
})
}

View File

@ -0,0 +1,51 @@
import { adminRequest } from '~/composables/adminRequest'
/**
*
* @param data
*/
export function captchaAdmin(uid: Number) {
return adminRequest.get("/captcha",{
params:{uuid:uid}
})
}
/**
*
* @param data
*/
export function loginAdmin(uuid:String,username:String, password:String,captcha:String) {
const data ={
"username": username,
"password": password,
"captcha": captcha,
"uuid": uuid
}
return adminRequest.post("/login", data)
}
/**
*
* @param data
*/
export function registerAdmin(data: any) {
return adminRequest.post("/register", data)
}
/**
* 退
*/
export function logoutAdmin() {
return adminRequest.post("/logout")
}
/**
*
* @param userId
*/
export function userInfoAdmin(userId: any) {
return adminRequest.get("/userInfo", {
params: { userId: userId }
})
}

View File

@ -0,0 +1,14 @@
import { getUuid } from '~/utils/utils'
/**
*
*/
export function getCaptchaUrl(){
const uuid = getUuid()
captchaAdmin(uuid)
return {
uid: uuid,
captchaUrl:import.meta.env.VITE_ADMIN_API_BASE_URL + `/captcha?uuid=${uuid}`
}
}

View File

@ -1,12 +1,17 @@
import axios from 'axios'
export const frontRequest = axios.create({
baseURL: import.meta.env.VITE_API_FRONT_BASE_URL,
export const adminRequest = axios.create({
baseURL: import.meta.env.VITE_ADMIN_API_BASE_URL,
})
// 添加请求拦截器
frontRequest.interceptors.request.use(
adminRequest.interceptors.request.use(
function (config) {
// 在发送请求之前做些什么
const token = userStore().adminToken
if (token !== null || token !== undefined) {
//添加header
config.headers.token = token
}
// 在发送请求之前做些什么
return config
},
@ -17,40 +22,34 @@ frontRequest.interceptors.request.use(
},
)
// 添加响应拦截器
frontRequest.interceptors.response.use(
adminRequest.interceptors.response.use(
function (response) {
// 2xx 范围内的状态码都会触发该函数。
// 对响应数据进行格式化
if (response.data.code){
const code = response.data.code
switch (code) {
case 500:
toast.success(response.data.msg)
break
case 401:
toast.success(response.data.msg)
break
default:
toast.success(response.data.msg)
break
}
}
if (response.data) {
return response.data
}
return response
},
function (error) {
const status = error.response?.status
let { msg, message } = error.response?.data ?? {}
if (!msg && message) {
msg = message
}
if (!msg) {
switch (status) {
case 400:
msg = '参数错误'
break
case 500:
msg = '服务端错误'
break
case 404:
msg = '路由未找到'
break
default:
msg = error.message ?? '未知响应错误'
break
}
}
toast.warning(msg)
// 超出 2xx 范围的状态码都会触发该函数。
// 对响应错误做点什么

View File

@ -1,12 +1,29 @@
<template>
<div class="login-container">
<img :src="state.captchaUrl" />
</div>
</template>
<script setup lang="ts">
import { loginAdmin } from '~/api/user/adminUserApi'
const state = reactive({
captchaUrl: '',
loginFrom: {}
})
const login = reactive({ username: '', password: '', captcha: '', uuid: '' })
function init() {
state.captchaUrl = getCaptchaUrl().captchaUrl
login.uuid = getCaptchaUrl().uid
loginAdmin(login.uuid,login.username,login.password,login.captcha)
}
init()
</script>
<style scoped>
@ -18,7 +35,6 @@
}
</style>
<route lang="json">
{

View File

@ -4,7 +4,7 @@ export default defineStore('userStore', {
state() {
return {
isLogin: false,
token: "",
adminToken: "",
}
},
actions: {

10
src/utils/utils.ts Normal file
View File

@ -0,0 +1,10 @@
/**
* uuid
*/
export const getUuid = (): string => {
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
const r = (Math.random() * 16) | 0,
v = c == "x" ? r : (r & 0x3) | 0x8;
return v.toString(16);
});
};