msg_statistic.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. var express = require('express');
  2. var router = express.Router();
  3. var msg_statistic = require("../models/msg_statistic");
  4. /**
  5. * 群组消息统计:statistic/getgroupchatinfo.im?uid=x&gid=xx
  6. * 参数:
  7. * uid:信息所有者id
  8. * gid:群组id
  9. */
  10. router.get('/getgroupchatinfo.im', function (req, res, next) {
  11. if (req.query.uid == null
  12. || req.query.gid == null) {
  13. res.send({errno: -1, errmsg: 'parameter error'});
  14. return;
  15. }
  16. msg_statistic.getGroupChatInfo(req.query.uid, req.query.gid, function (err, result) {
  17. if (err) {
  18. res.send({errno: 1, errmsg: 'get statistic from db error'});
  19. return;
  20. }
  21. if (result.length == 0) {
  22. var data = {"uid":req.query.uid,"from_uid":"","from_gid":req.query.gid,"at_me":0,"last_content_type":1,"last_content":"","new_msg_count":0,"timestamp":0};
  23. res.send(data);
  24. return;
  25. }
  26. result[0].timestamp = Date.parse(new Date(result[0].timestamp));
  27. res.send(result[0]);
  28. });
  29. });
  30. /**
  31. * 一对一聊天消息统计:statistic/getp2pchatinfo.im?uid=x&peer_uid=xx
  32. * 参数:
  33. * uid:信息所有者id
  34. * peer_uid:聊天对端id
  35. */
  36. router.get('/getp2pchatinfo.im', function (req, res, next) {
  37. if (req.query.uid == null
  38. || req.query.peer_uid == null) {
  39. res.send({errno: -1, errmsg: 'parameter error'});
  40. return;
  41. }
  42. msg_statistic.getP2PChatInfo(req.query.uid, req.query.peer_uid, function (err, result) {
  43. if (err) {
  44. res.send({errno: 1, errmsg: 'get statistic from db error'});
  45. return;
  46. }
  47. if (result.length == 0) {
  48. var data = {"uid":req.query.uid,"from_uid":req.query.peer_uid,"last_content_type":1,"last_content":"","new_msg_count":0,"timestamp":0};
  49. res.send(data);
  50. return;
  51. }
  52. result[0].timestamp = Date.parse(new Date(result[0].timestamp));
  53. res.send(result[0]);
  54. });
  55. });
  56. /**
  57. * 获取聊天列表:statistic/getchatlist.im?uid=x
  58. * 参数:
  59. * uid:信息所有者id
  60. */
  61. router.get('/getchatlist.im', function (req, res, next) {
  62. if (req.query.uid == null) {
  63. res.send({errno: -1, errmsg: 'parameter error'});
  64. return;
  65. }
  66. msg_statistic.getChatList(req.query.uid, function (err, result) {
  67. if (err) {
  68. res.send({errno: 1, errmsg: 'get statistic from db error'});
  69. return;
  70. }
  71. if (result.length == 0) {
  72. var data = {"uid":req.query.uid,"from_uid":req.query.peer_uid,"last_content_type":1,"last_content":"","new_msg_count":0,"timestamp":0};
  73. res.send(data);
  74. return;
  75. }
  76. //result[0].timestamp = Date.parse(new Date(result[0].timestamp));
  77. for (var index = 0; index < result.length; index++) {
  78. result[index].timestamp = Date.parse(new Date(result[index].timestamp));
  79. }
  80. res.send(result);
  81. });
  82. });
  83. /**
  84. * 群组聊天消息所有未读数:statistic/getgroupunreadcount.im?uid=x
  85. * 参数:
  86. * uid:信息所有者id
  87. */
  88. router.get('/getgroupunreadcount.im', function (req, res, next) {
  89. if (req.query.uid == null) {
  90. res.send({errno: -1, errmsg: 'parameter error'});
  91. return;
  92. }
  93. msg_statistic.getGroupChatAllUnRead(req.query.uid, function (err, result) {
  94. if (err) {
  95. res.send({errno: 1, errmsg: 'get statistic from db error'});
  96. return;
  97. }
  98. var data = {"uid":req.query.uid,"msg_type":2,"new_msg_count":0};
  99. if (result.length == 0) {
  100. res.send(data);
  101. return;
  102. }
  103. var count = 0;
  104. var index = 0;
  105. var length = result.length;
  106. for (; index < length; index++) {
  107. count += result[index].new_msg_count;
  108. }
  109. data.new_msg_count = count;
  110. res.send(data);
  111. });
  112. });
  113. /**
  114. * 一对一聊天消息所有未读数:statistic/getp2punreadcount.im?uid=x
  115. * 参数:
  116. * uid:信息所有者id
  117. */
  118. router.get('/getp2punreadcount.im', function (req, res, next) {
  119. if (req.query.uid == null) {
  120. res.send({errno: -1, errmsg: 'parameter error'});
  121. return;
  122. }
  123. msg_statistic.getP2PChatAllUnRead(req.query.uid, function (err, result) {
  124. if (err) {
  125. res.send({errno: 1, errmsg: 'get statistic from db error'});
  126. return;
  127. }
  128. var data = {"uid":req.query.uid,"msg_type":1,"new_msg_count":0};
  129. if (result.length == 0) {
  130. res.send(data);
  131. return;
  132. }
  133. var count = 0;
  134. var index = 0;
  135. var length = result.length;
  136. for (; index < length; index++) {
  137. count += result[index].new_msg_count;
  138. }
  139. data.new_msg_count = count;
  140. res.send(data);
  141. });
  142. });
  143. /**
  144. * 所有聊天消息未读数:statistic/getallunreadmsgcount.im?uid=x
  145. * 参数:
  146. * uid:信息所有者id
  147. */
  148. router.get('/getallunreadmsgcount.im', function (req, res, next) {
  149. if (req.query.uid == null) {
  150. res.send({errno: -1, errmsg: 'parameter error'});
  151. return;
  152. }
  153. msg_statistic.getChatAllUnRead(req.query.uid, function (err, result) {
  154. if (err) {
  155. res.send({errno: 1, errmsg: 'get statistic from db error'});
  156. return;
  157. }
  158. var data = {"uid":req.query.uid,"msg_type":0,"new_msg_count":0};
  159. if (result.length == 0) {
  160. res.send(data);
  161. return;
  162. }
  163. var count = 0;
  164. var index = 0;
  165. var length = result.length;
  166. for (; index < length; index++) {
  167. count += result[index].new_msg_count;
  168. }
  169. data.new_msg_count = count;
  170. res.send(data);
  171. });
  172. });
  173. /**
  174. * 获取角标数:statistic/getbadgenum.im?uid=x
  175. * 参数:
  176. * uid:用户id
  177. */
  178. router.get('/getbadgenum.im', function (req, res, next) {
  179. if (req.query.uid == null) {
  180. res.send({errno: -1, errmsg: 'parameter error'});
  181. return;
  182. }
  183. msg_statistic.getBadgeNumber(req.query.uid, function (err, result) {
  184. if (err) {
  185. res.send({errno: 1, errmsg: 'get badge error'});
  186. return;
  187. }
  188. var data = {"uid":req.query.uid,"badge":result};
  189. res.send(data);
  190. });
  191. });
  192. module.exports = router;