msg_statistic.js 8.0 KB

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