(c *gin.Context)
| 114 | } |
| 115 | |
| 116 | func Register(c *gin.Context) { |
| 117 | if !common.RegisterEnabled { |
| 118 | c.JSON(http.StatusOK, gin.H{ |
| 119 | "message": "管理员关闭了新用户注册", |
| 120 | "success": false, |
| 121 | }) |
| 122 | return |
| 123 | } |
| 124 | if !common.PasswordRegisterEnabled { |
| 125 | c.JSON(http.StatusOK, gin.H{ |
| 126 | "message": "管理员关闭了通过密码进行注册,请使用第三方账户验证的形式进行注册", |
| 127 | "success": false, |
| 128 | }) |
| 129 | return |
| 130 | } |
| 131 | var user model.User |
| 132 | err := json.NewDecoder(c.Request.Body).Decode(&user) |
| 133 | if err != nil { |
| 134 | c.JSON(http.StatusOK, gin.H{ |
| 135 | "success": false, |
| 136 | "message": "无效的参数", |
| 137 | }) |
| 138 | return |
| 139 | } |
| 140 | if err := common.Validate.Struct(&user); err != nil { |
| 141 | c.JSON(http.StatusOK, gin.H{ |
| 142 | "success": false, |
| 143 | "message": "输入不合法 " + err.Error(), |
| 144 | }) |
| 145 | return |
| 146 | } |
| 147 | if common.EmailVerificationEnabled { |
| 148 | if user.Email == "" || user.VerificationCode == "" { |
| 149 | c.JSON(http.StatusOK, gin.H{ |
| 150 | "success": false, |
| 151 | "message": "管理员开启了邮箱验证,请输入邮箱地址和验证码", |
| 152 | }) |
| 153 | return |
| 154 | } |
| 155 | if !common.VerifyCodeWithKey(user.Email, user.VerificationCode, common.EmailVerificationPurpose) { |
| 156 | c.JSON(http.StatusOK, gin.H{ |
| 157 | "success": false, |
| 158 | "message": "验证码错误或已过期", |
| 159 | }) |
| 160 | return |
| 161 | } |
| 162 | } |
| 163 | exist, err := model.CheckUserExistOrDeleted(user.Username, user.Email) |
| 164 | if err != nil { |
| 165 | c.JSON(http.StatusOK, gin.H{ |
| 166 | "success": false, |
| 167 | "message": "数据库错误,请稍后重试", |
| 168 | }) |
| 169 | common.SysError(fmt.Sprintf("CheckUserExistOrDeleted error: %v", err)) |
| 170 | return |
| 171 | } |
| 172 | if exist { |
| 173 | c.JSON(http.StatusOK, gin.H{ |
nothing calls this directly
no test coverage detected