zzf 1 éve
szülő
commit
79ff07ce22

+ 188 - 0
api/user.go

@@ -0,0 +1,188 @@
+package api
+
+import (
+	"confrontation-training/common"
+	"confrontation-training/constant"
+	errmsg "confrontation-training/err"
+	"confrontation-training/models"
+	"confrontation-training/response"
+	"confrontation-training/service"
+	"fmt"
+	"github.com/gin-gonic/gin"
+)
+
+type UserService struct {
+	service.UserService
+}
+
+func GetUser() *UserService {
+	return &UserService{}
+}
+
+// UseRegister
+// PingExample kjb-doc
+// @Summary 用户注册
+// @Schemes
+// @Description 用户注册
+// @Tags 用户管理
+// @Param user body string true "userName:用户名;password:密码;role:角色"
+// @Accept json
+// @Produce json
+// @Success 200 {string} string "ok"
+// @Router /v1/user/register [post]
+func (u *UserService) UseRegister(c *gin.Context) {
+	var param models.UserRegister
+	err := c.ShouldBindJSON(&param)
+	if err != nil {
+		fmt.Printf("参数格式化异常:%s", err.Error())
+		response.Failed(errmsg.ParamInvalid, c)
+		return
+	}
+	if _, count := u.FindUserByPhone(param.Phone); count > 0 {
+		response.Failed(errmsg.UserAlreadyExists, c)
+		return
+	}
+	if result := u.CreateUser(param); result.Error == nil {
+		response.Success(errmsg.UserRegisterSuccess, result.RowsAffected, c)
+		return
+	} else {
+		response.Failed(errmsg.UserRegisterFailed+"数据库错:"+result.Error.Error(), c)
+		return
+	}
+
+}
+
+// UserLogin
+// PingExample kjb-doc
+// @Summary 用户登录
+// @Schemes
+// @Description 用户登录
+// @Tags 用户管理
+// @Param user body string true "username:编号;password:密码;role:角色;"
+// @Accept json
+// @Produce json
+// @Success 200 {string} string "ok"
+// @Router /v1/user/login [post]
+func (u *UserService) UserLogin(c *gin.Context) {
+	var param models.UserLogin
+	err := c.ShouldBindJSON(&param)
+
+	if err != nil {
+		fmt.Printf("参数格式化异常:%s", err.Error())
+		response.Failed(errmsg.ParamInvalid, c)
+		return
+	}
+	user := u.Login(param)
+	if user != nil {
+		//生成token
+		toke, _ := common.GenerateToke(*user)
+		resultMap := map[string]string{
+			"token": toke,
+		}
+		response.Success(errmsg.UserLoginSuccess, resultMap, c)
+	} else {
+		response.Failed(errmsg.UserLoginFailed, c)
+	}
+}
+
+// ResetPassword
+// PingExample kjb-doc
+// @Summary 管理员重置普通用户密码
+// @Schemes
+// @Description 管理员重置普通用户密码
+// @Tags 用户管理
+// @Param user body string true "username:管理员编号;usernameInit:被重置用户编号"
+// @Accept json
+// @Produce json
+// @Success 200 {string} string "ok"
+// @Router /v1/user/reset [post]
+func (u *UserService) ResetPassword(c *gin.Context) {
+	var param models.ResetPassword
+	err := c.ShouldBindJSON(&param)
+	if err != nil {
+		fmt.Printf("参数格式化异常:%s", err.Error())
+		response.Failed(errmsg.ParamInvalid, c)
+		return
+	}
+	if admin := u.FindUserByPhoneAndRole(param.Phone, constant.RoleAdmin); admin == nil {
+		fmt.Printf("管理员"+errmsg.UserNotExists+"%s", param.Phone)
+		response.Failed("管理员"+errmsg.UserNotExists+param.Phone, c)
+		return
+	}
+	if normalUser := u.FindUserByPhoneAndRole(param.PhoneInit, constant.RoleNormal); normalUser == nil {
+		fmt.Printf(errmsg.UserNotExists+"%s", param.PhoneInit)
+		response.Failed(errmsg.UserNotExists+param.PhoneInit, c)
+		return
+	}
+	result := u.RestPassword(param.PhoneInit)
+	if result == nil {
+		response.Failed(errmsg.UserPasswordResetError, c)
+		return
+	}
+	if result.Error != nil {
+		response.Failed(errmsg.UserPasswordResetError+result.Error.Error(), c)
+		return
+	}
+	if result.RowsAffected >= 0 {
+		response.Success(errmsg.UserPasswordResetSuccess, result.RowsAffected, c)
+		return
+	}
+}
+
+// UserList
+// PingExample kjb-doc
+// @Summary 用户列表查询
+// @Schemes
+// @Description 用户列表查询
+// @Tags 用户管理
+// @Param user body string true "role:角色;userName:用户名;pageNum:页数;pageSize:每页记录数"
+// @Accept json
+// @Produce json
+// @Success 200 {string} string "ok"
+// @Router /v1/user/find [post]
+func (u *UserService) UserList(c *gin.Context) {
+	var param models.UserListParam
+	err := c.ShouldBindJSON(&param)
+	if err != nil {
+		fmt.Printf(errmsg.ParamInvalid+"%s", err.Error())
+		response.Failed(errmsg.ParamInvalid, c)
+		return
+	}
+	userList, rows := u.GetUserList(param)
+	response.SuccessPage(errmsg.FindSuccess, userList, rows, c)
+}
+
+// ModePass
+// PingExample kjb-doc
+// @Summary 用户修改密码
+// @Schemes
+// @Description 用户修改密码
+// @Tags 用户管理
+// @Param user body string true "userName:用户名 password:密码 newPassword:新密码"
+// @Accept json
+// @Produce json
+// @Success 200 {string} string "ok"
+// @Router /v1/user/change/password [post]
+func (u *UserService) ModePass(c *gin.Context) {
+	var param models.ChangePassword
+	err := c.ShouldBindJSON(&param)
+	if err != nil {
+		response.Failed(errmsg.ParamInvalid, c)
+		return
+	}
+	user, count := u.FindUserByPhone(param.Phone)
+	if count <= 0 {
+		response.Failed(errmsg.UserNotExists, c)
+		return
+	}
+	if user.Password != param.Password {
+		response.Failed(errmsg.UserOldPasswordError, c)
+		return
+	}
+	count = u.ChangePassword(user.ID, param.NewPassword)
+	if count <= 0 {
+		response.Failed(errmsg.UserPasswordChangeError, c)
+		return
+	}
+	response.Success(errmsg.UserPasswordChangeSuccess, count, c)
+}

