api 使用手册
完整规范请参考
https://www.yuque.com/xsued/book/lz3qo4
- 初始化
import { registerModule } from '@xsyx/easy-api-h5';
export const commonApi = registerModule({
prefix: 'http://test.xsts.xyz',
module:{},
loading: true,
mock: true,
timeout: 15* 1000,
standard: (response)=>{
const {rspCode, data, rspDesc} = response
return {
code: rspCode,
success: rspCode === 'success',
data: data,
message: rspDesc
}
},
headers: {
Authorization: async function(){
return await kylinApi.getToken();
}
},
hooks:{
afterFetch:[
(url, response, params, options)=>{
console.log(response)
}
],
beforeFetch:[
(url, response, params, options)=>{
console.log(response)
}
]
},
plugins:{
toast(option){
const instance = Message.info(option);
return ()=>{
instance.close();
}
}
loading(options){
Loading.service(options)
}
}
})
-
在 modules 中配置接口基本信息
export default {
fetchSomeData: {
url: "/sample/data/normal",
mockUrl: "/mock/data/normal",
mockData: { rspCode: "success", data: [] },
cached: "5000",
sensitive: "11",
method: "POST",
concurrency: false,
mock: false,
fit: (rsp, params) => {
return rsp;
},
repeat: 3,
feed: (rsp, params) => {
},
fail(rsp, params) {
},
},
};
-
在 api.js 中注册对应的模块,同时导出。
- 导出的变量名,既是最终使用时调用的名字。
- 如: api.brandHouse.fetch1(params, options)
- 其中 brandHouse 就是 模块名
- params 就是传给服务端的数据,例如
api.brandHouse.fetch1(
{
bcd: 123,
abc: 1344,
},
options
);
api.brandHouse.fetch1(
{a: 1 b:2},
{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}
);
- options 包含如下两个参数
- loading false string|bool|object
- silence false bool 是否在出错的时候静默