[Base] 修改 API 接口使得支持 GET 请求参数
All checks were successful
Build / build-and-test (push) Successful in 28s
All checks were successful
Build / build-and-test (push) Successful in 28s
This commit is contained in:
@ -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,
|
||||
|
@ -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
|
||||
}
|
||||
})
|
||||
|
Reference in New Issue
Block a user