94 lines
2.8 KiB
Go
94 lines
2.8 KiB
Go
package contactusser
|
|
|
|
import (
|
|
"context"
|
|
"net/mail"
|
|
"strings"
|
|
"time"
|
|
"unicode/utf8"
|
|
"videoweb/app/api/proto"
|
|
"videoweb/pub/common/srvdef"
|
|
"videoweb/pub/models/contactusmod"
|
|
)
|
|
|
|
const (
|
|
contactUsMaxEmailLen = 100
|
|
contactUsMaxTitleLen = 100
|
|
contactUsMaxContentLen = 1000
|
|
contactUsMaxAttachmentNum = 9
|
|
contactUsSubmitMessage = "提交成功,我们会在24小时内回复。"
|
|
)
|
|
|
|
const (
|
|
ContactUsTypeOrder int32 = 1 // 订单问题
|
|
ContactUsTypeAccount int32 = 2 // 账号问题
|
|
ContactUsTypeFeature int32 = 3 // 功能问题
|
|
ContactUsTypeExp int32 = 4 // 体验问题
|
|
ContactUsTypeOther int32 = 5 // 其他
|
|
)
|
|
|
|
// AddContactUs 提交联系我们记录
|
|
func AddContactUs(ctx context.Context, userId int64, req *proto.ContactUsAddReq) (*proto.ContactUsAddResp, *srvdef.SrvErr) {
|
|
if req == nil || userId <= 0 {
|
|
return nil, srvdef.New(srvdef.ErrParamError, "", "")
|
|
}
|
|
email := strings.TrimSpace(req.Email)
|
|
if !isValidEmail(email) || utf8.RuneCountInString(email) > contactUsMaxEmailLen {
|
|
return nil, srvdef.New(srvdef.ErrParamError, "邮箱格式错误")
|
|
}
|
|
if !isValidContactUsType(req.Type) {
|
|
return nil, srvdef.New(srvdef.ErrParamError, "问题类型无效")
|
|
}
|
|
title := strings.TrimSpace(req.Title)
|
|
if title == "" || utf8.RuneCountInString(title) > contactUsMaxTitleLen {
|
|
return nil, srvdef.New(srvdef.ErrParamError, "标题不能为空且不能超过100字")
|
|
}
|
|
content := strings.TrimSpace(req.Content)
|
|
if content == "" || utf8.RuneCountInString(content) > contactUsMaxContentLen {
|
|
return nil, srvdef.New(srvdef.ErrParamError, "内容不能为空且不能超过1000字")
|
|
}
|
|
attachments := normalizeAttachments(req.Attachments)
|
|
if len(attachments) > contactUsMaxAttachmentNum {
|
|
return nil, srvdef.New(srvdef.ErrParamError, "附件不能超过9个")
|
|
}
|
|
now := time.Now()
|
|
doc := &contactusmod.ContactUs{
|
|
UserId: userId,
|
|
Email: email,
|
|
QuestionType: req.Type,
|
|
Title: title,
|
|
Content: content,
|
|
Attachments: attachments,
|
|
Status: 0,
|
|
CreatedAt: now,
|
|
UpdatedAt: now,
|
|
}
|
|
if err := contactusmod.InsertOneContactUs(ctx, doc); err != nil {
|
|
return nil, err
|
|
}
|
|
return &proto.ContactUsAddResp{Message: contactUsSubmitMessage}, nil
|
|
}
|
|
|
|
func isValidEmail(email string) bool {
|
|
if email == "" {
|
|
return false
|
|
}
|
|
_, err := mail.ParseAddress(email)
|
|
return err == nil
|
|
}
|
|
|
|
func isValidContactUsType(questionType int32) bool {
|
|
return questionType >= ContactUsTypeOrder && questionType <= ContactUsTypeOther
|
|
}
|
|
|
|
func normalizeAttachments(rawAttachments []string) []string {
|
|
attachments := make([]string, 0, len(rawAttachments))
|
|
for _, attachment := range rawAttachments {
|
|
attachment = strings.TrimSpace(attachment)
|
|
if attachment != "" {
|
|
attachments = append(attachments, attachment)
|
|
}
|
|
}
|
|
return attachments
|
|
}
|