import * as ts from "typescript"; import {CONNECTION_END_TYPE, PARSE_TYPE, TRANS_DATA} from "../@types/types"; import {logger} from "../logger/Logger"; import DateUtil from "../utils/DateUtil"; import Bean from "../decorators/Bean"; import ObjectUtil from "../utils/ObjectUtil"; type TRANSACTION_BODY_TYPE = "request"|"response"|"body"|"header"; @Bean() export default class DefaultTransactionData{ public sqlSessonEndType:CONNECTION_END_TYPE = CONNECTION_END_TYPE.COMMIT; public data:TRANS_DATA = { header:{}, body:{ request:{}, response:{} } }; public common:any = {}; public init(req?:any):void{ this.defaultPacketData(); if(req){ this.setData("header", req.headers); this.setData("request", req.params); this.setData("request", req.query); if(req.body) { const body = ObjectUtil.clone(req.body); this.setData("header", body["header"]); this.setData("body", body["body"]); } if(req.body && req.body._packet_){ const packetParam = ObjectUtil.parse(decodeURIComponent(req.body._packet_), PARSE_TYPE.JSON); if (packetParam) { this.setData("request", packetParam); } } if (req.query && req.query.popup_id) { this.setData("header", {popup_id:req.query.popup_id}); } } } private defaultPacketData(){ this.data = { header:{ transDate:DateUtil.getCurrentDateTime("yyyyMMdd H:i:s"), resultCode:"0000", resultMsg:"정상" }, body:{ request:{}, response:{} } }; } public setData(type:TRANSACTION_BODY_TYPE, data:any){ let target:any = null; if(!data) return; if(type === "request" || type === "response"){ target = this.data.body[type]; }else if(type === "body"){ this.setData("request", data.request); this.setData("response", data.response); }else if(type === "header"){ target = this.data.header; } try{ if(target){ Object.keys(data).map(key=>target[key]=data[key]); } }catch(e){ logger.info(e); } } public get(path:string):any{ const dataPath = ts.transpile(`this.data.${path}`); try{ const ret = eval(dataPath); return ret; }catch(e){} return null; } public addResponse(key:string, val:any){ if(!this.data.body.response) this.data.body.response = {}; this.data.body.response[key] = val; } public exception(code?:string, msg?:string, stack?:string){ this.data.header.resultCode = "9999"; this.data.header.resultMsg = "에러가 발생하였습니다."; if (code == null || code == undefined) { code = "SYS00000"; } if (msg == null || msg == undefined) { msg = ""; } this.data.header.resultCode = code; this.data.header.resultMsg = msg; this.sqlSessonEndType = CONNECTION_END_TYPE.ROLLBACK; } public getInt(path:string):number{ const ret = this.get(path); try{ return parseInt(ret); }catch(e){} return 0; } public getFloat(path:string):number{ const ret = this.get(path); try{ return parseFloat(ret); }catch(e){} return 0; } public toJson():object{ return this.data; } public toJsonString():string{ return JSON.stringify(this.data); } }