6c39ac3538
* refactor: fields/views/pages... * update * update * update * updates * updates * add yarn.lock * updates * updates * updates * updates * updates * updates * updates * updates * updates * developerMode * 一大波更新 * bugfix * fix: hide the sorting settings * fix: reload menu when menu is updated * 页面重构 * modify text * 补充细节 * system settings * 继续更新补充 * fix: 多级菜单支持 * 无限嵌套 * fix: icon * 省市区参数调整 * 表单描述、文案调整 * 支持草稿 * 邮箱登录 * 细节补充 * 菜单页面权限初步 * 详情页打开方式 * 菜单父级、草稿问题 * 描述文字 * 详情分组显示 * 状态改为 radio * 菜单权限 * 跳过省市区 api * 修复权限数据范围 * onDraft * 页面跳转 * 修改文案 * 注册、登录 * fix: 权限过滤问题 * 微调上传组件样式 * 0.4.0-alpha.0 * father-build * remove father-build * 细节调整
84 lines
2.5 KiB
TypeScript
84 lines
2.5 KiB
TypeScript
import { values } from 'lodash';
|
|
import { stringify } from 'querystring';
|
|
import { request } from 'umi';
|
|
|
|
interface ActionParams {
|
|
resourceKey?: string | number;
|
|
// resourceName?: string;
|
|
// associatedName?: string;
|
|
associatedKey?: string | number;
|
|
fields?: any;
|
|
filter?: any;
|
|
values?: any;
|
|
page?: any;
|
|
perPage?: any;
|
|
[key: string]: any;
|
|
}
|
|
|
|
interface Resource {
|
|
get: (params?: ActionParams) => Promise<any>;
|
|
list: (params?: ActionParams) => Promise<any>;
|
|
create: (params?: ActionParams) => Promise<any>;
|
|
update: (params?: ActionParams) => Promise<any>;
|
|
destroy: (params?: ActionParams) => Promise<any>;
|
|
[name: string]: (params?: ActionParams) => Promise<any>;
|
|
}
|
|
|
|
class ApiClient {
|
|
resource(name: string): Resource {
|
|
const proxy: any = new Proxy({}, {
|
|
get(target, method, receiver) {
|
|
return (params: ActionParams = {}) => {
|
|
let { associatedKey, resourceKey, filter, sorter, sort = [], values, ...restParams } = params;
|
|
let url = `/${name}`;
|
|
sort = sort || [];
|
|
let options: any = {
|
|
params: {},
|
|
};
|
|
if (['list', 'get'].indexOf(method as string) !== -1) {
|
|
options.method = 'get';
|
|
options.params = restParams;
|
|
} else {
|
|
options.method = 'post';
|
|
options.params = restParams;
|
|
options.data = values;
|
|
}
|
|
if (associatedKey) {
|
|
url = `/${name.split('.').join(`/${associatedKey}/`)}`;
|
|
}
|
|
url += `:${method as string}`;
|
|
// console.log(name, name.split('.'), associatedKey, name.split('.').join(`/${associatedKey}/`));
|
|
if (resourceKey) {
|
|
url += `/${resourceKey}`;
|
|
}
|
|
if (filter) {
|
|
options.params['filter'] = JSON.stringify(filter);
|
|
}
|
|
if (sorter) {
|
|
const arr = Array.isArray(sorter) ? sorter : [sorter];
|
|
arr.forEach(({order, field}) => {
|
|
if (order === 'descend') {
|
|
sort.push(`-${field}`);
|
|
} else if (order === 'ascend') {
|
|
sort.push(field);
|
|
}
|
|
});
|
|
}
|
|
if (sort.length === 0) {
|
|
delete options.params['sort'];
|
|
} else {
|
|
options.params['sort'] = sort.join(',');
|
|
}
|
|
console.log({url, params});
|
|
return request(url, options);
|
|
};
|
|
}
|
|
});
|
|
return proxy;
|
|
}
|
|
}
|
|
|
|
const api = new ApiClient();
|
|
|
|
export default api;
|