聊天:添加屏蔽

This commit is contained in:
luying
2021-11-10 17:28:11 +08:00
parent b15a9088e8
commit 32fe508625
16 changed files with 260 additions and 54 deletions

View File

@@ -1,4 +1,4 @@
import { CHANNEL_PREFIX, MSG_SOURCE, CHANNEL_TYPE } from './../../../consts/constModules/chatConst';
import { CHANNEL_PREFIX, MSG_SOURCE, getChannelType } from './../../../consts/constModules/chatConst';
import { Application, BackendSession, HandlerService, } from 'pinus';
import { resResult } from '../../../pubUtils/util';
import { DEFAULT_MSG_PER_PAGE, STATUS, TASK_TYPE } from '../../../consts';
@@ -99,10 +99,10 @@ export class ChatHandler {
await pushGroupMsgToRoom(msgData);
// 任务
await checkTaskWithArgs(roleId, sid, TASK_TYPE.CHAT, [CHANNEL_TYPE.get(channel)]);
await checkTaskWithArgs(roleId, sid, TASK_TYPE.CHAT, [getChannelType(channel)]);
//活动任务
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.CHAT, 1, { chatType: CHANNEL_TYPE.get(channel) })
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.CHAT, 1, { chatType: getChannelType(channel) })
return resResult(STATUS.SUCCESS, msgData);
}
@@ -125,9 +125,9 @@ export class ChatHandler {
if (!msgData) return resResult(STATUS.WRONG_PARMS);
// 任务
await checkTaskWithArgs(roleId, sid, TASK_TYPE.CHAT, [CHANNEL_TYPE.get('private')]);
await checkTaskWithArgs(roleId, sid, TASK_TYPE.CHAT, [getChannelType('private')]);
//活动任务
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.CHAT, 1, { chatType: CHANNEL_TYPE.get('private') })
await checkActivityTask(serverId, sid, roleId, TASK_TYPE.CHAT, 1, { chatType: getChannelType('private') })
return resResult(STATUS.SUCCESS, msgData);
}

View File

