msg.stat.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. "use strict";
  2. var http = require('http');
  3. var qs = require('querystring');
  4. var async = require('async');
  5. var config = require('../include/commons').CONFIG_FILE;
  6. var log = require('../util/log');
  7. var wlyyRepo = require("../repository/wlyy.repo");
  8. var imRepo = require("../repository/im.repo");
  9. function updateGroupChatInfo(user_id, group_id, from_uid, at_me, type, content, msg_count_plus_one, handler) {
  10. var uuid = user_id + '_' + group_id;
  11. if (msg_count_plus_one) {
  12. imRepo.execQuery({
  13. "sql": "INSERT INTO msg_statistic (uid,uuid,from_uid,from_gid,at_me,msg_type,last_content_type,last_content,new_msg_count) VALUES (?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE from_uid=?,at_me=?,last_content_type=?,last_content=?,new_msg_count=new_msg_count+1",
  14. "args": [user_id, uuid, from_uid, group_id, at_me, 2, type, content, 1, from_uid, at_me, type, content],
  15. "handler": handler
  16. });
  17. } else {
  18. imRepo.execQuery({
  19. "sql": "INSERT INTO msg_statistic (uid,uuid,from_uid,from_gid,at_me,msg_type,last_content_type,last_content,new_msg_count) VALUES (?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE from_uid=?,at_me=?,last_content_type=?,last_content=?",
  20. "args": [user_id, uuid, from_uid, group_id, at_me, 2, type, content, 0, from_uid, at_me, type, content],
  21. "handler": handler
  22. });
  23. }
  24. }
  25. function updateP2PChatInfo(user_id, peer_uid, from_uid, type, content, handler) {
  26. var uuid = user_id + '_' + peer_uid;
  27. if (user_id == from_uid) {
  28. // 更新自身的统计信息
  29. imRepo.execQuery({
  30. "sql": "INSERT INTO msg_statistic (uid,uuid,from_uid,peer_uid,msg_type,last_content_type,last_content,new_msg_count) VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE peer_uid=?,last_content_type=?,last_content=?",
  31. "args": [user_id, uuid, from_uid, peer_uid, 1, type, content, 0, peer_uid, type, content],
  32. "handler": handler
  33. });
  34. } else {
  35. // 更新对端的统计信息
  36. imRepo.execQuery({
  37. "sql": "INSERT INTO msg_statistic (uid,uuid,from_uid,peer_uid,msg_type,last_content_type,last_content,new_msg_count) VALUES (?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE peer_uid=?,last_content_type=?,last_content=?,new_msg_count=new_msg_count+1",
  38. "args": [user_id, uuid, from_uid, peer_uid, 1, type, content, 1, peer_uid, type, content],
  39. "handler": handler
  40. });
  41. }
  42. }
  43. function clearGroupChatInfo(user_id, group_id, handler) {
  44. var uuid = user_id + '_' + group_id;
  45. imRepo.execQuery({
  46. "sql": "UPDATE msg_statistic SET new_msg_count='0' WHERE uuid=?",
  47. "args": [uuid],
  48. "handler": handler
  49. });
  50. }
  51. function clearP2PChatInfo(user_id, peer_uid, handler) {
  52. var uuid = user_id + '_' + peer_uid;
  53. imRepo.execQuery({
  54. "sql": "UPDATE msg_statistic SET new_msg_count='0' WHERE uuid=?",
  55. "args": [uuid],
  56. "handler": handler
  57. });
  58. }
  59. function getGroupChatInfo(user_id, group_id, handler) {
  60. var uuid = user_id + '_' + group_id;
  61. imRepo.execQuery({
  62. "sql": "SELECT uid,from_uid,from_gid,at_me,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uuid = ?",
  63. "args": [uuid],
  64. "handler": handler
  65. });
  66. }
  67. function getP2PChatInfo(user_id, peer_uid, handler) {
  68. var uuid = user_id + '_' + peer_uid;
  69. imRepo.execQuery({
  70. "sql": "SELECT uid,from_uid,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uuid = ?",
  71. "args": [uuid],
  72. "handler": handler
  73. });
  74. }
  75. function getChatList(user_id, handler) {
  76. imRepo.execQuery({
  77. "sql": "SELECT uid,from_uid,from_gid,peer_uid,at_me,msg_type,last_content_type,last_content,new_msg_count,timestamp from msg_statistic WHERE uid = ?",
  78. "args": [user_id],
  79. "handler": handler
  80. });
  81. }
  82. function getGroupChatAllUnRead(user_id, handler) {
  83. imRepo.execQuery({
  84. "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND msg_type=2 AND new_msg_count>0",
  85. "args": [user_id],
  86. "handler": handler
  87. });
  88. }
  89. function getP2PChatAllUnRead(user_id, handler) {
  90. imRepo.execQuery({
  91. "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND msg_type=1 AND new_msg_count>0",
  92. "args": [user_id],
  93. "handler": handler
  94. });
  95. }
  96. function getChatAllUnRead(user_id, handler) {
  97. imRepo.execQuery({
  98. "sql": "SELECT new_msg_count from msg_statistic WHERE uid=? AND new_msg_count>0",
  99. "args": [user_id],
  100. "handler": handler
  101. });
  102. }
  103. function getAppMsgAmount(user_id, handler) {
  104. wlyyRepo.execQuery({
  105. "sql": "SELECT imei,token from wlyy_token WHERE user=?",
  106. "args": [user_id],
  107. "handler": function(err, result) {
  108. if (err) {
  109. handler(null, 0);
  110. return;
  111. }
  112. if (result.length == 0) {
  113. handler(null, 0);
  114. return;
  115. }
  116. var options = {
  117. hostname: config.app_serer_cfg.hostname,
  118. port: config.app_serer_cfg.port,
  119. path: config.app_serer_cfg.path,
  120. method: config.app_serer_cfg.method,
  121. headers: {
  122. 'userAgent': '{"token":"'+ result[0].token +'","uid":"'+ user_id +'","imei":"'+ result[0].imei +'"}'
  123. }
  124. };
  125. var req = http.request(options, function (res) {
  126. res.setEncoding('utf8');
  127. res.on('data', function (chunk) {
  128. console.log('BODY: ' + chunk);
  129. var data = JSON.parse(chunk);
  130. handler(null, data);
  131. });
  132. });
  133. req.on('error', function (e) {
  134. console.log('problem with request: ' + e.message);
  135. handler(e, null);
  136. });
  137. req.end();
  138. }
  139. });
  140. }
  141. function getBadgeNumber(user_id, handler) {
  142. async.parallel([
  143. function(callback) {
  144. callback(null, 0);
  145. },
  146. function(callback) {
  147. getAppMsgAmount(user_id, function(err, result) {
  148. if (err) {
  149. callback(null, 0);
  150. } else {
  151. var count = 0;
  152. try {
  153. count += result.data.healthIndex;
  154. count += result.data.sign;
  155. count += result.data.consultTeam;
  156. callback(null, count);
  157. } catch (e) {
  158. callback(null, 0);
  159. }
  160. }
  161. });
  162. }],
  163. function(err, results) {
  164. var badge = 0;
  165. for (var index = 0; index < results.length; index++) {
  166. badge += results[index];
  167. }
  168. handler(null, badge);
  169. });
  170. }
  171. exports.updateGroupChatInfo = updateGroupChatInfo;
  172. exports.updateP2PChatInfo = updateP2PChatInfo;
  173. exports.clearGroupChatInfo = clearGroupChatInfo;
  174. exports.clearP2PChatInfo = clearP2PChatInfo;
  175. exports.getGroupChatInfo = getGroupChatInfo;
  176. exports.getP2PChatInfo = getP2PChatInfo;
  177. exports.getChatList = getChatList;
  178. exports.getGroupChatAllUnRead = getGroupChatAllUnRead;
  179. exports.getP2PChatAllUnRead = getP2PChatAllUnRead;
  180. exports.getChatAllUnRead = getChatAllUnRead;
  181. exports.getAppMsgAmount = getAppMsgAmount;
  182. exports.getBadgeNumber = getBadgeNumber;