275 lines
6.4 KiB
Go
275 lines
6.4 KiB
Go
package player
|
|
|
|
import (
|
|
"q5"
|
|
"f5"
|
|
"main/mt"
|
|
"sync"
|
|
"main/common"
|
|
"main/constant"
|
|
"main/model"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type inbox struct {
|
|
mailId int64
|
|
state int32
|
|
expireTime int32
|
|
}
|
|
|
|
type player struct {
|
|
lock sync.Mutex
|
|
accountId string
|
|
sessionId string
|
|
registerTime int32
|
|
loaded bool
|
|
inboxHash map[int64]*inbox
|
|
}
|
|
|
|
func (this *player) init() {
|
|
this.inboxHash = make(map[int64]*inbox)
|
|
}
|
|
|
|
func (this *player) Lock() bool {
|
|
this.lock.Lock()
|
|
if !this.loaded {
|
|
this.load()
|
|
}
|
|
ok := this.loaded
|
|
if !ok {
|
|
this.UnLock()
|
|
}
|
|
return ok
|
|
}
|
|
|
|
func (this *player) UnLock() {
|
|
this.lock.Unlock()
|
|
}
|
|
|
|
func (this *player) GetAccountId() string {
|
|
return this.accountId
|
|
}
|
|
|
|
func (this *player) GetSessionId() string {
|
|
return this.sessionId
|
|
}
|
|
|
|
func (this *player) GetRegisterTime() int32 {
|
|
return this.registerTime
|
|
}
|
|
|
|
func (this *player) MarkMails(mails []common.Mail) error {
|
|
this.checkLock()
|
|
var resultErr error
|
|
var nowTime = f5.GetApp().GetRealSeconds()
|
|
for _, m := range(mails) {
|
|
if m.IsValid(this) {
|
|
mi := this.getInbox(m.GetMailId())
|
|
if mi == nil {
|
|
err := model.Inbox.Mark(this.GetAccountId(), m.GetMailId(), nowTime, m.GetExpireTime())
|
|
if err != nil {
|
|
resultErr = err
|
|
break
|
|
}
|
|
mi = new(inbox)
|
|
mi.mailId = m.GetMailId()
|
|
mi.state = constant.INBOX_STATE_READ
|
|
mi.expireTime = m.GetExpireTime()
|
|
this.inboxHash[mi.mailId] = mi
|
|
} else if mi.state != constant.INBOX_STATE_NONE {
|
|
err := model.Inbox.Mark(this.GetAccountId(), m.GetMailId(), nowTime, m.GetExpireTime())
|
|
if err != nil {
|
|
resultErr = err
|
|
break
|
|
}
|
|
mi.state = constant.INBOX_STATE_READ
|
|
mi.expireTime = m.GetExpireTime()
|
|
}
|
|
}
|
|
}
|
|
return resultErr
|
|
}
|
|
|
|
func (this *player) GetAttachment(mails []common.Mail, c *gin.Context) {
|
|
this.checkLock()
|
|
accountId := c.DefaultQuery("account_id", "")
|
|
sessionId := c.DefaultQuery("session_id", "")
|
|
cbParams := struct {
|
|
AccountId string `json:"account_id"`
|
|
Mails []struct {
|
|
MailId string `json:"mailid"`
|
|
Attachments []*common.AttachmentDto `json:"attachments"`
|
|
} `json:"mails"`
|
|
}{}
|
|
rspObj := struct {
|
|
ErrCode interface{} `json:"errcode"`
|
|
ErrMsg string `json:"errmsg"`
|
|
}{}
|
|
cbParams.AccountId = this.GetAccountId()
|
|
{
|
|
for _, val := range mails {
|
|
p := q5.NewSliceElement(&cbParams.Mails)
|
|
p.MailId = q5.ToString(val.GetMailId())
|
|
val.TraverseAttachment(func (itemId int32, itemNum int32) {
|
|
a := new(common.AttachmentDto)
|
|
a.ItemId = itemId
|
|
a.ItemNum = itemNum
|
|
q5.AppendSlice(&p.Attachments, a)
|
|
})
|
|
}
|
|
}
|
|
var nowTime = f5.GetApp().GetRealSeconds()
|
|
cbParamsStr := q5.EncodeJson(&cbParams)
|
|
signStr := q5.Md5Str(cbParamsStr + mt.Table.Config.GetSecretKey() + q5.ToString(nowTime))
|
|
f5.GetHttpCliMgr().SendGoStyleJsonRspPost(
|
|
mt.Table.Config.GetGameApiUrl() + "/sapi/webapp/index.php?",
|
|
map[string]string{
|
|
"account_id": accountId,
|
|
"session_id": sessionId,
|
|
"c": "Mail",
|
|
"a": "getAttachmentCbS",
|
|
"sign": signStr,
|
|
"timestamp": q5.ToString(nowTime),
|
|
},
|
|
&rspObj,
|
|
"application/json",
|
|
cbParamsStr,
|
|
func(rsp f5.HttpCliResponse) {
|
|
if rsp.GetErr() != nil{
|
|
return
|
|
}
|
|
if errCode, err := q5.ToInt64Ex(rspObj.ErrCode); err == nil && errCode == 0 {
|
|
this.ReceivedMails(mails)
|
|
}
|
|
c.String(200, rsp.GetRawData())
|
|
})
|
|
}
|
|
|
|
func (this *player) ReceivedMails(mails []common.Mail) error {
|
|
this.checkLock()
|
|
var resultErr error
|
|
var nowTime int64 = int64(f5.GetApp().GetRealSeconds())
|
|
for _, m := range(mails) {
|
|
if m.IsValid(this) {
|
|
mi := this.getInbox(m.GetMailId())
|
|
if mi == nil {
|
|
err := model.Inbox.Delete(this.GetAccountId(), m.GetMailId(), nowTime, m.GetExpireTime())
|
|
if err != nil {
|
|
resultErr = err
|
|
break
|
|
}
|
|
mi = new(inbox)
|
|
mi.mailId = m.GetMailId()
|
|
mi.state = constant.INBOX_STATE_DELETED
|
|
mi.expireTime = m.GetExpireTime()
|
|
this.inboxHash[mi.mailId] = mi
|
|
} else if mi.state != constant.INBOX_STATE_DELETED {
|
|
err := model.Inbox.Delete(this.GetAccountId(), m.GetMailId(), nowTime, m.GetExpireTime())
|
|
if err != nil {
|
|
resultErr = err
|
|
break
|
|
}
|
|
mi.state = constant.INBOX_STATE_DELETED
|
|
mi.expireTime = m.GetExpireTime()
|
|
}
|
|
}
|
|
}
|
|
return resultErr
|
|
}
|
|
|
|
func (this *player) DeleteMails(mails []common.Mail) error {
|
|
this.checkLock()
|
|
var resultErr error
|
|
var nowTime int64 = int64(f5.GetApp().GetRealSeconds())
|
|
for _, m := range(mails) {
|
|
if m.IsValid(this) {
|
|
mi := this.getInbox(m.GetMailId())
|
|
if mi == nil {
|
|
if !m.HasAttachment() {
|
|
err := model.Inbox.Delete(this.GetAccountId(), m.GetMailId(), nowTime, m.GetExpireTime())
|
|
if err != nil {
|
|
resultErr = err
|
|
break
|
|
}
|
|
mi = new(inbox)
|
|
mi.mailId = m.GetMailId()
|
|
mi.state = constant.INBOX_STATE_DELETED
|
|
mi.expireTime = m.GetExpireTime()
|
|
this.inboxHash[mi.mailId] = mi
|
|
}
|
|
} else if mi.state != constant.INBOX_STATE_DELETED {
|
|
if !m.HasAttachment() || mi.state == constant.INBOX_STATE_RECEIVED {
|
|
err := model.Inbox.Delete(this.GetAccountId(), m.GetMailId(), nowTime, m.GetExpireTime())
|
|
if err != nil {
|
|
resultErr = err
|
|
break
|
|
}
|
|
mi.state = constant.INBOX_STATE_DELETED
|
|
mi.expireTime = m.GetExpireTime()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return resultErr
|
|
}
|
|
|
|
func (this *player) checkLock() {
|
|
if this.lock.TryLock() {
|
|
panic("player checkLock error")
|
|
}
|
|
}
|
|
|
|
func (this *player) load() {
|
|
f5.GetGoStyleDb().RawQuery(
|
|
constant.MAIL_DB,
|
|
"SELECT * FROM t_inbox WHERE account_id=? AND expiretime>?",
|
|
[]string{
|
|
this.GetAccountId(),
|
|
q5.ToString(f5.GetApp().GetRealSeconds() - 3600 * 24 * 7),
|
|
},
|
|
func (err error, ds *f5.DataSet) {
|
|
if err != nil {
|
|
return
|
|
}
|
|
for ds.Next() {
|
|
p := new(inbox)
|
|
p.mailId = q5.ToInt64(ds.GetByName("mail_id"))
|
|
p.state = q5.ToInt32(ds.GetByName("state"))
|
|
p.expireTime = q5.ToInt32(ds.GetByName("expiretime"))
|
|
this.inboxHash[p.mailId] = p
|
|
f5.GetSysLog().Info("load mail mail_id:%s", p.mailId)
|
|
}
|
|
this.loaded = true
|
|
})
|
|
}
|
|
|
|
func (this *player) getInbox(mailId int64) *inbox {
|
|
if m, ok := this.inboxHash[mailId]; ok {
|
|
return m
|
|
} else {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func (this* player) IsReadable(m common.Mail) bool {
|
|
mi := this.getInbox(m.GetMailId())
|
|
if mi != nil {
|
|
return mi.state != constant.INBOX_STATE_DELETED
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (this* player) IsUnread(m common.Mail) bool {
|
|
mi := this.getInbox(m.GetMailId())
|
|
if mi != nil {
|
|
return mi.state == constant.INBOX_STATE_NONE
|
|
}
|
|
return true
|
|
}
|
|
|
|
func newPlayer() *player {
|
|
p := new(player)
|
|
p.init()
|
|
return p
|
|
}
|