import React from "react" ; import { onlyUpdateForKeys } from "recompose" ; import Immutable from "immutable" ; import { BaseFactory , MergeLib } from "fx-schema-form-core" ; import { DefaultProps } from "fx-schema-form-react/libs/components" ; import { UtilsHocOutProps } from "fx-schema-form-react/libs/hocs/utils" ; import { RC , FxUiSchema } from "fx-schema-form-react/libs/models/index" ; import { ValidateHocOutProps } from "fx-schema-form-react/libs/hocs/validate" ; import schemaFormReact from "fx-schema-form-react" ; import { JSONSchema6 } from "json-schema" ; import { ConditionHocOutProps } from "fx-schema-form-extension/dist/dts/hocs/condition" ; import { IInterfaceModel } from "modelproxy/out/models/interface" ; import { IExecute } from "modelproxy/out/models/execute" ; const { SchemaForm , schemaFormTypes } = schemaFormReact ; export interface Props extends DefaultProps , UtilsHocOutProps , ValidateHocOutProps , ConditionHocOutProps { } export const name = "proxy" ; export interface ProxyHocSetting { proxyApi ? : IInterfaceModel ; options ? : IExecute ; dataTo ? : string [ ] ; dataFilter ? : ( data : any ) => any ; } export interface ProxyHocOutProps { exeucte ? : ( props : DefaultProps ) => void ; } param hocFactory param Component export const hoc = ( hocFactory ) => { return ( settings = { } ) => { return ( Component ) : RC < Props , any > => { @ ( onlyUpdateForKeys ( [ "condition" ] ) as any ) @ ( hocFactory . get ( "data" ) ( { meta : true , metaKeys : [ "isProxyLoaded" ] } ) as any ) class ComponentHoc extends React . PureComponent < Props , any > { private execute ( props , executeOptions ? ) { const { getOptions , updateItemMeta } = props , options = getOptions ( this . props , schemaFormTypes . hoc , name , Immutable . fromJS ( settings ) ) ; let params , timeId ; if ( ! options . proxyApi || ! options . dataTo ) { return ; } timeId = setTimeout ( ( ) => { updateItemMeta ( props , null , { isLoading : true , } ) ; } , 200 ) ; params = Immutable . fromJS ( options . options || { } ) ; if ( executeOptions ) { params = params . mergeDeep ( executeOptions ) ; } options . proxyApi . execute ( params . toJS ( ) ) . then ( ( data ) => { let dataTo = Immutable . fromJS ( { } ) ; clearTimeout ( timeId ) ; dataTo = dataTo . setIn ( options . dataTo , options . dataFilter ? options . dataFilter ( data ) : data ) ; updateItemMeta ( props , null , { isLoading : false , ... dataTo . toJS ( ) } ) ; } ) . catch ( ( e ) => { clearTimeout ( timeId ) ; updateItemMeta ( props , null , { isValid : false , isLoading : false , dirty : true , errorText : `接口请求失败【${ e . message }】` } ) ; } ) ; } param props public async componentDidUpdate ( props ) { const { formItemMeta } = props , isProxyLoaded = formItemMeta ? formItemMeta . get ( "isProxyLoaded" ) : false ; if ( ! isProxyLoaded ) { this . execute ( props ) ; } } public componentWillMount ( ) { this . componentDidUpdate ( this . props ) ; } /**
                 * 渲染组件
                 */ public render ( ) { return < Component { ... this . props } execute = { this . execute } / > ; } } return ComponentHoc as any ; } ; } ; } ; export default { name , hoc } ;