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 } })