118 lines
4.1 KiB
TypeScript
118 lines
4.1 KiB
TypeScript
import {Application, BackendSession} from 'pinus';
|
||
import { resResult } from '../../../pubUtils/util';
|
||
import { STATUS } from '../../../consts';
|
||
import { createPrivateMsg, pushMsgToRole } from '../../../services/chatService';
|
||
|
||
|
||
export default function(app: Application) {
|
||
return new ChatHandler(app);
|
||
}
|
||
|
||
export class ChatHandler {
|
||
constructor(private app: Application) {
|
||
}
|
||
|
||
/**
|
||
* Send messages to users
|
||
*
|
||
* @param {Object} msg message from client
|
||
* @param {Object} session
|
||
*
|
||
*/
|
||
async send(msg: {content: string , target: string}, session: BackendSession) {
|
||
let rid = session.get('rid');
|
||
let username = session.uid.split('*')[0];
|
||
let channelService = this.app.get('channelService');
|
||
let param = {
|
||
msg: msg.content,
|
||
from: username,
|
||
target: msg.target
|
||
};
|
||
let channel = channelService.getChannel(rid, false);
|
||
|
||
// the target is all users
|
||
if (msg.target === '*') {
|
||
channel.pushMessage('onChat', resResult(STATUS.SUCCESS, param));
|
||
}
|
||
// the target is specific user
|
||
else {
|
||
let tuid = msg.target + '*' + rid;
|
||
let tsid = channel.getMember(tuid)['sid'];
|
||
channelService.pushMessageByUids('onChat', resResult(STATUS.SUCCESS, param), [{
|
||
uid: tuid,
|
||
sid: tsid
|
||
}]);
|
||
}
|
||
}
|
||
|
||
async send2(msg: {content: string , target: string}, session: BackendSession) {
|
||
let rid = session.get('rid');
|
||
let username = session.uid.split('*')[0];
|
||
let channelService = this.app.get('channelService');
|
||
let param = {
|
||
msg: msg.content,
|
||
from: username,
|
||
target: msg.target
|
||
};
|
||
let channel = channelService.getChannel(rid, false);
|
||
|
||
// the target is all users
|
||
if (msg.target === '*') {
|
||
channel.pushMessage('onChat', resResult(STATUS.SUCCESS, param));
|
||
}
|
||
// the target is specific user
|
||
else {
|
||
let tuid = msg.target + '*' + rid;
|
||
let tsid = channel.getMember(tuid)['sid'];
|
||
channelService.pushMessageByUids('onChat', resResult(STATUS.SUCCESS, param), [{
|
||
uid: tuid,
|
||
sid: tsid
|
||
}]);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @description 群聊发送接口
|
||
* @param {{ channel: string, type: number, content: string, targetRoleId: string, targetMsgCode: string }} msg
|
||
* @param {BackendSession} session
|
||
* @memberof ChatHandler
|
||
*/
|
||
async sendGroupMessage(msg: { channel: string, channelId: string, type: number, content: string, targetRoleId: string, targetMsgCode: string }, session: BackendSession) {
|
||
const { channel, channelId, type, content, targetRoleId, targetMsgCode } = msg;
|
||
}
|
||
|
||
/**
|
||
* @description 私聊发送接口
|
||
* @param {{ type: number, content: string, targetRoleId: string, targetMsgCode: string }} msg
|
||
* @param {BackendSession} session
|
||
* @memberof ChatHandler
|
||
*/
|
||
async sendPrivateMessage(msg: { type: number, content: string, targetRoleId: string, targetMsgCode: string }, session: BackendSession) {
|
||
const { type, content, targetRoleId, targetMsgCode } = msg;
|
||
const roleId = session.get('roleId');
|
||
const roleName = session.get('roleName');
|
||
const msgData = await createPrivateMsg(roleId, roleName, type, content, targetRoleId, targetMsgCode);
|
||
await pushMsgToRole(targetRoleId, msgData);
|
||
if (!msgData) return resResult(STATUS.WRONG_PARMS);
|
||
return resResult(STATUS.SUCCESS);
|
||
}
|
||
|
||
/**
|
||
* @description 获取初始聊天消息,数据和登录时返回的一致
|
||
* @param {{}} msg
|
||
* @memberof ChatHandler
|
||
*/
|
||
async getInitMessage(msg: {}) {
|
||
|
||
}
|
||
|
||
/**
|
||
* @description 获取私聊历史消息
|
||
* @param {{targetRoleId: string, fromSeqId: number, count: number}} msg 聊天对象;聊天消息的顺序 id,不传为从最新开始;count 为消息条数
|
||
* @memberof ChatHandler
|
||
*/
|
||
async getPrivateMessage(msg: {targetRoleId: string, fromSeqId: number, count: number}) {
|
||
|
||
}
|
||
|
||
} |