增加渠道信息
This commit is contained in:
+54
-4
@@ -7,7 +7,10 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
var numberPattern = regexp.MustCompile(`[-+]?\d+(?:\.\d+)?`)
|
||||
var (
|
||||
numberPattern = regexp.MustCompile(`[-+]?\d+(?:\.\d+)?`)
|
||||
onlineTimePattern = regexp.MustCompile(`^\D*(\d{1,2})(?:\s*[::点.]\s*(\d{1,2}))?\D+(\d{1,2})(?:\s*[::点.]\s*(\d{1,2}))?\D*$`)
|
||||
)
|
||||
|
||||
func ParseUploadText(text string) (ParsedUpload, error) {
|
||||
values := make(map[string]string)
|
||||
@@ -55,7 +58,7 @@ func ParseUploadText(text string) (ParsedUpload, error) {
|
||||
account.BanRecord = normalizeBanFlag(banRaw)
|
||||
account.CommonRegion = firstValue(values, "常用地区", "常用区域")
|
||||
account.ContactPhone = firstValue(values, "联系电话", "手机号", "电话")
|
||||
account.OwnerOnlineTime = firstValue(values, "号主在线时间", "在线时间")
|
||||
account.OwnerOnlineTime = normalizeOwnerOnlineTime(firstValue(values, "号主在线时间", "在线时间"))
|
||||
account.Remark = composeRemark(firstValue(values, "备注", "说明"), banRaw)
|
||||
|
||||
account.Currency.HafuCoin, err = parseRequiredFloat(values, "哈夫币纯币", "哈夫币")
|
||||
@@ -107,10 +110,14 @@ func ParseUploadText(text string) (ParsedUpload, error) {
|
||||
}
|
||||
|
||||
func splitKeyValue(line string) (string, string, bool) {
|
||||
if idx := strings.Index(line, ":"); idx >= 0 {
|
||||
fullWidthIdx := strings.Index(line, ":")
|
||||
asciiIdx := strings.Index(line, ":")
|
||||
if fullWidthIdx >= 0 && (asciiIdx < 0 || fullWidthIdx < asciiIdx) {
|
||||
idx := fullWidthIdx
|
||||
return line[:idx], line[idx+len(":"):], true
|
||||
}
|
||||
if idx := strings.Index(line, ":"); idx >= 0 {
|
||||
if asciiIdx >= 0 {
|
||||
idx := asciiIdx
|
||||
return line[:idx], line[idx+1:], true
|
||||
}
|
||||
return "", "", false
|
||||
@@ -246,6 +253,49 @@ func parseFloatText(value string, preferLast bool) (float64, error) {
|
||||
return strconv.ParseFloat(pick, 64)
|
||||
}
|
||||
|
||||
func normalizeOwnerOnlineTime(value string) string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return ""
|
||||
}
|
||||
start, end, ok := parseOwnerOnlineTimeRange(value)
|
||||
if !ok {
|
||||
return value
|
||||
}
|
||||
return start + " 至 " + end
|
||||
}
|
||||
|
||||
func parseOwnerOnlineTimeRange(value string) (string, string, bool) {
|
||||
matches := onlineTimePattern.FindStringSubmatch(strings.TrimSpace(value))
|
||||
if len(matches) != 5 {
|
||||
return "", "", false
|
||||
}
|
||||
start, ok := normalizeTimePart(matches[1], matches[2])
|
||||
if !ok {
|
||||
return "", "", false
|
||||
}
|
||||
end, ok := normalizeTimePart(matches[3], matches[4])
|
||||
if !ok {
|
||||
return "", "", false
|
||||
}
|
||||
return start, end, true
|
||||
}
|
||||
|
||||
func normalizeTimePart(hourText, minuteText string) (string, bool) {
|
||||
hour, err := strconv.Atoi(strings.TrimSpace(hourText))
|
||||
if err != nil || hour < 0 || hour > 23 {
|
||||
return "", false
|
||||
}
|
||||
minute := 0
|
||||
if strings.TrimSpace(minuteText) != "" {
|
||||
minute, err = strconv.Atoi(strings.TrimSpace(minuteText))
|
||||
if err != nil || minute < 0 || minute > 59 {
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%02d:%02d", hour, minute), true
|
||||
}
|
||||
|
||||
func parseSkins(value string) []string {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" || value == "无" || strings.EqualFold(value, "none") {
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package hfb
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const sampleText = `上号方式:QQ扫码
|
||||
哈夫币纯币:156
|
||||
@@ -60,6 +63,31 @@ func TestParseUploadTextASCIIDelimiterKeepsRatioValue(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseUploadTextNormalizesOwnerOnlineTimeFormats(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
line string
|
||||
want string
|
||||
}{
|
||||
{name: "至", line: "号主在线时间:08:00 至 23:00", want: "08:00 至 23:00"},
|
||||
{name: "短横线和全角冒号", line: "号主在线时间:8:00-1:00", want: "08:00 至 01:00"},
|
||||
{name: "英文字段冒号", line: "号主在线时间:8:00-1:00", want: "08:00 至 01:00"},
|
||||
{name: "点和到", line: "在线时间:8点到1点", want: "08:00 至 01:00"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
text := strings.Replace(sampleText, "号主在线时间:11:00 至 00:00", tt.line, 1)
|
||||
parsed, err := ParseUploadText(text)
|
||||
if err != nil {
|
||||
t.Fatalf("ParseUploadText() error = %v", err)
|
||||
}
|
||||
if parsed.Account.OwnerOnlineTime != tt.want {
|
||||
t.Fatalf("OwnerOnlineTime = %q, want %q", parsed.Account.OwnerOnlineTime, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseUploadTextMissingRequiredField(t *testing.T) {
|
||||
_, err := ParseUploadText("上号方式:QQ扫码")
|
||||
if err == nil {
|
||||
|
||||
@@ -11,9 +11,10 @@ type ParsedUpload struct {
|
||||
}
|
||||
|
||||
type ExternalUploadRequest struct {
|
||||
UploadTime int64 `json:"uploadTime"`
|
||||
UploaderName string `json:"uploaderName"`
|
||||
Data ExternalAccountData `json:"data"`
|
||||
UploadTime int64 `json:"uploadTime"`
|
||||
UploaderName string `json:"uploaderName"`
|
||||
SourceChannel string `json:"sourceChannel"`
|
||||
Data ExternalAccountData `json:"data"`
|
||||
}
|
||||
|
||||
type ExternalAccountData struct {
|
||||
@@ -52,11 +53,12 @@ type ExternalUploadInventory struct {
|
||||
Skins []string `json:"skins"`
|
||||
}
|
||||
|
||||
func NewExternalUploadRequest(uploaderName string, account ExternalAccountData, now time.Time) ExternalUploadRequest {
|
||||
func NewExternalUploadRequest(uploaderName string, sourceChannel string, account ExternalAccountData, now time.Time) ExternalUploadRequest {
|
||||
return ExternalUploadRequest{
|
||||
UploadTime: now.Unix(),
|
||||
UploaderName: uploaderName,
|
||||
Data: account,
|
||||
UploadTime: now.Unix(),
|
||||
UploaderName: uploaderName,
|
||||
SourceChannel: sourceChannel,
|
||||
Data: account,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -36,7 +36,7 @@ func TestUploadSendsSignedRequest(t *testing.T) {
|
||||
defer server.Close()
|
||||
|
||||
client := Client{Server: server.URL, Secret: secret, Timeout: time.Second}
|
||||
req := NewExternalUploadRequest("张三", mustSampleAccount(t), time.Unix(100, 0))
|
||||
req := NewExternalUploadRequest("张三", "淘宝", mustSampleAccount(t), time.Unix(100, 0))
|
||||
resp, err := client.Upload(context.Background(), req)
|
||||
if err != nil {
|
||||
t.Fatalf("Upload() error = %v", err)
|
||||
|
||||
Reference in New Issue
Block a user