import BaseFunc from "../base/func"; import BaseDefine from "../base/define"; import BaseSupport from "../base/support"; import { IbaseFlagResult,IvalueFormData ,IstructFormData} from "../aim/face"; import { IformBase, IformInfo, TformActionType, IformSubmitConfig } from "../aim/form"; import PlusOperatePage from '../operate/page'; import PlusOperateData from '../operate/data'; import PlusCommonEvent from '../common/event' import PlusCommonRegex, { IregexDefine } from '../common/regex'; class CformInfo implements IformInfo { /** * 数据编号 默认为空则是添加 * * @type {string} * @memberOf CformInfo */ baseKey: string = '' /** * 页面操作类型 * * @type {FormActionType} * @memberOf FormInfo */ ationType: TformActionType = "add" /** * 页面链接 * * @type {string} * @memberOf CformInfo */ formPage: string = "" /** * 页面名称 * * @type {string} * @memberOf CformInfo */ formName: string = "" /** * 当前form是否修改过 * * @type {boolean} * @memberOf CformInfo */ flagChange: boolean = false dataInfo = new Map(); } const formOperate = new Map(); class CregexInfo { regexItem = new Map(); } const formRegex = new Map(); export default class { /** * 初始化form数据 * * @static * @param {IformBase} oFormBase * @param {*} oFormData */ static initPageForm(oFormBase: IformBase) { let oForm = new CformInfo(); oForm.formName = oFormBase.formName; oForm.formPage = oFormBase.formPage; let oUrlInfo = PlusOperatePage.upUrlInfo(oForm.formPage); if (!BaseFunc.stringIsEmpty(oUrlInfo.baseKey)) { oForm.baseKey = oUrlInfo.baseKey; let oFormData = PlusOperateData.formData.upFormData(oForm.formName, { key: oUrlInfo.baseKey }); if (oFormData.length > 0) { let oObject = oFormData[0].data; for (var p in oObject) { oForm.dataInfo.set(p, oObject[p]); } } if (oUrlInfo.baseType == 'edit') { oForm.ationType = 'edit'; } else if (oUrlInfo.baseType === 'book') { oForm.ationType = 'book'; } } else { if (BaseFunc.stringIsEmpty(oForm.baseKey)) { oForm.baseKey = BaseSupport.upUuid(); } } //oForm.dataInfo = new Map(); //扔入当前页面临时对象中 formOperate.set(oForm.formName, oForm); } /** * 根据元素名称获取form的名字 * * @static * @param {string} sItemName * @returns {string} */ static upFormNameByItem(sItemName: string): string { return sItemName.split(BaseDefine.upPlusDefine().formFieldSplit)[0]; } /** * 根据元素名称获取当前元素的实际名称 * * @static * @param {string} sItemName * @returns {string} */ static upItemJsonNameByItem(sItemName: string): string { return sItemName.split(BaseDefine.upPlusDefine().formFieldSplit)[1]; } /** * 保存form元素的值 * * @static * @param {string} sItemName * @param {string} sItemValue * @returns {boolean} */ static inFormValue(sItemName: string, sItemValue: string): boolean { var sFormName = this.upFormNameByItem(sItemName); formOperate.get(sFormName).flagChange = true; formOperate.get(sFormName).dataInfo.set(this.upItemJsonNameByItem(sItemName), sItemValue); return true; } static inFormFieldExtendText(sItemName: string, sItemValue: string): boolean { return this.inFormValue(sItemName + BaseDefine.upPlusDefine().formFieldText, sItemValue); } static inFormFieldExtendPage(sItemName: string, sItemValue: string): boolean { return this.inFormValue(sItemName + BaseDefine.upPlusDefine().formFieldPage, sItemValue); } static upFormFieldExtendText(sItemName: string): string { return this.upFormValue(sItemName + BaseDefine.upPlusDefine().formFieldText); } static upFormFieldExtendPage(sItemName: string): string { return this.upFormValue(sItemName + BaseDefine.upPlusDefine().formFieldPage); } /** * 获取扩展页面字段名 * * @static * @param {any} sFieldName * @returns */ static upExtendPageName(sFieldName) { return sFieldName + BaseDefine.upPlusDefine().formFieldPage; } /** * 获取隐藏字段名 * * @static * @param {any} sFieldName * @returns */ static upExtendHideName(sFieldName) { return sFieldName + BaseDefine.upPlusDefine().formFieldHide; } static upItemName(sFormName, sFieldName) { return sFormName + BaseDefine.upPlusDefine().formFieldSplit + sFieldName; } /** * 添加正则表达式验证规则 * * @static * @param {string} sItemName * @param {string} sRegexCode */ static inRegexField(sItemName: string, sRegexCode: string) { if (!BaseFunc.stringIsEmpty(sRegexCode)) { var sFormName = this.upFormNameByItem(sItemName); if (!formRegex.has(sFormName)) { formRegex.set(sFormName, new CregexInfo()); } formRegex.get(sFormName).regexItem.set(this.upItemJsonNameByItem(sItemName), PlusCommonRegex.upRegexDefineByString(sRegexCode)); } } /** * 获取元素的值 * * @static * @param {string} sItemName * @param {string} [sDefaultValue] 默认值 如果该参数为提供 不存在时返回null * @returns {string} */ static upFormValue(sItemName: string, sDefaultValue?: string): string { let sReturn = null; let oForm = formOperate.get(this.upFormNameByItem(sItemName)); if (sDefaultValue != undefined) { sReturn = sDefaultValue; if (!BaseFunc.stringIsEmpty(sDefaultValue)) { if (oForm.ationType === 'add') { oForm.dataInfo.set(this.upItemJsonNameByItem(sItemName), sDefaultValue); } } } //sReturn = sDefaultValue; if (oForm.dataInfo.has(this.upItemJsonNameByItem(sItemName))) { sReturn = oForm.dataInfo.get(this.upItemJsonNameByItem(sItemName)); } return sReturn; } /** * 获取页面操作的类型 * * @static * @param {any} sItemName * @returns */ static upFormType(sItemName): TformActionType { let oForm = formOperate.get(this.upFormNameByItem(sItemName)); return oForm.ationType; } /** * 获取页面信息 * * @static * @param {string} sFormName * @returns */ static upFormInfo(sFormName: string) { return formOperate.get(sFormName); } /** * 获取页面的数据 * * @static * @param {string} sFormName * @returns */ static upFormDataObject(sFormName: string) { var oReturn = {}; this.upFormInfo(sFormName).dataInfo.forEach((v, k) => { oReturn[k] = v; }); return oReturn; } /** * 保存数据到本地数据库中 * * @static * @param {any} sFormName * @param {any} [that] * @param {IformSubmitConfig} [oConfig] * @returns {IbaseFlagResult} */ static submitFormData(sFormName, that?, oConfig?: IformSubmitConfig): IbaseFlagResult { let oResult: IbaseFlagResult = { flagSuccess: true }; let oData = {}; let oFormInfo = this.upFormInfo(sFormName); if (oConfig.formFounder != undefined && !oFormInfo.dataInfo.has(BaseDefine.upPlusDefine().formFounderSystem)) { oFormInfo.dataInfo.set(BaseDefine.upPlusDefine().formFounderSystem, oConfig.formFounder); } oFormInfo.dataInfo.forEach((v, k) => { oData[k] = v; }); //进行正则验证 if (oResult.flagSuccess && formRegex.has(sFormName)) { formRegex.get(sFormName).regexItem.forEach( (fItem, fKey) => { if (oResult.flagSuccess) { let regResult = PlusCommonRegex.verify(oFormInfo.dataInfo.get(fKey), fItem); if (!regResult.flagSuccess) { oResult.flagSuccess = regResult.flagSuccess; oResult.errorMessage = regResult.errorMessage; } } } ) } //开始执行保存逻辑 if (oResult.flagSuccess) { if (BaseFunc.stringIsEmpty(oFormInfo.baseKey)) { oFormInfo.baseKey = BaseSupport.upUuid(); } let oFormDataValue: IvalueFormData = { formCode: sFormName, value: oData }; if (oConfig != undefined && oConfig.leaderKey != undefined) { oFormDataValue.leaderCode = oConfig.leaderKey; } //PlusOperateData.formData.inValue(oFormInfo.baseKey, oFormDataValue); this.saveAndUpdateWait(oFormInfo.baseKey, oFormDataValue); //如果有刷新的配置编号 则通知页面刷新 if (!BaseFunc.stringIsEmpty(oConfig.noticeListName)) { PlusCommonEvent.noticeListRefresh(oConfig.noticeListName, {}); } //this.waitForSync(1, oFormInfo.baseKey); } return oResult; } /** * 保存并且更新同步状态 * * @static * @param {string} sKey * @param {IvalueFormData} oFormDataValue */ static saveAndUpdateWait(sKey: string, oFormDataValue: IvalueFormData) { PlusOperateData.formData.inValue(sKey, oFormDataValue); this.waitForSync(1, sKey); } /** * 将编号添加到等待同步的存储中 如果code为空 则返回所有待同步的key * * @static * @param {any} iType 1为添加 4为删除 2为获取数组 * @param {any} sCode * @returns */ static waitForSync(iType: number, sCode?: string) { let sValue = PlusOperateData.upSystemVar("wait_for_sync"); let aArray = []; if (sValue != undefined) { aArray = sValue.split(','); } else { sValue = ''; } if (iType == 1 && !BaseFunc.stringIsEmpty(sCode)) { aArray.push(sCode); PlusOperateData.inSystemVar("wait_for_sync", aArray.join(',')); //PlusOperateData.systemVariable.inValue(BaseDefine.upPlusDefine().systemVarStart + "wait_for_sync", aArray.join(',')); } else if (iType == 4) { sCode.split(',').forEach((fItem) => { if (fItem) { sValue = BaseFunc.stringArrayOption(sValue, fItem, 4); } }) PlusOperateData.inSystemVar("wait_for_sync", sValue); //PlusOperateData.systemVariable.inValue(BaseDefine.upPlusDefine().systemVarStart + "wait_for_sync", sValue); //console.warn(sValue); } //console.warn(sValue); return aArray; } static upSyncData(): IstructFormData[] { let aData: IstructFormData[] = []; let aCodes = this.waitForSync(2); aCodes.forEach( (sKey) => { if (!BaseFunc.stringIsEmpty(sKey)) { let oDataItem = PlusOperateData.formData.rootUpValueByKey(sKey); if (oDataItem != undefined) { aData.push(oDataItem); } } } ); return aData; } }