query.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. 'use strict';
  2. var quick = require('quick-pomelo');
  3. var P = quick.Promise;
  4. var _ = require('lodash');
  5. var url = require('url');
  6. var md5 = require('md5');
  7. var util = require('util');
  8. var http = require('http');
  9. var qs = require('querystring');
  10. var C = require('../share/constant');
  11. var logger = quick.logger.getLogger('query', __filename);
  12. // 构造方法
  13. var Controller = function (app) {
  14. this.app = app;
  15. this.uptime = Date.now();
  16. this.port = app.getCurServer().httpPort;
  17. this.server = http.createServer((req, res) => this.handleAsync(req, res));
  18. this.server.listen(this.port);
  19. };
  20. // 导出方法
  21. module.exports = function (app) {
  22. return new Controller(app);
  23. };
  24. // 原型对象
  25. var proto = Controller.prototype;
  26. // 路由函数
  27. proto.route = function (pathname) {
  28. switch (pathname) {
  29. case '/getGameRecord': return (query, method, res) => this.getGameRecordAsync(query, method, res);
  30. case '/makeHistory': return (query, method, res) => this.makeHistoryAsync(query, method, res);
  31. case '/recvHistory': return (query, method, res) => this.recvHistoryAsync(query, method, res);
  32. case '/getUserInfo': return (query, method, res) => this.getUserInfoAsync(query, method, res);
  33. }
  34. };
  35. // 写出数据
  36. proto.writeOut = function (query, res) {
  37. if (typeof query != 'object') {
  38. return res.end(String(query));
  39. }
  40. return res.end(JSON.stringify(query));
  41. };
  42. // 跨域选项
  43. const ACCESS = {
  44. "Content-Type": "text/plain;charset=utf-8",
  45. "Access-Control-Allow-Origin": "*",
  46. "Access-Control-Allow-Headers": "X-Requested-With",
  47. "Access-Control-Allow-Methods": "PUT,POST,GET,DELETE,OPTIONS"
  48. };
  49. // 入口函数
  50. proto.handleAsync = P.coroutine(function* (req, res) {
  51. var pathname = url.parse(req.url).pathname;
  52. var phandler = this.route(pathname);
  53. if (!phandler) {
  54. res.writeHead(404, ACCESS);
  55. return res.end('WARNING: Not Found!');
  56. }
  57. else {
  58. res.writeHead(200, ACCESS);
  59. if (req.method.toLowerCase() == 'get') {
  60. let query = url.parse(req.url, true).query;
  61. return phandler(query, req.method, res);
  62. }
  63. else if (req.method.toLowerCase() == 'post') {
  64. let data = '';
  65. req.on('data', (d) => { data += d; });
  66. req.on('end', () => {
  67. let query = { __post__: data };
  68. let regexp = /^\s*{(\s*?".*?":.*\s*)*}\s*$/;
  69. if (regexp.test(data)) try { query = JSON.parse(data); } catch (e) { }
  70. else if (data.indexOf('=') != -1) query = qs.parse(data);
  71. return phandler(query, req.method, res);
  72. });
  73. }
  74. }
  75. });
  76. // 查询记录
  77. proto.getGameRecordAsync = P.coroutine(function* (query, method, res) {
  78. var list = [];
  79. var playerId = query.playerId;
  80. if (!playerId) return this.writeOut(list, res);
  81. var opts = { 'users._id': playerId };
  82. var gameId = parseInt(query.gameId);
  83. if (gameId) opts.gameId = gameId;
  84. var count = 20;
  85. var fields = 'gameId tableId type round stime time users._id users.score';
  86. var records = yield this.app.models.Record.findMongoAsync(opts, fields, { sort: { time: -1 }, limit: count });
  87. for (let record of records) {
  88. let scorer = record.users.id(playerId);
  89. if (scorer) {
  90. list.push({ gameId: record.gameId, tableId: record.tableId, type: record.type, round: record.round, time: record.stime || record.time, score: scorer.score });
  91. }
  92. }
  93. return this.writeOut(list, res);
  94. });
  95. // 发包记录
  96. proto.makeHistoryAsync = P.coroutine(function* (query, method, res) {
  97. var playerId = query.playerId;
  98. if (!playerId) {
  99. return this.writeOut({ code: C.FAILD, msg: C.HTTP_PARAM_ERROR }, res);
  100. }
  101. var list = yield this.app.models.RedPack.findMongoAsync({ sid: playerId }, 'diamond stime recvd', { sort: { stime: -1 }, limit: 20 });
  102. list = list.map(o => ({ rpId: o._id, diamond: o.diamond, stime: o.stime, recvd: o.recvd }));
  103. return this.writeOut({ code: C.OK, list: list }, res);
  104. });
  105. // 领包记录
  106. proto.recvHistoryAsync = P.coroutine(function* (query, method, res) {
  107. var playerId = query.playerId;
  108. if (!playerId) {
  109. return this.writeOut({ code: C.FAILD, msg: C.HTTP_PARAM_ERROR }, res);
  110. }
  111. var list = yield this.app.models.RedPack.findMongoAsync({ rid: playerId }, 'diamond rtime', { sort: { rtime: -1 }, limit: 20 });
  112. list = list.map(o => ({ rpId: o._id, diamond: o.diamond, rtime: o.rtime }));
  113. return this.writeOut({ code: C.OK, list: list }, res);
  114. });
  115. // 个人信息
  116. proto.getUserInfoAsync = P.coroutine(function* (query, method, res) {
  117. var playerId = query.playerId;
  118. if (!playerId) {
  119. return this.writeOut({ code: C.FAILD, msg: C.HTTP_PARAM_ERROR }, res);
  120. }
  121. var self = this;
  122. var app = this.app;
  123. return app.memdb.goose.transactionAsync(P.coroutine(function* () {
  124. var player = yield self.app.models.Player.findByIdReadOnlyAsync(playerId, 'diamond');
  125. if (!player) {
  126. return self.writeOut({ code: C.FAILD, msg: C.HTTP_NOT_PLAYER }, res);
  127. }
  128. return self.writeOut({ code: C.OK, diamond: player.diamond }, res);
  129. }), app.getServerId())
  130. .then(() => app.event.emit('transactionSuccess'), () => app.event.emit('transactionFail'));
  131. });