import { IUdkModuleConfiger, IUdkModuleConfig, IUdkSerivces } from '@amc-udk/core' import { ApiRepository } from '@amc-udk/config' import { EoConfiger, SystemConfiger, EoBaseModel, IUwpModuleConfig } from '@amc-udk/uwp' import { IIrsApis } from '@amc-udk/types' import { IrsApis, IrsModelOptionsForWeb } from './config' import { IrsService } from './services' import { IrsModels } from './models' const namespace = 'irs' class IIrsConfiger extends IUdkModuleConfiger { init = (config: IUwpModuleConfig) => { if (!EoConfiger.isInitialized()) { EoConfiger.setRuntime(this.getRuntime()) EoConfiger.init({ namespace: 'eo', request: config.request, }) } /** 依赖System模块的某些功能 */ if (!SystemConfiger.isInitialized()) { SystemConfiger.setRuntime(this.getRuntime()) SystemConfiger.init({ namespace: 'system', request: config.request, }) } /** 初始化接口地址 */ ApiRepository.set(namespace, IrsApis) /** 不指定服务接口,则使用默认实现 */ if (!config.services) { IrsService.init() config.services = { _default: IrsService, } } super.init(config) // this.initModel() /** 加载models */ this.loadModels(config.modules || Object.keys(IrsModelOptionsForWeb)) } loadModels = (modules: (number | string)[]) => { if (modules) { modules.forEach(module => { let model = IrsModels[module] if (model) { if (!model.state) { model.state = {} } model.state.url = ApiRepository.get(`/irs/${module}`) } else { model = { namespace: module as string, state: { url: ApiRepository.get(`/irs/${module}`) }, } } this.getRuntime().loadModel( this.getRuntime().createModel( model, this.getRuntime().isWeb() ? IrsModelOptionsForWeb[module] : {}, EoBaseModel, ), ) }) } } initModel = () => { let options = {} if (this.getRuntime().isMobile()) { /** * 移动端和网页端的初始化参数不同: * 1. 存储类型 */ options = { module: namespace, loadStateOnload: true, storageType: 'async', } } else { options = { module: namespace, storeable: true, loadStateOnload: true, storageType: 'local', createdByContainer: false, } } const model = this.getRuntime().createModel( { namespace, state: { url: ApiRepository.get(`/irs/${namespace}`) } }, options, EoBaseModel, ) this.getRuntime().loadModel(model) } } export const IrsConfiger = new IIrsConfiger()