diff --git a/controller/user.go b/controller/user.go index 65e3eee..b472bf2 100644 --- a/controller/user.go +++ b/controller/user.go @@ -34,6 +34,7 @@ func InitializeUserController(router *gin.Engine) { UserController.Router.PUT("/account/enabled/state", security.OPSAuthorize, UserController.SwitchUserEnabling) 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) } type LoginFormData struct { @@ -256,3 +257,49 @@ func (_UserController) GetUserDetail(c *gin.Context) { } result.Json(http.StatusOK, http.StatusOK, "用户详细信息已获取到。", gin.H{"user": userDetail}) } + +type EnterpriseCreationFormData struct { + Username string `json:"username" form:"username"` + 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) CreateEnterpriseAccount(c *gin.Context) { + result := response.NewResult(c) + creationForm := new(EnterpriseCreationFormData) + 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 = 0 + newUser.Enabled = true + newUserDetail := new(model.UserDetail) + newUserDetail.Name = &creationForm.Name + newUserDetail.Contact = creationForm.Contact + newUserDetail.Phone = creationForm.Phone + newUserDetail.UnitServiceFee, err = decimal.NewFromString(*creationForm.UnitServiceFee) + if err != nil { + result.Error(http.StatusBadRequest, "用户月服务费无法解析。") + return + } + newUserDetail.ServiceExpiration = time.Now() + + 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/model/user_detail.go b/model/user_detail.go index f7c7aef..8db74e4 100644 --- a/model/user_detail.go +++ b/model/user_detail.go @@ -9,7 +9,7 @@ import ( type UserDetail struct { CreatedAndModifiedWithUser `xorm:"extends"` DeletedWithUser `xorm:"extends"` - Id string `xorm:"varchar(120) pk not null -" json:"-"` + Id string `xorm:"varchar(120) pk not null" json:"-"` Name *string `xorm:"varchar(100)" json:"name"` Abbr *string `xorm:"varchar(50)" json:"abbr"` Region *string `xorm:"varchar(10)" json:"region"`