123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- var express = require('express');
- var router = express.Router();
- var msg_p2p = require("../models/msg_p2p");
- var msg_statistic = require("../models/msg_statistic");
- var user = require("../models/user");
- var push = require("../models/push_notify");
- var getui = require('getui');
- /**
- * 一对一消息:p2p/sendmsg.im?from_uid=x&to_uid=xx&content=xxx&type=1
- * 参数:
- * from_uid:发送者id
- * to_uid:接收者id
- * content:消息内容
- * type:消息类型:1文本,2图片,3语音
- */
- router.get('/sendmsg.im', 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;
- }
- msg_p2p.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;
- }
- // 保存一对一消息
- msg_p2p.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'});
- // 更新自身的聊天统计信息
- msg_statistic.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);
- }
- });
- // 更新对端的聊天统计信息
- msg_statistic.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 user 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/getmsg.im?uid=x&peer_uid=xx&start=0&count=20
- * 参数:
- * uid:用户id
- * peer_uid:对端id
- * start;分页查询起始条目
- * count:查询条数
- * 备注:按时间倒序
- */
- router.get('/getmsg.im', 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;
- }
- msg_p2p.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);
- // 清空统计信息
- msg_statistic.clearP2PChatInfo(req.query.uid,
- req.query.peer_uid,
- function (err, result) {
- if (err) {
- console.log(err);
- }
- });
- });
- });
- module.exports = router;
|