Refactor controllers to use httpx for JSON responses and eliminate context dependency
- Updated TagController methods to use httpx.WriteJSON for consistent JSON response handling. - Refactored TicketController to replace context-dependent methods with standalone functions using httpx. - Modified UserController to utilize httpx for error handling and response formatting. - Adjusted WechatController to remove context dependency and standardize response handling.
This commit is contained in:
@@ -6,124 +6,148 @@ import (
|
||||
"cs-agent/internal/pkg/dto"
|
||||
"cs-agent/internal/pkg/dto/request"
|
||||
"cs-agent/internal/pkg/enums"
|
||||
"cs-agent/internal/pkg/httpx"
|
||||
"cs-agent/internal/services"
|
||||
|
||||
"cs-agent/internal/pkg/httpx/params"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/mlogclub/simple/web"
|
||||
)
|
||||
|
||||
type CustomerController struct {
|
||||
Ctx *gin.Context
|
||||
}
|
||||
|
||||
func (c *CustomerController) PostList() *web.JsonResult {
|
||||
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerView); err != nil {
|
||||
return web.JsonError(err)
|
||||
func CustomerPostList(ctx *gin.Context) {
|
||||
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerView); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
var req request.CustomerListRequest
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
list, paging := services.CustomerService.ListCustomers(req)
|
||||
return web.JsonData(&web.PageResult{Results: builders.BuildCustomerList(list), Page: paging})
|
||||
httpx.WriteJSON(ctx, &web.PageResult{Results: builders.BuildCustomerList(list), Page: paging})
|
||||
return
|
||||
}
|
||||
|
||||
func (c *CustomerController) GetBy(id int64) *web.JsonResult {
|
||||
if _, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerView); err != nil {
|
||||
return web.JsonError(err)
|
||||
func CustomerGetBy(ctx *gin.Context, id int64) {
|
||||
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerView); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
item := services.CustomerService.Get(id)
|
||||
if item == nil || item.Status == enums.StatusDeleted {
|
||||
return web.JsonData(nil)
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
return
|
||||
}
|
||||
ret := builders.BuildCustomer(item)
|
||||
return web.JsonData(&ret)
|
||||
httpx.WriteJSON(ctx, &ret)
|
||||
return
|
||||
}
|
||||
|
||||
// PostSave_profile POST /save_profile — 客户主信息与联系方式在同一事务中保存。
|
||||
func (c *CustomerController) PostSave_profile() *web.JsonResult {
|
||||
func CustomerPostSave_profile(ctx *gin.Context) {
|
||||
req := request.SaveCustomerProfileRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
createMode := req.ID == nil || *req.ID <= 0
|
||||
var user *dto.AuthPrincipal
|
||||
var err error
|
||||
if createMode {
|
||||
user, err = services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerCreate)
|
||||
user, err = services.AuthService.RequirePermission(ctx, constants.PermissionCustomerCreate)
|
||||
} else {
|
||||
user, err = services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerUpdate)
|
||||
user, err = services.AuthService.RequirePermission(ctx, constants.PermissionCustomerUpdate)
|
||||
}
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
item, err := services.CustomerService.SaveCustomerProfile(req, user)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
ret := builders.BuildCustomer(item)
|
||||
return web.JsonData(&ret)
|
||||
httpx.WriteJSON(ctx, &ret)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *CustomerController) PostCreate() *web.JsonResult {
|
||||
user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerCreate)
|
||||
func CustomerPostCreate(ctx *gin.Context) {
|
||||
user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerCreate)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
req := request.CreateCustomerRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
item, err := services.CustomerService.CreateCustomer(req, user)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
ret := builders.BuildCustomer(item)
|
||||
return web.JsonData(&ret)
|
||||
httpx.WriteJSON(ctx, &ret)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *CustomerController) PostUpdate() *web.JsonResult {
|
||||
user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerUpdate)
|
||||
func CustomerPostUpdate(ctx *gin.Context) {
|
||||
user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerUpdate)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
req := request.UpdateCustomerRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := services.CustomerService.UpdateCustomer(req, user); err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
return web.JsonSuccess()
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *CustomerController) PostDelete() *web.JsonResult {
|
||||
user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerDelete)
|
||||
func CustomerPostDelete(ctx *gin.Context) {
|
||||
user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerDelete)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
req := request.DeleteCustomerRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := services.CustomerService.DeleteCustomer(req.ID, *user); err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
return web.JsonSuccess()
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *CustomerController) PostUpdate_status() *web.JsonResult {
|
||||
user, err := services.AuthService.RequirePermission(c.Ctx, constants.PermissionCustomerUpdate)
|
||||
func CustomerPostUpdate_status(ctx *gin.Context) {
|
||||
user, err := services.AuthService.RequirePermission(ctx, constants.PermissionCustomerUpdate)
|
||||
if err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
req := request.UpdateCustomerStatusRequest{}
|
||||
if err := params.ReadJSON(c.Ctx, &req); err != nil {
|
||||
return web.JsonError(err)
|
||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
if err := services.CustomerService.UpdateStatus(req.ID, req.Status, user); err != nil {
|
||||
return web.JsonError(err)
|
||||
httpx.WriteJSON(ctx, err)
|
||||
return
|
||||
}
|
||||
return web.JsonSuccess()
|
||||
httpx.WriteJSON(ctx, nil)
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user