IM 通信系统 --- 注册与登录
1. 注册
1. params
gin_any_chat/user_api/params.go
package user_api
type UserRegisterParam struct {
UserName string `json:"user_name" binding:"required" msg:"请填写用户名"`
Password string `json:"password" binding:"required" msg:"请填写密码"`
RePassword string `json:"re_password" binding:"required,eqfield=Password" msg:"两次密码不一致"` // eqfield=Password 表示, RePassword 必须与 Password 相同才会校验通过
}
2. 路由
gin_any_chat/routers/user_router.go
package routers
import "gin_any_chat/api"
func (router *RouterGroup) UserRouters() {
router.POST("/user/register", api.ApiGroupApp.UserApis.UserRegisterView)
}
3. 视图
gin_any_chat/user_api/views.go
package user_api
import (
"gin_any_chat/global"
"gin_any_chat/models"
"gin_any_chat/models/response"
"github.com/gin-gonic/gin"
"gorm.io/gorm"
)
// @Tags 用户注册
// @Summary 用户注册
// @Description 描述,可以有多个
// @Param data body UserRegisterParam true "如果有body参数,则加上,没有则不加,body表示请求体查询参数,false表示没有,true表示必填"
// @Router /api/user/register [post] "表示post请求"
// @Produce json
// @Success 200 {object} response.Response{}
func (ua *UserApi) UserRegisterView(c *gin.Context) {
var urp UserRegisterParam
err := c.ShouldBindJSON(&urp)
if err != nil {
response.FailWithMessage("注册用户错误!", c)
return
}
// 用户名重复性判断
var user_info models.UserInfoModel
err = global.DB.Take(&user_info, "user_name = ?", urp.UserName).Error
if err != nil && err != gorm.ErrRecordNotFound {
global.Logger.Error("系统错误: ", err.Error())
response.FailWithMessage("创建用户失败, 请求重试!", c)
return
}
if err == nil && user_info.UserName != "" {
response.FailWithMessage("用户名已存在!", c)
return
}
// 在数据库创建记录
err = global.DB.Create(&models.UserInfoModel{
UserName: urp.UserName,
}).Error
if err != nil {
global.Logger.Error(err.Error())
response.FailWithMessage("创建用户失败, 请求重试!", c)
return
}
response.OkWithMessage("创建用户成功!", c)
}
python防脱发技巧

浙公网安备 33010602011771号