@@ -1,10 +1,11 @@
import {Application, RouteRecord, FrontendOrBackendSession, HandlerCallback, pinus} from "pinus";
// import { checkEvent } from '../../../services/eventSercive';
import { refresh } from '../../../services/refreshService';
import { checkFilterWords, getWorldChannelSid } from "../../../services/chatService";
import { checkPrivateChat, checkGuildChat, checkOtherChat, getWorldChannelSid } from "../../../services/chatService";
import { resResult, checkWhiteListWithUid } from "../../../pubUtils/util";
import { STATUS } from "../../../consts";
import { CHANNEL_PREFIX, STATUS } from "../../../consts";
import { UserModel } from "../../../db/User";
import { SERVER_DEBUG_MODE } from "../../../pubUtils/dicParam";
export function globalFilter(app: Application) {
return new Filter(app);
}
@@ -31,15 +32,39 @@ Filter.prototype.before = async function (routeRecord: RouteRecord, msg: any, se
}
let hasBlockWords = false;
if(pinus.app.get('env') == 'production') {
if(routeRecord.route == 'guild.guildHandler.createGuild') {
hasBlockWords = await checkFilterWords(msg.name);
} else if (routeRecord.route == 'role.roleHandler.initRole') {
hasBlockWords = await checkFilterWords(msg.roleName);
} else if (routeRecord.route == 'role.roleHandler.rename') {
hasBlockWords = await checkFilterWords(msg.roleName);
// if(pinus.app.get('env') == 'production') {
// if(routeRecord.route == 'guild.guildHandler.createGuild') {
// hasBlockWords = await checkFilterWords(msg.name);
// } else if (routeRecord.route == 'role.roleHandler.initRole') {
// hasBlockWords = await checkFilterWords(msg.roleName);
// } else if (routeRecord.route == 'role.roleHandler.rename') {
// hasBlockWords = await checkFilterWords(msg.roleName);
// }
// }
if(SERVER_DEBUG_MODE.CHECK_WORD == 1) {
switch(routeRecord.route) {
case 'chat.chatHandler.sendPrivateMessage':
{
let result = await checkPrivateChat(roleId, msg.targetRoleId, msg.content);
if(!result) hasBlockWords = true;
break;
}
case 'chat.chatHandler.sendGroupMessage':
{
if(msg.channel == CHANNEL_PREFIX.GUILD) {
const guildCode = session.get('guildCode');
let result = await checkGuildChat(roleId, guildCode, msg.content);
if(!result) hasBlockWords = true;
break;
} else {
let result = await checkOtherChat(roleId, msg.channel, msg.content);
if(!result) hasBlockWords = true;
break;
}
}
}
}
if(hasBlockWords) return next(new Error(), resResult(STATUS.BLOCK_WORDS));
next(null);

View File

@@ -1,6 +1,6 @@
import { CHAT_SYSTEM } from './../pubUtils/dicParam';
import { PrivateChatRec } from './../db/ChatInfo';
import { RoleModel } from './../db/Role';
import { RoleModel, RoleType } from './../db/Role';
import { GroupMessageModel } from './../db/GroupMessage';
import { CounterModel } from './../db/Counter';
import { STATUS } from './../consts/statusCode';
@@ -8,13 +8,16 @@ import { PrivateMessageModel, PrivateMessageParam, PrivateMessageType } from './
import { GroupMessageParam, GroupMessageType } from '../db/GroupMessage';
import { genCode, resResult } from '../pubUtils/util';
import { pinus } from 'pinus';
import { CHANNEL_PREFIX, MSG_CODE_LEN, MSG_STATUS, ON_PRIVATE_MSG_ROUTE, MSG_TYPE, MSG_SOURCE } from '../consts';
import { CHANNEL_PREFIX, MSG_CODE_LEN, MSG_STATUS, ON_PRIVATE_MSG_ROUTE, MSG_TYPE, MSG_SOURCE, getSdkChannelId, SDK_37_CONST, SDK_37_ADDR } from '../consts';
import { getAllServers, getRoleOnlineInfo } from './redisService';
import { ChatInfoModel } from '../db/ChatInfo';
import { channelServer } from './chatChannelService';
import { AccuseRecModel, AccueseParam } from '../db/AccuseRec';
import { getSimpleRoleInfo, getSimpleRoleInfos } from './roleService';
import * as FCClient from '@alicloud/fc2';
import { Chat37Params } from '../domain/sdk';
import { UserModel } from '../db/User';
import { request37CheckChat } from '../pubUtils/httpUtil';
export * from './chatChannelService';
export * from './sysChatService';
@@ -378,6 +381,7 @@ export async function createAccuseData(roleId: string, roleName: string, targetR
return result;
}
/*
const client = new FCClient('1475752363809339', {
accessKeyID: 'LTAI5tFTwE7vwH5HGL7eV8xr',
accessKeySecret: 'HZznAZfOrmXttBMevhA55jQX3OGw9j',
@@ -399,6 +403,44 @@ export async function checkFilterWords(word: string) {
return hasBlock;
}
*/
export async function checkPrivateChat(roleId: string, targetRoleId: string, message: string) {
let body = new Chat37Params(message, getSdkChannelId('private'));
let myRole = await RoleModel.findByRoleId(roleId);
let myUser = await UserModel.findUserByUid(myRole.userInfo.uid);
if(myUser.channelType == '37') {
body.setByMyRole(myRole, myUser);
let targetRole = await RoleModel.findByRoleId(targetRoleId);
let targetUser = await UserModel.findUserByUid(targetRole.userInfo.uid);
body.setByTargetRole(targetRole, targetUser);
return await request37CheckChat(SDK_37_ADDR.CHECK_CHAT, body, SDK_37_CONST.CHAT_KEY);
}
return true;
}
export async function checkGuildChat(roleId: string, guildCode: string, message: string) {
let body = new Chat37Params(message, getSdkChannelId(CHANNEL_PREFIX.GUILD));
let myRole = await RoleModel.findByRoleId(roleId);
let myUser = await UserModel.findUserByUid(myRole.userInfo.uid);
if(myUser.channelType == '37') {
body.setByMyRole(myRole, myUser);
body.setByGuild(guildCode);
return await request37CheckChat(SDK_37_ADDR.CHECK_CHAT, body, SDK_37_CONST.CHAT_KEY);
}
return true;
}
export async function checkOtherChat(roleId: string, channelPrefix: string, message: string) {
let body = new Chat37Params(message, getSdkChannelId(channelPrefix));
let myRole = await RoleModel.findByRoleId(roleId);
let myUser = await UserModel.findUserByUid(myRole.userInfo.uid);
if(myUser.channelType == '37') {
body.setByMyRole(myRole, myUser);
return await request37CheckChat(SDK_37_ADDR.CHECK_CHAT, body, SDK_37_CONST.CHAT_KEY);
}
return true;
}
export async function pushCurrentTime(time: number) {
let serverlists = await getAllServers()

View File

@@ -28,7 +28,7 @@ export async function GTAuth() {
timestamp,
appkey: APP_KEY,
};
let result = await httpRequest(url, HTTP_METHOD.POST, headers, body)
let result = await httpRequest(url, HTTP_METHOD.POST, body, headers)
if (result.code == 0) {
console.log('GETUI_TOKEN', result.data.token)
return await ServerTempModel.updateGetuiData(result.data.token, result.data.expire_time);
@@ -151,7 +151,7 @@ export async function request(url: string, method: string, headers: any, body: a
}
headers = { ...headers, 'content-type': 'application/json;charset=utf-8', 'token': tokenData.getuiToken };
let result = await httpRequest(url, method, headers, body)
let result = await httpRequest(url, method, body, headers)
if (result.code == 10001) {
if (await GTAuth()) {
console.log('token success11')

View File

@@ -23,13 +23,33 @@ export const CHANNEL_PREFIX = {
WORLD_AUCTION: 'w_auction', // 军团拍卖
}
export const CHANNEL_TYPE = new Map([
[ CHANNEL_PREFIX.SYS, 1 ],
[ CHANNEL_PREFIX.WORLD, 2 ],
[ CHANNEL_PREFIX.GUILD, 3 ],
[ CHANNEL_PREFIX.TEAM, 4 ],
[ 'private', 5 ],
]);
export const getChannelType = function(prefix: string) {
switch(prefix) {
case CHANNEL_PREFIX.SYS:
return 1;
case CHANNEL_PREFIX.WORLD:
return 2;
case CHANNEL_PREFIX.GUILD:
return 3;
case CHANNEL_PREFIX.TEAM:
return 4;
case 'private':
return 5;
}
}
export const getSdkChannelId = function(prefix: string) {
switch(prefix) {
case CHANNEL_PREFIX.WORLD:
return 1;
case CHANNEL_PREFIX.GUILD:
return 4;
case CHANNEL_PREFIX.TEAM:
return 3;
case 'private':
return 2;
}
}
// 消息来源
export const MSG_SOURCE = {

View File

@@ -13,7 +13,8 @@ export enum BANTU_VID_ADDR {
export const BANTU_VID_APP_KEY = '05c1c495369769e3c5d98426e9c8c2c0';
export enum SDK_37_ADDR {
LOGIN = 'https://apimyh5.37.com/index.php?c=sdk-unified&a=validate'
LOGIN = 'https://apimyh5.37.com/index.php?c=sdk-unified&a=validate',
CHECK_CHAT = 'http://api.gamechat.37.com/checkChatV2.php'
}
export enum SDK_37_CONST {
GAME_ID = 165, // 研发使用的GAME_ID

View File

@@ -11,6 +11,9 @@ export const STATUS = {
VERSION_ERR: { code: 8, simStr: '版本号太低,请更新' },
BLOCK_WORDS: { code: 9, simStr: '内容有不合法词汇' },
GLOBAL_ERR: { code: 1003, simStr: '服务器内部错误' },
// http请求
REQUEST_TIME_OUT: { code: 2000, simStr: '请求超时' },
REQUEST_FAIL: { code: 2001, simStr: '请求错误' },
// 账号相关状态 10000 - 19999
SMS_IN_60S: { code: 10001, simStr: '60秒内只能发送一次' },

View File

@@ -259,7 +259,7 @@ export default class User extends BaseModel {
}
public static async findUserByUid(uid: number) {
const user: UserType = await UserModel.findOne({ uid }).select('uid tel channelInfo').lean({ getters: true });
const user: UserType = await UserModel.findOne({ uid }).select('uid tel channelInfo ip').lean({ getters: true });
return user;
}

View File

@@ -1,4 +1,8 @@
import { prop } from "@typegoose/typegoose";
import { SDK_37_CONST } from "../consts";
import { RoleType } from "../db/Role";
import { UserType } from "../db/User";
import { nowSeconds } from "../pubUtils/timeUtil";
// 37的登录验证返回参数
export class LoginValidateData37 {
@@ -31,6 +35,13 @@ export class LoginValidateData37 {
@prop({ required: true })
unique_realname_id: string; // 用户身份证唯一标识
@prop({ required: true })
childGameId: number; // 子游戏id
@prop({ required: true })
platformAppid: string; // 渠道标识
}
export interface LoginValidataReturn37 {
@@ -56,4 +67,66 @@ export interface PayCallback37Data {
time: number; // 请求时间
ext: string; // 扩展参数
sign: string;
}
export class Chat37Params {
game_key: string;
sid: number; // 游戏区
username: number; // channelInfo.uid
actor: string; // roleName
actor_id: string; // roleId
content: string; // 聊天内容
channel: number; // 世界=1私聊=2队伍=3工会=4
ip: string; // 玩家ip
time: number; // unix时间戳
sign: string;
op: string; // channelInfo.platformAppid
actor_level: number; // 等级
actor_recharge_gold: number; // 元宝
guildid: string = ''; // 公会id
to_username: number|string = ''; // 对方channelInfo.uid
to_actor: string = ''; // 对方roleName
to_actor_id: string = ''; // 对方roleId
to_actor_level: number; // 对方等级
to_actor_recharge_gold: number; // 对方元宝
constructor(content: string, channel: number) {
this.game_key = SDK_37_CONST.GAME_KEY;
this.content = content;
this.channel = channel;
this.time = nowSeconds();
}
setByMyRole(role: RoleType, user: UserType) {
let channelInfo = <LoginValidateData37>user.channelInfo;
this.username = channelInfo.uid;
this.actor = role.roleName;
this.actor_id = role.roleId;
this.ip = user.ip;
this.op = channelInfo.platformAppid;
this.actor_level = role.lv;
this.actor_recharge_gold = role.gold;
this.sid = role.serverId;
}
setByGuild(guildCode: string) {
this.guildid = guildCode;
}
setByTargetRole(role: RoleType, user: UserType) {
let channelInfo = <LoginValidateData37>user.channelInfo;
if(channelInfo) {
this.to_username = channelInfo.uid;
this.to_actor = role.roleName;
this.to_actor_id = role.roleId;
this.to_actor_level = role.lv;
this.to_actor_recharge_gold = role.gold;
}
}
setSign(sign: string) {
this.sign = sign;
}
}

View File

@@ -236,4 +236,5 @@ export const GK_MAINELITE = {
};
export const SERVER_DEBUG_MODE = {
CURRENT_TIME: 1, // 服务器是否打开推送时间debug模式
CHECK_WORD: 0, // 服务器是否打开聊天屏蔽
};

View File

@@ -1,17 +1,27 @@
import * as request from "request-promise";
import { RequestError } from "request-promise/errors";
import { BANTU_VID_ADDR, BANTU_VID_APP_KEY, HTTP_METHOD } from '../consts';
import { checkVidObjSign, get37Md5Sign, getVidObjSign } from "./sdkUtil";
import { checkVidObjSign, get37CheckChatMd5Sign, get37Md5Sign, getVidObjSign } from "./sdkUtil";
import { STATUS } from '../consts'
import { resResult } from "./util";
// 通用请求http
export async function httpRequest(url: string, method: string, headers: any, body: any) {
export async function httpRequest(url: string, method: string, body: any, headers?: any, timeout = 150) {
console.log(`httpRequest*********: ${url}, ${method}, ${JSON.stringify(headers)}, ${JSON.stringify(body)}`)
if(!headers) {
headers = {
'content-type': 'application/json;charset=utf-8',
}
}
let options = {
url,
method,
headers,
body,
json: true
json: true,
timeout
}
try {
@@ -20,8 +30,13 @@ export async function httpRequest(url: string, method: string, headers: any, bod
console.log(JSON.stringify(res));
return res;
} catch (e) {
console.error(e);
return false
let code = (<RequestError>e).cause.code;
if(code == 'ESOCKETTIMEDOUT') {
return resResult(STATUS.REQUEST_TIME_OUT);
} else {
console.error(e);
return resResult(STATUS.REQUEST_TIME_OUT);
}
}
}
@@ -105,4 +120,14 @@ export async function request37(url: string, body: any, key: string) {
let result = await httpRequest(url, HTTP_METHOD.POST, {}, body);
console.log('******result', result)
return result;
}
export async function request37CheckChat(url: string, body: any, key: string) {
body['sign'] = get37CheckChatMd5Sign(body, key);
let result = await httpRequest(url, HTTP_METHOD.GET, {}, body, 3 * 60 * 1000);
if(result != 1 && result.code != STATUS.REQUEST_TIME_OUT.code) {
return false
}
return true;
}

View File

@@ -1,7 +1,7 @@
import { REDIS_KEY, SDK_37_ADDR, SDK_37_CONST } from '../consts';
import { request37 } from './httpUtil';
import { nowSeconds } from './timeUtil';
import { LoginValidataReturn37 } from '../domain/sdk';
import { LoginValidataReturn37, Chat37Params } from '../domain/sdk';
import * as crypto from 'crypto'
// 通用加密方法
@@ -43,14 +43,21 @@ export function get37Md5Sign(body: any, key: string) {
return getMd5ObjSign(body, '&', (str) => `${str}&${key}`);
}
export async function loginValidate37(clientId: string, pst: string) {
export function get37CheckChatMd5Sign(body: Chat37Params, key: string) {
let { game_key = '', sid = '', username = '', channel = '', to_username = '', ip = '', time } = body;
let str = `${game_key}${sid}${username}${to_username}${channel}${ip}${time}${key}`;
console.log('** origin str', body, str);
return md5(str);
}
export async function loginValidate37(clientId: string, pst: string, platformAppid: string, childGameId: number) {
let result: LoginValidataReturn37 = await request37(SDK_37_ADDR.LOGIN, {
clientid: clientId,
pid: SDK_37_CONST.PID,
pid: platformAppid,
game_id: SDK_37_CONST.GAME_ID,
pst,
time: nowSeconds(),
c_game_id: SDK_37_CONST.FX_C_GAME_ID
c_game_id: childGameId
}, SDK_37_CONST.LOGIN_KEY);
if (result && result.code !== 1) {
console.error(result.msg);
@@ -59,9 +66,10 @@ export async function loginValidate37(clientId: string, pst: string) {
return result;
}
export async function loginValidata(channelType: string, clientId: string, pst: string) {
export async function loginValidata(channelType: string, params: { clientId: string, pst: string, platformAppid: string, childGameId: number }) {
if(channelType == '37') {
return await loginValidate37(clientId, pst);
let { clientId, pst, platformAppid, childGameId } = params;
return await loginValidate37(clientId, pst, platformAppid, childGameId);
} else {
return false;
}
@@ -72,7 +80,6 @@ export function getChannelId(channelType: string, uid: number|string) {
}
/********* 厚土防沉迷 *********/
export function getVidObjSign(body: any) {

View File

@@ -265,7 +265,7 @@ export function getRandValueByMinMax(min: number, max: number, decimal = 2): num
}
export function resResult(status: { code: number, simStr: string }, data = null, customMsg = '') {
export function resResult<T>(status: { code: number, simStr: string }, data: T = null, customMsg = ''): { code: number, msg: string, data: T } {
const { code, simStr } = status;
if (code !== STATUS.SUCCESS.code) {
console.log(`normal err, code: ${code}, des: ${customMsg || simStr}`);

View File

@@ -64,7 +64,6 @@ export default class AccountController extends Controller {
public async channelLogin() {
const { ctx } = this;
const { channelType, pst, clientId, deviceId, platform, pkgName, serverType, getuiCID } = ctx.request.body;
ctx.body = await ctx.service.auth.channelLogin(channelType, clientId, pst, deviceId, platform, pkgName, serverType, getuiCID);
ctx.body = await ctx.service.auth.channelLogin(ctx.request.body);
}
}

View File

@@ -1,4 +1,5 @@
import { STATUS } from '@consts';
import { httpRequest } from 'app/pubUtils/httpUtil';
import { Controller } from 'egg';
import { PayCallback37Data } from '../domain/sdk';
@@ -14,15 +15,26 @@ export default class SdkController extends Controller {
public async treatRoleName() {
const { ctx } = this;
let test = await httpRequest('http://127.0.0.1:7001/cb/treatguildname', "POST", {}, null);
console.log('result: ', typeof test, test);
ctx.body = ctx.service.utils.resResult(STATUS.SUCCESS, '');
return;
}
public async treatGuildName() {
const { ctx } = this;
let promiseFun = () => {
return new Promise(resolve => {
ctx.body = ctx.service.utils.resResult(STATUS.SUCCESS, '');
setTimeout(() => {
resolve(ctx.service.utils.resResult(STATUS.SUCCESS, ''));
}, 1000)
})
}
ctx.body = await promiseFun();
return;
}
public async getGuildByUser() {

View File

@@ -386,21 +386,17 @@ export default class Auth extends Service {
/**
* 渠道服登录
* @param channelType 渠道类型 37: '37'
* @param clientId
* @param pst
* @param deviceId
* @param platform
* @param pkgName
* @param serverType
* @param getuiCID
* @returns
*/
public async channelLogin(channelType: string, clientId: string, pst: string, deviceId: string, platform: string, pkgName: string, serverType: string, getuiCID: string) {
public async channelLogin(params: {
channelType: string, pst: string, clientId: string, deviceId: string, platform: string, platformAppid: string, childGameId: number, pkgName: string, serverType: string, getuiCID: string
}) {
const { channelType, pst, clientId, deviceId, platform, platformAppid, childGameId, pkgName, serverType, getuiCID } = params;
const ctx = this.ctx;
const ip = ctx.request.ip;
let requestResult = await loginValidata(channelType, clientId, pst)
let requestResult = await loginValidata(channelType, { clientId, pst, platformAppid, childGameId });
if(!requestResult) return this.ctx.service.utils.resResult(STATUS.CHANNEL_ERR);
if(requestResult.code != 1) {
return this.ctx.service.utils.resResult(STATUS.VALIDATE_ERR, requestResult);
@@ -408,7 +404,9 @@ export default class Auth extends Service {
let channelId = getChannelId(channelType, requestResult.data.uid);
const token = ctx.service.utils.generateStr(256);
let user = await UserModel.createOrUpdateChannelUser(channelId, channelType, requestResult.data, token, platform, pkgName, serverType, deviceId, ip);
let user = await UserModel.createOrUpdateChannelUser(channelId, channelType, {
...requestResult.data, childGameId, platformAppid
}, token, platform, pkgName, serverType, deviceId, ip);
if (getuiCID) {//更新个推cid
await UserModel.updateGetuiCIDByChannel(channelId, getuiCID);
}