UNPKG

dm-web-react

Version:

The DM web client with React.

391 lines (364 loc) 13.9 kB
import { SqlTemplate } from "tsbatis"; import { StringUtils } from "ts-commons"; import { CreateTableTemplate } from "../modules/bond/models/database/createTableTemplate"; import { TableInfo } from "../modules/bond/models/database/tableInfo"; import { deserialize } from "class-transformer"; import { DatabaseResponse } from "../modules/bond/models/database/databaseResponse"; /** * 获取最外层window */ const getOutermostWindow = (w: any): any => { if (w.parent === w.self) { return w; } return getOutermostWindow(w.parent); }; const topWindow = getOutermostWindow(window); export class DmLiteMessageService { private timeout = 20000; private static instance = new DmLiteMessageService(topWindow); private mapCallbacks = new Map(); private global: any; private isDmliteClient: boolean; private constructor(global: any) { this.global = global; this.isDmliteClient = navigator.userAgent.indexOf("dmlite") > -1; // 注册 数据库操作 this.global.Shell_DatabaseResponse = (requestIdBase64: string, databaseResponseBas64: string) => { var requestId = this.b64_to_utf8(requestIdBase64); var dataResponse = this.b64_to_utf8(databaseResponseBas64); this.invokeCallback(requestId, dataResponse); }; } public static get Instance() { return this.instance; } // utf-8 to base64 // private utf8_to_b64 = (str: string) => { // return this.global.btoa(this.global.unescape(encodeURIComponent(str))); // }; // base64 to utf-8 private b64_to_utf8 = (str: string) => { return decodeURIComponent(this.global.escape(this.global.atob(str))); }; /** * invokeCallback */ private invokeCallback(requestId: string, args: any) { try { var callbacks = this.mapCallbacks.get(requestId); if (!callbacks || typeof callbacks !== "function") { throw new Error("callback invalid method"); } callbacks(args); } catch (err) { throw new Error("Error description: " + err.message); } finally { if (this.mapCallbacks.has(requestId)) { this.mapCallbacks.delete(requestId); } } } public log(browserId: string, log: string): void { this.global.dmlite && this.global.dmlite.Log(browserId, log); } public pushError(browserId: string, errMsg: string): void { this.global.dmlite && this.global.dmlite.PushError(browserId, errMsg); } public InitAppContent(initConfigStr: string): any { return this.global.dmlite ? this.global.dmlite.InitAppContent(initConfigStr) : false; } public InitAppSingleContent(url: string): any { return this.global.dmlite ? this.global.dmlite.InitAppSingleContent(url) : false; } /** * 打开 tab */ public OpenTab(id: number | string, title: string, url: string) { // if (this.isDmliteClient) { // this.global.dmlite.OpenTab(id, title, url); // } else { // topWindow.postMessage( // { // param: { id, title, source: url }, // action: "new", // }, // "*" // ); // } topWindow.postMessage( { param: { id, title, source: url }, action: "new", }, "*" ); } /** * 关闭客户端 */ public ShutdownApp() { if (this.isDmliteClient) { return this.global.dmlite.ShutdownApp(); } return "fail"; } /** * 重启客户端 */ public RestartApp() { if (this.isDmliteClient) { this.global.dmlite.RestartApp(); } return "fail"; } /** * DatabaseQueryJsonRequest 查询数据返回json */ public DatabaseQueryJsonRequest(browserId: string, sqlTemplate: SqlTemplate, timeout?: number): Promise<any> { return new Promise((resolve, reject) => { if (this.global.dmlite && this.global.dmlite.DatabaseQueryJsonRequest) { const requestId = StringUtils.newGuid(); const timer = window.setTimeout(() => { this.mapCallbacks.delete(requestId); window.clearTimeout(timer); reject("timeout"); }, timeout || this.timeout); const callback = (data: string) => { window.clearTimeout(timer); const databaseResponse = deserialize(DatabaseResponse, data); if (StringUtils.isBlank(databaseResponse.exception)) { resolve(databaseResponse.data); } else { reject(databaseResponse.exception); } }; this.global.dmlite.DatabaseQueryJsonRequest(browserId, requestId, JSON.stringify(sqlTemplate)); this.mapCallbacks.set(requestId, callback); } else { reject("not dmlite"); } }); } /** * DatabaseQueryJsonsRequest 查询数据返回jsons */ public DatabaseQueryJsonsRequest(browserId: string, sqlTemplates: SqlTemplate[], timeout?: number): Promise<any> { return new Promise((resolve, reject) => { if (this.global.dmlite && this.global.dmlite.DatabaseQueryJsonsRequest) { const requestId = StringUtils.newGuid(); const timer = window.setTimeout(() => { this.mapCallbacks.delete(requestId); window.clearTimeout(timer); reject("timeout"); }, timeout || this.timeout); const callback = (data: string) => { window.clearTimeout(timer); const databaseResponse = deserialize(DatabaseResponse, data); if (StringUtils.isBlank(databaseResponse.exception)) { resolve(databaseResponse.data); } else { reject(databaseResponse.exception); } }; this.global.dmlite.DatabaseQueryJsonsRequest(browserId, requestId, JSON.stringify(sqlTemplates)); this.mapCallbacks.set(requestId, callback); } else { reject("not dmlite"); } }); } /** * DatabaseExecuteNonQueryRequest 执行non query */ public DatabaseExecuteNonQueryRequest(browserId: string, sqlTemplate: SqlTemplate, timeout?: number): Promise<any> { return new Promise((resolve, reject) => { if (this.global.dmlite && this.global.dmlite.DatabaseExecuteNonQueryRequest) { const requestId = StringUtils.newGuid(); const timer = window.setTimeout(() => { this.mapCallbacks.delete(requestId); window.clearTimeout(timer); reject("timeout"); }, timeout || this.timeout); const callback = (data: string) => { window.clearTimeout(timer); const databaseResponse = deserialize(DatabaseResponse, data); if (StringUtils.isBlank(databaseResponse.exception)) { resolve(databaseResponse.data); } else { reject(databaseResponse.exception); } }; this.global.dmlite.DatabaseExecuteNonQueryRequest(browserId, requestId, JSON.stringify(sqlTemplate)); this.mapCallbacks.set(requestId, callback); } else { reject("not dmlite"); } }); } /** * DatabaseExecuteScalarRequest 执行scalar */ public DatabaseExecuteScalarRequest(browserId: string, sqlTemplate: SqlTemplate, timeout?: number): Promise<any> { return new Promise((resolve, reject) => { if (this.global.dmlite && this.global.dmlite.DatabaseExecuteScalarRequest) { const requestId = StringUtils.newGuid(); const timer = window.setTimeout(() => { this.mapCallbacks.delete(requestId); window.clearTimeout(timer); reject("timeout"); }, timeout || this.timeout); const callback = (data: string) => { window.clearTimeout(timer); const databaseResponse = deserialize(DatabaseResponse, data); if (StringUtils.isBlank(databaseResponse.exception)) { resolve(databaseResponse.data); } else { reject(databaseResponse.exception); } }; this.global.dmlite.DatabaseExecuteScalarRequest(browserId, requestId, JSON.stringify(sqlTemplate)); this.mapCallbacks.set(requestId, callback); } else { reject("not dmlite"); } }); } /** * DatabaseExecuteDMLRequest 批量执行DML */ public DatabaseExecuteDMLRequest(browserId: string, sqlTemplateArray: SqlTemplate[], timeout?: number): Promise<any> { return new Promise((resolve, reject) => { if (this.global.dmlite && this.global.dmlite.DatabaseExecuteDMLRequest) { const requestId = StringUtils.newGuid(); const timer = window.setTimeout(() => { this.mapCallbacks.delete(requestId); window.clearTimeout(timer); reject("timeout"); }, timeout || this.timeout); const callback = (data: string) => { window.clearTimeout(timer); const databaseResponse = deserialize(DatabaseResponse, data); if (StringUtils.isBlank(databaseResponse.exception)) { resolve(databaseResponse.data); } else { reject(databaseResponse.exception); } }; this.global.dmlite.DatabaseExecuteDMLRequest(browserId, requestId, JSON.stringify(sqlTemplateArray)); this.mapCallbacks.set(requestId, callback); } else { reject("not dmlite"); } }); } /** * DatabaseCreateTableTemplateRequest 创建表 */ public DatabaseCreateTableTemplateRequest(browserId: string, createTableTemplate: CreateTableTemplate, timeout?: number): Promise<any> { return new Promise((resolve, reject) => { if (this.global.dmlite && this.global.dmlite.DatabaseCreateTableTemplateRequest) { const requestId = StringUtils.newGuid(); const timer = window.setTimeout(() => { this.mapCallbacks.delete(requestId); window.clearTimeout(timer); reject("timeout"); }, timeout || this.timeout); const callback = (data: any) => { window.clearTimeout(timer); const databaseResponse = deserialize(DatabaseResponse, data); if (StringUtils.isBlank(databaseResponse.exception)) { resolve(databaseResponse.data); } else { reject(databaseResponse.exception); } }; this.global.dmlite.DatabaseCreateTableTemplateRequest(browserId, requestId, JSON.stringify(createTableTemplate)); this.mapCallbacks.set(requestId, callback); } else { reject("not dmlite"); } }); } /** * DatabaseFillWithJsonUrlRequest 填充表 */ public DatabaseFillWithJsonRequest(browserId: string, tableInfo: TableInfo, jsonArray: any[], timeout?: number): Promise<any> { return new Promise((resolve, reject) => { if (this.global.dmlite && this.global.dmlite.DatabaseFillWithJsonRequest) { const requestId = StringUtils.newGuid(); const timer = window.setTimeout(() => { this.mapCallbacks.delete(requestId); window.clearTimeout(timer); reject("timeout"); }, timeout || this.timeout); const callback = (data: string) => { window.clearTimeout(timer); const databaseResponse = deserialize(DatabaseResponse, data); if (StringUtils.isBlank(databaseResponse.exception)) { resolve(databaseResponse.data); } else { reject(databaseResponse.exception); } }; this.global.dmlite.DatabaseFillWithJsonRequest(browserId, requestId, JSON.stringify(tableInfo), JSON.stringify(jsonArray)); this.mapCallbacks.set(requestId, callback); } else { reject("not dmlite"); } }); } /** * DatabaseFillWithJsonUrlRequest 填充表 */ public DatabaseFillWithJsonUrlEncryptRequest(browserId: string, tableInfo: TableInfo, jsonGzipUrl: string, timeout?: number): Promise<any> { return new Promise((resolve, reject) => { if (this.global.dmlite && this.global.dmlite.DatabaseFillWithJsonUrlRequest) { const requestId = StringUtils.newGuid(); const timer = window.setTimeout(() => { this.mapCallbacks.delete(requestId); window.clearTimeout(timer); reject("timeout"); }, timeout || this.timeout); const callback = (data: string) => { window.clearTimeout(timer); const databaseResponse = deserialize(DatabaseResponse, data); if (StringUtils.isBlank(databaseResponse.exception)) { resolve(databaseResponse.data); } else { reject(databaseResponse.exception); } }; this.global.dmlite.DatabaseFillWithJsonUrlEncryptRequest(browserId, requestId, JSON.stringify(tableInfo), jsonGzipUrl); this.mapCallbacks.set(requestId, callback); } else { reject("not dmlite"); } }); } public DatabaseFillWithBatchJsonUrlEncryptRequest(browserId: string, tableInfo: TableInfo, jsonGzipUrls: string[], timeout?: number): Promise<any> { return new Promise((resolve, reject) => { if (this.global.dmlite && this.global.dmlite.DatabaseFillWithBatchJsonUrlEncryptRequest) { const requestId = StringUtils.newGuid(); const timer = window.setTimeout(() => { this.mapCallbacks.delete(requestId); window.clearTimeout(timer); reject("timeout"); }, timeout || this.timeout); const callback = (data: string) => { window.clearTimeout(timer); const databaseResponse = deserialize(DatabaseResponse, data); if (StringUtils.isBlank(databaseResponse.exception)) { resolve(databaseResponse.data); } else { reject(databaseResponse.exception); } }; this.global.dmlite.DatabaseFillWithBatchJsonUrlEncryptRequest(browserId, requestId, JSON.stringify(tableInfo), JSON.stringify(jsonGzipUrls)); this.mapCallbacks.set(requestId, callback); } else { reject("not dmlite"); } }); } }