diff --git a/game-server/app/services/mailService.ts b/game-server/app/services/mailService.ts index d1dfe47e4..98bb190af 100644 --- a/game-server/app/services/mailService.ts +++ b/game-server/app/services/mailService.ts @@ -84,8 +84,13 @@ export class SendMailFun { let gmmail = await GMMailModel.getGmMailById(gmmailId); if(gmmail) { this.gmmail = gmmail; - this.sendTime = gmmail.sendTime; - this.endTime = gmmail.endTime; + if(gmmail.useTempTime) { + this.sendTime = gmmail.sendTime; + this.endTime = gmmail.endTime; + } else { + this.sendTime = nowSeconds(); + this.endTime = this.sendTime + (gmmail.continueHour||0) * 60 * 60; + } this.setParam({ ...gmmail }); } } @@ -166,19 +171,17 @@ export class SendMailFun { } } - if (uids.length > 0) {//下发邮件,对应前端红点提示 - if(mailType == GM_MAIL_TYPE.SINGLE) { - let mails = await this.createSingleMails(roleIds); - for(let { uid, sid } of uids) { - let mail = mails.get(uid); - if(mail && !this.notPush) pinus.app.channelService.pushMessageByUids('onMailsAdd', resResult(STATUS.SUCCESS, { mails: [mail] }), [{ uid, sid }]); - } - } else if (mailType == GM_MAIL_TYPE.GROUP) { - let mail = await this.createGroupMails(roleIds); - if(!this.notPush) pinus.app.channelService.pushMessageByUids('onMailsAdd', resResult(STATUS.SUCCESS, { mails: [mail] }), uids); - } else { // mailType错误 - return false; + if(mailType == GM_MAIL_TYPE.SINGLE) { + let mails = await this.createSingleMails(roleIds); + for(let { uid, sid } of uids) { + let mail = mails.get(uid); + if(uids.length > 0 && mail && !this.notPush) pinus.app.channelService.pushMessageByUids('onMailsAdd', resResult(STATUS.SUCCESS, { mails: [mail] }), [{ uid, sid }]); } + } else if (mailType == GM_MAIL_TYPE.GROUP) { + let mail = await this.createGroupMails(roleIds); + if(uids.length > 0 && !this.notPush) pinus.app.channelService.pushMessageByUids('onMailsAdd', resResult(STATUS.SUCCESS, { mails: [mail] }), uids); + } else { // mailType错误 + return false; } return true; } diff --git a/gm-server/app/controller/mail.ts b/gm-server/app/controller/mail.ts new file mode 100644 index 000000000..02f7f9fc0 --- /dev/null +++ b/gm-server/app/controller/mail.ts @@ -0,0 +1,24 @@ +import { Controller } from 'egg'; +import { STATUS } from '@consts'; + +export default class MailController extends Controller { + public async getGMMailList() { + const { ctx } = this; + const { page, pageSize, sortField, sortOrder, form } = ctx.request.body; + ctx.body = await ctx.service.mail.getGMMailList(page, pageSize, sortField, sortOrder, form); + return + } + + public async updateGMMail() { + const { ctx } = this; + const { _id, content, sendName, sendTime, endTime, useTempTime, continueHour } = ctx.request.body; + if(!_id) return ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS); + if(useTempTime) { + if(!sendTime || !endTime) return ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS); + } else { + if(!continueHour) return ctx.body = ctx.service.utils.resResult(STATUS.WRONG_PARMS); + } + ctx.body = await ctx.service.mail.updateGMMail(_id, content, sendName, useTempTime, sendTime, endTime, continueHour); + return + } +} diff --git a/gm-server/app/router.ts b/gm-server/app/router.ts index 046992401..d570a2de6 100644 --- a/gm-server/app/router.ts +++ b/gm-server/app/router.ts @@ -72,4 +72,7 @@ export default (app: Application) => { router.post('/api/activity/deleteactivitygrouptype', controller.activity.deleteActivityGroupType); router.post('/api/activity/saveactivitygrouptype', controller.activity.saveGroupTypeToActivityGroup); router.post('/api/activity/getgroupdatabyid', controller.activity.getGroupDataById); + + router.post('/api/mail/getgmmaillist', controller.mail.getGMMailList); + router.post('/api/mail/updategmmail', controller.mail.updateGMMail); }; diff --git a/gm-server/app/service/Mail.ts b/gm-server/app/service/Mail.ts new file mode 100644 index 000000000..4cfc77cd5 --- /dev/null +++ b/gm-server/app/service/Mail.ts @@ -0,0 +1,26 @@ + +import { Service } from 'egg'; +import { GMMailModel } from '@db/GMMail'; +import { STATUS } from '@consts'; + +export default class Mail extends Service { + + public async getGMMailList(page: number, pageSize: number, sortField: string, sortOrder: string, form: {}) { + const { ctx } = this; + const list = await GMMailModel.findByCondition(page, pageSize, sortField, sortOrder, form); + const total = await GMMailModel.countByCondition( form ) + return ctx.service.utils.resResult(STATUS.SUCCESS, { + list, total + }); + } + + public async updateGMMail(_id: string, content: string, sendName: string, useTempTime: boolean, sendTime: number, endTime: number, continueHour: number) { + const { ctx } = this; + if(_id == 'new') { + await GMMailModel.addMail({ content, sendName, useTempTime, sendTime, endTime, continueHour }); + } else { + await GMMailModel.updateMailById(_id, { content, sendName, useTempTime, sendTime, endTime, continueHour }); + } + return ctx.service.utils.resResult(STATUS.SUCCESS); + } +} \ No newline at end of file diff --git a/gm-server/typings/app/controller/index.d.ts b/gm-server/typings/app/controller/index.d.ts index 96a354477..d7f45fcc4 100644 --- a/gm-server/typings/app/controller/index.d.ts +++ b/gm-server/typings/app/controller/index.d.ts @@ -7,6 +7,7 @@ import ExportGame from '../../../app/controller/game'; import ExportGmaccount from '../../../app/controller/gmaccount'; import ExportHome from '../../../app/controller/home'; import ExportLogin from '../../../app/controller/login'; +import ExportMail from '../../../app/controller/mail'; import ExportUpload from '../../../app/controller/upload'; import ExportUsers from '../../../app/controller/users'; @@ -17,6 +18,7 @@ declare module 'egg' { gmaccount: ExportGmaccount; home: ExportHome; login: ExportLogin; + mail: ExportMail; upload: ExportUpload; users: ExportUsers; } diff --git a/gm-server/typings/app/service/index.d.ts b/gm-server/typings/app/service/index.d.ts index 60b7611c4..89ca7225f 100644 --- a/gm-server/typings/app/service/index.d.ts +++ b/gm-server/typings/app/service/index.d.ts @@ -9,6 +9,7 @@ type AutoInstanceType; game: AutoInstanceType; gmUser: AutoInstanceType; + mail: AutoInstanceType; test: AutoInstanceType; utils: AutoInstanceType; users: AutoInstanceType; diff --git a/shared/db/GMMail.ts b/shared/db/GMMail.ts index eedbcc3e2..d85215028 100644 --- a/shared/db/GMMail.ts +++ b/shared/db/GMMail.ts @@ -4,7 +4,6 @@ import BaseModel from './BaseModel'; import { getModelForClass, prop, DocumentType } from '@typegoose/typegoose'; import { nowSeconds } from '../pubUtils/timeUtil'; -import { GM_MAIL_TYPE } from '../consts/constModules/mailConst'; class Reward { @prop({ required: true }) @@ -18,11 +17,17 @@ export default class GMMail extends BaseModel { @prop({ required: true, type: Reward, default: [], _id: false }) goods: Reward[]; - @prop({ required: true, default: nowSeconds() }) - sendTime: number; + @prop({ required: true, default: true }) + useTempTime: boolean; // 生成邮件是否按照模板时间 + + @prop({ required: false}) + continueHour: number; // 如果不按模板时间,sendTime为生成单独邮件的时候,endTime为sendTime+continueHour,单位小时 @prop({ required: true }) - endTime: number; + sendTime: number; // 如果按照模板时间,发送时间,10位时间戳 + + @prop({ required: true }) + endTime: number; // 如果按照模板时间,过期时间,10位时间戳 @prop({ required: true }) content: string; @@ -30,13 +35,15 @@ export default class GMMail extends BaseModel { @prop({ required: true }) sendName: string; - @prop({ required: true, enum: GM_MAIL_TYPE }) - mailType: GM_MAIL_TYPE; // 1-单人邮件 2-多人邮件 3-全服邮件 - - public static async addMail(params: GMMailTypeParam) { + public static async addMail(params: GMMailTypeParam, uid = 1) { const doc = new GMMailModel(); let mail = Object.assign(doc.toJSON(), params); - mail = await GMMailModel.findOneAndUpdate({ _id: mail._id }, { $set: mail }, {upsert: true, new: true}).lean(); + let result: GMMailType = await GMMailModel.findOneAndUpdate({ _id: mail._id }, { $set: {...mail, updatedBy: uid}, $setOnInsert: { createdBy: uid } }, {upsert: true, new: true}).lean(); + return result; + } + + public static async updateMailById(_id: string, params: GMMailTypeParam, uid: number = 1) { + let mail: GMMailType = await GMMailModel.findByIdAndUpdate(_id, { $set: {...params, updatedBy: uid}}, {new: true}).lean(); return mail; } @@ -55,6 +62,38 @@ export default class GMMail extends BaseModel { return result; } + + private static getSearchObj(form: {_id?: string, content?: string}) { + let searchObj = {}; + if(form['_id']) searchObj['_id'] = form._id; + if(form['content']) searchObj['context'] = { $regex: new RegExp(form.content.toString(), 'i') } + return searchObj + } + + public static async findByCondition(page: number, pageSize: number, sortField: string, sortOrder: string, form: {_id?: string, content?: string} = {}) { + + let searchObj = this.getSearchObj(form); + let sort = {}; + if(sortField && sortOrder) { + if(sortOrder == 'ascend') { + sort[sortField] = 1; + } else if (sortOrder == 'descend') { + sort[sortField] = -1; + } + } + const result: GMMailType[] = await GMMailModel.find(searchObj).limit(pageSize).skip((page - 1) * pageSize).sort(sort).lean({ getters: true, virtuals: true }); + return result; + + } + + public static async countByCondition(form: {_id?: string, content?: string} = {}) { + + let searchObj = this.getSearchObj(form); + const result = await GMMailModel.count(searchObj); + return result; + } + + } export const GMMailModel = getModelForClass(GMMail);