form-service-base.ts 3.8 KB
Newer Older
ibizdev's avatar
ibizdev committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
import { ControlServiceBase } from './control-service-base';
import { HttpResponse, Util } from '../utils';

/**
 * 表单部件服务基类
 *
 * @export
 * @class FormServiceBase
 * @extends {ControlServiceBase}
 */
export class FormServiceBase extends ControlServiceBase {

    /**
     * 处理数据
     *
     * @protected
     * @param {HttpResponse} res
     * @returns {Promise<any[]>}
     * @memberof GridServiceBase
     */
    protected async doItems(res: HttpResponse): Promise<any[]> {
        if (res.status === 200) {
            return [res.data];
        }
        return [];
    }

    /**
     * 启动工作流
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isLoading]
     * @returns {Promise<HttpResponse>}
     * @memberof FormServiceBase
     */
    public async wfstart(action: string, context: any = {}, data: any = {}, isLoading: boolean): Promise<HttpResponse> {
        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);
        } else {
            response = await this.service.Create(context, data);
        }
        if (!response.isError()) {
            response = this.handleResponse(action, response);
        }
        await this.onAfterAction(action, context, response);
        return response;
    }

    /**
     * 提交工作流
     *
     * @param {string} action
     * @param {*} [context={}]
     * @param {*} [data={}]
     * @param {boolean} [isLoading]
zcdtk's avatar
zcdtk committed
61
     * @param {*} [wfdata]
ibizdev's avatar
ibizdev committed
62 63 64
     * @returns {Promise<HttpResponse>}
     * @memberof FormServiceBase
     */
zcdtk's avatar
zcdtk committed
65
    public async wfsubmit(action: string, context: any = {}, data: any = {}, isLoading?: boolean, wfdata?: any): Promise<HttpResponse> {
ibizdev's avatar
ibizdev committed
66 67 68 69 70 71
        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);
        } else {
zcdtk's avatar
zcdtk committed
72
            response = await this.service.WFSubmit(context, data, wfdata);
ibizdev's avatar
ibizdev committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        }
        if (!response.isError()) {
            response = this.handleResponse(action, response);
        }
        await this.onAfterAction(action, context, response);
        return response;
    }

    /**
     * 处理返回数据
     *
     * @param {string} action
     * @param {*} [data={}]
     * @param {boolean} [isCreate]
     * @returns {*}
     * @memberof FormServiceBase
     */
    public handleResponseData(action: string, data: any = {}, isCreate?: boolean): any {
        if (!this.model || !Util.isFunction(this.model.getDataItems)) {
            return data;
        }
        const item: any = {};
        const dataItems: any[] = this.model.getDataItems();
        dataItems.forEach(dataitem => {
            let val = data.hasOwnProperty(dataitem.prop) ? data[dataitem.prop] : null;
            if (!val) {
                val = data.hasOwnProperty(dataitem.name) ? data[dataitem.name] : null;
            }
            if ((isCreate === undefined || isCreate === null) && Object.is(dataitem.dataType, 'GUID') && Object.is(dataitem.name, 'srfkey') && (val && !Object.is(val, ''))) {
                isCreate = true;
            }
            item[dataitem.name] = val;
        });
        if (isCreate) {
            if (!item.srfuf) {
                Object.assign(item, { srfuf: '0' });
            }
        } else {
            if (!item.srfuf) {
                Object.assign(item, { srfuf: '1' });
            }
        }
        Object.assign(data, item);
        return data;
    }

}