From 5214147e49a80e6a2bc6348fbfd1e86f2a3ac9d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sat, 13 Aug 2022 19:46:27 +0800 Subject: [PATCH] =?UTF-8?q?feat(user):=E5=AE=8C=E6=88=90=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BF=A1=E6=81=AF=E4=BF=AE=E6=94=B9=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/user.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/controller/user.go b/controller/user.go index 9fe9814..85af316 100644 --- a/controller/user.go +++ b/controller/user.go @@ -3,6 +3,7 @@ package controller import ( "electricity_bill_calc/cache" "electricity_bill_calc/exceptions" + "electricity_bill_calc/global" "electricity_bill_calc/model" "electricity_bill_calc/repository" "electricity_bill_calc/response" @@ -35,6 +36,7 @@ func InitializeUserController(router *gin.Engine) { UserController.Router.POST("/account", security.OPSAuthorize, UserController.CreateOPSAndManagementAccount) UserController.Router.GET("/account/:uid", security.MustAuthenticated, UserController.GetUserDetail) UserController.Router.POST("/enterprise", security.OPSAuthorize, UserController.CreateEnterpriseAccount) + UserController.Router.PUT("/account/:uid", security.OPSAuthorize, UserController.ModifyAccountDetail) } type LoginFormData struct { @@ -302,3 +304,45 @@ func (_UserController) CreateEnterpriseAccount(c *gin.Context) { } result.Json(http.StatusCreated, "用户已经成功创建。", gin.H{"verify": verifyCode}) } + +type AccountModificationFormData struct { + Name string `json:"name" form:"name"` + Region *string `json:"region" form:"region"` + Address *string `json:"address" form:"address"` + Contact *string `json:"contact" form:"contact"` + Phone *string `json:"phone" form:"phone"` + UnitServiceFee *string `json:"unitServiceFee" form:"unitServiceFee"` +} + +func (_UserController) ModifyAccountDetail(c *gin.Context) { + result := response.NewResult(c) + targetUserId := c.Param("uid") + modForm := new(AccountModificationFormData) + c.BindJSON(modForm) + exists, err := service.UserService.IsUserExists(targetUserId) + if !exists { + result.NotFound("指定的用户不存在。") + return + } + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + newUserInfo := new(model.UserDetail) + newUserInfo.Name = &modForm.Name + newUserInfo.Region = modForm.Region + newUserInfo.Address = modForm.Address + newUserInfo.Contact = modForm.Contact + newUserInfo.Phone = modForm.Phone + newUserInfo.UnitServiceFee, err = decimal.NewFromString(*modForm.UnitServiceFee) + if err != nil { + result.BadRequest("用户月服务费无法解析。") + return + } + _, err = global.DBConn.ID(targetUserId).Update(newUserInfo) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + result.Success("指定用户的信息已经更新。") +}