基于request进行请求的二次封装
export function getCreater<T>(url: string, params?: any) {
return request<T>(`${url}`, {
method: 'GET',
params,
});
}
export function postCreater<T>(url: string, params: any) {
return request<T>(`${url}`, {
method: 'POST',
data: stringify(params),
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
},
});
}
export function postJSONCreater<T>(url: string, params: any) {
return request<T>(`${url}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
data: JSON.stringify(params),
});
}
使用案例
get
export async function getCardBankList(params?: Record<string, any>) {
return getCreater<API.SuccessResponse>('/admin/card-secret/library/list', params);
}
const invoke=async (params = {}) => {
const res = await getCardBankList({
...params,
});
console.log(res);
}
post
export async function postSavePostage(params?: API.ParamsType) {
return postCreater<API.SuccessResponse>('/admin/delivery/save-postage', params);
}
const invoke=async()=>{
const getData = await postSavePostage({
...newValues,
});
}
post json
export async function editCardBankInfo(params?: Record<string, any>) {
return postJSONCreater<API.SuccessResponse>('/admin/card-secret/library/edit', params);
}
...
const invoke=async()=>{
const getRes = await editCardBankInfo(getValues);
}