完成客服工作台闭环
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const (
|
||||
maxTextMessageLength = 2000
|
||||
maxImageMessageSize = 5 * 1024 * 1024
|
||||
)
|
||||
|
||||
func validateMessageContent(messageType, content string) (string, error) {
|
||||
switch messageType {
|
||||
case "text":
|
||||
content = strings.TrimSpace(content)
|
||||
if content == "" {
|
||||
return "", errors.New("消息内容不能为空")
|
||||
}
|
||||
if utf8.RuneCountInString(content) > maxTextMessageLength {
|
||||
return "", errors.New("单条文本消息不能超过 2000 字")
|
||||
}
|
||||
return content, nil
|
||||
case "image":
|
||||
parts := strings.SplitN(content, ",", 2)
|
||||
if len(parts) != 2 {
|
||||
return "", errors.New("图片格式无效")
|
||||
}
|
||||
if parts[0] != "data:image/jpeg;base64" && parts[0] != "data:image/png;base64" && parts[0] != "data:image/gif;base64" {
|
||||
return "", errors.New("仅支持 jpg、png、gif 图片")
|
||||
}
|
||||
bytes, err := base64.StdEncoding.DecodeString(parts[1])
|
||||
if err != nil || len(bytes) == 0 {
|
||||
return "", errors.New("图片内容无效")
|
||||
}
|
||||
if len(bytes) > maxImageMessageSize {
|
||||
return "", errors.New("图片不能超过 5 MB")
|
||||
}
|
||||
return content, nil
|
||||
default:
|
||||
return "", errors.New("不支持的消息类型")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user