electricity_bill_calc_service/response/base_response.go

124 lines
3.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package response
import (
"electricity_bill_calc/config"
"github.com/gofiber/fiber/v2"
)
type Result struct {
Ctx *fiber.Ctx
}
type BaseResponse struct {
Code int `json:"code"`
Message string `json:"message"`
}
type PagedResponse struct {
Page int `json:"current"`
Size int `json:"pageSize"`
Total int64 `json:"total"`
}
func NewResult(ctx *fiber.Ctx) Result {
return Result{Ctx: ctx}
}
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
for _, m := range payloads {
for k, v := range m {
finalPayload[k] = v
}
}
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) error {
return r.response(fiber.StatusInternalServerError, 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) error {
return r.response(fiber.StatusOK, fiber.StatusUnauthorized, msg)
}
// 简易操作成功信息
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{}) error {
return r.response(fiber.StatusOK, fiber.StatusCreated, msg, payloads...)
}
// 数据成功更新
func (r Result) Updated(msg string) error {
return r.response(fiber.StatusOK, fiber.StatusAccepted, msg)
}
// 数据已成功删除
func (r Result) Deleted(msg string) error {
return r.response(fiber.StatusOK, fiber.StatusNoContent, msg)
}
// 指定操作未被接受
func (r Result) BadRequest(msg string) error {
return r.response(fiber.StatusOK, fiber.StatusBadRequest, msg)
}
// 指定操作未被接受
func (r Result) NotAccept(msg string) error {
return r.response(fiber.StatusOK, fiber.StatusNotAcceptable, msg)
}
// 数据未找到
func (r Result) NotFound(msg string) error {
return r.response(fiber.StatusOK, fiber.StatusNotFound, 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{}) error {
return r.response(fiber.StatusOK, code, msg, payloads...)
}
func NewPagedResponse(page int, total int64) *PagedResponse {
return &PagedResponse{page, config.ServiceSettings.ItemsPageSize, total}
}
func (r PagedResponse) ToMap() map[string]interface{} {
return fiber.Map{
"current": r.Page,
"pageSize": r.Size,
"total": r.Total,
}
}