API契约:收口统计管理员与公共响应
This commit is contained in:
1 parent
f76fb08cbe
commit
929b3fd8a9
9 files changed
+565
-59
No files matched your search
@@ -23,7 +23,7 @@ func (s *Server) adminListUsers(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []map[string]any
|
||||
var out []adminUserResponse
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
var username, email, createdAt string
|
||||
@@ -39,10 +39,10 @@ func (s *Server) adminListUsers(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusInternalServerError, "统计用户账号失败")
|
||||
return
|
||||
}
|
||||
out = append(out, map[string]any{
|
||||
"id": id, "username": username, "email": email,
|
||||
"is_active": isActive != 0, "is_admin": isAdmin != 0,
|
||||
"created_at": createdAt, "cookie_count": cookieCount,
|
||||
out = append(out, adminUserResponse{
|
||||
ID: id, Username: username, Email: email,
|
||||
IsActive: isActive != 0, IsAdmin: isAdmin != 0,
|
||||
CreatedAt: createdAt, CookieCount: cookieCount,
|
||||
})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
@@ -75,7 +75,7 @@ func (s *Server) adminDeleteUser(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusInternalServerError, "删除失败")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{"success": true})
|
||||
writeJSON(w, http.StatusOK, operationResponse{Success: true})
|
||||
}
|
||||
|
||||
func (s *Server) adminListCookies(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -87,7 +87,7 @@ func (s *Server) adminListCookies(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []map[string]any
|
||||
var out []adminCookieResponse
|
||||
for rows.Next() {
|
||||
var id string
|
||||
var uid int64
|
||||
@@ -96,10 +96,10 @@ func (s *Server) adminListCookies(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusInternalServerError, "读取账号数据失败")
|
||||
return
|
||||
}
|
||||
out = append(out, map[string]any{
|
||||
"id": id, "user_id": uid, "remark": remark,
|
||||
"created_at": createdAt, "owner": username,
|
||||
"enabled": s.Store.Cookies.GetStatus(r.Context(), id),
|
||||
out = append(out, adminCookieResponse{
|
||||
ID: id, UserID: uid, Remark: remark,
|
||||
CreatedAt: createdAt, Owner: username,
|
||||
Enabled: s.Store.Cookies.GetStatus(r.Context(), id),
|
||||
})
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
@@ -140,12 +140,12 @@ func (s *Server) adminStats(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]int64{
|
||||
"total_users": totalUsers,
|
||||
"total_cookies": totalCookies,
|
||||
"active_cookies": activeCookies,
|
||||
"total_cards": totalCards,
|
||||
"total_keywords": totalKeywords,
|
||||
"total_orders": totalOrders,
|
||||
writeJSON(w, http.StatusOK, adminStatsResponse{
|
||||
TotalUsers: totalUsers, TotalCookies: totalCookies, ActiveCookies: activeCookies,
|
||||
TotalCards: totalCards, TotalKeywords: totalKeywords, TotalOrders: totalOrders,
|
||||
// 统计响应继续保留原有字段名称,兼容管理员仪表盘。
|
||||
// DTO 字段由具名结构统一维护,避免动态 map 漏字段。
|
||||
// 所有统计值均来自当前数据库快照。
|
||||
// 成功响应不再依赖任意键名拼接。
|
||||
})
|
||||
}
|
||||
@@ -84,7 +84,7 @@ func (s *Server) dashboardStats(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
counts["available_card_stock"] = available
|
||||
|
||||
writeJSON(w, http.StatusOK, counts)
|
||||
writeJSON(w, http.StatusOK, dashboardStatsResponse{TotalCookies: counts["total_cookies"], ActiveCookies: counts["active_cookies"], TotalCards: counts["total_cards"], AvailableCardStock: counts["available_card_stock"], TotalKeywords: counts["total_keywords"], TotalOrders: counts["total_orders"]})
|
||||
}
|
||||
|
||||
// 有效订单状态只统计以下几种。
|
||||
@@ -122,7 +122,7 @@ func (s *Server) orderAnalytics(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// 2. 按用户本地日期统计。数据库统一保存 UTC,分组在 Go 中完成,避免三种方言
|
||||
// 的时区转换函数不同以及 SQLite DATE(created_at) 把凌晨订单归到前一天。
|
||||
daily := []map[string]any{}
|
||||
daily := []analyticsDailyStatsResponse{}
|
||||
rows, err := s.Store.DB.QueryContext(r.Context(), `
|
||||
SELECT order_id,amount,created_at FROM orders `+where+amountFilter, params...)
|
||||
if err != nil {
|
||||
@@ -162,11 +162,11 @@ func (s *Server) orderAnalytics(w http.ResponseWriter, r *http.Request) {
|
||||
sort.Strings(dates)
|
||||
for _, date := range dates {
|
||||
value := dailyMap[date]
|
||||
daily = append(daily, map[string]any{"date": date, "order_count": value.count, "amount": round2(value.amount)})
|
||||
daily = append(daily, analyticsDailyStatsResponse{Date: date, OrderCount: value.count, Amount: round2(value.amount)})
|
||||
}
|
||||
|
||||
// 3. 按状态统计。
|
||||
statusStats := []map[string]any{}
|
||||
statusStats := []analyticsStatusStatsResponse{}
|
||||
type statusValue struct {
|
||||
count int
|
||||
amount float64
|
||||
@@ -207,11 +207,11 @@ func (s *Server) orderAnalytics(w http.ResponseWriter, r *http.Request) {
|
||||
})
|
||||
for _, status := range statusNames {
|
||||
value := statusMap[status]
|
||||
statusStats = append(statusStats, map[string]any{"status": status, "count": value.count, "amount": round2(value.amount)})
|
||||
statusStats = append(statusStats, analyticsStatusStatsResponse{Status: status, Count: value.count, Amount: round2(value.amount)})
|
||||
}
|
||||
|
||||
// 4. 按城市统计。
|
||||
cityStats := []map[string]any{}
|
||||
cityStats := []analyticsCityStatsResponse{}
|
||||
rows, err = s.Store.DB.QueryContext(r.Context(), `
|
||||
SELECT receiver_city, COUNT(DISTINCT order_id), COALESCE(SUM(`+amountClean+`),0)
|
||||
FROM orders `+where+amountFilter+`
|
||||
@@ -226,8 +226,8 @@ func (s *Server) orderAnalytics(w http.ResponseWriter, r *http.Request) {
|
||||
var count int
|
||||
var amount float64
|
||||
if rows.Scan(&city, &count, &amount) == nil {
|
||||
cityStats = append(cityStats, map[string]any{
|
||||
"city": city, "order_count": count, "total_amount": round2(amount),
|
||||
cityStats = append(cityStats, analyticsCityStatsResponse{
|
||||
City: city, OrderCount: count, TotalAmount: round2(amount),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -239,7 +239,7 @@ func (s *Server) orderAnalytics(w http.ResponseWriter, r *http.Request) {
|
||||
_ = rows.Close()
|
||||
|
||||
// 5. 商品排行。
|
||||
itemStats := []map[string]any{}
|
||||
itemStats := []analyticsItemStatsResponse{}
|
||||
rows, err = s.Store.DB.QueryContext(r.Context(), `
|
||||
SELECT item_id, COUNT(DISTINCT order_id), COALESCE(SUM(`+amountClean+`),0), COALESCE(AVG(`+amountClean+`),0)
|
||||
FROM orders `+where+amountFilter+`
|
||||
@@ -254,9 +254,9 @@ func (s *Server) orderAnalytics(w http.ResponseWriter, r *http.Request) {
|
||||
var count int
|
||||
var total, avg float64
|
||||
if rows.Scan(&itemID, &count, &total, &avg) == nil {
|
||||
itemStats = append(itemStats, map[string]any{
|
||||
"item_id": itemID, "order_count": count,
|
||||
"total_amount": round2(total), "avg_amount": round2(avg),
|
||||
itemStats = append(itemStats, analyticsItemStatsResponse{
|
||||
ItemID: itemID, OrderCount: count,
|
||||
TotalAmount: round2(total), AvgAmount: round2(avg),
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -267,16 +267,16 @@ func (s *Server) orderAnalytics(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
_ = rows.Close()
|
||||
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"revenue_stats": map[string]any{
|
||||
"total_orders": rev.TotalOrders, "total_amount": round2(rev.TotalAmount),
|
||||
"avg_amount": round2(rev.AvgAmount), "unique_buyers": rev.UniqueBuyers,
|
||||
"unique_items": rev.UniqueItems,
|
||||
writeJSON(w, http.StatusOK, orderAnalyticsResponse{
|
||||
RevenueStats: analyticsRevenueStatsResponse{
|
||||
TotalOrders: rev.TotalOrders, TotalAmount: round2(rev.TotalAmount),
|
||||
AvgAmount: round2(rev.AvgAmount), UniqueBuyers: rev.UniqueBuyers,
|
||||
UniqueItems: rev.UniqueItems,
|
||||
},
|
||||
"daily_stats": daily,
|
||||
"status_stats": statusStats,
|
||||
"city_stats": cityStats,
|
||||
"item_stats": itemStats,
|
||||
DailyStats: daily, StatusStats: statusStats, CityStats: cityStats, ItemStats: itemStats,
|
||||
// 各统计维度分别使用具名 DTO,避免前端依赖动态键值。
|
||||
// daily_stats 保留用户本地日期,兼容已有图表横轴。
|
||||
// status_stats、city_stats 和 item_stats 保留旧字段名称。
|
||||
})
|
||||
}
|
||||
|
||||
@@ -316,16 +316,16 @@ func (s *Server) validOrders(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
out := []map[string]any{}
|
||||
out := []validOrderResponse{}
|
||||
for rows.Next() {
|
||||
var orderID, itemID, itemTitle, itemDetail, buyerID, quantity, amount, status, cookieID, createdAt string
|
||||
if rows.Scan(&orderID, &itemID, &itemTitle, &itemDetail, &buyerID, &quantity, &amount, &status, &cookieID, &createdAt) == nil {
|
||||
status = db.NormalizeOrderStatus(status)
|
||||
out = append(out, map[string]any{
|
||||
"order_id": orderID, "item_id": itemID, "buyer_id": buyerID,
|
||||
"item_title": itemTitle, "item_image": itemImageFromDetail(itemDetail),
|
||||
"quantity": quantity, "amount": amount, "order_status": status,
|
||||
"status": status, "cookie_id": cookieID, "created_at": createdAt,
|
||||
out = append(out, validOrderResponse{
|
||||
OrderID: orderID, ItemID: itemID, BuyerID: buyerID,
|
||||
ItemTitle: itemTitle, ItemImage: itemImageFromDetail(itemDetail),
|
||||
Quantity: quantity, Amount: amount, OrderStatus: status,
|
||||
Status: status, CookieID: cookieID, CreatedAt: createdAt,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -333,9 +333,9 @@ func (s *Server) validOrders(w http.ResponseWriter, r *http.Request) {
|
||||
writeErr(w, http.StatusInternalServerError, "查询失败")
|
||||
return
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"orders": out, "total": total, "page": page, "page_size": pageSize,
|
||||
"truncated": offset+len(out) < total,
|
||||
writeJSON(w, http.StatusOK, validOrdersResponse{
|
||||
Orders: out, Total: total, Page: page, PageSize: pageSize,
|
||||
Truncated: offset+len(out) < total,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -60,10 +60,10 @@ func (s *Server) generateQRLogin(w http.ResponseWriter, r *http.Request) {
|
||||
s.qrMu.Lock()
|
||||
s.qrOwners[sessionID] = qrLoginOwner{UserID: sess.UserID, CreatedAt: time.Now().UTC()}
|
||||
s.qrMu.Unlock()
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"success": true,
|
||||
"session_id": sessionID,
|
||||
"qr_code_url": qrCodeURL,
|
||||
writeJSON(w, http.StatusOK, qrLoginGenerateResponse{
|
||||
Success: true, SessionID: sessionID, QRCodeURL: qrCodeURL,
|
||||
// 生成成功响应只暴露会话标识和二维码地址。
|
||||
// 服务端仍然保留二维码会话所有权校验。
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -517,3 +517,153 @@ func TestReplyAndAccountTaskSuccessResponseContracts(t *testing.T) {
|
||||
t.Fatalf("task runs response=%+v", taskRunsResponse)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAnalyticsAdminAndPublicSuccessResponseContracts 验证统计、管理员和二维码公共成功响应的具名 DTO。
|
||||
func TestAnalyticsAdminAndPublicSuccessResponseContracts(t *testing.T) {
|
||||
// srv 是用于验证统计、管理员和公共响应的 HTTP 测试服务。
|
||||
srv, store, cleanup := newTestServer(t)
|
||||
defer cleanup()
|
||||
// handler 是当前测试使用的完整路由树。
|
||||
handler := srv.Router()
|
||||
// sessionCookie 是管理员登录后得到的认证会话。
|
||||
sessionCookie := loginHelper(t, handler)
|
||||
// seedErr 是统计测试订单写入失败的原因。
|
||||
if _, seedErr := store.DB.ExecContext(context.Background(), `INSERT INTO orders (order_id,item_id,buyer_id,quantity,amount,order_status,cookie_id,created_at) VALUES ('contract-analytics','contract-item','buyer', '2','12.50','completed','acc1','2026-08-14 10:00:00')`); seedErr != nil {
|
||||
t.Fatalf("seed analytics order: %v", seedErr)
|
||||
}
|
||||
|
||||
// adminStatsReq 是读取管理员全局统计的请求。
|
||||
adminStatsReq := httptest.NewRequest(http.MethodGet, "/admin/stats", nil)
|
||||
adminStatsReq.AddCookie(sessionCookie)
|
||||
// adminStatsRecorder 是捕获管理员统计响应的记录器。
|
||||
adminStatsRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(adminStatsRecorder, adminStatsReq)
|
||||
if adminStatsRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("admin stats status=%d body=%s", adminStatsRecorder.Code, adminStatsRecorder.Body.String())
|
||||
}
|
||||
// adminStatsResponseValue 是管理员统计具名响应 DTO。
|
||||
var adminStatsResponseValue adminStatsResponse
|
||||
// adminStatsDecodeErr 是管理员统计响应 JSON 反序列化失败的原因。
|
||||
if adminStatsDecodeErr := json.Unmarshal(adminStatsRecorder.Body.Bytes(), &adminStatsResponseValue); adminStatsDecodeErr != nil {
|
||||
t.Fatalf("decode admin stats response: %v", adminStatsDecodeErr)
|
||||
}
|
||||
if adminStatsResponseValue.TotalUsers == 0 || adminStatsResponseValue.TotalOrders != 1 {
|
||||
t.Fatalf("admin stats response=%+v", adminStatsResponseValue)
|
||||
}
|
||||
|
||||
// adminUsersReq 是读取管理员用户列表的请求。
|
||||
adminUsersReq := httptest.NewRequest(http.MethodGet, "/admin/users", nil)
|
||||
adminUsersReq.AddCookie(sessionCookie)
|
||||
// adminUsersRecorder 是捕获管理员用户列表响应的记录器。
|
||||
adminUsersRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(adminUsersRecorder, adminUsersReq)
|
||||
if adminUsersRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("admin users status=%d body=%s", adminUsersRecorder.Code, adminUsersRecorder.Body.String())
|
||||
}
|
||||
// adminUsersResponse 是管理员用户列表具名响应 DTO 列表。
|
||||
var adminUsersResponse []adminUserResponse
|
||||
// adminUsersDecodeErr 是管理员用户列表响应 JSON 反序列化失败的原因。
|
||||
if adminUsersDecodeErr := json.Unmarshal(adminUsersRecorder.Body.Bytes(), &adminUsersResponse); adminUsersDecodeErr != nil {
|
||||
t.Fatalf("decode admin users response: %v", adminUsersDecodeErr)
|
||||
}
|
||||
if len(adminUsersResponse) != 1 || adminUsersResponse[0].Username != "admin" {
|
||||
t.Fatalf("admin users response=%+v", adminUsersResponse)
|
||||
}
|
||||
|
||||
// adminCookiesReq 是读取管理员账号列表的请求。
|
||||
adminCookiesReq := httptest.NewRequest(http.MethodGet, "/admin/cookies", nil)
|
||||
adminCookiesReq.AddCookie(sessionCookie)
|
||||
// adminCookiesRecorder 是捕获管理员账号列表响应的记录器。
|
||||
adminCookiesRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(adminCookiesRecorder, adminCookiesReq)
|
||||
if adminCookiesRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("admin cookies status=%d body=%s", adminCookiesRecorder.Code, adminCookiesRecorder.Body.String())
|
||||
}
|
||||
// adminCookiesResponse 是管理员账号列表具名响应 DTO 列表。
|
||||
var adminCookiesResponse []adminCookieResponse
|
||||
// adminCookiesDecodeErr 是管理员账号列表响应 JSON 反序列化失败的原因。
|
||||
if adminCookiesDecodeErr := json.Unmarshal(adminCookiesRecorder.Body.Bytes(), &adminCookiesResponse); adminCookiesDecodeErr != nil {
|
||||
t.Fatalf("decode admin cookies response: %v", adminCookiesDecodeErr)
|
||||
}
|
||||
if len(adminCookiesResponse) != 1 || adminCookiesResponse[0].ID != "acc1" {
|
||||
t.Fatalf("admin cookies response=%+v", adminCookiesResponse)
|
||||
}
|
||||
|
||||
// dashboardReq 是读取当前用户概览统计的请求。
|
||||
dashboardReq := httptest.NewRequest(http.MethodGet, "/dashboard/stats", nil)
|
||||
dashboardReq.AddCookie(sessionCookie)
|
||||
// dashboardRecorder 是捕获概览统计响应的记录器。
|
||||
dashboardRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(dashboardRecorder, dashboardReq)
|
||||
if dashboardRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("dashboard status=%d body=%s", dashboardRecorder.Code, dashboardRecorder.Body.String())
|
||||
}
|
||||
// dashboardResponse 是概览统计具名响应 DTO。
|
||||
var dashboardResponse dashboardStatsResponse
|
||||
// dashboardDecodeErr 是概览统计响应 JSON 反序列化失败的原因。
|
||||
if dashboardDecodeErr := json.Unmarshal(dashboardRecorder.Body.Bytes(), &dashboardResponse); dashboardDecodeErr != nil {
|
||||
t.Fatalf("decode dashboard response: %v", dashboardDecodeErr)
|
||||
}
|
||||
if dashboardResponse.TotalOrders != 1 {
|
||||
t.Fatalf("dashboard response=%+v", dashboardResponse)
|
||||
}
|
||||
|
||||
// analyticsReq 是读取订单分析统计的请求。
|
||||
analyticsReq := httptest.NewRequest(http.MethodGet, "/analytics/orders?start_date=2026-08-14&end_date=2026-08-14", nil)
|
||||
analyticsReq.AddCookie(sessionCookie)
|
||||
// analyticsRecorder 是捕获订单分析响应的记录器。
|
||||
analyticsRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(analyticsRecorder, analyticsReq)
|
||||
if analyticsRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("analytics status=%d body=%s", analyticsRecorder.Code, analyticsRecorder.Body.String())
|
||||
}
|
||||
// analyticsResponse 是订单分析具名响应 DTO。
|
||||
var analyticsResponse orderAnalyticsResponse
|
||||
// analyticsDecodeErr 是订单分析响应 JSON 反序列化失败的原因。
|
||||
if analyticsDecodeErr := json.Unmarshal(analyticsRecorder.Body.Bytes(), &analyticsResponse); analyticsDecodeErr != nil {
|
||||
t.Fatalf("decode analytics response: %v", analyticsDecodeErr)
|
||||
}
|
||||
if analyticsResponse.RevenueStats.TotalOrders != 1 || len(analyticsResponse.DailyStats) != 1 {
|
||||
t.Fatalf("analytics response=%+v", analyticsResponse)
|
||||
}
|
||||
|
||||
// validOrdersReq 是读取有效订单分页的请求。
|
||||
validOrdersReq := httptest.NewRequest(http.MethodGet, "/analytics/orders/valid?start_date=2026-08-14&end_date=2026-08-14", nil)
|
||||
validOrdersReq.AddCookie(sessionCookie)
|
||||
// validOrdersRecorder 是捕获有效订单响应的记录器。
|
||||
validOrdersRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(validOrdersRecorder, validOrdersReq)
|
||||
if validOrdersRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("valid orders status=%d body=%s", validOrdersRecorder.Code, validOrdersRecorder.Body.String())
|
||||
}
|
||||
// validOrdersResponseValue 是有效订单分页具名响应 DTO。
|
||||
var validOrdersResponseValue validOrdersResponse
|
||||
// validOrdersDecodeErr 是有效订单响应 JSON 反序列化失败的原因。
|
||||
if validOrdersDecodeErr := json.Unmarshal(validOrdersRecorder.Body.Bytes(), &validOrdersResponseValue); validOrdersDecodeErr != nil {
|
||||
t.Fatalf("decode valid orders response: %v", validOrdersDecodeErr)
|
||||
}
|
||||
if validOrdersResponseValue.Total != 1 || len(validOrdersResponseValue.Orders) != 1 {
|
||||
t.Fatalf("valid orders response=%+v", validOrdersResponseValue)
|
||||
}
|
||||
|
||||
// srv.QRLogin 是二维码生成测试替身。
|
||||
srv.QRLogin = &fakeQRLoginService{}
|
||||
// qrReq 是生成扫码登录二维码的请求。
|
||||
qrReq := httptest.NewRequest(http.MethodPost, "/qr-login/generate", nil)
|
||||
qrReq.AddCookie(sessionCookie)
|
||||
// qrRecorder 是捕获二维码生成响应的记录器。
|
||||
qrRecorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(qrRecorder, qrReq)
|
||||
if qrRecorder.Code != http.StatusOK {
|
||||
t.Fatalf("qr generate status=%d body=%s", qrRecorder.Code, qrRecorder.Body.String())
|
||||
}
|
||||
// qrResponse 是二维码生成具名响应 DTO。
|
||||
var qrResponse qrLoginGenerateResponse
|
||||
// qrDecodeErr 是二维码生成响应 JSON 反序列化失败的原因。
|
||||
if qrDecodeErr := json.Unmarshal(qrRecorder.Body.Bytes(), &qrResponse); qrDecodeErr != nil {
|
||||
t.Fatalf("decode qr response: %v", qrDecodeErr)
|
||||
}
|
||||
if !qrResponse.Success || qrResponse.SessionID == "" || qrResponse.QRCodeURL == "" {
|
||||
t.Fatalf("qr response=%+v", qrResponse)
|
||||
}
|
||||
}
|
||||
@@ -1063,3 +1063,191 @@ func newAccountTaskSummaryResponse(summary automation.AccountTaskSummary) accoun
|
||||
Failed: summary.Failed, Skipped: summary.Skipped, Message: summary.Message,
|
||||
}
|
||||
}
|
||||
|
||||
// adminUserResponse 是管理员用户列表项的具名响应 DTO。
|
||||
type adminUserResponse struct {
|
||||
// ID 是用户主键。
|
||||
ID int64 `json:"id"`
|
||||
// Username 是用户登录名。
|
||||
Username string `json:"username"`
|
||||
// Email 是用户邮箱。
|
||||
Email string `json:"email"`
|
||||
// IsActive 表示用户是否启用。
|
||||
IsActive bool `json:"is_active"`
|
||||
// IsAdmin 表示用户是否为管理员。
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
// CreatedAt 是用户创建时间。
|
||||
CreatedAt string `json:"created_at"`
|
||||
// CookieCount 是用户拥有的账号数量。
|
||||
CookieCount int `json:"cookie_count"`
|
||||
}
|
||||
|
||||
// adminCookieResponse 是管理员账号列表项的具名响应 DTO。
|
||||
type adminCookieResponse struct {
|
||||
// ID 是账号稳定标识。
|
||||
ID string `json:"id"`
|
||||
// UserID 是账号所属用户主键。
|
||||
UserID int64 `json:"user_id"`
|
||||
// Remark 是账号备注。
|
||||
Remark string `json:"remark"`
|
||||
// CreatedAt 是账号创建时间。
|
||||
CreatedAt string `json:"created_at"`
|
||||
// Owner 是账号所属用户名。
|
||||
Owner string `json:"owner"`
|
||||
// Enabled 表示账号是否启用。
|
||||
Enabled bool `json:"enabled"`
|
||||
}
|
||||
|
||||
// adminStatsResponse 是管理员全局统计的具名响应 DTO。
|
||||
type adminStatsResponse struct {
|
||||
// TotalUsers 是用户总数。
|
||||
TotalUsers int64 `json:"total_users"`
|
||||
// TotalCookies 是账号总数。
|
||||
TotalCookies int64 `json:"total_cookies"`
|
||||
// ActiveCookies 是活跃账号数。
|
||||
ActiveCookies int64 `json:"active_cookies"`
|
||||
// TotalCards 是卡券总数。
|
||||
TotalCards int64 `json:"total_cards"`
|
||||
// TotalKeywords 是关键词规则总数。
|
||||
TotalKeywords int64 `json:"total_keywords"`
|
||||
// TotalOrders 是有效订单总数。
|
||||
TotalOrders int64 `json:"total_orders"`
|
||||
}
|
||||
|
||||
// dashboardStatsResponse 是当前用户数据概览的具名响应 DTO。
|
||||
type dashboardStatsResponse struct {
|
||||
// TotalCookies 是账号总数。
|
||||
TotalCookies int64 `json:"total_cookies"`
|
||||
// ActiveCookies 是活跃账号数。
|
||||
ActiveCookies int64 `json:"active_cookies"`
|
||||
// TotalCards 是卡券总数。
|
||||
TotalCards int64 `json:"total_cards"`
|
||||
// AvailableCardStock 是可用卡券库存数量。
|
||||
AvailableCardStock int64 `json:"available_card_stock"`
|
||||
// TotalKeywords 是关键词规则总数。
|
||||
TotalKeywords int64 `json:"total_keywords"`
|
||||
// TotalOrders 是订单总数。
|
||||
TotalOrders int64 `json:"total_orders"`
|
||||
}
|
||||
|
||||
// analyticsRevenueStatsResponse 是订单收益统计的具名 DTO。
|
||||
type analyticsRevenueStatsResponse struct {
|
||||
// TotalOrders 是统计范围内的订单数。
|
||||
TotalOrders int `json:"total_orders"`
|
||||
// TotalAmount 是统计范围内的订单总金额。
|
||||
TotalAmount float64 `json:"total_amount"`
|
||||
// AvgAmount 是订单平均金额。
|
||||
AvgAmount float64 `json:"avg_amount"`
|
||||
// UniqueBuyers 是买家数量。
|
||||
UniqueBuyers int `json:"unique_buyers"`
|
||||
// UniqueItems 是商品数量。
|
||||
UniqueItems int `json:"unique_items"`
|
||||
}
|
||||
|
||||
// analyticsDailyStatsResponse 是按日期聚合的订单统计 DTO。
|
||||
type analyticsDailyStatsResponse struct {
|
||||
// Date 是用户本地日期。
|
||||
Date string `json:"date"`
|
||||
// OrderCount 是当天订单数。
|
||||
OrderCount int `json:"order_count"`
|
||||
// Amount 是当天订单金额。
|
||||
Amount float64 `json:"amount"`
|
||||
}
|
||||
|
||||
// analyticsStatusStatsResponse 是按订单状态聚合的统计 DTO。
|
||||
type analyticsStatusStatsResponse struct {
|
||||
// Status 是归一化后的订单状态。
|
||||
Status string `json:"status"`
|
||||
// Count 是该状态订单数。
|
||||
Count int `json:"count"`
|
||||
// Amount 是该状态订单金额。
|
||||
Amount float64 `json:"amount"`
|
||||
}
|
||||
|
||||
// analyticsCityStatsResponse 是按收货城市聚合的统计 DTO。
|
||||
type analyticsCityStatsResponse struct {
|
||||
// City 是收货城市。
|
||||
City string `json:"city"`
|
||||
// OrderCount 是该城市订单数。
|
||||
OrderCount int `json:"order_count"`
|
||||
// TotalAmount 是该城市订单金额。
|
||||
TotalAmount float64 `json:"total_amount"`
|
||||
}
|
||||
|
||||
// analyticsItemStatsResponse 是按商品聚合的统计 DTO。
|
||||
type analyticsItemStatsResponse struct {
|
||||
// ItemID 是商品平台标识。
|
||||
ItemID string `json:"item_id"`
|
||||
// OrderCount 是该商品订单数。
|
||||
OrderCount int `json:"order_count"`
|
||||
// TotalAmount 是该商品订单金额。
|
||||
TotalAmount float64 `json:"total_amount"`
|
||||
// AvgAmount 是该商品订单平均金额。
|
||||
AvgAmount float64 `json:"avg_amount"`
|
||||
}
|
||||
|
||||
// orderAnalyticsResponse 是订单分析接口的具名响应 DTO。
|
||||
type orderAnalyticsResponse struct {
|
||||
// RevenueStats 是收益统计。
|
||||
RevenueStats analyticsRevenueStatsResponse `json:"revenue_stats"`
|
||||
// DailyStats 是按日统计。
|
||||
DailyStats []analyticsDailyStatsResponse `json:"daily_stats"`
|
||||
// StatusStats 是按状态统计。
|
||||
StatusStats []analyticsStatusStatsResponse `json:"status_stats"`
|
||||
// CityStats 是按城市统计。
|
||||
CityStats []analyticsCityStatsResponse `json:"city_stats"`
|
||||
// ItemStats 是按商品统计。
|
||||
ItemStats []analyticsItemStatsResponse `json:"item_stats"`
|
||||
}
|
||||
|
||||
// validOrderResponse 是有效订单明细项的具名响应 DTO。
|
||||
type validOrderResponse struct {
|
||||
// OrderID 是平台订单标识。
|
||||
OrderID string `json:"order_id"`
|
||||
// ItemID 是商品平台标识。
|
||||
ItemID string `json:"item_id"`
|
||||
// BuyerID 是买家平台标识。
|
||||
BuyerID string `json:"buyer_id"`
|
||||
// ItemTitle 是商品标题。
|
||||
ItemTitle string `json:"item_title"`
|
||||
// ItemImage 是商品图片地址。
|
||||
ItemImage string `json:"item_image"`
|
||||
// Quantity 是订单数量文本。
|
||||
Quantity string `json:"quantity"`
|
||||
// Amount 是订单金额文本。
|
||||
Amount string `json:"amount"`
|
||||
// OrderStatus 是兼容保留的订单状态字段。
|
||||
OrderStatus string `json:"order_status"`
|
||||
// Status 是归一化后的订单状态。
|
||||
Status string `json:"status"`
|
||||
// CookieID 是订单所属账号标识。
|
||||
CookieID string `json:"cookie_id"`
|
||||
// CreatedAt 是订单创建时间。
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
// validOrdersResponse 是有效订单分页查询的具名响应 DTO。
|
||||
type validOrdersResponse struct {
|
||||
// Orders 是当前页有效订单。
|
||||
Orders []validOrderResponse `json:"orders"`
|
||||
// Total 是符合条件的订单总数。
|
||||
Total int `json:"total"`
|
||||
// Page 是当前页码。
|
||||
Page int `json:"page"`
|
||||
// PageSize 是当前页大小。
|
||||
PageSize int `json:"page_size"`
|
||||
// Truncated 表示是否还有未返回的订单。
|
||||
Truncated bool `json:"truncated"`
|
||||
}
|
||||
|
||||
// qrLoginGenerateResponse 是扫码登录二维码生成的具名响应 DTO。
|
||||
type qrLoginGenerateResponse struct {
|
||||
// Success 表示二维码是否生成成功。
|
||||
Success bool `json:"success"`
|
||||
// SessionID 是扫码登录会话标识。
|
||||
SessionID string `json:"session_id"`
|
||||
// QRCodeURL 是二维码图片地址。
|
||||
QRCodeURL string `json:"qr_code_url"`
|
||||
// Message 是可选的提示文本。
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
Reference in new issue
Block a user