添加请求内容~
This commit is contained in:
parent
4a1cf186dc
commit
95644362ff
2
.env
2
.env
@ -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
|
||||
|
@ -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 }
|
||||
})
|
||||
}
|
51
src/api/user/adminUserApi.ts
Normal file
51
src/api/user/adminUserApi.ts
Normal 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 }
|
||||
})
|
||||
}
|
14
src/api/user/adminUserUtils.ts
Normal file
14
src/api/user/adminUserUtils.ts
Normal 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}`
|
||||
}
|
||||
}
|
@ -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 范围的状态码都会触发该函数。
|
||||
// 对响应错误做点什么
|
||||
|
@ -1,24 +1,40 @@
|
||||
|
||||
<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>
|
||||
|
||||
.login-container{
|
||||
.login-container {
|
||||
width: 100%;
|
||||
height: 100vh;
|
||||
background-image: url('/e36341619bf8f04dcbdc6b01105a85a.png')
|
||||
}
|
||||
|
||||
|
||||
|
||||
</style>
|
||||
<route lang="json">
|
||||
{
|
||||
|
@ -4,7 +4,7 @@ export default defineStore('userStore', {
|
||||
state() {
|
||||
return {
|
||||
isLogin: false,
|
||||
token: "",
|
||||
adminToken: "",
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
|
10
src/utils/utils.ts
Normal file
10
src/utils/utils.ts
Normal 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);
|
||||
});
|
||||
};
|
Loading…
Reference in New Issue
Block a user