forked from free-lancers/electricity_bill_calc_service
refactor(app):基本完成基础服务框架的功能构建。
This commit is contained in:
@@ -2,13 +2,12 @@ package response
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/config"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type Result struct {
|
||||
Ctx *gin.Context
|
||||
Ctx *fiber.Ctx
|
||||
}
|
||||
|
||||
type BaseResponse struct {
|
||||
@@ -22,11 +21,11 @@ type PagedResponse struct {
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
func NewResult(ctx *gin.Context) *Result {
|
||||
return &Result{Ctx: ctx}
|
||||
func NewResult(ctx *fiber.Ctx) Result {
|
||||
return Result{Ctx: ctx}
|
||||
}
|
||||
|
||||
func (r *Result) response(status, code int, msg string, payloads ...map[string]interface{}) {
|
||||
func (r Result) response(status, code int, msg string, payloads ...map[string]interface{}) error {
|
||||
var finalPayload = make(map[string]interface{}, 0)
|
||||
finalPayload["code"] = code
|
||||
finalPayload["message"] = msg
|
||||
@@ -37,68 +36,78 @@ func (r *Result) response(status, code int, msg string, payloads ...map[string]i
|
||||
}
|
||||
}
|
||||
|
||||
r.Ctx.JSON(status, finalPayload)
|
||||
return r.Ctx.Status(status).JSON(finalPayload)
|
||||
}
|
||||
|
||||
// 禁止访问响应
|
||||
func (r Result) Forbidden(msg string) error {
|
||||
return r.response(fiber.StatusForbidden, fiber.StatusForbidden, msg)
|
||||
}
|
||||
|
||||
// 操作出错(未成功)响应
|
||||
func (r *Result) Failure(code int, msg string) {
|
||||
r.response(http.StatusInternalServerError, code, msg)
|
||||
func (r Result) Failure(code int, msg string) error {
|
||||
return r.response(fiber.StatusInternalServerError, code, msg)
|
||||
}
|
||||
|
||||
// 操作出错(未成功)响应
|
||||
func (r *Result) Error(code int, msg string) {
|
||||
r.response(http.StatusOK, code, msg)
|
||||
func (r Result) Error(code int, msg string) error {
|
||||
return r.response(fiber.StatusOK, code, msg)
|
||||
}
|
||||
|
||||
// 不能解析传入的参数
|
||||
func (r Result) UnableToParse(msg string) error {
|
||||
return r.response(fiber.StatusInternalServerError, fiber.StatusInternalServerError, msg)
|
||||
}
|
||||
|
||||
// 用户未获得授权)响应
|
||||
func (r *Result) Unauthorized(msg string) {
|
||||
r.response(http.StatusOK, http.StatusUnauthorized, msg)
|
||||
func (r *Result) Unauthorized(msg string) error {
|
||||
return r.response(fiber.StatusOK, fiber.StatusUnauthorized, msg)
|
||||
}
|
||||
|
||||
// 简易操作成功信息
|
||||
func (r *Result) Success(msg string, payloads ...map[string]interface{}) {
|
||||
r.response(http.StatusOK, http.StatusOK, msg, payloads...)
|
||||
func (r *Result) Success(msg string, payloads ...map[string]interface{}) error {
|
||||
return r.response(fiber.StatusOK, fiber.StatusOK, msg, payloads...)
|
||||
}
|
||||
|
||||
// 数据成功创建
|
||||
func (r *Result) Created(msg string, payloads ...map[string]interface{}) {
|
||||
r.response(http.StatusOK, http.StatusCreated, msg, payloads...)
|
||||
func (r Result) Created(msg string, payloads ...map[string]interface{}) error {
|
||||
return r.response(fiber.StatusOK, fiber.StatusCreated, msg, payloads...)
|
||||
}
|
||||
|
||||
// 数据成功更新
|
||||
func (r *Result) Updated(msg string) {
|
||||
r.response(http.StatusOK, http.StatusAccepted, msg)
|
||||
func (r Result) Updated(msg string) error {
|
||||
return r.response(fiber.StatusOK, fiber.StatusAccepted, msg)
|
||||
}
|
||||
|
||||
// 数据已成功删除
|
||||
func (r *Result) Deleted(msg string) {
|
||||
r.response(http.StatusOK, http.StatusNoContent, msg)
|
||||
func (r Result) Deleted(msg string) error {
|
||||
return r.response(fiber.StatusOK, fiber.StatusNoContent, msg)
|
||||
}
|
||||
|
||||
// 指定操作未被接受
|
||||
func (r *Result) BadRequest(msg string) {
|
||||
r.response(http.StatusOK, http.StatusBadRequest, msg)
|
||||
func (r Result) BadRequest(msg string) error {
|
||||
return r.response(fiber.StatusOK, fiber.StatusBadRequest, msg)
|
||||
}
|
||||
|
||||
// 指定操作未被接受
|
||||
func (r *Result) NotAccept(msg string) {
|
||||
r.response(http.StatusOK, http.StatusNotAcceptable, msg)
|
||||
func (r Result) NotAccept(msg string) error {
|
||||
return r.response(fiber.StatusOK, fiber.StatusNotAcceptable, msg)
|
||||
}
|
||||
|
||||
// 数据未找到
|
||||
func (r *Result) NotFound(msg string) {
|
||||
r.response(http.StatusOK, http.StatusNotFound, msg)
|
||||
func (r Result) NotFound(msg string) error {
|
||||
return r.response(fiber.StatusOK, fiber.StatusNotFound, msg)
|
||||
}
|
||||
|
||||
// 数据存在冲突
|
||||
func (r *Result) Conflict(msg string) {
|
||||
r.response(http.StatusOK, http.StatusConflict, msg)
|
||||
func (r Result) Conflict(msg string) error {
|
||||
return r.response(fiber.StatusOK, fiber.StatusConflict, msg)
|
||||
}
|
||||
|
||||
// 快速自由JSON格式响应
|
||||
// ! 注意,给定的map中,同名的键会被覆盖。
|
||||
func (r *Result) Json(code int, msg string, payloads ...map[string]interface{}) {
|
||||
r.response(http.StatusOK, code, msg, payloads...)
|
||||
func (r Result) Json(code int, msg string, payloads ...map[string]interface{}) error {
|
||||
return r.response(fiber.StatusOK, code, msg, payloads...)
|
||||
}
|
||||
|
||||
func NewPagedResponse(page int, total int64) *PagedResponse {
|
||||
@@ -106,7 +115,7 @@ func NewPagedResponse(page int, total int64) *PagedResponse {
|
||||
}
|
||||
|
||||
func (r PagedResponse) ToMap() map[string]interface{} {
|
||||
return gin.H{
|
||||
return fiber.Map{
|
||||
"current": r.Page,
|
||||
"pageSize": r.Size,
|
||||
"total": r.Total,
|
||||
|
@@ -3,6 +3,8 @@ package response
|
||||
import (
|
||||
"electricity_bill_calc/model"
|
||||
"net/http"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
type LoginResponse struct {
|
||||
@@ -17,7 +19,7 @@ func (r *Result) LoginSuccess(session *model.Session) {
|
||||
res.Message = "用户已成功登录。"
|
||||
res.NeedReset = false
|
||||
res.Session = session
|
||||
r.Ctx.JSON(http.StatusOK, res)
|
||||
r.Ctx.Status(fiber.StatusOK).JSON(res)
|
||||
}
|
||||
|
||||
func (r *Result) LoginNeedReset() {
|
||||
@@ -25,5 +27,5 @@ func (r *Result) LoginNeedReset() {
|
||||
res.Code = http.StatusUnauthorized
|
||||
res.Message = "用户凭据已失效。"
|
||||
res.NeedReset = true
|
||||
r.Ctx.JSON(http.StatusOK, res)
|
||||
r.Ctx.Status(fiber.StatusOK).JSON(res)
|
||||
}
|
||||
|
Reference in New Issue
Block a user