feat: Axios 响应数据支持通过泛型推导
This commit is contained in:
parent
f3a62504d6
commit
61675f74ba
@ -1,5 +1,5 @@
|
|||||||
/** 模拟接口响应数据 */
|
/** 模拟接口响应数据 */
|
||||||
const SELECT_DATA = {
|
const SELECT_RESPONSE_DATA = {
|
||||||
code: 0,
|
code: 0,
|
||||||
data: [
|
data: [
|
||||||
{
|
{
|
||||||
@ -21,12 +21,12 @@ const SELECT_DATA = {
|
|||||||
|
|
||||||
/** 模拟接口 */
|
/** 模拟接口 */
|
||||||
export function getSelectDataApi() {
|
export function getSelectDataApi() {
|
||||||
return new Promise<typeof SELECT_DATA>((resolve, reject) => {
|
return new Promise<typeof SELECT_RESPONSE_DATA>((resolve, reject) => {
|
||||||
// 模拟接口响应时间 2s
|
// 模拟接口响应时间 2s
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// 模拟接口调用成功
|
// 模拟接口调用成功
|
||||||
if (Math.random() < 0.8) {
|
if (Math.random() < 0.8) {
|
||||||
resolve(SELECT_DATA)
|
resolve(SELECT_RESPONSE_DATA)
|
||||||
} else {
|
} else {
|
||||||
// 模拟接口调用出错
|
// 模拟接口调用出错
|
||||||
reject(new Error("接口发生错误"))
|
reject(new Error("接口发生错误"))
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/** 模拟接口响应数据 */
|
/** 模拟接口响应数据 */
|
||||||
const SUCCESS_DATA = {
|
const SUCCESS_RESPONSE_DATA = {
|
||||||
code: 0,
|
code: 0,
|
||||||
data: {},
|
data: {},
|
||||||
message: "获取成功"
|
message: "获取成功"
|
||||||
@ -7,9 +7,9 @@ const SUCCESS_DATA = {
|
|||||||
|
|
||||||
/** 模拟请求接口成功 */
|
/** 模拟请求接口成功 */
|
||||||
export function getSuccessApi() {
|
export function getSuccessApi() {
|
||||||
return new Promise<typeof SUCCESS_DATA>((resolve) => {
|
return new Promise<typeof SUCCESS_RESPONSE_DATA>((resolve) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
resolve(SUCCESS_DATA)
|
resolve(SUCCESS_RESPONSE_DATA)
|
||||||
}, 1000)
|
}, 1000)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { request } from "@/utils/service"
|
import { request } from "@/utils/service"
|
||||||
|
|
||||||
export interface ILoginData {
|
export interface ILoginRequestData {
|
||||||
/** admin 或 editor */
|
/** admin 或 editor */
|
||||||
username: "admin" | "editor"
|
username: "admin" | "editor"
|
||||||
/** 密码 */
|
/** 密码 */
|
||||||
@ -9,16 +9,21 @@ export interface ILoginData {
|
|||||||
code: string
|
code: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LoginCodeResponseData = IApiResponseData<string>
|
||||||
|
type LoginResponseData = IApiResponseData<{ token: string }>
|
||||||
|
type UserInfoResponseData = IApiResponseData<{ username: string; roles: string[] }>
|
||||||
|
|
||||||
/** 获取登录验证码 */
|
/** 获取登录验证码 */
|
||||||
export function getLoginCodeApi() {
|
export function getLoginCodeApi() {
|
||||||
return request({
|
return request<LoginCodeResponseData>({
|
||||||
url: "login/code",
|
url: "login/code",
|
||||||
method: "get"
|
method: "get"
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/** 登录并返回 Token */
|
/** 登录并返回 Token */
|
||||||
export function loginApi(data: ILoginData) {
|
export function loginApi(data: ILoginRequestData) {
|
||||||
return request({
|
return request<LoginResponseData>({
|
||||||
url: "users/login",
|
url: "users/login",
|
||||||
method: "post",
|
method: "post",
|
||||||
data
|
data
|
||||||
@ -26,7 +31,7 @@ export function loginApi(data: ILoginData) {
|
|||||||
}
|
}
|
||||||
/** 获取用户详情 */
|
/** 获取用户详情 */
|
||||||
export function getUserInfoApi() {
|
export function getUserInfoApi() {
|
||||||
return request({
|
return request<UserInfoResponseData>({
|
||||||
url: "users/info",
|
url: "users/info",
|
||||||
method: "get"
|
method: "get"
|
||||||
})
|
})
|
||||||
|
@ -1,17 +1,17 @@
|
|||||||
import { request } from "@/utils/service"
|
import { request } from "@/utils/service"
|
||||||
|
|
||||||
interface ICreateTableDataApi {
|
interface ICreateTableRequestData {
|
||||||
username: string
|
username: string
|
||||||
password: string
|
password: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IUpdateTableDataApi {
|
interface IUpdateTableRequestData {
|
||||||
id: string
|
id: string
|
||||||
username: string
|
username: string
|
||||||
password?: string
|
password?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
interface IGetTableDataApi {
|
interface IGetTableRequestData {
|
||||||
/** 当前页码 */
|
/** 当前页码 */
|
||||||
currentPage: number
|
currentPage: number
|
||||||
/** 查询条数 */
|
/** 查询条数 */
|
||||||
@ -21,8 +21,21 @@ interface IGetTableDataApi {
|
|||||||
phone?: string
|
phone?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GetTableResponseData = IApiResponseData<{
|
||||||
|
list: {
|
||||||
|
createTime: string
|
||||||
|
email: string
|
||||||
|
id: string
|
||||||
|
phone: string
|
||||||
|
roles: string
|
||||||
|
status: boolean
|
||||||
|
username: string
|
||||||
|
}[]
|
||||||
|
total: number
|
||||||
|
}>
|
||||||
|
|
||||||
/** 增 */
|
/** 增 */
|
||||||
export function createTableDataApi(data: ICreateTableDataApi) {
|
export function createTableDataApi(data: ICreateTableRequestData) {
|
||||||
return request({
|
return request({
|
||||||
url: "table",
|
url: "table",
|
||||||
method: "post",
|
method: "post",
|
||||||
@ -39,7 +52,7 @@ export function deleteTableDataApi(id: string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 改 */
|
/** 改 */
|
||||||
export function updateTableDataApi(data: IUpdateTableDataApi) {
|
export function updateTableDataApi(data: IUpdateTableRequestData) {
|
||||||
return request({
|
return request({
|
||||||
url: "table",
|
url: "table",
|
||||||
method: "put",
|
method: "put",
|
||||||
@ -48,8 +61,8 @@ export function updateTableDataApi(data: IUpdateTableDataApi) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/** 查 */
|
/** 查 */
|
||||||
export function getTableDataApi(params: IGetTableDataApi) {
|
export function getTableDataApi(params: IGetTableRequestData) {
|
||||||
return request({
|
return request<GetTableResponseData>({
|
||||||
url: "table",
|
url: "table",
|
||||||
method: "get",
|
method: "get",
|
||||||
params
|
params
|
||||||
|
@ -4,7 +4,7 @@ import { defineStore } from "pinia"
|
|||||||
import { usePermissionStore } from "./permission"
|
import { usePermissionStore } from "./permission"
|
||||||
import { getToken, removeToken, setToken } from "@/utils/cache/cookies"
|
import { getToken, removeToken, setToken } from "@/utils/cache/cookies"
|
||||||
import router, { resetRouter } from "@/router"
|
import router, { resetRouter } from "@/router"
|
||||||
import { type ILoginData, loginApi, getUserInfoApi } from "@/api/login"
|
import { type ILoginRequestData, loginApi, getUserInfoApi } from "@/api/login"
|
||||||
import { type RouteRecordRaw } from "vue-router"
|
import { type RouteRecordRaw } from "vue-router"
|
||||||
|
|
||||||
export const useUserStore = defineStore("user", () => {
|
export const useUserStore = defineStore("user", () => {
|
||||||
@ -17,14 +17,14 @@ export const useUserStore = defineStore("user", () => {
|
|||||||
roles.value = value
|
roles.value = value
|
||||||
}
|
}
|
||||||
/** 登录 */
|
/** 登录 */
|
||||||
const login = (loginData: ILoginData) => {
|
const login = (loginData: ILoginRequestData) => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
loginApi({
|
loginApi({
|
||||||
username: loginData.username,
|
username: loginData.username,
|
||||||
password: loginData.password,
|
password: loginData.password,
|
||||||
code: loginData.code
|
code: loginData.code
|
||||||
})
|
})
|
||||||
.then((res: any) => {
|
.then((res) => {
|
||||||
setToken(res.data.token)
|
setToken(res.data.token)
|
||||||
token.value = res.data.token
|
token.value = res.data.token
|
||||||
resolve(true)
|
resolve(true)
|
||||||
@ -38,7 +38,7 @@ export const useUserStore = defineStore("user", () => {
|
|||||||
const getInfo = () => {
|
const getInfo = () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
getUserInfoApi()
|
getUserInfoApi()
|
||||||
.then((res: any) => {
|
.then((res) => {
|
||||||
roles.value = res.data.roles
|
roles.value = res.data.roles
|
||||||
username.value = res.data.username
|
username.value = res.data.username
|
||||||
resolve(res)
|
resolve(res)
|
||||||
|
@ -88,7 +88,7 @@ function createService() {
|
|||||||
|
|
||||||
/** 创建请求方法 */
|
/** 创建请求方法 */
|
||||||
function createRequestFunction(service: AxiosInstance) {
|
function createRequestFunction(service: AxiosInstance) {
|
||||||
return function (config: AxiosRequestConfig) {
|
return function <T>(config: AxiosRequestConfig): Promise<T> {
|
||||||
const configDefault = {
|
const configDefault = {
|
||||||
headers: {
|
headers: {
|
||||||
// 携带 Token
|
// 携带 Token
|
||||||
|
@ -5,7 +5,7 @@ import { useUserStore } from "@/store/modules/user"
|
|||||||
import { User, Lock, Key, Picture, Loading } from "@element-plus/icons-vue"
|
import { User, Lock, Key, Picture, Loading } from "@element-plus/icons-vue"
|
||||||
import ThemeSwitch from "@/components/ThemeSwitch/index.vue"
|
import ThemeSwitch from "@/components/ThemeSwitch/index.vue"
|
||||||
import { type FormInstance, FormRules } from "element-plus"
|
import { type FormInstance, FormRules } from "element-plus"
|
||||||
import { type ILoginData, getLoginCodeApi } from "@/api/login"
|
import { type ILoginRequestData, getLoginCodeApi } from "@/api/login"
|
||||||
|
|
||||||
const router = useRouter()
|
const router = useRouter()
|
||||||
const loginFormRef = ref<FormInstance | null>(null)
|
const loginFormRef = ref<FormInstance | null>(null)
|
||||||
@ -15,7 +15,7 @@ const loading = ref(false)
|
|||||||
/** 验证码图片 URL */
|
/** 验证码图片 URL */
|
||||||
const codeUrl = ref("")
|
const codeUrl = ref("")
|
||||||
/** 登录表单数据 */
|
/** 登录表单数据 */
|
||||||
const loginForm: ILoginData = reactive({
|
const loginForm: ILoginRequestData = reactive({
|
||||||
username: "admin",
|
username: "admin",
|
||||||
password: "12345678",
|
password: "12345678",
|
||||||
code: ""
|
code: ""
|
||||||
@ -61,7 +61,7 @@ const createCode = () => {
|
|||||||
loginForm.code = ""
|
loginForm.code = ""
|
||||||
// 获取验证码
|
// 获取验证码
|
||||||
codeUrl.value = ""
|
codeUrl.value = ""
|
||||||
getLoginCodeApi().then((res: any) => {
|
getLoginCodeApi().then((res) => {
|
||||||
codeUrl.value = res.data
|
codeUrl.value = res.data
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ const getTableData = () => {
|
|||||||
username: searchData.username || undefined,
|
username: searchData.username || undefined,
|
||||||
phone: searchData.phone || undefined
|
phone: searchData.phone || undefined
|
||||||
})
|
})
|
||||||
.then((res: any) => {
|
.then((res) => {
|
||||||
paginationData.total = res.data.total
|
paginationData.total = res.data.total
|
||||||
tableData.value = res.data.list
|
tableData.value = res.data.list
|
||||||
})
|
})
|
||||||
|
6
types/api.d.ts
vendored
Normal file
6
types/api.d.ts
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
/** 所有 api 接口的响应数据都应该准守该格式 */
|
||||||
|
interface IApiResponseData<T> {
|
||||||
|
code: number
|
||||||
|
data: T
|
||||||
|
message: string
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user