feat: 完善用户基础操作
This commit is contained in:
138
docs/docs.go
138
docs/docs.go
@@ -15,6 +15,96 @@ const docTemplate = `{
|
||||
"host": "{{.Host}}",
|
||||
"basePath": "{{.BasePath}}",
|
||||
"paths": {
|
||||
"/user/FindUserByNameAndPwd": {
|
||||
"post": {
|
||||
"tags": [
|
||||
"用户模块"
|
||||
],
|
||||
"summary": "用户登录",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "username",
|
||||
"name": "username",
|
||||
"in": "formData"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "password",
|
||||
"name": "password",
|
||||
"in": "formData"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "code\",\"message\"}",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/user/createUser": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"用户模块"
|
||||
],
|
||||
"summary": "新增用户",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "用户名",
|
||||
"name": "username",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "密码",
|
||||
"name": "password",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "确认密码",
|
||||
"name": "repassword",
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "code\",\"message\"}",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/user/deleteUser": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"用户模块"
|
||||
],
|
||||
"summary": "删除用户",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "id",
|
||||
"name": "id",
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "code\",\"message\"}",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/user/getUserList": {
|
||||
"get": {
|
||||
"tags": [
|
||||
@@ -30,6 +120,54 @@ const docTemplate = `{
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/user/updateUser": {
|
||||
"post": {
|
||||
"tags": [
|
||||
"用户模块"
|
||||
],
|
||||
"summary": "修改用户",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "id",
|
||||
"name": "id",
|
||||
"in": "formData"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "username",
|
||||
"name": "username",
|
||||
"in": "formData"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "password",
|
||||
"name": "password",
|
||||
"in": "formData"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "phone",
|
||||
"name": "phone",
|
||||
"in": "formData"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "email",
|
||||
"name": "email",
|
||||
"in": "formData"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "code\",\"message\"}",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}`
|
||||
|
@@ -4,6 +4,96 @@
|
||||
"contact": {}
|
||||
},
|
||||
"paths": {
|
||||
"/user/FindUserByNameAndPwd": {
|
||||
"post": {
|
||||
"tags": [
|
||||
"用户模块"
|
||||
],
|
||||
"summary": "用户登录",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "username",
|
||||
"name": "username",
|
||||
"in": "formData"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "password",
|
||||
"name": "password",
|
||||
"in": "formData"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "code\",\"message\"}",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/user/createUser": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"用户模块"
|
||||
],
|
||||
"summary": "新增用户",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "用户名",
|
||||
"name": "username",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "密码",
|
||||
"name": "password",
|
||||
"in": "query"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "确认密码",
|
||||
"name": "repassword",
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "code\",\"message\"}",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/user/deleteUser": {
|
||||
"get": {
|
||||
"tags": [
|
||||
"用户模块"
|
||||
],
|
||||
"summary": "删除用户",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "id",
|
||||
"name": "id",
|
||||
"in": "query"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "code\",\"message\"}",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/user/getUserList": {
|
||||
"get": {
|
||||
"tags": [
|
||||
@@ -19,6 +109,54 @@
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"/user/updateUser": {
|
||||
"post": {
|
||||
"tags": [
|
||||
"用户模块"
|
||||
],
|
||||
"summary": "修改用户",
|
||||
"parameters": [
|
||||
{
|
||||
"type": "string",
|
||||
"description": "id",
|
||||
"name": "id",
|
||||
"in": "formData"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "username",
|
||||
"name": "username",
|
||||
"in": "formData"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "password",
|
||||
"name": "password",
|
||||
"in": "formData"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "phone",
|
||||
"name": "phone",
|
||||
"in": "formData"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "email",
|
||||
"name": "email",
|
||||
"in": "formData"
|
||||
}
|
||||
],
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "code\",\"message\"}",
|
||||
"schema": {
|
||||
"type": "string"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,6 +1,63 @@
|
||||
info:
|
||||
contact: {}
|
||||
paths:
|
||||
/user/FindUserByNameAndPwd:
|
||||
post:
|
||||
parameters:
|
||||
- description: username
|
||||
in: formData
|
||||
name: username
|
||||
type: string
|
||||
- description: password
|
||||
in: formData
|
||||
name: password
|
||||
type: string
|
||||
responses:
|
||||
"200":
|
||||
description: code","message"}
|
||||
schema:
|
||||
type: string
|
||||
summary: 用户登录
|
||||
tags:
|
||||
- 用户模块
|
||||
/user/createUser:
|
||||
get:
|
||||
parameters:
|
||||
- description: 用户名
|
||||
in: query
|
||||
name: username
|
||||
type: string
|
||||
- description: 密码
|
||||
in: query
|
||||
name: password
|
||||
type: string
|
||||
- description: 确认密码
|
||||
in: query
|
||||
name: repassword
|
||||
type: string
|
||||
responses:
|
||||
"200":
|
||||
description: code","message"}
|
||||
schema:
|
||||
type: string
|
||||
summary: 新增用户
|
||||
tags:
|
||||
- 用户模块
|
||||
/user/deleteUser:
|
||||
get:
|
||||
parameters:
|
||||
- description: id
|
||||
in: query
|
||||
name: id
|
||||
type: string
|
||||
responses:
|
||||
"200":
|
||||
description: code","message"}
|
||||
schema:
|
||||
type: string
|
||||
summary: 删除用户
|
||||
tags:
|
||||
- 用户模块
|
||||
/user/getUserList:
|
||||
get:
|
||||
responses:
|
||||
@@ -11,4 +68,35 @@ paths:
|
||||
summary: 所有用户
|
||||
tags:
|
||||
- 用户模块
|
||||
/user/updateUser:
|
||||
post:
|
||||
parameters:
|
||||
- description: id
|
||||
in: formData
|
||||
name: id
|
||||
type: string
|
||||
- description: username
|
||||
in: formData
|
||||
name: username
|
||||
type: string
|
||||
- description: password
|
||||
in: formData
|
||||
name: password
|
||||
type: string
|
||||
- description: phone
|
||||
in: formData
|
||||
name: phone
|
||||
type: string
|
||||
- description: email
|
||||
in: formData
|
||||
name: email
|
||||
type: string
|
||||
responses:
|
||||
"200":
|
||||
description: code","message"}
|
||||
schema:
|
||||
type: string
|
||||
summary: 修改用户
|
||||
tags:
|
||||
- 用户模块
|
||||
swagger: "2.0"
|
||||
|
1
go.mod
1
go.mod
@@ -14,6 +14,7 @@ require (
|
||||
|
||||
require (
|
||||
github.com/KyleBanks/depth v1.2.1 // indirect
|
||||
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
|
||||
github.com/bytedance/sonic v1.12.0 // indirect
|
||||
github.com/bytedance/sonic/loader v0.2.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.4 // indirect
|
||||
|
2
go.sum
2
go.sum
@@ -1,5 +1,7 @@
|
||||
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
|
||||
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
|
||||
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
|
||||
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
|
||||
github.com/bytedance/sonic v1.12.0 h1:YGPgxF9xzaCNvd/ZKdQ28yRovhfMFZQjuk6fKBzZ3ls=
|
||||
github.com/bytedance/sonic v1.12.0/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk=
|
||||
github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU=
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go-chat/utils"
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
@@ -10,6 +11,7 @@ type UserBasic struct {
|
||||
gorm.Model
|
||||
Username string
|
||||
Password string
|
||||
Avatar string
|
||||
Phone string `valid:"matches(^1[3-9]{1}\\d{9}$)"`
|
||||
Email string `valid:"email"`
|
||||
Identity string
|
||||
@@ -20,6 +22,7 @@ type UserBasic struct {
|
||||
LoginOutTime *time.Time `gorm:"column:login_out_time" json:"login_out_time"`
|
||||
IsLogout bool
|
||||
DeviceInfo string
|
||||
Salt string
|
||||
}
|
||||
|
||||
func (table *UserBasic) TableName() string {
|
||||
@@ -31,3 +34,36 @@ func GetUserList() []*UserBasic {
|
||||
utils.InitMySQL().Find(&data)
|
||||
return data
|
||||
}
|
||||
func CreateUser(user UserBasic) *gorm.DB {
|
||||
return utils.InitMySQL().Create(&user)
|
||||
}
|
||||
func DeleteUser(user UserBasic) *gorm.DB {
|
||||
return utils.InitMySQL().Delete(&user)
|
||||
}
|
||||
func UpdateUser(user UserBasic) *gorm.DB {
|
||||
return utils.InitMySQL().Model(&user).Updates(UserBasic{Username: user.Username, Password: user.Password, Phone: user.Phone, Email: user.Email, Avatar: user.Avatar})
|
||||
}
|
||||
|
||||
func FindUserByName(name string) UserBasic {
|
||||
user := UserBasic{}
|
||||
utils.InitMySQL().Where("username = ?", name).Find(&user)
|
||||
return user
|
||||
}
|
||||
func FindUserByPhone(phone string) *gorm.DB {
|
||||
user := UserBasic{}
|
||||
return utils.InitMySQL().Where("phone= ?", phone).Find(&user)
|
||||
}
|
||||
func FindUserByEmail(email string) *gorm.DB {
|
||||
user := UserBasic{}
|
||||
return utils.InitMySQL().Where("email = ?", email).First(&user)
|
||||
}
|
||||
|
||||
func FindUserByNameAndPwd(name string, password string) UserBasic {
|
||||
user := UserBasic{}
|
||||
utils.InitMySQL().Where("name = ? and password = ?", name, password).First(&user)
|
||||
//token加密
|
||||
str := fmt.Sprintf("%d", time.Now().Unix())
|
||||
temp := utils.MD5Encode(str)
|
||||
utils.InitMySQL().Model(&user).Where("id = ?", user.ID).Update("identity", temp)
|
||||
return user
|
||||
}
|
||||
|
@@ -14,5 +14,9 @@ func Router() *gin.Engine {
|
||||
r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
|
||||
r.GET("/index", service.GetIndex)
|
||||
r.GET("/user/getUserList", service.GetUserList)
|
||||
r.GET("/user/createUser", service.CreateUser)
|
||||
r.GET("/user/deleteUser", service.DeleteUser)
|
||||
r.POST("/user/updateUser", service.UpdateUser)
|
||||
r.POST("/user/FindUserByNameAndPwd", service.FindUserByNameAndPwd)
|
||||
return r
|
||||
}
|
||||
|
@@ -1,8 +1,13 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/asaskevich/govalidator"
|
||||
"github.com/gin-gonic/gin"
|
||||
"go-chat/models"
|
||||
"go-chat/utils"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// GetUserList
|
||||
@@ -16,3 +21,124 @@ func GetUserList(c *gin.Context) {
|
||||
"message": data,
|
||||
})
|
||||
}
|
||||
|
||||
// CreateUser
|
||||
// @Summary 新增用户
|
||||
// @Tags 用户模块
|
||||
// @Param username query string false "用户名"
|
||||
// @Param password query string false "密码"
|
||||
// @Param repassword query string false "确认密码"
|
||||
// @Success 200 {string} json{"code","message"}
|
||||
// @Router /user/createUser [get]
|
||||
func CreateUser(c *gin.Context) {
|
||||
user := models.UserBasic{}
|
||||
user.Username = c.Query("username")
|
||||
password := c.Query("password")
|
||||
repassword := c.Query("repassword")
|
||||
salt := fmt.Sprintf("%06d", rand.Int31())
|
||||
data := models.FindUserByName(user.Username)
|
||||
if data.Username != "" {
|
||||
c.JSON(-1, gin.H{
|
||||
"message": "用户名已注册!",
|
||||
})
|
||||
return
|
||||
}
|
||||
if password != repassword {
|
||||
c.JSON(-1, gin.H{
|
||||
"message": "两次密码不一致",
|
||||
})
|
||||
return
|
||||
}
|
||||
user.Password = utils.MakePassword(password, salt)
|
||||
user.Salt = salt
|
||||
models.CreateUser(user)
|
||||
c.JSON(200, gin.H{
|
||||
"message": "新增用户成功",
|
||||
})
|
||||
}
|
||||
|
||||
// DeleteUser
|
||||
// @Summary 删除用户
|
||||
// @Tags 用户模块
|
||||
// @Param id query string false "id"
|
||||
// @Success 200 {string} json{"code","message"}
|
||||
// @Router /user/deleteUser [get]
|
||||
func DeleteUser(c *gin.Context) {
|
||||
user := models.UserBasic{}
|
||||
id, _ := strconv.Atoi(c.Query("id"))
|
||||
user.ID = uint(id)
|
||||
models.DeleteUser(user)
|
||||
c.JSON(200, gin.H{
|
||||
"message": "删除用户成功",
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateUser
|
||||
// @Summary 修改用户
|
||||
// @Tags 用户模块
|
||||
// @param id formData string false "id"
|
||||
// @param username formData string false "username"
|
||||
// @param password formData string false "password"
|
||||
// @param phone formData string false "phone"
|
||||
// @param email formData string false "email"
|
||||
// @Success 200 {string} json{"code","message"}
|
||||
// @Router /user/updateUser [post]
|
||||
func UpdateUser(c *gin.Context) {
|
||||
user := models.UserBasic{}
|
||||
id, _ := strconv.Atoi(c.PostForm("id"))
|
||||
user.ID = uint(id)
|
||||
user.Username = c.PostForm("username")
|
||||
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,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// FindUserByNameAndPwd
|
||||
// @Summary 用户登录
|
||||
// @Tags 用户模块
|
||||
// @param username formData string false "username"
|
||||
// @param password formData string false "password"
|
||||
// @Success 200 {string} json{"code","message"}
|
||||
// @Router /user/FindUserByNameAndPwd [post]
|
||||
func FindUserByNameAndPwd(c *gin.Context) {
|
||||
username := c.PostForm("username")
|
||||
password := c.PostForm("password")
|
||||
user := models.FindUserByName(username)
|
||||
if user.Username == "" {
|
||||
c.JSON(200, gin.H{
|
||||
"message": "该用户不存在",
|
||||
})
|
||||
return
|
||||
}
|
||||
flag := utils.ValidPassword(password, user.Salt, user.Password)
|
||||
if !flag {
|
||||
c.JSON(200, gin.H{
|
||||
"message": "密码不正确",
|
||||
})
|
||||
return
|
||||
}
|
||||
pwd := utils.MakePassword(password, user.Salt)
|
||||
data := models.FindUserByNameAndPwd(username, pwd)
|
||||
c.JSON(200, gin.H{
|
||||
"message": data,
|
||||
})
|
||||
}
|
||||
|
34
utils/md5.go
Normal file
34
utils/md5.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// Package utils /**
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Md5Encode 小写
|
||||
func Md5Encode(data string) string {
|
||||
h := md5.New()
|
||||
h.Write([]byte(data))
|
||||
tempStr := h.Sum(nil)
|
||||
return hex.EncodeToString(tempStr)
|
||||
}
|
||||
|
||||
// MD5Encode 大写
|
||||
func MD5Encode(data string) string {
|
||||
return strings.ToUpper(Md5Encode(data))
|
||||
}
|
||||
|
||||
// MakePassword 加密
|
||||
func MakePassword(plainpwd, salt string) string {
|
||||
return Md5Encode(plainpwd + salt)
|
||||
}
|
||||
|
||||
// ValidPassword 解密
|
||||
func ValidPassword(plainpwd, salt string, password string) bool {
|
||||
md := Md5Encode(plainpwd + salt)
|
||||
fmt.Println(md + " " + password)
|
||||
return md == password
|
||||
}
|
@@ -11,8 +11,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
var DB *gorm.DB
|
||||
|
||||
func InitConfig() {
|
||||
viper.SetConfigName("app")
|
||||
viper.AddConfigPath("config")
|
||||
|
Reference in New Issue
Block a user