| 254 | * 单例模式 |
| 255 | */ |
| 256 | export class FetchAPIManager { |
| 257 | private static instance: FetchAPIManager; |
| 258 | |
| 259 | /** |
| 260 | * 获取 FetchAPIManager 实例 |
| 261 | * @param url API URL |
| 262 | * @param fm fetchManager |
| 263 | * @returns FetchAPIManager实例 |
| 264 | */ |
| 265 | static getInstance(url: string, fm = fetchManager) { |
| 266 | if (!FetchAPIManager.instance || FetchAPIManager.instance.url !== url) { |
| 267 | FetchAPIManager.instance = new FetchAPIManager(url, fm); |
| 268 | } |
| 269 | return FetchAPIManager.instance; |
| 270 | } |
| 271 | |
| 272 | private url: string; |
| 273 | private fm: FetchManager; |
| 274 | |
| 275 | constructor(url: string, fm = fetchManager) { |
| 276 | this.url = url.charAt(url.length - 1) === '/' ? url : `${url}/`; |
| 277 | this.fm = fm; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * 获取实例列表 |
| 282 | * @param data 筛选参数 |
| 283 | * @param custom FetchCustomParams |
| 284 | * @returns Promise |
| 285 | */ |
| 286 | get = (data?: any, custom?: FetchCustomParams) => this.fm.get(this.url, data, custom); |
| 287 | |
| 288 | /** |
| 289 | * 创建实例 |
| 290 | * @param data 数据 |
| 291 | * @param custom FetchCustomParams |
| 292 | * @returns Promise |
| 293 | */ |
| 294 | create = (data: any, custom?: FetchCustomParams) => this.fm.post(this.url, data, custom); |
| 295 | |
| 296 | /** |
| 297 | * 获取实例详情 |
| 298 | * @param id 实例ID |
| 299 | * @param data 参数 |
| 300 | * @param custom FetchCustomParams |
| 301 | * @returns Promise |
| 302 | */ |
| 303 | getDetail = (id: number | string, data?: any, custom?: FetchCustomParams) => this.fm.get(`${this.url}${id}/`, data, custom); |
| 304 | |
| 305 | /** |
| 306 | * 更新实例 |
| 307 | * @param id 实例ID |
| 308 | * @param data 更新数据 |
| 309 | * @param custom FetchCustomParams |
| 310 | * @returns Promise |
| 311 | */ |
| 312 | update = (id: number | string, data: any, custom?: FetchCustomParams) => this.fm.put(`${this.url}${id}/`, data, custom); |
| 313 | |