umi项目关于请求的封装


基于request进行请求的二次封装

// get请求
export function getCreater<T>(url: string, params?: any) {
  return request<T>(`${url}`, {
    method: 'GET',
    params,
  });
}

// post请求
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',
    },
  });
}

// post JSON请求
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);
}

文章作者: ycs
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 ycs !
  目录