package v1
import (
"mall/pkg/util"
"mall/service"
"net/http"
"github.com/gin-gonic/gin"
)
// handler 绑定路由的一个方法
func UserRegister(c *gin.Context) {
var userRegister service.UserService
if err := c.ShouldBind(&userRegister); err == nil {
res := userRegister.Register(c.Request.Context())
c.JSON(http.StatusOK, res)
} else {
c.JSON(http.StatusBadRequest, ErrorResponse(err))
}
}
package v1
import (
"encoding/json"
"mall/serializer"
)
func ErrorResponse(err error) serializer.Response{
if _,ok:=err.(*json.UnmarshalTypeError);ok{
return serializer.Response{
Status: 400,
Msg: "JSON类型不匹配",
Error: err.Error(),
}
}
return serializer.Response{
Status: 400,
Msg: "参数错误",
Error: err.Error(),
}
}
package serializer
// 定义序列化返回结构体
type Response struct {
Status int `json:"status"`
Data interface{} `json:"data"`
Msg string `json:"msg"`
Error string `json:"error"`
}