283 lines
7.1 KiB
Go
283 lines
7.1 KiB
Go
package mail
|
|
|
|
import (
|
|
"context"
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
cfg "go_dreamfactory/sys/configure/structs"
|
|
"go_dreamfactory/sys/db"
|
|
"go_dreamfactory/utils"
|
|
"time"
|
|
|
|
"go_dreamfactory/lego/base"
|
|
"go_dreamfactory/lego/core"
|
|
"go_dreamfactory/lego/sys/log"
|
|
|
|
"go.mongodb.org/mongo-driver/bson/primitive"
|
|
)
|
|
|
|
/*
|
|
模块名:Mail
|
|
描述:邮件系统模块
|
|
开发:梅雄风
|
|
*/
|
|
func NewModule() core.IModule {
|
|
m := new(Mail)
|
|
return m
|
|
}
|
|
|
|
type Mail struct {
|
|
modules.ModuleBase
|
|
api *apiComp
|
|
modelMail *modelMail
|
|
configure *Configure_Comp
|
|
service base.IRPCXService
|
|
}
|
|
|
|
func (this *Mail) GetType() core.M_Modules {
|
|
return comm.ModuleMail
|
|
}
|
|
|
|
func (this *Mail) OnInstallComp() {
|
|
this.ModuleBase.OnInstallComp()
|
|
this.api = this.RegisterComp(new(apiComp)).(*apiComp)
|
|
this.modelMail = this.RegisterComp(new(modelMail)).(*modelMail)
|
|
this.configure = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp)
|
|
}
|
|
|
|
func (this *Mail) Init(service core.IService, module core.IModule, options core.IModuleOptions) (err error) {
|
|
err = this.ModuleBase.Init(service, module, options)
|
|
this.service = service.(base.IRPCXService)
|
|
return
|
|
}
|
|
func (this *Mail) Start() (err error) {
|
|
err = this.ModuleBase.Start()
|
|
this.service.RegisterFunctionName(string(comm.Rpc_Mail), this.Rpc_Mail)
|
|
return
|
|
}
|
|
|
|
// 获得新邮件 推送给玩家
|
|
func (this *Mail) AddNewMailPush(session comm.IUserSession, mail *pb.DBMailData) {
|
|
session.SendMsg(string(this.GetType()), "getnewmail", &pb.MailGetNewMailPush{Mail: mail})
|
|
return
|
|
}
|
|
|
|
// 给多个用户发邮件
|
|
func (this *Mail) SendNewMail(mail *pb.DBMailData, uid ...string) bool {
|
|
if db.IsCross() {
|
|
for _, id := range uid {
|
|
tag, _, b := utils.UIdSplit(id)
|
|
if b {
|
|
if conn, err := db.ServerDBConn(tag); err == nil {
|
|
dbModel := db.NewDBModel(comm.TableMail, time.Hour, conn)
|
|
mail.ObjId = primitive.NewObjectID().Hex()
|
|
mail.Check = false
|
|
mail.Reward = true
|
|
|
|
if len(mail.GetItems()) > 0 {
|
|
mail.Reward = false
|
|
}
|
|
_, err = dbModel.DB.InsertOne(comm.TableMail, mail)
|
|
this.SendMsgToUser(string(this.GetType()), "getnewmail", &pb.MailGetNewMailPush{Mail: mail}, id)
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
for _, id := range uid {
|
|
mail.Uid = id
|
|
mail.ObjId = primitive.NewObjectID().Hex()
|
|
mail.Check = false
|
|
mail.Reward = true
|
|
|
|
if len(mail.GetItems()) > 0 {
|
|
mail.Reward = false
|
|
}
|
|
this.modelMail.DB.InsertOne(comm.TableMail, mail)
|
|
this.SendMsgToUser(string(this.GetType()), "getnewmail", &pb.MailGetNewMailPush{Mail: mail}, id)
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
|
|
//红点查询
|
|
func (this *Mail) Reddot(session comm.IUserSession, rid ...comm.ReddotType) (reddot map[comm.ReddotType]bool) {
|
|
reddot = make(map[comm.ReddotType]bool)
|
|
for _, v := range rid {
|
|
switch v {
|
|
case comm.Reddot26:
|
|
if isredot := this.modelMail.checkReddot26(session.GetUserId()); isredot {
|
|
reddot[comm.Reddot26] = true
|
|
} else {
|
|
reddot[comm.Reddot26] = false
|
|
}
|
|
break
|
|
case comm.Reddot30:
|
|
if isredot := this.modelMail.checkReddot30(session.GetUserId()); isredot {
|
|
reddot[comm.Reddot30] = true
|
|
} else {
|
|
reddot[comm.Reddot30] = false
|
|
}
|
|
break
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
func (this *Mail) Rpc_Mail(ctx context.Context, args *pb.DBMailData) (err error) {
|
|
this.Debug("Rpc_Mail", log.Field{Key: "args", Value: args.String()})
|
|
var (
|
|
conn *db.DBConn
|
|
session comm.IUserSession
|
|
online bool
|
|
)
|
|
tag, _, b := utils.UIdSplit(args.Uid)
|
|
if b {
|
|
if conn, err = db.ServerDBConn(tag); err == nil {
|
|
dbModel := db.NewDBModel(comm.TableMail, time.Hour, conn)
|
|
_, err = dbModel.DB.InsertOne(comm.TableMail, args)
|
|
if err != nil {
|
|
this.Error("Create Rpc_Mail failed", log.Field{Key: "uid", Value: args.Uid}, log.Field{Key: "err", Value: err.Error()})
|
|
return
|
|
}
|
|
}
|
|
}
|
|
defer func() {
|
|
this.PutUserSession(session)
|
|
}()
|
|
if session, online = this.GetUserSession(args.Uid); online {
|
|
session.SendMsg(string(this.GetType()), "getnewmail", &pb.MailGetNewMailPush{Mail: args})
|
|
}
|
|
return
|
|
}
|
|
func (this *Mail) SendMailByCid(session comm.IUserSession, cid string, res []*pb.UserAssets) bool {
|
|
|
|
// 获取额外配置
|
|
conf := this.configure.GetMailConf(cid)
|
|
if conf == nil {
|
|
this.Errorf("can't found mail by cid: %s", cid)
|
|
return false
|
|
}
|
|
if len(conf.Reword) > 0 {
|
|
for _, v := range conf.Reword {
|
|
res = append(res, &pb.UserAssets{
|
|
A: v.A,
|
|
T: v.T,
|
|
N: v.N,
|
|
})
|
|
}
|
|
}
|
|
// 构建一个每日奖励邮件对象
|
|
mail := &pb.DBMailData{
|
|
ObjId: primitive.NewObjectID().Hex(),
|
|
Uid: session.GetUserId(),
|
|
CreateTime: uint64(configure.Now().Unix()),
|
|
DueTime: uint64(configure.Now().Unix() + 30*24*3600),
|
|
Items: res,
|
|
Cid: cid,
|
|
Param: []string{},
|
|
}
|
|
mail.Check = false
|
|
mail.Reward = true
|
|
|
|
if len(mail.GetItems()) > 0 {
|
|
mail.Reward = false
|
|
}
|
|
|
|
if db.IsCross() { // 如果是跨服 则取本服的db
|
|
tag, _, b := utils.UIdSplit(session.GetUserId())
|
|
if b {
|
|
if conn, err := db.ServerDBConn(tag); err == nil {
|
|
dbModel := db.NewDBModel(comm.TableMail, time.Hour, conn)
|
|
|
|
if _, err = dbModel.DB.InsertOne(comm.TableMail, mail); err != nil {
|
|
this.Errorf("InsertOne mail failed:%v", err)
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
err := this.modelMail.MailInsertUserMail(mail)
|
|
if err != nil {
|
|
this.Errorf("create mail failed :%v", err)
|
|
return false
|
|
}
|
|
}
|
|
// 通知玩家
|
|
this.AddNewMailPush(session, mail)
|
|
|
|
return true
|
|
}
|
|
|
|
// 创建邮件之前检测邮件是否达到上限
|
|
func (this *Mail) CheckMaxMail(session comm.IUserSession) bool {
|
|
count, err := this.modelMail.GetMailCountByUid(session.GetUserId())
|
|
if err == nil && count >= comm.MaxMailCount {
|
|
this.modelMail.DelOtherMail(session.GetUserId())
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (this *Mail) SendMailByUID(uid string, cid string, res []*cfg.Gameatn) bool {
|
|
// 获取额外配置
|
|
var reward []*pb.UserAssets
|
|
for _, v := range res {
|
|
if v.N > 0 {
|
|
reward = append(reward, &pb.UserAssets{
|
|
A: v.A,
|
|
T: v.T,
|
|
N: v.N,
|
|
})
|
|
}
|
|
}
|
|
conf := this.configure.GetMailConf(cid)
|
|
if conf == nil {
|
|
this.Errorf("can't found mail by cid: %s", cid)
|
|
return false
|
|
}
|
|
|
|
// 构建一个每日奖励邮件对象
|
|
mail := &pb.DBMailData{
|
|
ObjId: primitive.NewObjectID().Hex(),
|
|
Uid: uid,
|
|
CreateTime: uint64(configure.Now().Unix()),
|
|
DueTime: uint64(configure.Now().Unix() + 30*24*3600),
|
|
Items: reward,
|
|
Cid: cid,
|
|
Param: []string{},
|
|
}
|
|
mail.Check = false
|
|
mail.Reward = true
|
|
|
|
if len(mail.GetItems()) > 0 {
|
|
mail.Reward = false
|
|
}
|
|
|
|
if db.IsCross() { // 如果是跨服 则取本服的db
|
|
tag, _, b := utils.UIdSplit(uid)
|
|
if b {
|
|
if conn, err := db.ServerDBConn(tag); err == nil {
|
|
dbModel := db.NewDBModel(comm.TableMail, time.Hour, conn)
|
|
|
|
if _, err = dbModel.DB.InsertOne(comm.TableMail, mail); err != nil {
|
|
this.Errorf("InsertOne mail failed:%v", err)
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
err := this.modelMail.MailInsertUserMail(mail)
|
|
if err != nil {
|
|
this.Errorf("create mail failed :%v", err)
|
|
return false
|
|
}
|
|
}
|
|
|
|
if session, ok := this.GetUserSession(uid); ok { // 通知玩家
|
|
session.SendMsg(string(this.GetType()), "getnewmail", &pb.MailGetNewMailPush{Mail: mail})
|
|
if err := session.Push(); err != nil {
|
|
this.Errorf("err:%v", err)
|
|
}
|
|
}
|
|
return true
|
|
}
|