From 63e24791f2b045c530560193e68d73af26027556 Mon Sep 17 00:00:00 2001 From: xingyy <64720302+Concur-max@users.noreply.github.com> Date: Wed, 22 Jan 2025 11:34:36 +0800 Subject: [PATCH] =?UTF-8?q?refactor(api):=20=E9=87=8D=E6=9E=84=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=87=BD=E6=95=B0=E5=B9=B6=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 重构了 http.js 中的 request 函数,使用解构赋值简化参数 - 更新了 auth 和 goods模块中的请求函数,采用新的 request 函数格式 - 优化了代码格式,包括缩进、换行和空格 --- app/api/auth/index.js | 20 +++++++++++--------- app/api/goods/index.js | 24 ++++++++++++++---------- app/api/http.js | 15 +++++++-------- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/app/api/auth/index.js b/app/api/auth/index.js index 5187568..8d5a06d 100644 --- a/app/api/auth/index.js +++ b/app/api/auth/index.js @@ -1,23 +1,25 @@ -import { getHttp } from '~/api/http.js' +import { request } from '@/api/http.js' export async function senCode(data) { - const http = getHttp() - return await http('/api/v1/m/user/send', { + + return await request({ + url:'/api/v1/m/user/send', method: 'POST', body: data, }) } export async function userLogin(data) { - const http = getHttp() - return await http('/api/v1/m/user/login', { + + return await request( { + url:'/api/v1/m/user/login', method: 'POST', - body: data, + data }) } export async function userUpdate(data) { - const http = getHttp() - return await http('/api/v1/m/user/update', { + return await request( { + url:'/api/v1/m/user/update', method: 'POST', - body: data, + data }) } \ No newline at end of file diff --git a/app/api/goods/index.js b/app/api/goods/index.js index 75999ff..bcdf66a 100644 --- a/app/api/goods/index.js +++ b/app/api/goods/index.js @@ -1,28 +1,32 @@ -import { request } from '~/api/http.js' +import { request } from '@/api/http.js' export async function artworkList(data) { - - return await request('/api/v1/m/auction/default/artwork/list', { + return await request( { + url:'/api/v1/m/auction/default/artwork/list', method: 'POST', - body: data, + data }) } export async function defaultDetail(data) { - return await request('/api/v1/m/auction/default/detail', { + return await request ({ + url:'/api/v1/m/auction/default/detail', method: 'POST', - body: data, + data }) } export async function artworkDetail(data) { - return await request('/api/v1/m/artwork/detail', { + return await request( { + url:'/api/v1/m/artwork/detail', method: 'POST', - body: data, + data, }) } export async function userArtworks(data) { - return await request('/api/v1/m/user/artworks', { + + return await request( { + url:'/api/v1/m/user/artworks', method: 'POST', - body: data, + data }) } \ No newline at end of file diff --git a/app/api/http.js b/app/api/http.js index 18159ec..6540a25 100644 --- a/app/api/http.js +++ b/app/api/http.js @@ -1,9 +1,9 @@ -import { useRuntimeConfig } from '#app' -import { ofetch} from 'ofetch' -import { message } from '@/components/x-message/useMessage.js' -import { authStore } from "@/stores/auth/index.js" +import {useRuntimeConfig} from '#app' +import {ofetch} from 'ofetch' +import {message} from '@/components/x-message/useMessage.js' +import {authStore} from "@/stores/auth/index.js" - let httpStatusErrorHandler +let httpStatusErrorHandler let http // HTTP 状态码映射 @@ -120,11 +120,10 @@ export function getHttp() { } // 导出请求工具函数 -export async function request(url, options) { +export async function request({url,...options}) { const http = getHttp() try { - const response = await http(url, options) - return response.data + return await http(url, {...options,body:options.data}) } catch (error) { throw error }