124 lines
3.0 KiB
Go
124 lines
3.0 KiB
Go
package mail
|
|
|
|
import (
|
|
"go_dreamfactory/comm"
|
|
"go_dreamfactory/modules"
|
|
"go_dreamfactory/pb"
|
|
"go_dreamfactory/sys/configure"
|
|
"go_dreamfactory/sys/db"
|
|
"go_dreamfactory/utils"
|
|
"time"
|
|
|
|
"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_comp *Configure_Comp
|
|
}
|
|
|
|
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_comp = this.RegisterComp(new(Configure_Comp)).(*Configure_Comp)
|
|
}
|
|
|
|
func (this *Mail) CreateNewMail(session comm.IUserSession, mail *pb.DBMailData) bool {
|
|
t := configure.Now()
|
|
defer func() {
|
|
log.Debugf("创建邮件 耗时:%v", time.Since(t))
|
|
}()
|
|
if mail == nil {
|
|
return 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)
|
|
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)
|
|
}
|
|
}
|
|
} 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) 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
|
|
}
|