398 lines
11 KiB
Go
398 lines
11 KiB
Go
package system
|
|
|
|
import (
|
|
"encoding/json"
|
|
"f5"
|
|
"jccommon"
|
|
"main/common"
|
|
"main/constant"
|
|
"main/model/system"
|
|
"main/mt"
|
|
"net/http"
|
|
"q5"
|
|
"strings"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type MailApi struct {
|
|
}
|
|
|
|
func (this *MailApi) ListMail(c *gin.Context) {
|
|
var pageSize = q5.SafeToInt32(c.DefaultQuery("pagesize", ""))
|
|
var cursor = q5.SafeToInt32(c.DefaultQuery("cursor", ""))
|
|
reqObj := struct {
|
|
Subject string `json:"subject"`
|
|
Content string `json:"content"`
|
|
CreateTime struct {
|
|
Start int32 `json:"start"`
|
|
End int32 `json:"end"`
|
|
} `json:"createtime"`
|
|
SendTime struct {
|
|
Start int32 `json:"start"`
|
|
End int32 `json:"end"`
|
|
} `json:"sendtime"`
|
|
CreateAddress string `json:"create_address"`
|
|
UpdateAddress string `json:"update_address"`
|
|
}{}
|
|
|
|
if err := c.ShouldBindJSON(&reqObj); err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 1,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
orderBy := ""
|
|
sql := fmt.Sprintf("SELECT * FROM t_mail WHERE 1=1 AND tag1 = %d AND unikey is not null ",
|
|
jccommon.MAIL_TAG1_CUSTOM)
|
|
subFilters := []f5.DbQueryFilter{
|
|
f5.GetDbFilter().Custom("deleted = 0").And(),
|
|
}
|
|
|
|
if reqObj.Subject != "" {
|
|
q5.AppendSlice(&subFilters, f5.GetDbFilter().Like("subject", reqObj.Subject).And())
|
|
}
|
|
|
|
if reqObj.Content != "" {
|
|
q5.AppendSlice(&subFilters, f5.GetDbFilter().Like("content", reqObj.Content).And())
|
|
}
|
|
|
|
if reqObj.CreateTime.End > 0 {
|
|
q5.AppendSlice(&subFilters, f5.GetDbFilter().GE("createtime", q5.SafeToString(reqObj.CreateTime.Start)).And())
|
|
q5.AppendSlice(&subFilters, f5.GetDbFilter().LE("createtime", q5.SafeToString(reqObj.CreateTime.End)).And())
|
|
}
|
|
|
|
if reqObj.SendTime.End > 0 {
|
|
q5.AppendSlice(&subFilters, f5.GetDbFilter().GE("sendtime", q5.SafeToString(reqObj.SendTime.Start)).And())
|
|
q5.AppendSlice(&subFilters, f5.GetDbFilter().LE("sendtime", q5.SafeToString(reqObj.SendTime.End)).And())
|
|
}
|
|
|
|
if reqObj.CreateAddress != "" {
|
|
q5.AppendSlice(&subFilters, f5.GetDbFilter().EQ("create_address", reqObj.CreateAddress).And())
|
|
}
|
|
|
|
if reqObj.UpdateAddress != "" {
|
|
q5.AppendSlice(&subFilters, f5.GetDbFilter().EQ("update_address", reqObj.UpdateAddress).And())
|
|
}
|
|
|
|
mails := []*system.Mail{}
|
|
rspObj := struct {
|
|
Code int32 `json:"code"`
|
|
Message string `json:"message"`
|
|
Curpage int32 `json:"curpage"`
|
|
TotalPage int32 `json:"totalpage"`
|
|
Data []*system.Mail `json:"data"`
|
|
}{}
|
|
f5.GetGoStyleDb().PageQuery(
|
|
constant.MAIL_DB,
|
|
pageSize,
|
|
cursor,
|
|
sql,
|
|
[]string{},
|
|
f5.GetDbFilter().Comp(subFilters...),
|
|
orderBy,
|
|
func(err error, pagination *f5.Pagination) {
|
|
rspObj.Curpage = pagination.CurrentPage
|
|
rspObj.TotalPage = pagination.TotalPages
|
|
f5.UnmarshalModelList(pagination.Rows, &mails)
|
|
})
|
|
rspObj.Data = mails
|
|
c.JSON(http.StatusOK, rspObj)
|
|
}
|
|
|
|
func (this *MailApi) AddMail(c *gin.Context) {
|
|
reqJson := struct {
|
|
UniKey string `json:"unikey"`
|
|
MailType int32 `binding:"required" json:"mailtype"`
|
|
SendTime int32 `json:"sendtime"`
|
|
ExpireTime int32 `json:"expiretime"`
|
|
UserRegStartTime int32 `json:"user_reg_start_time"`
|
|
UserRegEndTime int32 `json:"user_reg_end_time"`
|
|
Subject string `json:"subject"`
|
|
Content string `json:"content"`
|
|
Attachments []common.Attachment `json:"attachments"`
|
|
Recipients []string `json:"recipients"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&reqJson); err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 1,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
if reqJson.MailType <= jccommon.MAIL_TYPE_BEGIN || reqJson.MailType >= jccommon.MAIL_TYPE_END {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "mail_type参数错误",
|
|
})
|
|
return
|
|
}
|
|
if reqJson.UniKey == "" {
|
|
f5.RspErr2(c, 101, "unikey param error")
|
|
return
|
|
}
|
|
|
|
if !this.CheckAttachment(reqJson.Attachments, c) {
|
|
return
|
|
}
|
|
|
|
if reqJson.MailType == jccommon.MAIL_TYPE_ALL {
|
|
if len(reqJson.Recipients) > 0 {
|
|
return
|
|
}
|
|
} else if reqJson.MailType == jccommon.MAIL_TYPE_GROUP && !this.CheckRecipients(&reqJson.Recipients, c) {
|
|
return
|
|
}
|
|
|
|
var count int64 = 0
|
|
if f5.GetApp().GetOrmDb(constant.MAIL_DB).Table("t_mail").Where("unikey = ?", reqJson.UniKey).Count(&count); count > 0 {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "unikey 重复",
|
|
})
|
|
return
|
|
}
|
|
|
|
s := c.MustGet("session").(common.Session)
|
|
acc := s.GetAccountAddress()
|
|
|
|
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
|
|
mail := new(system.Mail)
|
|
mail.MailId = f5.GetApp().NewLockNodeUuid()
|
|
mail.MailType = reqJson.MailType
|
|
mail.UniKey = reqJson.UniKey
|
|
mail.SendTime = reqJson.SendTime
|
|
mail.ExpireTime = reqJson.ExpireTime
|
|
mail.UserRegStartTime = reqJson.UserRegStartTime
|
|
mail.UserRegEndTime = reqJson.UserRegEndTime
|
|
mail.Subject = reqJson.Subject
|
|
mail.Content = reqJson.Content
|
|
mail.Attachments = reqJson.Attachments
|
|
mail.Recipients = reqJson.Recipients
|
|
mail.Tag1 = jccommon.MAIL_TAG1_CUSTOM
|
|
mail.Tag2 = jccommon.MAIL_TAG2_CUSTOM_NORMAL
|
|
mail.CreateTime = nowDaySeconds
|
|
mail.ModifyTime = nowDaySeconds
|
|
mail.CreateAddress = acc
|
|
mail.UpdateAddress = acc
|
|
if f5.GetApp().GetOrmDb(constant.MAIL_DB).Create(mail).Error != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 500,
|
|
"message": "",
|
|
})
|
|
return
|
|
}
|
|
{
|
|
e := new(jccommon.MailEvent)
|
|
e.EventName = jccommon.EVENT_MAIL_UPDATE
|
|
e.Param1 = q5.ToString(mail.MailId)
|
|
e.CreateTime = nowDaySeconds
|
|
e.ModifyTime = nowDaySeconds
|
|
f5.GetApp().GetOrmDb(constant.MAIL_DB).Create(e)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 0,
|
|
"message": "",
|
|
})
|
|
}
|
|
|
|
func (this *MailApi) EditMail(c *gin.Context) {
|
|
reqJson := struct {
|
|
MailId int64 `binding:"required" json:"mail_id,string"`
|
|
MailType int32 `binding:"required" json:"mail_type"`
|
|
SendTime int32 `json:"sendtime"`
|
|
ExpireTime int32 `json:"expiretime"`
|
|
UserRegStartTime int32 `json:"user_reg_start_time"`
|
|
UserRegEndTime int32 `json:"user_reg_end_time"`
|
|
Subject string `json:"subject"`
|
|
Content string `json:"content"`
|
|
Attachments []common.Attachment `json:"attachments"`
|
|
Recipients []string `json:"recipients"`
|
|
}{}
|
|
if err := c.ShouldBindJSON(&reqJson); err != nil {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 1,
|
|
"message": err.Error(),
|
|
})
|
|
return
|
|
}
|
|
|
|
if !this.CheckAttachment(reqJson.Attachments, c) {
|
|
return
|
|
}
|
|
// if reqJson.MailType <= jccommon.MAIL_TYPE_BEGIN || reqJson.MailType >= jccommon.MAIL_TYPE_END {
|
|
// c.JSON(http.StatusOK, gin.H{
|
|
// "code": 2,
|
|
// "message": "mail_type参数错误",
|
|
// })
|
|
// return
|
|
// }
|
|
|
|
var count int64 = 0
|
|
mail := new(system.Mail)
|
|
if f5.GetApp().GetOrmDb(constant.MAIL_DB).Table(mail.TableName()).Take(mail, "mail_id = ?", reqJson.MailId).Count(&count); count < 1 {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "mailid不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
if mail.MailType == jccommon.MAIL_TYPE_ALL {
|
|
if len(reqJson.Recipients) > 0 {
|
|
return
|
|
}
|
|
} else if mail.MailType == jccommon.MAIL_TYPE_GROUP && !this.CheckRecipients(&reqJson.Recipients, c) {
|
|
return
|
|
}
|
|
|
|
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
|
|
mail.MailId = reqJson.MailId
|
|
// mail.MailType = reqJson.MailType
|
|
mail.SendTime = reqJson.SendTime
|
|
mail.ExpireTime = reqJson.ExpireTime
|
|
mail.UserRegStartTime = reqJson.UserRegStartTime
|
|
mail.UserRegEndTime = reqJson.UserRegEndTime
|
|
mail.Subject = reqJson.Subject
|
|
mail.Content = reqJson.Content
|
|
mail.Attachments = reqJson.Attachments
|
|
mail.Recipients = reqJson.Recipients
|
|
mail.ModifyTime = nowDaySeconds
|
|
|
|
s := c.MustGet("session").(common.Session)
|
|
acc := s.GetAccountAddress()
|
|
mail.UpdateAddress = acc
|
|
f5.GetApp().GetOrmDb(constant.MAIL_DB).Save(mail)
|
|
{
|
|
e := new(jccommon.MailEvent)
|
|
e.EventName = jccommon.EVENT_MAIL_UPDATE
|
|
e.Param1 = q5.ToString(mail.MailId)
|
|
e.CreateTime = nowDaySeconds
|
|
e.ModifyTime = nowDaySeconds
|
|
f5.GetApp().GetOrmDb(constant.MAIL_DB).Create(e)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 0,
|
|
"message": "",
|
|
})
|
|
}
|
|
|
|
func (this *MailApi) DelMail(c *gin.Context) {
|
|
mailid := q5.ToInt64(c.Param("mailid"))
|
|
|
|
var count int64 = 0
|
|
db := f5.GetApp().GetOrmDb(constant.MAIL_DB).Table("t_mail").Where("mail_id = ?", mailid)
|
|
if db.Count(&count); count < 1 {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "mailid不存在",
|
|
})
|
|
return
|
|
}
|
|
|
|
nowDaySeconds := int32(f5.GetApp().GetRealSeconds())
|
|
delMail := struct {
|
|
Deleted int32 `gorm:"column:deleted" json:"-"`
|
|
ModifyTime int32 `gorm:"column:modifytime" json:"modifytime"`
|
|
}{
|
|
1,
|
|
nowDaySeconds,
|
|
}
|
|
|
|
db.Save(delMail)
|
|
{
|
|
e := new(jccommon.MailEvent)
|
|
e.EventName = jccommon.EVENT_MAIL_UPDATE
|
|
e.Param1 = q5.ToString(mailid)
|
|
e.CreateTime = nowDaySeconds
|
|
e.ModifyTime = nowDaySeconds
|
|
f5.GetApp().GetOrmDb(constant.MAIL_DB).Create(e)
|
|
}
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 0,
|
|
"message": "",
|
|
})
|
|
}
|
|
|
|
func (this *MailApi) CheckAttachment(list []common.Attachment, c *gin.Context) bool {
|
|
if data, err := json.Marshal(list); err != nil || len(data) > 0xFF {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "附件过多",
|
|
})
|
|
return false
|
|
}
|
|
|
|
passeditem := map[int32]int32{}
|
|
for _, item := range list {
|
|
if _, exist := passeditem[item.ItemId]; exist {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "重复的item id:" + q5.SafeToString(item.ItemId),
|
|
})
|
|
return false
|
|
}
|
|
|
|
if !mt.Table.Item.Search(item.ItemId) {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "item id error:" + q5.SafeToString(item.ItemId),
|
|
})
|
|
return false
|
|
}
|
|
|
|
if item.ItemNum > 1000*10000 {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "item 数量不超过10000000 :" + q5.SafeToString(item.ItemId),
|
|
})
|
|
return false
|
|
}
|
|
|
|
if item.ItemNum < 1 {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "item 数量错误 :" + q5.SafeToString(item.ItemId),
|
|
})
|
|
return false
|
|
}
|
|
|
|
passeditem[item.ItemId] = item.ItemNum
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
func (this *MailApi) CheckRecipients(list *[]string, c *gin.Context) bool {
|
|
if data, err := json.Marshal(list); err != nil || len(data) > 0xFFFF {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "收件人过多",
|
|
})
|
|
return false
|
|
}
|
|
|
|
newlist := []string{}
|
|
for idx := range *list {
|
|
(*list)[idx] = strings.ReplaceAll((*list)[idx], " ", "")
|
|
if (*list)[idx] != "" {
|
|
newlist = append(newlist, (*list)[idx])
|
|
}
|
|
}
|
|
|
|
if len(newlist) == 0 {
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"code": 2,
|
|
"message": "收件人错误",
|
|
})
|
|
return false
|
|
}
|
|
|
|
list = &newlist
|
|
|
|
return true
|
|
}
|