From 409f9af1c44405e0d12d32932e07897b95bf6101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Sat, 13 Aug 2022 11:08:29 +0800 Subject: [PATCH] =?UTF-8?q?feat(user):=E5=9F=BA=E6=9C=AC=E5=AE=8C=E6=88=90?= =?UTF-8?q?=E9=97=B4=E9=9A=94=E4=B8=8E=E8=BF=90=E7=BB=B4=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=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 | 46 ++++++++++++++++++++++++++++++++++++++++++++-- service/user.go | 5 +++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/controller/user.go b/controller/user.go index f68b3f9..36ea0f6 100644 --- a/controller/user.go +++ b/controller/user.go @@ -11,8 +11,10 @@ import ( "errors" "net/http" "strconv" + "time" "github.com/gin-gonic/gin" + "github.com/shopspring/decimal" ) type _UserController struct { @@ -31,6 +33,7 @@ func InitializeUserController(router *gin.Engine) { UserController.Router.PUT("/password", UserController.ResetUserPassword) UserController.Router.GET("/accounts", security.OPSAuthorize, UserController.ListPagedUser) UserController.Router.PUT("/account/enabled/state", security.OPSAuthorize, UserController.SwitchUserEnabling) + UserController.Router.POST("/account", security.OPSAuthorize, UserController.CreateOPSAndManagementAccount) } type LoginFormData struct { @@ -174,14 +177,14 @@ func (_UserController) ListPagedUser(c *gin.Context) { ) } -type UserStateFormData struct { +type UserStateChangeFormData struct { UserID string `json:"uid" form:"uid"` Enabled bool `json:"enabled" form:"enabled"` } func (_UserController) SwitchUserEnabling(c *gin.Context) { result := response.NewResult(c) - switchForm := new(UserStateFormData) + switchForm := new(UserStateChangeFormData) c.BindJSON(switchForm) err := service.UserService.SwitchUserState(switchForm.UserID, switchForm.Enabled) if err != nil { @@ -195,3 +198,42 @@ func (_UserController) SwitchUserEnabling(c *gin.Context) { } result.Success("用户状态已经更新。") } + +type OPSAccountCreationFormData struct { + Username string `json:"username" form:"username"` + Name string `json:"name" form:"name"` + Contact *string `json:"contact" form:"contact"` + Phone *string `json:"phone" form:"phone"` + Type int `json:"type" form:"type"` +} + +func (_UserController) CreateOPSAndManagementAccount(c *gin.Context) { + result := response.NewResult(c) + creationForm := new(OPSAccountCreationFormData) + c.BindJSON(creationForm) + exists, err := service.UserService.IsUsernameExists(creationForm.Username) + if exists { + result.Error(http.StatusConflict, "指定的用户名已经被使用了。") + return + } + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + newUser := new(model.User) + newUser.Username = creationForm.Username + newUser.Type = int8(creationForm.Type) + newUser.Enabled = true + newUserDetail := new(model.UserDetail) + newUserDetail.Name = &creationForm.Name + newUserDetail.Contact = creationForm.Contact + newUserDetail.Phone = creationForm.Phone + newUserDetail.UnitServiceFee = decimal.Zero + newUserDetail.ServiceExpiration, _ = time.Parse("2006-01-02 15:04:05", "2099-12-31 23:59:59") + verifyCode, err := service.UserService.CreateUser(newUser, newUserDetail) + if err != nil { + result.Error(http.StatusInternalServerError, err.Error()) + return + } + result.Json(http.StatusOK, http.StatusOK, "用户已经成功创建。", gin.H{"verify": verifyCode}) +} diff --git a/service/user.go b/service/user.go index a0475c4..b6bcda8 100644 --- a/service/user.go +++ b/service/user.go @@ -13,6 +13,7 @@ import ( "time" "github.com/google/uuid" + "xorm.io/builder" ) type _UserService struct{} @@ -169,6 +170,10 @@ func (_UserService) IsUserExists(uid string) (bool, error) { return global.DBConn.ID(uid).Exist(&model.User{}) } +func (_UserService) IsUsernameExists(username string) (bool, error) { + return global.DBConn.Where(builder.Eq{"username": username}).Exist(&model.User{}) +} + func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (string, error) { if len(user.Id) == 0 { user.Id = uuid.New().String()