msg_p2p.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. var express = require('express');
  2. var router = express.Router();
  3. var msg_p2p = require("../models/msg_p2p");
  4. var msg_statistic = require("../models/msg_statistic");
  5. var user = require("../models/user");
  6. var push = require("../models/push_notify");
  7. var getui = require('getui');
  8. /**
  9. * 一对一消息:p2p/sendmsg.im?from_uid=x&to_uid=xx&content=xxx&type=1
  10. * 参数:
  11. * from_uid:发送者id
  12. * to_uid:接收者id
  13. * content:消息内容
  14. * type:消息类型:1文本,2图片,3语音
  15. */
  16. router.get('/sendmsg.im', function (req, res, next) {
  17. if (req.query.from_uid == null
  18. || req.query.to_uid == null
  19. || req.query.content == null
  20. || req.query.type == null) {
  21. res.send({errno: -1, errmsg: 'parameter error'});
  22. return;
  23. }
  24. msg_p2p.isUserExist(req.query.to_uid, function (err, result) {
  25. if (err) {
  26. res.send({errno: 1, errmsg: 'get users from db error'});
  27. return;
  28. }
  29. if (result.length == 0) {
  30. res.send({errno: 2, errmsg: 'no receive users'});
  31. return;
  32. }
  33. // 保存一对一消息
  34. msg_p2p.saveP2PMsg(req.query.to_uid, req.query.from_uid, req.query.type, req.query.content, function (err, result) {
  35. if (err) {
  36. res.send({errno: 3, errmsg: 'save msg to db error'});
  37. return;
  38. }
  39. res.send({errno: 0, errmsg: 'send p2p msg success'});
  40. // 更新自身的聊天统计信息
  41. msg_statistic.updateP2PChatInfo(req.query.from_uid,
  42. req.query.to_uid,
  43. req.query.from_uid,
  44. req.query.type,
  45. req.query.content,
  46. function (err, result) {
  47. if (err) {
  48. console.log(err);
  49. }
  50. });
  51. // 更新对端的聊天统计信息
  52. msg_statistic.updateP2PChatInfo(req.query.to_uid,
  53. req.query.from_uid,
  54. req.query.from_uid,
  55. req.query.type,
  56. req.query.content,
  57. function (err, result) {
  58. if (err) {
  59. console.log(err);
  60. }
  61. });
  62. // 推送通知消息给对端
  63. user.getUserbyID(req.query.to_uid, function (err, result) {
  64. if (err) {
  65. console.log('group msg:get user by id from db failed');
  66. return;
  67. }
  68. var title = '';
  69. var content = '';
  70. if (req.query.type == 1) {
  71. title = '新消息';
  72. content = req.query.content;
  73. } else if (req.query.type == 2) {
  74. title = '新消息';
  75. content = '接收到 [图片]';
  76. } else if (req.query.type == 3) {
  77. title = '新消息';
  78. content = '接收到 [语音]';
  79. } else {
  80. title = '新消息';
  81. content = '接收到一条新消息';
  82. }
  83. var bMustPush = 0;
  84. var data;
  85. if (result.length > 0) {
  86. data = result[0];
  87. if (data.is_online) {
  88. bMustPush = 1;
  89. }
  90. }
  91. var push_data = JSON.stringify({
  92. type:'p2p_msg',
  93. from_uid:req.query.from_uid
  94. });
  95. // 保存通知到数据库中
  96. push.savePushNotify(req.query.to_uid,
  97. req.query.type,
  98. title,
  99. content,
  100. push_data,
  101. bMustPush,
  102. function (err, result) {
  103. if (err) {
  104. // 保存失败
  105. console.log('save msg to db failed');
  106. } else {
  107. console.log('save msg to db success');
  108. if (bMustPush == true) {
  109. if (data.platform == 0) {// iOS
  110. getui.pushAPN(req.query.to_uid,
  111. data.token,
  112. req.query.type,
  113. title,
  114. content,
  115. push_data,
  116. function (err, result) {
  117. if (err != null) {
  118. console.log(err);
  119. } else {
  120. console.log(result);
  121. }
  122. });
  123. } else {// Android
  124. getui.pushAndroid(data.client_id,
  125. req.query.type,
  126. title,
  127. content,
  128. push_data,
  129. data.status,
  130. function (err, result) {
  131. if (err != null) {
  132. console.log(err);
  133. } else {
  134. console.log(result);
  135. }
  136. });
  137. }
  138. }
  139. }
  140. });
  141. });
  142. });
  143. });
  144. });
  145. /**
  146. * 一对一消息:p2p/getmsg.im?uid=x&peer_uid=xx&start=0&count=20
  147. * 参数:
  148. * uid:用户id
  149. * peer_uid:对端id
  150. * start;分页查询起始条目
  151. * count:查询条数
  152. * 备注:按时间倒序
  153. */
  154. router.get('/getmsg.im', function (req, res, next) {
  155. if (req.query.uid == null
  156. || req.query.peer_uid == null
  157. || req.query.start == null
  158. || req.query.count == null) {
  159. res.send({errno: -1, errmsg: 'parameter error'});
  160. return;
  161. }
  162. msg_p2p.getP2PMsg(req.query.uid, req.query.peer_uid, req.query.start, req.query.count, function (err, result) {
  163. if (err) {
  164. res.send({errno: 1, errmsg: 'get group msg from db error'});
  165. return;
  166. }
  167. var data = {};
  168. data.start = parseInt(req.query.start);
  169. data.count = result.length;
  170. data.list = new Array();
  171. for (var nIndex = 0; nIndex < result.length; nIndex++) {
  172. result[nIndex].timestamp = Date.parse(new Date(result[nIndex].timestamp));
  173. data.list.push(result[nIndex]);
  174. }
  175. res.send(data);
  176. // 清空统计信息
  177. msg_statistic.clearP2PChatInfo(req.query.uid,
  178. req.query.peer_uid,
  179. function (err, result) {
  180. if (err) {
  181. console.log(err);
  182. }
  183. });
  184. });
  185. });
  186. module.exports = router;