123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- "use strict";
- var CallLog = require('./CallLog.js');
- class Response {
- constructor(req, res) {
- this._req = req;
- this._res = res;
- this.codeMap = require('../../conf/code.inc.js');
- }
- /**
- * sucess
- * @param {Array|String|Number} data 返回的数据
- * @param {String} msg 返回的msg
- * @param {String} debugMsg 调试的msg
- * @author benzhan
- */
- success(data, msg, debugMsg) {
- var code = CODE_SUCCESS;
- this.debugMsg = debugMsg;
- msg = msg || this.codeMap[code];
- if (global['DEBUG'] && debugMsg) {
- msg = msg + " 【调试信息:" + debugMsg + "】";
- }
- var ret = {
- result: 1,
- code: code,
- msg: msg,
- data: data
- };
- this.exitData(ret);
- }
- /**
- * error with code
- * @param {Number} code
- * @param {String} msg
- * @param {String} debugMsg
- * @param {String} extData
- * @author benzhan
- */
- error(code, msg, debugMsg, extData) {
- this.debugMsg = debugMsg;
- msg = msg || this.codeMap[code];
- if (global['DEBUG'] && debugMsg) {
- msg = msg + " 【调试信息:" + debugMsg + "】";
- }
- var ret = {
- result: 0,
- code: code,
- msg: msg
- };
- if (extData) {
- ret['data'] = extData;
- }
- this.exitData(ret);
- }
- exitData(ret) {
- var json = JSON.stringify(ret);
- if (!this._res._headerSent) {
- this._res.header('Content-Type', 'application/json;charset=utf8');
- }
- this.exitMsg(json, ret['code']);
- }
- exitMsg(content, code) {
- var res = this._res;
- var req = this._req;
- code = code || CODE_SUCCESS;
- //必须是字符串
- if (typeof content != "string") {
- content = JSON.stringify(content);
- }
- //jquery jsonp callback处理
- if (req.query && req.query.callback) {
- if (/^jQuery(\d+)_(\d+)$/.test(req.query.callback)) {
- content = req.query.callback + '(' + content + ');';
- }
- }
- // 记录访问日志
- var objCallLog = new CallLog(this);
- objCallLog.logSelfCall(code, content);
- if (!res.finished) {
- res.write(content);
- res.end();
- }
- }
- }
- module.exports = Response;
|