import PlusOperateData from '../operate/data'; import BaseDefine from '../base/define'; import BaseIo from '../base/io'; import PlusOperateForm from '../operate/form'; import BaseFunc from '../base/func'; import BaseSupport from "../base/support"; import Aliyun from '../helper/aliyun'; import PlusCommonCheck from '../common/check'; import PlusCommonLog from '../common/log'; import PlusCommonEvent from '../common/event'; import { IprojectDefine,IstructFormData } from '../aim/face'; interface IimageOption { orignUrl: string localUrl: string } interface IformSyncOption { timeStamp: number syncData: IstructFormData[] syncOperate: string } interface IhttpFile { serverUrl: string localUrl: string } interface IsyncHttp { sign: string, files: IhttpFile[] } export default class PlusOperateSync { /** * 发送一个同步操作的消息 * * @static * @param {string} [sUpdateType] */ static autoSyncEmit(sUpdateType?: string) { PlusCommonEvent.noticeSyncData({ eventValue: sUpdateType }); } /** * 注册一个同步的监听事件 * * @static */ static initSyncListener(formPromise: (oData: IformSyncOption) => Promise) { PlusCommonLog.logInfo(9610104); PlusCommonEvent.addListener(BaseDefine.upPlusDefine().eventSyncUpdate, (oData) => { let iTimeDeepSecond = 0; //这里判断最后一次是否同步成功 如果不成功则判断间距大于300秒才下一次同步 否则跳过同步操作 if (!BaseFunc.stringIsEmpty(BaseDefine.upPlusTemp().syncErrorTime)) { let sNowTimeStamp = BaseSupport.upNowTimeStamp(); iTimeDeepSecond = (parseInt(sNowTimeStamp) - parseInt(BaseDefine.upPlusTemp().syncErrorTime)); //如果时间差距大于60秒 则设置为可同步状态 if (iTimeDeepSecond > 60 || oData.eventValue === "force") { BaseDefine.upPlusTemp().syncErrorTime = ''; BaseDefine.upPlusTemp().syncStatus = 0; } } if (BaseDefine.upPlusTemp().syncStatus === 0) { //将同步状态设置为1正在同步中 BaseDefine.upPlusTemp().syncStatus = 1; let oFormSyncInput: IformSyncOption = { syncData: PlusOperateForm.upSyncData(), timeStamp: 0, syncOperate: '' } //let syncTimestamp = PlusOperateData.systemVariable.upValue(BaseDefine.upPlusDefine().systemVarStart + 'sync_timestamp'); let syncTimestamp = PlusOperateData.upSystemVar('sync_timestamp'); if (!BaseFunc.stringIsEmpty(syncTimestamp)) { oFormSyncInput.timeStamp = parseInt(syncTimestamp); } if (!BaseFunc.stringIsEmpty(oData.eventObject)) { oFormSyncInput.syncOperate = oData.eventObject; } let sCodes = PlusOperateForm.waitForSync(2, ''); //开始执行同步逻辑 formPromise(oFormSyncInput) .then((oFormSyncResult) => { if (oFormSyncResult.syncData.length > 0) { PlusOperateData.formData.rootWriteItemValue(oFormSyncResult.syncData); } //PlusOperateData.systemVariable.inValue(BaseDefine.upPlusDefine().systemVarStart + 'sync_timestamp', oFormSyncResult.timeStamp.toString()); PlusOperateData.inSystemVar('sync_timestamp', oFormSyncResult.timeStamp.toString()); PlusOperateForm.waitForSync(4, sCodes.join(',')); PlusCommonLog.logInfo(9610107, { upload: oFormSyncInput.syncData.length, download: oFormSyncResult.syncData.length, utime: oFormSyncInput.timeStamp, dtime: oFormSyncResult.timeStamp }); }) .then(() => { //这里顺带执行下同步图片的逻辑代码 该执行成功与否都不影响 继续执行 PlusOperateSync.syncImage() }) .then(() => { //在同步成功后触发一条消息 if (oData.eventValue === "force") { PlusCommonEvent.emit(BaseDefine.upPlusDefine().eventSyncResult, { eventValue: 'success' }); } BaseDefine.upPlusTemp().syncStatus = 0; }).catch((error) => { //同步失败时触发一条消息 if (oData.eventValue === "force") { PlusCommonEvent.emit(BaseDefine.upPlusDefine().eventSyncResult, { eventValue: 'error' }); } PlusCommonLog.logError(9310106, error); BaseDefine.upPlusTemp().syncStatus = 5; BaseDefine.upPlusTemp().syncErrorTime = BaseSupport.upNowTimeStamp(); }); } else { PlusCommonLog.logWarn(9410105, { status: BaseDefine.upPlusTemp().syncStatus, deep: iTimeDeepSecond.toString() }); if (oData.eventValue === "force") { PlusCommonEvent.emit(BaseDefine.upPlusDefine().eventSyncResult, { eventValue: 'error' }); } } }); } static syncHttpFiles(syncHttp: IsyncHttp) { let aPromise = []; let syncHttpSign = PlusOperateData.upSystemVar('sync_http_sign'); if (syncHttpSign === undefined || syncHttpSign !== syncHttp.sign) { syncHttp.files.forEach((fItem) => { aPromise.push(BaseIo.getFile(fItem.serverUrl, BaseIo.upDocumentFilePath(fItem.localUrl))); }); } return Promise.all(aPromise).then(() => { if (aPromise.length > 0) { PlusOperateData.inSystemVar('sync_http_sign', syncHttp.sign) } }); } /** * 同步图片操作 * * @static */ static syncImage() { //let sWaitFor = PlusOperateData.systemVariable.upValue(BaseDefine.upPlusDefine().systemVarStart + "wait_image_sync"); let sWaitFor = PlusOperateData.upSystemVar('wait_image_sync'); if (sWaitFor) { let aSyncArray = sWaitFor.split(','); aSyncArray.forEach((fStr) => { let oUrl = PlusOperateData.systemVariable.upValue(BaseDefine.upPlusDefine().systemVarStart + fStr); //console.warn(JSON.stringify(oUrl)); let sUrl = oUrl.localUrl; //判断 如果是ios的话这里会替换掉url的file://开头的文本字段 以拿到真实路径 if (PlusCommonCheck.flagPlatformIos() && sUrl.startsWith("file://")) { sUrl = sUrl.replace("file://", ""); //PlusCommonLog.logDebug(9811101, sUrl); } Aliyun.uploadBlob(fStr, sUrl).then( (res) => { if (res.respInfo.status === 200) { let sSave = BaseFunc.stringArrayOption(PlusOperateData.upSystemVar('wait_image_sync'), fStr, 4); //PlusOperateData.systemVariable.inValue(BaseDefine.upPlusDefine().systemVarStart + "wait_image_sync", sSave); PlusOperateData.inSystemVar('wait_image_sync', sSave); } else { PlusCommonLog.logWarn(9410103, res); } } ).catch( (error) => { PlusCommonLog.logError(9310102, error); } ) ; }); } } /** * 图片保存到待同步列表中 * * @static * @param {any} sImageName * @param {IimageOption} oFile */ static imageInData(sImageName, oFile: IimageOption) { PlusOperateData.systemVariable.inValue(BaseDefine.upPlusDefine().systemVarStart + sImageName, oFile); let sWaitFor = PlusOperateData.upSystemVar('wait_image_sync'); if (sWaitFor) { sWaitFor = BaseFunc.stringArrayOption(sWaitFor, sImageName, 1); } else { sWaitFor = sImageName; } let aWait = PlusOperateData.inSystemVar('wait_image_sync', sWaitFor); } /** * 获取图片地址 * * @static * @param {any} sImageName * @returns {string} */ static imageUpUrl(sImageName): string { let oUrl = PlusOperateData.systemVariable.upValue(BaseDefine.upPlusDefine().systemVarStart + sImageName); let sReturn = ''; if (oUrl === undefined) { sReturn = BaseFunc.replaceWithObject(BaseDefine.upProjectDefine().ossImageUrl, { img: sImageName }); } else { sReturn = oUrl.localUrl; } //console.warn(sReturn); return sReturn; } static initProjectDefine(define: IprojectDefine) { BaseDefine.inProjectDefine(define); } }