80 lines
1.9 KiB
Go
80 lines
1.9 KiB
Go
package controller
|
|
|
|
import (
|
|
"time"
|
|
|
|
"fafa-crawler/src/colly_service"
|
|
"fafa-crawler/src/services"
|
|
)
|
|
|
|
var (
|
|
hdyColly *colly_service.HdyCollyService
|
|
productService *services.ProductService
|
|
sxGoodsService *services.SxGoodsService
|
|
)
|
|
|
|
func init() {
|
|
hdyColly = colly_service.NewHdyCollyService()
|
|
productService = services.NewProductService()
|
|
sxGoodsService = services.NewSxGoodsService()
|
|
}
|
|
|
|
// APIResponse 标准API响应结构体(符合国内大厂规范)
|
|
// 包含状态码、消息、数据和时间戳字段
|
|
type APIResponse struct {
|
|
Code int `json:"code"` // 状态码: 0成功, 非0错误
|
|
Msg string `json:"msg"` // 响应消息
|
|
Data interface{} `json:"data"` // 业务数据
|
|
Timestamp int64 `json:"timestamp"` // 服务器时间戳(秒级)
|
|
}
|
|
|
|
// NewSuccessResponse 创建成功响应
|
|
func NewSuccessResponse(data interface{}) *APIResponse {
|
|
return &APIResponse{
|
|
Code: 0,
|
|
Msg: "success",
|
|
Data: data,
|
|
Timestamp: time.Now().Unix(),
|
|
}
|
|
}
|
|
|
|
// NewErrorResponse 创建错误响应
|
|
func NewErrorResponse(code int, msg string) *APIResponse {
|
|
return &APIResponse{
|
|
Code: code,
|
|
Msg: msg,
|
|
Data: nil,
|
|
Timestamp: time.Now().Unix(),
|
|
}
|
|
}
|
|
|
|
// Success 简化成功响应(仅数据)
|
|
func Success(data interface{}) *APIResponse {
|
|
return NewSuccessResponse(data)
|
|
}
|
|
|
|
// SuccessMsg 简化成功响应(仅消息)
|
|
func SuccessMsg(msg string) *APIResponse {
|
|
return &APIResponse{
|
|
Code: 0,
|
|
Msg: msg,
|
|
Data: nil,
|
|
Timestamp: time.Now().Unix(),
|
|
}
|
|
}
|
|
|
|
// SuccessEmpty 无参数成功响应
|
|
func SuccessEmpty() *APIResponse {
|
|
return Success(nil)
|
|
}
|
|
|
|
// Error 简化错误响应(仅消息)
|
|
func Error(code int, msg string) *APIResponse {
|
|
return NewErrorResponse(code, msg)
|
|
}
|
|
|
|
// ErrorEmpty 无参数错误响应
|
|
func ErrorEmpty() *APIResponse {
|
|
return Error(1, "操作失败")
|
|
}
|