+ 3 - 10
config/application.yaml

@@ -22,13 +22,6 @@ jwt:
 cron:
   enable: true
 
-# 微信小程序配置
-code2Session:
-  appId: wx3cb7b0s2793390f63
-  appSecret: 86d8z38cac01ff375b2676d35f65e021
-
-# 问题反馈配置
-feedback:
-  qqSmtp: smtp.qq.com
-  qqEmail: 1933757688@qq.com
-  qqEmailSecret: jjstmwtidhwhxhzc
+#蓝牙网关地址
+gateway:
+  ip: 192.168.1.100

+ 7 - 0
constant/constant.go

@@ -0,0 +1,7 @@
+package constant
+
+const (
+	RoleAdmin       = 1
+	RoleNormal      = 0
+	DefaultPassword = "e10adc3949ba59abbe56e057f20f883e" //系统默认密码123456
+)

BIN
db/confrontation-training.db


+ 2 - 1
err/errMsg.go

@@ -1,4 +1,4 @@
-package errmsg
+package err
 
 const (
 	ParamInvalid              = "请求参数无效"
@@ -7,6 +7,7 @@ const (
 	UserRegisterSuccess       = "用户注册成功"
 	UserRegisterFailed        = "用户注册失败"
 	UserLoginSuccess          = "登录成功"
+	UserLoginFailed           = "用户名或密码错误"
 	UserNotExists             = "用户不存在"
 	UserPasswordResetError    = "用户密码重置失败"
 	UserPasswordResetSuccess  = "用户密码重置成功"

+ 4 - 1
initialize/gorm.go

@@ -5,13 +5,16 @@ import (
 	"fmt"
 	"gorm.io/driver/sqlite"
 	"gorm.io/gorm"
+	"gorm.io/gorm/schema"
 	"time"
 )
 
 func SQLite() {
 	sqliteConfig := global.Config.SQLite
 
-	db, err := gorm.Open(sqlite.Open(sqliteConfig.Url), &gorm.Config{})
+	db, err := gorm.Open(sqlite.Open(sqliteConfig.Url), &gorm.Config{
+		NamingStrategy: schema.NamingStrategy{SingularTable: false},
+	})
 	if err != nil {
 		fmt.Printf("mysql error :%s", err.Error())
 		return

+ 18 - 15
initialize/router.go

@@ -1,7 +1,10 @@
 package initialize
 
 import (
+	"confrontation-training/api"
+	"confrontation-training/global"
 	"confrontation-training/middleware"
+	"fmt"
 	"github.com/gin-gonic/gin"
 	"net/http"
 )
@@ -22,24 +25,24 @@ func Router() {
 	//engine.Static("/file", "./files")
 	//engine.GET("/ws", WsHandler)
 	//
-	////group
-	//v1 := engine.Group("/v1")
-	//user := v1.Group("/user")
-	//user.POST("/login", api.GetUser().UserLogin)
-	//
-	////user.Use(middleware.JwtAuth())
-	//user.POST("/register", api.GetUser().UseRegister)
-	//user.POST("/reset", api.GetUser().ResetPassword)
-	//user.POST("/find", api.GetUser().UserList)
-	//user.POST("/change/password", api.GetUser().ModePass)
+	//group
+	v1 := engine.Group("/v1")
+	user := v1.Group("/user")
+	user.POST("/login", api.GetUser().UserLogin)
+
+	user.Use(middleware.JwtAuth())
+	user.POST("/register", api.GetUser().UseRegister)
+	user.POST("/reset", api.GetUser().ResetPassword)
+	user.POST("/find", api.GetUser().UserList)
+	user.POST("/change/password", api.GetUser().ModePass)
 	//
 	//record := v1.Group("/record")
 	//record.POST("/create", api.GetRecord().SaveRecord)
 	//record.POST("/find", api.GetRecord().RecordList)
 	//record.GET("/:recordId/find", api.GetRecord().GetRecordById)
-	//err := engine.Run(fmt.Sprintf(":%s", global.Config.Server.Port))
-	//if err != nil {
-	//	fmt.Printf("%s", err.Error())
-	//	return
-	//}
+	err := engine.Run(fmt.Sprintf(":%s", global.Config.Server.Port))
+	if err != nil {
+		fmt.Printf("%s", err.Error())
+		return
+	}
 }

+ 9 - 6
models/user.go

@@ -1,29 +1,32 @@
 package models
 
+import "gorm.io/gorm"
+
 //User 用户数据映射模型
 type User struct {
+	gorm.Model
 	Password string `gorm:"password"`
 	UserName string `gorm:"user_name"`
+	Phone    string `gorm:"phone"`
 	Role     *uint8 `gorm:"role" `
-	Id       string `gorm:"id"`
 }
 
 type UserRegister struct {
-	Id       string `json:"id"`
+	Phone    string `json:"phone" binding:"required"`
 	UserName string `json:"userName" binding:"required"`
 	Password string `json:"password" binding:"required"`
 	Role     *uint8 `json:"role" binding:"required,gte=0" `
 }
 
 type UserLogin struct {
-	Username string `json:"username" binding:"required"`
+	Phone    string `json:"phone" binding:"required"`
 	Password string `json:"password" binding:"required"`
 	Role     *uint8 `json:"role" binding:"required,gte=0"`
 }
 
 type ResetPassword struct {
-	Username     string `json:"username" binding:"required"`
-	UsernameInit string `json:"usernameInit" binding:"required"`
+	Phone     string `json:"phone" binding:"required"`
+	PhoneInit string `json:"phoneInit" binding:"required"`
 }
 
 type UserListParam struct {
@@ -34,7 +37,7 @@ type UserListParam struct {
 }
 
 type ChangePassword struct {
-	UserName    string `json:"userName" binding:"required"`
+	Phone       string `json:"phone" binding:"required"`
 	Password    string `json:"password" binding:"required"`
 	NewPassword string `json:"newPassword" binding:"required"`
 }

+ 88 - 0
service/userService.go

@@ -0,0 +1,88 @@
+package service
+
+import (
+	"confrontation-training/common"
+	"confrontation-training/constant"
+	"confrontation-training/global"
+	"confrontation-training/models"
+	"gorm.io/gorm"
+)
+
+type UserService struct {
+}
+
+// FindUserByPhone  find by phone
+func (u *UserService) FindUserByPhone(phone string) (models.User, int64) {
+	var user models.User
+	count := global.Db.Where("phone = ?", phone).First(&user).RowsAffected
+	return user, count
+}
+
+// CreateUser create user
+func (u *UserService) CreateUser(user models.UserRegister) *gorm.DB {
+	userCreate := models.User{
+		UserName: user.UserName,
+		Password: user.Password,
+		Role:     user.Role,
+		Phone:    user.Phone,
+	}
+	return global.Db.Create(&userCreate)
+}
+
+func (u *UserService) Login(userLogin models.UserLogin) *models.User {
+	var user models.User
+	rows := global.Db.Where(" phone = ? and password = ? and role = ?", userLogin.Phone, userLogin.Password, userLogin.Role).First(&user).RowsAffected
+	if rows == 0 {
+		return nil
+	} else {
+		return &user
+	}
+}
+
+func (u *UserService) FindUserByPhoneAndRole(phone string, role uint8) *models.User {
+	var user models.User
+	rows := global.Db.Where(" phone = ? and role = ?", phone, role).First(&user).RowsAffected
+	if rows == 0 {
+		return nil
+	} else {
+		return &user
+	}
+}
+
+func (u *UserService) RestPassword(phone string) *gorm.DB {
+	user, count := u.FindUserByPhone(phone)
+	if count <= 0 {
+		return nil
+	}
+	userUpdate := models.User{
+		Password: constant.DefaultPassword,
+	}
+	userUpdate.ID = user.ID
+	result := global.Db.Updates(&userUpdate)
+	return result
+}
+
+func (u *UserService) GetUserList(param models.UserListParam) ([]models.User, int64) {
+	query := models.User{
+		Role: param.Role,
+	}
+	if len(param.UserName) > 0 {
+		query.UserName = param.UserName
+	}
+	page := models.Page{
+		PageNum:  param.PageNum,
+		PageSize: param.PageSize,
+	}
+	userList := make([]models.User, 0)
+	rows := common.RestPage(page, "user", query, "", &userList, &[]models.User{})
+	return userList, rows
+}
+
+func (u *UserService) ChangePassword(id uint, newPassword string) int64 {
+	userUpdate := models.User{
+		Password: newPassword,
+	}
+	userUpdate.ID = id
+	count := global.Db.Debug().Updates(&userUpdate).RowsAffected
+	return count
+}