import { ControlServiceBase } from './control-service-base'; import { Util, HttpResponse } from '../utils'; /** * 向导面板部件服务基类 * * @export * @class WizardPanelServiceBase * @extends {ControlServiceBase} */ export class WizardPanelServiceBase extends ControlServiceBase { /** * 初始化向导 * * @param {string} action * @param {*} [context={}] * @param {*} [data={}] * @param {boolean} [isLoading] * @returns {Promise} * @memberof WizardPanelServiceBase */ public async init(action: string, context: any = {}, data: any = {}, isLoading?: boolean): Promise { await this.onBeforeAction(action, context, data, isLoading); data = this.handleRequestData(action, context, data); let response: HttpResponse; if (Util.isFunction(this.service[action])) { response = await this.service[action](context, data, isLoading); } else { response = await this.service.Create(context, data); } if (!response.isError()) { response = this.handleResponse(action, response); } return response; } /** * 向导结束 * * @param {string} action * @param {*} [context={}] * @param {*} [data={}] * @param {boolean} [isLoading] * @returns {Promise} * @memberof WizardPanelServiceBase */ public async finish(action: string, context: any = {}, data: any = {}, isLoading?: boolean): Promise { await this.onBeforeAction(action, context, data, isLoading); data = this.handleRequestData(action, context, data); let response: HttpResponse; if (Util.isFunction(this.service[action])) { response = await this.service[action](context, data, isLoading); } else { response = await this.service.Update(context, data); } if (!response.isError()) { response = this.handleResponse(action, response); } return response; } /** * 处理数据 * * @param {string} action 行为名称 * @param {*} [data] * @returns * @memberof MdServiceBase */ public handleResponseData(action: string, data: any) { if (!this.model || !Util.isFunction(this.model.getDataItems)) { return data; } const result: any = {}; const dataItems: any[] = this.model.getDataItems(); const tempData: any = data; if (!tempData) { Object.assign(result, tempData); } else if (tempData instanceof Array) { if (tempData.length > 0) { tempData.forEach((item: any) => { dataItems.forEach(dataitem => { let val = item.hasOwnProperty(dataitem.prop) ? item[dataitem.prop] : null; if (!val) { val = item.hasOwnProperty(dataitem.name) ? item[dataitem.name] : null; } item[dataitem.name] = val; }); }); Object.assign(result, tempData); } else { Object.assign(result, {}); } } else { dataItems.forEach(dataitem => { let val = tempData.hasOwnProperty(dataitem.prop) ? tempData[dataitem.prop] : null; if (!val) { val = tempData.hasOwnProperty(dataitem.name) ? tempData[dataitem.name] : null; } if (action != 'Remove') { tempData[dataitem.name] = val; } }); Object.assign(result, tempData); } return result; } }