|
@@ -1,90 +1,50 @@
|
|
|
package v1
|
|
|
|
|
|
import (
|
|
|
- "fmt"
|
|
|
"gin-vue-admin/global"
|
|
|
- "gin-vue-admin/global/response"
|
|
|
"gin-vue-admin/middleware"
|
|
|
"gin-vue-admin/model"
|
|
|
"gin-vue-admin/model/request"
|
|
|
- resp "gin-vue-admin/model/response"
|
|
|
+ "gin-vue-admin/model/response"
|
|
|
"gin-vue-admin/service"
|
|
|
"gin-vue-admin/utils"
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
"github.com/go-redis/redis"
|
|
|
- "mime/multipart"
|
|
|
+ "go.uber.org/zap"
|
|
|
"time"
|
|
|
)
|
|
|
|
|
|
-// @Tags Base
|
|
|
-// @Summary 用户注册账号
|
|
|
-// @Produce application/json
|
|
|
-// @Param data body model.SysUser true "用户注册接口"
|
|
|
-// @Success 200 {string} string "{"success":true,"data":{},"msg":"注册成功"}"
|
|
|
-// @Router /user/register [post]
|
|
|
-func Register(c *gin.Context) {
|
|
|
- var R request.RegisterStruct
|
|
|
- _ = c.ShouldBindJSON(&R)
|
|
|
- UserVerify := utils.Rules{
|
|
|
- "Username": {utils.NotEmpty()},
|
|
|
- "NickName": {utils.NotEmpty()},
|
|
|
- "Password": {utils.NotEmpty()},
|
|
|
- "AuthorityId": {utils.NotEmpty()},
|
|
|
- }
|
|
|
- UserVerifyErr := utils.Verify(R, UserVerify)
|
|
|
- if UserVerifyErr != nil {
|
|
|
- response.FailWithMessage(UserVerifyErr.Error(), c)
|
|
|
- return
|
|
|
- }
|
|
|
- user := &model.SysUser{Username: R.Username, NickName: R.NickName, Password: R.Password, HeaderImg: R.HeaderImg, AuthorityId: R.AuthorityId}
|
|
|
- err, userReturn := service.Register(*user)
|
|
|
- if err != nil {
|
|
|
- response.FailWithDetailed(response.ERROR, resp.SysUserResponse{User: userReturn}, fmt.Sprintf("%v", err), c)
|
|
|
- } else {
|
|
|
- response.OkDetailed(resp.SysUserResponse{User: userReturn}, "注册成功", c)
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
// @Tags Base
|
|
|
// @Summary 用户登录
|
|
|
// @Produce application/json
|
|
|
-// @Param data body request.RegisterAndLoginStruct true "用户登录接口"
|
|
|
+// @Param data body request.Login true "用户名, 密码, 验证码"
|
|
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"登陆成功"}"
|
|
|
// @Router /base/login [post]
|
|
|
func Login(c *gin.Context) {
|
|
|
- var L request.RegisterAndLoginStruct
|
|
|
+ var L request.Login
|
|
|
_ = c.ShouldBindJSON(&L)
|
|
|
- UserVerify := utils.Rules{
|
|
|
- "CaptchaId": {utils.NotEmpty()},
|
|
|
- "Captcha": {utils.NotEmpty()},
|
|
|
- "Username": {utils.NotEmpty()},
|
|
|
- "Password": {utils.NotEmpty()},
|
|
|
- }
|
|
|
- UserVerifyErr := utils.Verify(L, UserVerify)
|
|
|
- if UserVerifyErr != nil {
|
|
|
- response.FailWithMessage(UserVerifyErr.Error(), c)
|
|
|
+ if err := utils.Verify(L, utils.LoginVerify); err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
return
|
|
|
}
|
|
|
if store.Verify(L.CaptchaId, L.Captcha, true) {
|
|
|
U := &model.SysUser{Username: L.Username, Password: L.Password}
|
|
|
if err, user := service.Login(U); err != nil {
|
|
|
- response.FailWithMessage(fmt.Sprintf("用户名密码错误或%v", err), c)
|
|
|
+ global.GVA_LOG.Error("登陆失败! 用户名不存在或者密码错误", zap.Any("err", err))
|
|
|
+ response.FailWithMessage("用户名不存在或者密码错误", c)
|
|
|
} else {
|
|
|
tokenNext(c, *user)
|
|
|
}
|
|
|
} else {
|
|
|
response.FailWithMessage("验证码错误", c)
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
|
|
|
// 登录以后签发jwt
|
|
|
func tokenNext(c *gin.Context, user model.SysUser) {
|
|
|
- j := &middleware.JWT{
|
|
|
- SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey), // 唯一签名
|
|
|
- }
|
|
|
- clams := request.CustomClaims{
|
|
|
+ j := &middleware.JWT{SigningKey: []byte(global.GVA_CONFIG.JWT.SigningKey)} // 唯一签名
|
|
|
+ claims := request.CustomClaims{
|
|
|
UUID: user.UUID,
|
|
|
ID: user.ID,
|
|
|
NickName: user.NickName,
|
|
@@ -97,32 +57,34 @@ func tokenNext(c *gin.Context, user model.SysUser) {
|
|
|
Issuer: "qmPlus", // 签名的发行者
|
|
|
},
|
|
|
}
|
|
|
- token, err := j.CreateToken(clams)
|
|
|
+ token, err := j.CreateToken(claims)
|
|
|
if err != nil {
|
|
|
+ global.GVA_LOG.Error("获取token失败", zap.Any("err", err))
|
|
|
response.FailWithMessage("获取token失败", c)
|
|
|
return
|
|
|
}
|
|
|
if !global.GVA_CONFIG.System.UseMultipoint {
|
|
|
- response.OkWithData(resp.LoginResponse{
|
|
|
+ response.OkWithDetailed(response.LoginResponse{
|
|
|
User: user,
|
|
|
Token: token,
|
|
|
- ExpiresAt: clams.StandardClaims.ExpiresAt * 1000,
|
|
|
- }, c)
|
|
|
+ ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
|
|
|
+ }, "登录成功", c)
|
|
|
return
|
|
|
}
|
|
|
- err, jwtStr := service.GetRedisJWT(user.Username)
|
|
|
- if err == redis.Nil {
|
|
|
+ if err, jwtStr := service.GetRedisJWT(user.Username); err == redis.Nil {
|
|
|
if err := service.SetRedisJWT(token, user.Username); err != nil {
|
|
|
+ global.GVA_LOG.Error("设置登录状态失败", zap.Any("err", err))
|
|
|
response.FailWithMessage("设置登录状态失败", c)
|
|
|
return
|
|
|
}
|
|
|
- response.OkWithData(resp.LoginResponse{
|
|
|
+ response.OkWithDetailed(response.LoginResponse{
|
|
|
User: user,
|
|
|
Token: token,
|
|
|
- ExpiresAt: clams.StandardClaims.ExpiresAt * 1000,
|
|
|
- }, c)
|
|
|
+ ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
|
|
|
+ }, "登录成功", c)
|
|
|
} else if err != nil {
|
|
|
- response.FailWithMessage(fmt.Sprintf("%v", err), c)
|
|
|
+ global.GVA_LOG.Error("设置登录状态失败", zap.Any("err", err))
|
|
|
+ response.FailWithMessage("设置登录状态失败", c)
|
|
|
} else {
|
|
|
var blackJWT model.JwtBlacklist
|
|
|
blackJWT.Jwt = jwtStr
|
|
@@ -134,11 +96,34 @@ func tokenNext(c *gin.Context, user model.SysUser) {
|
|
|
response.FailWithMessage("设置登录状态失败", c)
|
|
|
return
|
|
|
}
|
|
|
- response.OkWithData(resp.LoginResponse{
|
|
|
+ response.OkWithDetailed(response.LoginResponse{
|
|
|
User: user,
|
|
|
Token: token,
|
|
|
- ExpiresAt: clams.StandardClaims.ExpiresAt * 1000,
|
|
|
- }, c)
|
|
|
+ ExpiresAt: claims.StandardClaims.ExpiresAt * 1000,
|
|
|
+ }, "登录成功", c)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// @Tags SysUser
|
|
|
+// @Summary 用户注册账号
|
|
|
+// @Produce application/json
|
|
|
+// @Param data body model.SysUser true "用户名, 昵称, 密码, 角色ID"
|
|
|
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"注册成功"}"
|
|
|
+// @Router /user/register [post]
|
|
|
+func Register(c *gin.Context) {
|
|
|
+ var R request.Register
|
|
|
+ _ = c.ShouldBindJSON(&R)
|
|
|
+ if err := utils.Verify(R, utils.RegisterVerify); err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ user := &model.SysUser{Username: R.Username, NickName: R.NickName, Password: R.Password, HeaderImg: R.HeaderImg, AuthorityId: R.AuthorityId}
|
|
|
+ err, userReturn := service.Register(*user)
|
|
|
+ if err != nil {
|
|
|
+ global.GVA_LOG.Error("注册失败", zap.Any("err", err))
|
|
|
+ response.FailWithDetailed(response.SysUserResponse{User: userReturn}, "注册失败", c)
|
|
|
+ } else {
|
|
|
+ response.OkWithDetailed(response.SysUserResponse{User: userReturn}, "注册成功", c)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -146,60 +131,50 @@ func tokenNext(c *gin.Context, user model.SysUser) {
|
|
|
// @Summary 用户修改密码
|
|
|
// @Security ApiKeyAuth
|
|
|
// @Produce application/json
|
|
|
-// @Param data body request.ChangePasswordStruct true "用户修改密码"
|
|
|
+// @Param data body request.ChangePasswordStruct true "用户名, 原密码, 新密码"
|
|
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
|
|
|
// @Router /user/changePassword [put]
|
|
|
func ChangePassword(c *gin.Context) {
|
|
|
- var params request.ChangePasswordStruct
|
|
|
- _ = c.ShouldBindJSON(¶ms)
|
|
|
- UserVerify := utils.Rules{
|
|
|
- "Username": {utils.NotEmpty()},
|
|
|
- "Password": {utils.NotEmpty()},
|
|
|
- "NewPassword": {utils.NotEmpty()},
|
|
|
- }
|
|
|
- UserVerifyErr := utils.Verify(params, UserVerify)
|
|
|
- if UserVerifyErr != nil {
|
|
|
- response.FailWithMessage(UserVerifyErr.Error(), c)
|
|
|
+ var user request.ChangePasswordStruct
|
|
|
+ _ = c.ShouldBindJSON(&user)
|
|
|
+ if err := utils.Verify(user, utils.ChangePasswordVerify); err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
return
|
|
|
}
|
|
|
- U := &model.SysUser{Username: params.Username, Password: params.Password}
|
|
|
- if err, _ := service.ChangePassword(U, params.NewPassword); err != nil {
|
|
|
- response.FailWithMessage("修改失败,请检查用户名密码", c)
|
|
|
+ U := &model.SysUser{Username: user.Username, Password: user.Password}
|
|
|
+ if err, _ := service.ChangePassword(U, user.NewPassword); err != nil {
|
|
|
+ global.GVA_LOG.Error("修改失败", zap.Any("err", err))
|
|
|
+ response.FailWithMessage("修改失败,原密码与当前账户不符", c)
|
|
|
} else {
|
|
|
response.OkWithMessage("修改成功", c)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-type UserHeaderImg struct {
|
|
|
- HeaderImg multipart.File `json:"headerImg"`
|
|
|
-}
|
|
|
-
|
|
|
// @Tags SysUser
|
|
|
// @Summary 分页获取用户列表
|
|
|
// @Security ApiKeyAuth
|
|
|
// @accept application/json
|
|
|
// @Produce application/json
|
|
|
-// @Param data body request.PageInfo true "分页获取用户列表"
|
|
|
+// @Param data body request.PageInfo true "页码, 每页大小"
|
|
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
|
|
|
// @Router /user/getUserList [post]
|
|
|
func GetUserList(c *gin.Context) {
|
|
|
var pageInfo request.PageInfo
|
|
|
_ = c.ShouldBindJSON(&pageInfo)
|
|
|
- PageVerifyErr := utils.Verify(pageInfo, utils.CustomizeMap["PageVerify"])
|
|
|
- if PageVerifyErr != nil {
|
|
|
- response.FailWithMessage(PageVerifyErr.Error(), c)
|
|
|
+ if err := utils.Verify(pageInfo, utils.PageInfoVerify); err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
return
|
|
|
}
|
|
|
- err, list, total := service.GetUserInfoList(pageInfo)
|
|
|
- if err != nil {
|
|
|
- response.FailWithMessage(fmt.Sprintf("获取数据失败,%v", err), c)
|
|
|
+ if err, list, total := service.GetUserInfoList(pageInfo); err != nil {
|
|
|
+ global.GVA_LOG.Error("获取失败", zap.Any("err", err))
|
|
|
+ response.FailWithMessage("获取失败", c)
|
|
|
} else {
|
|
|
- response.OkWithData(resp.PageResult{
|
|
|
+ response.OkWithDetailed(response.PageResult{
|
|
|
List: list,
|
|
|
Total: total,
|
|
|
Page: pageInfo.Page,
|
|
|
PageSize: pageInfo.PageSize,
|
|
|
- }, c)
|
|
|
+ }, "获取成功", c)
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -208,24 +183,19 @@ func GetUserList(c *gin.Context) {
|
|
|
// @Security ApiKeyAuth
|
|
|
// @accept application/json
|
|
|
// @Produce application/json
|
|
|
-// @Param data body request.SetUserAuth true "设置用户权限"
|
|
|
+// @Param data body request.SetUserAuth true "用户UUID, 角色ID"
|
|
|
// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
|
|
|
// @Router /user/setUserAuthority [post]
|
|
|
func SetUserAuthority(c *gin.Context) {
|
|
|
var sua request.SetUserAuth
|
|
|
_ = c.ShouldBindJSON(&sua)
|
|
|
- UserVerify := utils.Rules{
|
|
|
- "UUID": {utils.NotEmpty()},
|
|
|
- "AuthorityId": {utils.NotEmpty()},
|
|
|
- }
|
|
|
- UserVerifyErr := utils.Verify(sua, UserVerify)
|
|
|
- if UserVerifyErr != nil {
|
|
|
+ if UserVerifyErr := utils.Verify(sua, utils.SetUserAuthorityVerify); UserVerifyErr != nil {
|
|
|
response.FailWithMessage(UserVerifyErr.Error(), c)
|
|
|
return
|
|
|
}
|
|
|
- err := service.SetUserAuthority(sua.UUID, sua.AuthorityId)
|
|
|
- if err != nil {
|
|
|
- response.FailWithMessage(fmt.Sprintf("修改失败,%v", err), c)
|
|
|
+ if err := service.SetUserAuthority(sua.UUID, sua.AuthorityId); err != nil {
|
|
|
+ global.GVA_LOG.Error("修改失败", zap.Any("err", err))
|
|
|
+ response.FailWithMessage("修改失败", c)
|
|
|
} else {
|
|
|
response.OkWithMessage("修改成功", c)
|
|
|
}
|
|
@@ -236,42 +206,81 @@ func SetUserAuthority(c *gin.Context) {
|
|
|
// @Security ApiKeyAuth
|
|
|
// @accept application/json
|
|
|
// @Produce application/json
|
|
|
-// @Param data body request.GetById true "删除用户"
|
|
|
-// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
|
|
|
+// @Param data body request.GetById true "用户ID"
|
|
|
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
|
|
|
// @Router /user/deleteUser [delete]
|
|
|
func DeleteUser(c *gin.Context) {
|
|
|
var reqId request.GetById
|
|
|
_ = c.ShouldBindJSON(&reqId)
|
|
|
- IdVerifyErr := utils.Verify(reqId, utils.CustomizeMap["IdVerify"])
|
|
|
- if IdVerifyErr != nil {
|
|
|
- response.FailWithMessage(IdVerifyErr.Error(), c)
|
|
|
+ if err := utils.Verify(reqId, utils.IdVerify); err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
return
|
|
|
}
|
|
|
- err := service.DeleteUser(reqId.Id)
|
|
|
- if err != nil {
|
|
|
- response.FailWithMessage(fmt.Sprintf("删除失败,%v", err), c)
|
|
|
+ jwtId := getUserID(c)
|
|
|
+ if jwtId == uint(reqId.Id) {
|
|
|
+ response.FailWithMessage("删除失败, 自杀失败", c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if err := service.DeleteUser(reqId.Id); err != nil {
|
|
|
+ global.GVA_LOG.Error("删除失败!", zap.Any("err", err))
|
|
|
+ response.FailWithMessage("删除失败", c)
|
|
|
} else {
|
|
|
response.OkWithMessage("删除成功", c)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// @Tags SysUser
|
|
|
-// @Summary 删除用户
|
|
|
+// @Summary 设置用户信息
|
|
|
// @Security ApiKeyAuth
|
|
|
// @accept application/json
|
|
|
// @Produce application/json
|
|
|
-// @Param data body model.SysUser true "删除用户"
|
|
|
-// @Success 200 {string} string "{"success":true,"data":{},"msg":"修改成功"}"
|
|
|
+// @Param data body model.SysUser true "ID, 用户名, 昵称, 头像链接"
|
|
|
+// @Success 200 {string} string "{"success":true,"data":{},"msg":"设置成功"}"
|
|
|
// @Router /user/setUserInfo [put]
|
|
|
func SetUserInfo(c *gin.Context) {
|
|
|
var user model.SysUser
|
|
|
- c.ShouldBindJSON(&user)
|
|
|
- err, ReqUser := service.SetUserInfo(user)
|
|
|
- if err != nil {
|
|
|
- response.FailWithMessage(fmt.Sprintf("更新失败,%v", err), c)
|
|
|
+ _ = c.ShouldBindJSON(&user)
|
|
|
+ if err := utils.Verify(user, utils.SetUserVerify); err != nil {
|
|
|
+ response.FailWithMessage(err.Error(), c)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if err, ReqUser := service.SetUserInfo(user); err != nil {
|
|
|
+ global.GVA_LOG.Error("设置失败", zap.Any("err", err))
|
|
|
+ response.FailWithMessage("设置失败", c)
|
|
|
+ } else {
|
|
|
+ response.OkWithDetailed(gin.H{"userInfo": ReqUser}, "设置成功", c)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 从Gin的Context中获取从jwt解析出来的用户ID
|
|
|
+func getUserID(c *gin.Context) uint {
|
|
|
+ if claims, exists := c.Get("claims"); !exists {
|
|
|
+ global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户ID失败, 请检查路由是否使用jwt中间件")
|
|
|
+ return 0
|
|
|
+ } else {
|
|
|
+ waitUse := claims.(*request.CustomClaims)
|
|
|
+ return waitUse.ID
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 从Gin的Context中获取从jwt解析出来的用户UUID
|
|
|
+func getUserUuid(c *gin.Context) string {
|
|
|
+ if claims, exists := c.Get("claims"); !exists {
|
|
|
+ global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件")
|
|
|
+ return ""
|
|
|
+ } else {
|
|
|
+ waitUse := claims.(*request.CustomClaims)
|
|
|
+ return waitUse.UUID.String()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 从Gin的Context中获取从jwt解析出来的用户角色id
|
|
|
+func getUserAuthorityId(c *gin.Context) string {
|
|
|
+ if claims, exists := c.Get("claims"); !exists {
|
|
|
+ global.GVA_LOG.Error("从Gin的Context中获取从jwt解析出来的用户UUID失败, 请检查路由是否使用jwt中间件")
|
|
|
+ return ""
|
|
|
} else {
|
|
|
- response.OkWithData(gin.H{
|
|
|
- "userInfo": ReqUser,
|
|
|
- }, c)
|
|
|
+ waitUse := claims.(*request.CustomClaims)
|
|
|
+ return waitUse.AuthorityId
|
|
|
}
|
|
|
}
|