149 lines
7.4 KiB
TypeScript
149 lines
7.4 KiB
TypeScript
import { RewardInter, pushMail, mailData } from "../pubUtils/interface";
|
|
import { MailModel, MailType } from "../db/Mail";
|
|
import { getRedis } from "./redisService";
|
|
import { pinus } from "pinus";
|
|
import { gameData } from "../pubUtils/data";
|
|
import { nowSeconds } from '../pubUtils/timeUtil';
|
|
import { STATUS } from '../consts/statusCode';
|
|
import { resResult } from '../pubUtils/util';
|
|
import { GroupMailModel, GroupMailType } from "../db/GroupMail";
|
|
import { findWhere } from "underscore";
|
|
import { MAIL_TYPE, MAIL_TEM_TYPE, GM_MAIL_TYPE, MAIL_STATUS } from "../consts/constModules/mailConst";
|
|
import { DATA_NAME } from '../consts/dataName';
|
|
import { lockData } from './redLockService';
|
|
import GMMail from "../db/GMMail";
|
|
export async function sendMail(operate: number, toRoleId: string, sendName: string = '系统', params: string[] = [], goods: RewardInter[] = [], sendTime: number = nowSeconds()) {
|
|
let { content, time } = getContent(operate, params);
|
|
if (!content)
|
|
return;
|
|
let mail = await MailModel.addMail({roleId: toRoleId, sendTime, goods, sendName, mailId: JSON.stringify(operate), endTime: time + nowSeconds(), mailTemType: MAIL_TEM_TYPE.GAMEMAIL, params});
|
|
let key = 'login_roleId_' + toRoleId;
|
|
let sid = await getRedis(key);
|
|
if (!!sid) {
|
|
pinus.app.channelService.pushMessageByUids('onMailsAdd', resResult(STATUS.SUCCESS, { mails:[{
|
|
id: mail._id, goods, sendTime: mail.sendTime, endTime: mail.endTime, content, status: mail.status, mailType: MAIL_TYPE.SINGLEMAIL, sendName
|
|
}]}), [{uid: toRoleId, sid}]);
|
|
}
|
|
}
|
|
|
|
export function getContent(operate: number, params: string[]) {
|
|
let mail = gameData.mail.get(operate);
|
|
if (!mail)
|
|
return {};
|
|
let { content, time } = mail;
|
|
content = content||'%d';
|
|
for(let p of params) {
|
|
content = content.replace(/%d/, p);
|
|
}
|
|
return { content, time }
|
|
}
|
|
|
|
export async function getMailContent(roleId: string, operate: number, params: string[], goods: RewardInter[], mails: MailType[], pushMessage: pushMail[], sendTime?:number, sendName: string = '系统') {
|
|
const doc = new MailModel();
|
|
let { content, time } = getContent(operate, params);
|
|
if (!content)
|
|
return;
|
|
const mail = Object.assign(doc.toJSON(), { roleId, goods, sendName, mailId: operate, sendTime: sendTime||nowSeconds(), mailTemType: MAIL_TEM_TYPE.GAMEMAIL, endTime: time + nowSeconds(), params });
|
|
mails.push(mail);
|
|
let key = 'login_roleId_' + roleId;
|
|
let sid = await getRedis(key);
|
|
if (!!sid) {
|
|
pushMessage.push({route: 'onMailsAdd', data:[{
|
|
id: mail._id, goods, sendTime: mail.sendTime, endTime: mail.endTime, content, status: mail.status, mailType: MAIL_TYPE.SINGLEMAIL, sendName
|
|
}], uids: [{ uid: roleId, sid }]});
|
|
}
|
|
}
|
|
|
|
export async function refreshMails(roleId: string, sid: string, serverId: number, updatedMailAt: number) {
|
|
let res:any = await lockData(serverId, DATA_NAME.GAMEMAIL, roleId);//加锁
|
|
let gmMails = await pinus.app.rpc.gm.gmRemote.getMails.toServer('gm-server-1', updatedMailAt, serverId);
|
|
let addGroupMails: GroupMailType[] = [];
|
|
let addMails: MailType[] = [];
|
|
let updateMails: mailData[] = [];
|
|
let pushMails: mailData[] = [];
|
|
for (let {gmMailType, id: mailId, sendRoles, endTime, content, goods, sendTime, sendName } of gmMails) {
|
|
if (gmMailType == GM_MAIL_TYPE.GROUPMAIL) { //群体邮件
|
|
let mail = await GroupMailModel.getMail(mailId, MAIL_TEM_TYPE.GMTYPE);
|
|
if (!mail) {
|
|
const doc = new GroupMailModel();
|
|
const mail = Object.assign(doc.toJSON(), { mailId, mailTemType: MAIL_TEM_TYPE.GMTYPE, sendRoles, sendName });
|
|
addGroupMails.push(mail);
|
|
pushMails.push({ id: mail._id, goods, sendTime, endTime, content, status: mail.status, mailType: MAIL_TYPE.GROUPMAIL, sendName });
|
|
} else {
|
|
let { sendRoles } = mail;
|
|
let sendRole = findWhere(sendRoles, {roleId});
|
|
if (!sendRole) {
|
|
await GroupMailModel.pushRoleMail(roleId, mailId, MAIL_TEM_TYPE.GMTYPE);
|
|
pushMails.push({ id: mail._id, goods, sendTime, endTime, content, status: MAIL_STATUS.CREATE, mailType: MAIL_TYPE.GROUPMAIL, sendName });
|
|
}
|
|
}
|
|
} else if (gmMailType == GM_MAIL_TYPE.SERVER) { //游戏分服邮件
|
|
let mail = await MailModel.getMail(roleId, mailId, MAIL_TEM_TYPE.GMTYPE);
|
|
if (!mail) {
|
|
const doc = new MailModel();
|
|
const mail = Object.assign(doc.toJSON(), { mailId, mailTemType: MAIL_TEM_TYPE.GMTYPE, roleId, sendName });
|
|
addMails.push(mail);
|
|
pushMails.push({ id: mail._id, goods, sendTime, endTime, content, status: mail.status, mailType: MAIL_TYPE.SINGLEMAIL, sendName });
|
|
} else {
|
|
updateMails.push({ id: mail._id, goods, sendTime, endTime, content, status: mail.status, mailType: MAIL_TYPE.SINGLEMAIL, sendName })
|
|
}
|
|
}
|
|
}
|
|
if (!!updateMails.length) {
|
|
pinus.app.channelService.pushMessageByUids('onMailsUpdate', resResult(STATUS.SUCCESS, { mails: updateMails }), [{uid: roleId, sid}]);
|
|
}
|
|
if (!!addGroupMails.length) {
|
|
await GroupMailModel.addGroupMails(addGroupMails);
|
|
}
|
|
if (!!addMails.length) {
|
|
await MailModel.addMails(addMails);
|
|
}
|
|
if (!!pushMails.length) {
|
|
pinus.app.channelService.pushMessageByUids('onMailsAdd', resResult(STATUS.SUCCESS, { mails: pushMails}), [{uid: roleId, sid}]);
|
|
}
|
|
res.releaseCallback();
|
|
return nowSeconds();
|
|
}
|
|
|
|
export async function getMails(roleId: string, serverId: number) {
|
|
let mails = await MailModel.getMailsByRoleId(roleId)||[];
|
|
let groupMails = await GroupMailModel.getGroupMailsByRoleId(roleId)||[];
|
|
let list: mailData[] = await pinus.app.rpc.gm.gmRemote.getMailInfos.toServer('gm-server-1', roleId, serverId, mails, groupMails);
|
|
return list;
|
|
}
|
|
|
|
export async function sendRolesMails(mails) {
|
|
let addGroupMails: GroupMailType[] = [];
|
|
let pushMessage: pushMail[] = [];
|
|
for (let { gmMailType, id: mailId, sendRoles, endTime, content, goods, sendTime, sendName } of mails) {
|
|
let uids = [];
|
|
if (endTime < nowSeconds()|| sendTime > nowSeconds())
|
|
continue;
|
|
if (gmMailType != GM_MAIL_TYPE.GROUPMAIL)
|
|
return;
|
|
let mail = await GroupMailModel.getMail(mailId, MAIL_TEM_TYPE.GMTYPE);
|
|
if (!mail) {
|
|
const doc = new GroupMailModel();
|
|
mail = Object.assign(doc.toJSON(), { mailId, mailTemType: MAIL_TEM_TYPE.GMTYPE, sendRoles, sendName });
|
|
addGroupMails.push(mail);
|
|
} else {
|
|
return;
|
|
}
|
|
for (let { roleId } of sendRoles) {
|
|
let key = 'login_roleId_' + roleId;
|
|
let sid = await getRedis(key);
|
|
if (!!sid) {
|
|
uids.push({sid, uid: roleId})
|
|
}
|
|
}
|
|
pushMessage.push({route: 'onMailsAdd', data:[{
|
|
id: mail._id, goods, sendTime: mail.sendTime, endTime: mail.endTime, content, status: MAIL_STATUS.CREATE, mailType: MAIL_TYPE.SINGLEMAIL, sendName
|
|
}], uids});
|
|
}
|
|
if (!!addGroupMails.length) {
|
|
await GroupMailModel.addGroupMails(addGroupMails);
|
|
}
|
|
pushMessage.forEach( message=> {
|
|
pinus.app.channelService.pushMessageByUids('onMailsAdd', resResult(STATUS.SUCCESS, { mails: message.data }), message.uids);
|
|
});
|
|
} |