feat(user):基本完成间隔与运维用户创建功能。

This commit is contained in:
徐涛 2022-08-13 11:08:29 +08:00
parent 0fc57b7506
commit 409f9af1c4
2 changed files with 49 additions and 2 deletions

View File

@ -11,8 +11,10 @@ import (
"errors" "errors"
"net/http" "net/http"
"strconv" "strconv"
"time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/shopspring/decimal"
) )
type _UserController struct { type _UserController struct {
@ -31,6 +33,7 @@ func InitializeUserController(router *gin.Engine) {
UserController.Router.PUT("/password", UserController.ResetUserPassword) UserController.Router.PUT("/password", UserController.ResetUserPassword)
UserController.Router.GET("/accounts", security.OPSAuthorize, UserController.ListPagedUser) UserController.Router.GET("/accounts", security.OPSAuthorize, UserController.ListPagedUser)
UserController.Router.PUT("/account/enabled/state", security.OPSAuthorize, UserController.SwitchUserEnabling) UserController.Router.PUT("/account/enabled/state", security.OPSAuthorize, UserController.SwitchUserEnabling)
UserController.Router.POST("/account", security.OPSAuthorize, UserController.CreateOPSAndManagementAccount)
} }
type LoginFormData struct { 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"` UserID string `json:"uid" form:"uid"`
Enabled bool `json:"enabled" form:"enabled"` Enabled bool `json:"enabled" form:"enabled"`
} }
func (_UserController) SwitchUserEnabling(c *gin.Context) { func (_UserController) SwitchUserEnabling(c *gin.Context) {
result := response.NewResult(c) result := response.NewResult(c)
switchForm := new(UserStateFormData) switchForm := new(UserStateChangeFormData)
c.BindJSON(switchForm) c.BindJSON(switchForm)
err := service.UserService.SwitchUserState(switchForm.UserID, switchForm.Enabled) err := service.UserService.SwitchUserState(switchForm.UserID, switchForm.Enabled)
if err != nil { if err != nil {
@ -195,3 +198,42 @@ func (_UserController) SwitchUserEnabling(c *gin.Context) {
} }
result.Success("用户状态已经更新。") 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})
}

View File

@ -13,6 +13,7 @@ import (
"time" "time"
"github.com/google/uuid" "github.com/google/uuid"
"xorm.io/builder"
) )
type _UserService struct{} type _UserService struct{}
@ -169,6 +170,10 @@ func (_UserService) IsUserExists(uid string) (bool, error) {
return global.DBConn.ID(uid).Exist(&model.User{}) 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) { func (u _UserService) CreateUser(user *model.User, detail *model.UserDetail) (string, error) {
if len(user.Id) == 0 { if len(user.Id) == 0 {
user.Id = uuid.New().String() user.Id = uuid.New().String()