Files
be.ems/src/modules/common/controller/register.go
T

91 lines
2.8 KiB
Go

package controller
import (
commonConstants "ems.agt/src/framework/constants/common"
ctxUtils "ems.agt/src/framework/utils/ctx"
"ems.agt/src/framework/utils/regular"
"ems.agt/src/framework/vo/result"
commonModel "ems.agt/src/modules/common/model"
commonService "ems.agt/src/modules/common/service"
systemService "ems.agt/src/modules/system/service"
"github.com/gin-gonic/gin"
)
// 实例化控制层 RegisterController 结构体
var NewRegister = &RegisterController{
registerService: commonService.NewRegisterImpl,
sysLogLoginService: systemService.NewSysLogLoginImpl,
}
// 账号注册操作处理
//
// PATH /
type RegisterController struct {
// 账号注册操作服务
registerService commonService.IRegister
// 系统登录访问
sysLogLoginService systemService.ISysLogLogin
}
// 账号注册
//
// GET /register
func (s *RegisterController) Register(c *gin.Context) {
var registerBody commonModel.RegisterBody
if err := c.ShouldBindJSON(&registerBody); err != nil {
c.JSON(400, result.ErrMsg("parameter error"))
return
}
// 判断必传参数
if !regular.ValidUsername(registerBody.Username) {
// 账号不能以数字开头,可包含大写小写字母,数字,且不少于5位
c.JSON(200, result.ErrMsg("The account cannot start with a number and can contain uppercase and lowercase letters, numbers, and no less than 5 digits"))
return
}
if !regular.ValidPassword(registerBody.Password) {
// 登录密码至少包含大小写字母、数字、特殊符号,且不少于6位
c.JSON(200, result.ErrMsg("The login password should contain at least uppercase and lowercase letters, numbers, special symbols, and no less than 6 digits"))
return
}
if registerBody.Password != registerBody.ConfirmPassword {
// 用户确认输入密码不一致
c.JSON(200, result.ErrMsg("The user confirmed that the input password is inconsistent"))
return
}
// 当前请求信息
ipaddr, location := ctxUtils.IPAddrLocation(c)
os, browser := ctxUtils.UaOsBrowser(c)
// 校验验证码
err := s.registerService.ValidateCaptcha(
registerBody.Code,
registerBody.UUID,
)
// 根据错误信息,创建系统访问记录
if err != nil {
msg := err.Error() + " " + registerBody.Code
s.sysLogLoginService.CreateSysLogLogin(
registerBody.Username, commonConstants.STATUS_NO, msg,
ipaddr, location, os, browser,
)
c.JSON(200, result.ErrMsg(err.Error()))
return
}
userID, err := s.registerService.ByUserName(registerBody.Username, registerBody.Password, registerBody.UserType)
if err == nil {
msg := registerBody.Username + " registered success " + userID
s.sysLogLoginService.CreateSysLogLogin(
registerBody.Username, commonConstants.STATUS_YES, msg,
ipaddr, location, os, browser,
)
// 注册成功
c.JSON(200, result.OkMsg("registered success"))
return
}
c.JSON(200, result.ErrMsg(err.Error()))
}