From eb5b28249fe383f9f5cfc3b941de22e08f00749f Mon Sep 17 00:00:00 2001 From: UnknownMp Date: Sat, 24 May 2025 10:33:57 +0800 Subject: [PATCH] =?UTF-8?q?[Base]=20=E4=BF=AE=E6=94=B9=20API=20=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=BD=BF=E5=BE=97=E6=94=AF=E6=8C=81=20GET=20=E8=AF=B7?= =?UTF-8?q?=E6=B1=82=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/composables/apiRequest.js | 7 ++++++- src/stores/api.js | 6 +++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/composables/apiRequest.js b/src/composables/apiRequest.js index 9996408..70bb934 100644 --- a/src/composables/apiRequest.js +++ b/src/composables/apiRequest.js @@ -1,6 +1,8 @@ import { useAxios } from '@vueuse/integrations/useAxios' import axios from 'axios' +import { objectToQueryString } from '../utils.js' + function getEndpoint() { const apiMapping = { '': ['http://localhost:28001/', '/api/'], @@ -20,7 +22,10 @@ function replaceUrl(url) { export function useApiRequest(method, url, data, config = {}) { const start = Date.now() const baseURL = getEndpoint() - const fullURL = `${baseURL}${url}` + // 若为 GET 请求,将 data 转为查询参数拼接到 URL 上 + const fullURL = method === 'GET' && data + ? `${baseURL}${url}?${objectToQueryString(data)}` + : `${baseURL}${url}` const { response, error, diff --git a/src/stores/api.js b/src/stores/api.js index dc7d4d5..3065bc6 100644 --- a/src/stores/api.js +++ b/src/stores/api.js @@ -8,13 +8,13 @@ export const useApiStore = defineStore('api', () => { return await execute() } - async function postData(url, payload) { - const { execute } = useApiRequest('POST', url, payload) + async function workSimpleSearch(keyword, page = 1) { + const { execute } = useApiRequest('GET', 'search/simple', { keyword, page }) return await execute() } return { getWork, - postData, + workSimpleSearch } })