123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793 |
- /**
- * 消息控制器。
- *
- * 此控制器处理点对点,组及消息消息。为三类消息提供发送及查询功能。
- */
- var http = require('http');
- var qs = require('querystring');
- var getui = require('getui');
- var endpoints = require('../include/endpoints').END_POINTS;
- var express = require('express');
- var router = express.Router();
- var groupMsg = require("../models/msg.group");
- var privateMsg = require('../models/msg.p2p');
- var StatMsg = require("../models/msg.stat");
- var systemMsg = require("../models/msg.system");
- var user = require("../models/user");
- var push = require("../models/push_notify");
- /**
- * 发送P2P消息。
- *
- * from_uid=x&to_uid=xx&content=xxx&type=1
- *
- * 参数:
- * from_uid:发送者id
- * to_uid:接收者id
- * content:消息内容
- * type:消息类型:1文本,2图片,3语音
- */
- router.post(endpoints.Msg.Privates, function (req, res, next) {
- if (req.query.from_uid == null
- || req.query.to_uid == null
- || req.query.content == null
- || req.query.type == null) {
- res.send({errno: -1, errmsg: 'parameter error'});
- return;
- }
- privateMsg.isUserExist(req.query.to_uid, function (err, result) {
- if (err) {
- res.send({errno: 1, errmsg: 'get users from db error'});
- return;
- }
- if (result.length == 0) {
- res.send({errno: 2, errmsg: 'no receive users'});
- return;
- }
- // 保存一对一消息
- privateMsg.saveP2PMsg(req.query.to_uid, req.query.from_uid, req.query.type, req.query.content, function (err, result) {
- if (err) {
- res.send({errno: 3, errmsg: 'save msg to db error'});
- return;
- }
- res.send({errno: 0, errmsg: 'send p2p msg success'});
- // 更新自身的聊天统计信息
- StatMsg.updateP2PChatInfo(req.query.from_uid,
- req.query.to_uid,
- req.query.from_uid,
- req.query.type,
- req.query.content,
- function (err, result) {
- if (err) {
- console.log(err);
- }
- });
- // 更新对端的聊天统计信息
- StatMsg.updateP2PChatInfo(req.query.to_uid,
- req.query.from_uid,
- req.query.from_uid,
- req.query.type,
- req.query.content,
- function (err, result) {
- if (err) {
- console.log(err);
- }
- });
- // 推送通知消息给对端
- user.getUserbyID(req.query.to_uid, function (err, result) {
- if (err) {
- console.log('group msg:get users by id from db failed');
- return;
- }
- var title = '';
- var content = '';
- if (req.query.type == 1) {
- title = '新消息';
- content = req.query.content;
- } else if (req.query.type == 2) {
- title = '新消息';
- content = '接收到 [图片]';
- } else if (req.query.type == 3) {
- title = '新消息';
- content = '接收到 [语音]';
- } else {
- title = '新消息';
- content = '接收到一条新消息';
- }
- var bMustPush = 0;
- var data;
- if (result.length > 0) {
- data = result[0];
- if (data.is_online) {
- bMustPush = 1;
- }
- }
- var push_data = JSON.stringify({
- type: 'p2p_msg',
- from_uid: req.query.from_uid
- });
- // 保存通知到数据库中
- push.savePushNotify(req.query.to_uid,
- req.query.type,
- title,
- content,
- push_data,
- bMustPush,
- function (err, result) {
- if (err) {
- // 保存失败
- console.log('save msg to db failed');
- } else {
- console.log('save msg to db success');
- if (bMustPush == true) {
- if (data.platform == 0) {// iOS
- getui.pushAPN(req.query.to_uid,
- data.token,
- req.query.type,
- title,
- content,
- push_data,
- function (err, result) {
- if (err != null) {
- console.log(err);
- } else {
- console.log(result);
- }
- });
- } else {// Android
- getui.pushAndroid(data.client_id,
- req.query.type,
- title,
- content,
- push_data,
- data.status,
- function (err, result) {
- if (err != null) {
- console.log(err);
- } else {
- console.log(result);
- }
- });
- }
- }
- }
- });
- });
- });
- });
- });
- /**
- * 获取P2P消息。
- *
- * p2p/getmsg.im?uid=x&peer_uid=xx&start=0&count=20
- *
- * 参数:
- * uid:用户id
- * peer_uid:对端id
- * start;分页查询起始条目
- * count:查询条数
- * 备注:按时间倒序
- */
- router.get(endpoints.Msg.Privates, function (req, res, next) {
- if (req.query.uid == null
- || req.query.peer_uid == null
- || req.query.start == null
- || req.query.count == null) {
- res.send({errno: -1, errmsg: 'parameter error'});
- return;
- }
- privateMsg.getP2PMsg(req.query.uid, req.query.peer_uid, req.query.start, req.query.count, function (err, result) {
- if (err) {
- res.send({errno: 1, errmsg: 'get group msg from db error'});
- return;
- }
- var data = {};
- data.start = parseInt(req.query.start);
- data.count = result.length;
- data.list = new Array();
- for (var nIndex = 0; nIndex < result.length; nIndex++) {
- result[nIndex].timestamp = Date.parse(new Date(result[nIndex].timestamp));
- data.list.push(result[nIndex]);
- }
- res.send(data);
- // 清空统计信息
- StatMsg.clearP2PChatInfo(req.query.uid,
- req.query.peer_uid,
- function (err, result) {
- if (err) {
- console.log(err);
- }
- });
- });
- });
- /**
- * 发送群组消息。
- *
- * group/sendmsg.im?from_uid=x&to_gid=xx&content=xxx&type=1&at_uid=xxxx&group_type
- *
- * 参数:
- * from_uid:发送者id
- * to_gid:群组id
- * content:消息内容
- * type:消息类型:1文本,2图片,3语音
- * at_uid:@用户id:1为行政组,null或者其他值为自定义组
- * group_type:区分是行政组还是自定义组,从不同表中查找组成员
- */
- router.post(endpoints.Msg.Groups, function (req, res, next) {
- if (req.query.from_uid == null
- || req.query.to_gid == null
- || req.query.content == null
- || req.query.type == null) {
- res.send({errno: -1, errmsg: 'parameter error'});
- return;
- }
- groupMsg.isGroupUser(req.query.from_uid, req.query.to_gid, req.query.group_type, function (err, result) {
- if (err) {
- res.send({errno: 1, errmsg: 'get users from db error'});
- return;
- }
- if (result.length == 0) {
- res.send({errno: 2, errmsg: 'no receive users'});
- return;
- }
- var at_uid = '';
- if (req.query.at_uid != null) {
- at_uid = req.query.at_uid;
- }
- // 保存群组消息
- groupMsg.saveGroupMsg(req.query.from_uid, req.query.to_gid, at_uid, req.query.type, req.query.content, function (err, result) {
- if (err) {
- res.send({errno: 3, errmsg: 'save msg to db error'});
- return;
- }
- res.send({errno: 0, errmsg: 'send group msg success'});
- // 统计信息
- StatMsg.updateGroupChatInfo(req.query.from_uid,
- req.query.to_gid,
- req.query.from_uid,
- 0,
- req.query.type,
- req.query.content,
- false,
- function (err, result) {
- if (err) {
- console.log(err);
- }
- });
- // 推送通知消息给群组各成员
- groupMsg.getGroupUsers(req.query.to_gid, req.query.group_type, function (err, result) {
- if (err) {
- console.log('get users from db error');
- return;
- }
- if (result.length == 0) {
- console.log('no receive users');
- return;
- }
- // 推送通知
- for (var nIndex = 0; nIndex < result.length; nIndex++) {
- if (result[nIndex].member_code == req.query.from_uid) {
- continue;
- }
- (function () {
- var toUserID = result[nIndex].member_code;
- user.getUserbyID(toUserID, function (err, result) {
- var tmp = toUserID;
- if (err) {
- console.log('group msg:get users by id from db failed');
- return;
- }
- var title = '';
- var content = '';
- if (req.query.type == 1) {
- title = '群组消息';
- content = req.query.content;
- } else if (req.query.type == 2) {
- title = '群组消息';
- content = '接收到 [图片]';
- } else if (req.query.type == 3) {
- title = '群组消息';
- content = '接收到 [语音]';
- } else {
- title = '群组消息';
- content = '接收到一条新消息';
- }
- var bMustPush = 0;
- var data;
- if (result.length > 0) {
- data = result[0];
- if (data.is_online) {
- bMustPush = 1;
- }
- }
- var push_data = JSON.stringify({
- type: 'group_msg',
- gid: req.query.to_gid
- });
- // 保存通知到数据库中
- push.savePushNotify(toUserID,
- req.query.type,
- title,
- content,
- push_data,
- bMustPush,
- function (err, result) {
- if (err) {
- // 保存失败
- console.log('save msg to db failed');
- } else {
- console.log('save msg to db success');
- if (bMustPush == true) {
- if (data.platform == 0) {// iOS
- getui.pushAPN(toUserID,
- data.token,
- req.query.type,
- title,
- content,
- push_data,
- function (err, result) {
- if (err != null) {
- console.log(err);
- } else {
- console.log(result);
- }
- });
- } else {// Android
- getui.pushAndroid(data.client_id,
- req.query.type,
- title,
- content,
- push_data,
- data.status,
- function (err, result) {
- if (err != null) {
- console.log(err);
- } else {
- console.log(result);
- }
- });
- }
- }
- }
- });
- });
- // 统计信息
- var at_me = 0;
- if (at_uid == toUserID) {
- at_me = 1;
- }
- StatMsg.updateGroupChatInfo(toUserID,
- req.query.to_gid,
- req.query.from_uid,
- at_me,
- req.query.type,
- req.query.content,
- true,
- function (err, result) {
- if (err) {
- console.log(err);
- }
- });
- })();
- }
- });
- });
- });
- });
- /**
- * 读取群组消息。
- *
- * :group/getmsg.im?uid=x&gid=xx&start=0&count=20
- *
- * 参数:
- * uid:用户id
- * gid:群组id
- * start;分页查询起始条目
- * count:查询条数
- * 备注:按时间倒序
- */
- router.get(endpoints.Msg.Groups, function (req, res, next) {
- if (req.query.uid == null
- || req.query.gid == null
- || req.query.start == null
- || req.query.count == null) {
- res.send({errno: -1, errmsg: 'parameter error'});
- return;
- }
- groupMsg.getGroupMsg(req.query.gid, req.query.start, req.query.count, function (err, result) {
- if (err) {
- res.send({errno: 1, errmsg: 'get group msg from db error'});
- return;
- }
- var data = {};
- data.start = parseInt(req.query.start);
- data.count = result.length;
- data.list = new Array();
- for (var nIndex = 0; nIndex < result.length; nIndex++) {
- result[nIndex].timestamp = Date.parse(new Date(result[nIndex].timestamp));
- data.list.push(result[nIndex]);
- }
- res.send(data);
- // 清空统计信息
- StatMsg.clearGroupChatInfo(req.query.uid,
- req.query.gid,
- function (err, result) {
- if (err) {
- console.log(err);
- }
- });
- });
- });
- /**
- * 发送系统消息。
- *
- * system/sendmsg.im?to_uid=x&type=xx&title=xxx&content=xxxx&data=xxxxx
- *
- * 参数:
- * to_uid:消息接收者ID
- * type:消息类型
- * title:消息标题
- * content:推送消息提示内容
- * data:推送消息内容
- */
- router.get(endpoints.Msg.System, function (req, res, next) {
- if (req.query.to_uid == null
- || req.query.title == null
- || req.query.type == null
- || req.query.content == null
- || req.query.data == null) {
- res.send({errno: -1, errmsg: 'parameter error'});
- return;
- }
- user.getUserbyID(req.query.to_uid, function (err, result) {
- if (err) {
- res.send({errno: 1, errmsg: 'get users by id from db failed'});
- return;
- }
- var bMustPush = 0;
- var data;
- if (result.length > 0) {
- data = result[0];
- if (data.is_online) {
- bMustPush = 1;
- }
- }
- var push_data = JSON.stringify({
- type: req.query.type,
- data: req.query.data
- });
- // 保存该条推送信息
- systemMsg.saveSystemMsg(req.query.to_uid,
- req.query.type,
- req.query.title,
- req.query.content,
- req.query.data,
- function (err, result) {
- if (err) {
- // 保存失败
- res.send({errno: 2, errmsg: 'save msg to db failed'});
- } else {
- // 保存通知到数据库中
- push.savePushNotify(req.query.to_uid,
- req.query.type,
- req.query.title,
- req.query.content,
- push_data,
- bMustPush,
- function (err, result) {
- if (err) {
- // 保存失败
- res.send({errno: 3, errmsg: 'save msg to db failed'});
- } else {
- res.send({errno: 0, errmsg: 'save msg to db success'});
- if (bMustPush == true) {
- if (data.platform == 0) {// iOS
- getui.pushAPN(req.query.to_uid,
- data.token,
- req.query.type,
- req.query.title,
- req.query.content,
- push_data,
- function (err, result) {
- if (err != null) {
- console.log(err);
- } else {
- console.log(result);
- }
- });
- } else {// Android
- getui.pushAndroid(data.client_id,
- req.query.type,
- req.query.title,
- req.query.content,
- push_data,
- data.status,
- function (err, result) {
- if (err != null) {
- console.log(err);
- } else {
- console.log(result);
- }
- });
- }
- }
- }
- });
- }
- });
- });
- });
- /**
- * 获取群组消息统计。
- *
- * statistic/getgroupchatinfo.im?uid=x&gid=xx
- *
- * 参数:
- * uid:信息所有者id
- * gid:群组id
- */
- router.get(endpoints.Msg.GroupStat, function (req, res, next) {
- if (req.query.uid == null
- || req.query.gid == null) {
- res.send({errno: -1, errmsg: 'parameter error'});
- return;
- }
- StatMsg.getGroupChatInfo(req.query.uid, req.query.gid, function (err, result) {
- if (err) {
- res.send({errno: 1, errmsg: 'get statistic from db error'});
- return;
- }
- if (result.length == 0) {
- 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};
- res.send(data);
- return;
- }
- result[0].timestamp = Date.parse(new Date(result[0].timestamp));
- res.send(result[0]);
- });
- });
- /**
- * 获取点对点消息统计。
- *
- * statistic/getp2pchatinfo.im?uid=x&peer_uid=xx
- *
- * 参数:
- * uid:信息所有者id
- * peer_uid:聊天对端id
- */
- router.get(endpoints.Msg.PrivateStat, function (req, res, next) {
- if (req.query.uid == null
- || req.query.peer_uid == null) {
- res.send({errno: -1, errmsg: 'parameter error'});
- return;
- }
- StatMsg.getP2PChatInfo(req.query.uid, req.query.peer_uid, function (err, result) {
- if (err) {
- res.send({errno: 1, errmsg: 'get statistic from db error'});
- return;
- }
- if (result.length == 0) {
- var data = {"uid":req.query.uid,"from_uid":req.query.peer_uid,"last_content_type":1,"last_content":"","new_msg_count":0,"timestamp":0};
- res.send(data);
- return;
- }
- result[0].timestamp = Date.parse(new Date(result[0].timestamp));
- res.send(result[0]);
- });
- });
- /**
- * 获取参与的聊天列表,包括:点对点,@我,参与的讨论组等。
- *
- * statistic/getchatlist.im?uid=x
- *
- * 参数:
- * uid:信息所有者id
- */
- router.get(endpoints.Users.ChatList, function (req, res, next) {
- if (req.query.uid == null) {
- res.send({errno: -1, errmsg: 'parameter error'});
- return;
- }
- StatMsg.getChatList(req.query.uid, function (err, result) {
- if (err) {
- res.send({errno: 1, errmsg: 'get statistic from db error'});
- return;
- }
- if (result.length == 0) {
- var data = {"uid":req.query.uid,"from_uid":req.query.peer_uid,"last_content_type":1,"last_content":"","new_msg_count":0,"timestamp":0};
- res.send(data);
- return;
- }
- //result[0].timestamp = Date.parse(new Date(result[0].timestamp));
- for (var index = 0; index < result.length; index++) {
- result[index].timestamp = Date.parse(new Date(result[index].timestamp));
- }
- res.send(result);
- });
- });
- /**
- * 群组聊天消息所有未读数。
- *
- * statistic/getgroupunreadcount.im?uid=x
- *
- * 参数:
- * uid:信息所有者id
- */
- router.get(endpoints.Users.GroupUnreadMsgCount, function (req, res, next) {
- if (req.query.uid == null) {
- res.send({errno: -1, errmsg: 'parameter error'});
- return;
- }
- StatMsg.getGroupChatAllUnRead(req.query.uid, function (err, result) {
- if (err) {
- res.send({errno: 1, errmsg: 'get statistic from db error'});
- return;
- }
- var data = {"uid":req.query.uid,"msg_type":2,"new_msg_count":0};
- if (result.length == 0) {
- res.send(data);
- return;
- }
- var count = 0;
- var index = 0;
- var length = result.length;
- for (; index < length; index++) {
- count += result[index].new_msg_count;
- }
- data.new_msg_count = count;
- res.send(data);
- });
- });
- /**
- * 一对一聊天消息所有未读数。
- *
- * statistic/getp2punreadcount.im?uid=x
- *
- * 参数:
- * uid:信息所有者id
- */
- router.get(endpoints.Users.PrivateUnreadMsgCount, function (req, res, next) {
- if (req.query.uid == null) {
- res.send({errno: -1, errmsg: 'parameter error'});
- return;
- }
- StatMsg.getP2PChatAllUnRead(req.query.uid, function (err, result) {
- if (err) {
- res.send({errno: 1, errmsg: 'get statistic from db error'});
- return;
- }
- var data = {"uid":req.query.uid,"msg_type":1,"new_msg_count":0};
- if (result.length == 0) {
- res.send(data);
- return;
- }
- var count = 0;
- var index = 0;
- var length = result.length;
- for (; index < length; index++) {
- count += result[index].new_msg_count;
- }
- data.new_msg_count = count;
- res.send(data);
- });
- });
- /**
- * 所有聊天消息未读数。
- *
- * statistic/getallunreadmsgcount.im?uid=x
- *
- * 参数:
- * uid:信息所有者id
- */
- router.get(endpoints.Users.UnreadMsgCount, function (req, res, next) {
- if (req.query.uid == null) {
- res.send({errno: -1, errmsg: 'parameter error'});
- return;
- }
- StatMsg.getChatAllUnRead(req.query.uid, function (err, result) {
- if (err) {
- res.send({errno: 1, errmsg: 'get statistic from db error'});
- return;
- }
- var data = {"uid":req.query.uid,"msg_type":0,"new_msg_count":0};
- if (result.length == 0) {
- res.send(data);
- return;
- }
- var count = 0;
- var index = 0;
- var length = result.length;
- for (; index < length; index++) {
- count += result[index].new_msg_count;
- }
- data.new_msg_count = count;
- res.send(data);
- });
- });
- /**
- * 获取角标数。
- *
- * statistic/getbadgenum.im?uid=x
- *
- * 参数:
- * uid:用户id
- */
- router.get('/getbadgenum.im', function (req, res, next) {
- if (req.query.uid == null) {
- res.send({errno: -1, errmsg: 'parameter error'});
- return;
- }
- StatMsg.getBadgeNumber(req.query.uid, function (err, result) {
- if (err) {
- res.send({errno: 1, errmsg: 'get badge error'});
- return;
- }
- var data = {"uid":req.query.uid,"badge":result};
- res.send(data);
- });
- });
- module.exports = router;
|