app-form-part.vue 4.8 KB
Newer Older
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 61 62 63 64 65 66 67 68 69 70 71 72 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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
<template>
 <div class='app-form-part' v-if="loadState">
    <avue-form :option="formOption" v-model="formVal"></avue-form>
  </div>
</template>
<script lang = 'ts'>
import { Vue, Component, Prop, Watch } from 'vue-property-decorator';
import { Subject, Subscription } from "rxjs";
@Component({
})
export default class AppFormPart extends Vue {

  /**
   * 系统名称
   *
   * @type {string}
   * @memberof AppFormPart
   */
  @Prop() public systemCodeName!: string;

  /**
   * 应用名称
   *
   * @type {string}
   * @memberof AppFormPart
   */
  @Prop() public appCodeName!: string;

  /**
   * 实体名称
   *
   * @type {string}
   * @memberof AppFormPart
   */
  @Prop() public deCodeName!: string;

  /**
   * 表单代码标识
   *
   * @type {string}
   * @memberof AppFormPart
   */
  @Prop() public formCodeName!: string;

  /**
   * 表单成员标识
   *
   * @type {string}
   * @memberof AppFormPart
   */
  @Prop() public formDetailCodeName!: string;

  /**
   * 当前表单项名称
   *
   * @type {string}
   * @memberof AppFormPart
   */
  @Prop() public name!: string;

  /**
   * 导航上下文
   *
   * @type {string}
   * @memberof AppFormPart
   */
  @Prop() public context!: any;

  /**
   * 导航参数
   *
   * @type {string}
   * @memberof AppFormPart
   */
  @Prop() public viewparams!: any;

  /**
   * 表单数据
   *
   * @type {any}
   * @memberof AppFormPart
   */
  @Prop() public data: any;

  /**
   * 表单状态对象
   *
   * @type {Subject<any>}
   * @memberof AppFormPart
   */
  @Prop() public formState!: Subject<any>;

  /**
   * 订阅对象
   *
   * @protected
   * @type {(Subscription | undefined)}
   * @memberof AppFormPart
   */
  public formStateEvent: Subscription | undefined;

  /**
   * 远程地址
   *
   * @protected
   * @type {(Subscription | undefined)}
   * @memberof AppFormPart
   */
  public remoteUrl:string =`/lite/${this.systemCodeName.toLowerCase()}-${this.appCodeName.toLowerCase()}/components/${this.formDetailCodeName.toLowerCase()}`;

  /**
   * 动态表单配置对象
   *
   * @type {*}
   * @memberof AppFormPart
   */
  public formOption:any;

  /**
   * 当前动态表单绑定值
   *
   * @type {*}
   * @memberof AppFormPart
   */
  public curFormValue:any = {};

  /**
   * 获取动态表单值对象
   *
   * @type {*}
   * @memberof AppFormPart
   */
  get formVal(){
      return this.curFormValue;
  }

  /**
   * 设置动态表单值对象
   *
   * @type {*}
   * @memberof AppFormPart
   */
  set formVal(data:any){
      this.$emit("change",{name:this.name,value:data});
  }

  /**
   * 加载数据状态
   *
   * @type {*}
   * @memberof AppFormPart
   */
  public loadState:boolean = false;

  /**
   * 初始化组件(vue生命周期)
   *
   * @type {Subject<any>}
   * @memberof AppFormPart
   */
  public created(){
    if (this.formState) {
      this.formStateEvent = this.formState.subscribe(({ type, data }) => {
        if (Object.is("load", type)){
            this.loadRemoteFormModel().then((result:any) =>{
                this.initStateData(result);
            })
        }
      });
    }
  }

  /**
   * 初始化状态数据
   *
   * @type {Subject<any>}
   * @memberof AppFormPart
   */
  public initStateData(modelData:any){
    this.computedFormVal(modelData);
    this.formOption = modelData;
    this.loadState = true;
  }

  /**
   * 计算动态表单绑定数据
   *
   * @type {Subject<any>}
   * @memberof AppFormPart
   */
  public  computedFormVal(modelData:any){
    this.curFormValue = {};
    if(modelData && modelData.column && modelData.column.length > 0){
        modelData.column.forEach((element:any) => {
            Object.assign(this.curFormValue,{[element.prop]:null});
        });
    }
    if(Object.keys(this.curFormValue).length > 0){
        Object.keys(this.curFormValue).forEach((item:any) =>{
            if(this.data && this.data[item]){
                this.curFormValue[item] = this.data[item];
            }
        })
    }
  }

  /**
   * 加载动态表单数据模型
   *
   * @type {Subject<any>}
   * @memberof AppFormPart
   */
  public loadRemoteFormModel(){
    return new Promise((resolve:any,reject:any) =>{
        this.$http.get(this.remoteUrl).then((res:any) =>{
            if(res.status && res.status == 200){
                let result:any = res.data;
                resolve(result);
            }else{
                console.warn("加载动态表单模型数据异常");
            }
        }).catch((error:any) =>{
            console.warn("加载动态表单模型数据异常");
        })
    })
  }

  /**
   * 销毁组件(vue生命周期)
   *
   * @type {Subject<any>}
   * @memberof AppFormPart
   */
  public destroy(){
      if(this.formStateEvent){
        this.formStateEvent.unsubscribe();
      }
  }

}
</script>
<style lang = "less">
@import './app-form-part.less';
</style>