前置:
代码层次还是由低到高:model层数据库操作->service层封装->路由层调用业务
手把手详细教你如何使用go-swagger文档 - 掘金 (juejin.cn)
golang swagger注解说明_go swagger 注释_mctlilac的博客-CSDN博客
Gin篇:gorm 使用 - -零 - 博客园 (cnblogs.com)
1)get方式获取参数context.Query("xxx")
golang gin 获取请求中的查询参数,并设置默认值 - golang - 大象笔记 (sunzhongwei.com)
2)post方式获取参数context.Request.FormValue("xxx")
作用基本同上:可以查询url后的?参数、href超链里?后参数
Golang 中Ruquest.FormValue方法_goland formvalue_Kotlin20180728的博客-CSDN博客
1.路由层
//用户模块
r.POST("/user/getUserList", service.GetUserList) //遇到指定请求,调用service层的函数
r.POST("/user/createUser", service.CreateUser)
//r.GET("/user/createUser", service.CreateUser) //用于swagger UI测试
r.POST("/user/deleteUser", service.DeleteUser)
r.POST("/user/updateUser", service.UpdateUser)
r.POST("/user/findUserByNameAndPwd", service.FindUserByNameAndPwd)
r.POST("/user/find", service.FindByID)
2.service层,这里都是post方式
1).用户增加
func CreateUser(c *gin.Context) {
user := models.UserBasic{}
//get请求专用c.Query
查询请求URL ?后面的参数
// user.Name = c.Query("name")
// password := c.Query("password")
// repassword := c.Query("repassword")
//post请求用函数FormValue
//设置属性值
user.Name = c.Request.FormValue("name")
password := c.Request.FormValue("password")
repassword := c.Request.FormValue("Identity")
fmt.Println(user.Name, " >>>>>>>>>>> ", password, repassword)
salt := fmt.Sprintf("%06d", rand.Int31())
data := models.FindUserByName(user.Name)
if user.Name == "" || password == "" || repassword == "" {
c.JSON(200, gin.H{
"code": -1, // 0成功 -1失败
"message": "用户名或密码不能为空!",
"data": user,
})
return
}
if data.Name != "" {
c.JSON(200, gin.H{
"code": -1, // 0成功 -1失败
"message": "用户名已注册!",
"data": user,
})
return
}
if password != repassword {
c.JSON(200, gin.H{
"code": -1, // 0成功 -1失败
"message": "两次密码不一致!",
"data": user,
})
return
}
//user.PassWord = password
user.PassWord = utils.MakePassword(password, salt)
user.Salt = salt
fmt.Println(user.PassWord)
user.LoginTime = time.Now()
user.LoginOutTime = time.Now()
user.HeartbeatTime = time.Now()
models.CreateUser(user)
c.JSON(200, gin.H{
"code": 0, // 0成功 -1失败
"message": "新增用户成功!",
"data": user,
})
}
2).删除
func DeleteUser(c *gin.Context) {
user := models.UserBasic{}
id, _ := strconv.Atoi(c.Query("id"))
fmt.Println(id)
user.ID = uint(id)
models.DeleteUser(user)
c.JSON(200, gin.H{
"code": 0, // 0成功 -1失败
"message": "删除用户成功!",
"data": user,
})
}
3).修改
func UpdateUser(c *gin.Context) {
user := models.UserBasic{}
id, _ := strconv.Atoi(c.PostForm("id"))
user.ID = uint(id)
user.Name = c.PostForm("name")
user.PassWord = c.PostForm("password")
user.Phone = c.PostForm("phone")
user.Avatar = c.PostForm("icon")
user.Email = c.PostForm("email")
fmt.Println("update :", user)
_, err := govalidator.ValidateStruct(user)
if err != nil {
fmt.Println(err)
c.JSON(200, gin.H{
"code": -1, // 0成功 -1失败
"message": "修改参数不匹配!",
"data": user,
})
} else {
models.UpdateUser(user)
c.JSON(200, gin.H{
"code": 0, // 0成功 -1失败
"message": "修改用户成功!",
"data": user,
})
}
}
4).查
func FindUserByNameAndPwd(c *gin.Context) {
data := models.UserBasic{}
//name := c.Query("name")
//password := c.Query("password")
name := c.Request.FormValue("name")
password := c.Request.FormValue("password")
fmt.Println(name, password)
user := models.FindUserByName(name)
if user.Name == "" {
c.JSON(200, gin.H{
"code": -1, // 0成功 -1失败
"message": "该用户不存在",
"data": data,
})
return
}
flag := utils.ValidPassword(password, user.Salt, user.PassWord)
if !flag {
c.JSON(200, gin.H{
"code": -1, // 0成功 -1失败
"message": "密码不正确",
"data": data,
})
return
}
pwd := utils.MakePassword(password, user.Salt)
data = models.FindUserByNameAndPwd(name, pwd)
c.JSON(200, gin.H{
"code": 0, // 0成功 -1失败
"message": "登录成功",
"data": data,
})
}
func FindUserByNameAndPwd(c *gin.Context) {
data := models.UserBasic{}
//name := c.Query("name")
//password := c.Query("password")
name := c.Request.FormValue("name")
password := c.Request.FormValue("password")
fmt.Println(name, password)
user := models.FindUserByName(name)
if user.Name == "" {
c.JSON(200, gin.H{
"code": -1, // 0成功 -1失败
"message": "该用户不存在",
"data": data,
})
return
}
flag := utils.ValidPassword(password, user.Salt, user.PassWord)
if !flag {
c.JSON(200, gin.H{
"code": -1, // 0成功 -1失败
"message": "密码不正确",
"data": data,
})
return
}
pwd := utils.MakePassword(password, user.Salt)
data = models.FindUserByNameAndPwd(name, pwd)
c.JSON(200, gin.H{
"code": 0, // 0成功 -1失败
"message": "登录成功",
"data": data,
})
}
扫描二维码关注公众号,回复:
16506800 查看本文章
![](/qrcode.jpg)