-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
31 changed files
with
724 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package api |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package v1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package v1 | ||
|
||
import ( | ||
"anew-server/dto/request" | ||
"anew-server/dto/response" | ||
"anew-server/dto/service" | ||
"anew-server/utils" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// 创建用户 | ||
func CreateUser(c *gin.Context) { | ||
// 请求校验 | ||
var user request.CreateUserReq | ||
err := c.ShouldBindJSON(&user) | ||
if err != nil{ | ||
response.Resp(c, 400,user) | ||
return | ||
} | ||
code := service.CheckUser(user.Username) | ||
if code == response.SUCCSE { | ||
code = service.CreateUser(&user) | ||
response.Resp(c, code,user) | ||
} else { | ||
response.Resp(c, code,user) | ||
} | ||
} | ||
|
||
// 获取用户列表 | ||
func GetUsers(c *gin.Context) { | ||
pageSize := utils.Str2Int(c.Query("pagesize")) | ||
pageNum := utils.Str2Int(c.Query("pagenum")) | ||
users := service.GetUsers(pageSize, pageNum) | ||
// 转为Resp Struct, 隐藏部分字段 | ||
var respUsers []response.UserListResp | ||
utils.Struct2StructByJson(users, &respUsers) | ||
response.Resp(c, response.SUCCSE, respUsers) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package request | ||
|
||
// User login structure | ||
type RegisterAndLoginReq struct { | ||
Username string `json:"username"` | ||
Password string `json:"password"` | ||
} | ||
|
||
// 修改密码结构体 | ||
type ChangePwdReq struct { | ||
OldPassword string `json:"oldPassword" form:"oldPassword"` | ||
NewPassword string `json:"newPassword" form:"newPassword"` | ||
} | ||
|
||
// 创建用户结构体 | ||
type CreateUserReq struct { | ||
Username string `json:"username" validate:"required"` | ||
Password string `json:"password" validate:"required"` // 不使用SysUser的Password字段, 避免请求劫持绕过系统校验 | ||
Mobile string `json:"mobile" validate:"eq=11"` | ||
Avatar string `json:"avatar"` | ||
Name string `json:"name"` | ||
Email string `json:"mail"` | ||
Status *bool `json:"status"` | ||
RoleId uint `json:"roleId" validate:"required"` | ||
Creator string `json:"creator"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package response | ||
|
||
const ( | ||
SUCCSE = 200 | ||
FAILURE = 400 | ||
FORBIDDEN = 403 | ||
TOKEN_UNAUTHORIZED = 1000 | ||
ERROR_USERNAME_USED = 1001 | ||
ERROR_LOGIN_WRONG = 1002 | ||
) | ||
|
||
var Custom_Msg = map[int]string{ | ||
SUCCSE: "操作完成", | ||
FAILURE: "操作失败", | ||
FORBIDDEN: "无权操作", | ||
TOKEN_UNAUTHORIZED: "未经授权", | ||
ERROR_USERNAME_USED: "用户已存在", | ||
ERROR_LOGIN_WRONG: "用户名或密码错误", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package response | ||
|
||
import "github.com/gin-gonic/gin" | ||
|
||
func Resp(c *gin.Context, code int, resp interface{}) { | ||
c.JSON(SUCCSE, gin.H{ | ||
"status": code, | ||
"data": resp, | ||
"msg": Custom_Msg[code], | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package response | ||
|
||
import "anew-server/models" | ||
|
||
// User login response structure | ||
type LoginResp struct { | ||
Username string `json:"username"` // 登录用户名 | ||
Token string `json:"token"` // jwt令牌 | ||
ExpiresAt int64 `json:"expiresAt"` // 过期时间, 秒 | ||
} | ||
|
||
// 用户信息响应 | ||
type UserInfoResp struct { | ||
Id uint `json:"id"` | ||
Username string `json:"username"` | ||
Mobile string `json:"mobile"` | ||
Avatar string `json:"avatar"` | ||
Name string `json:"name"` | ||
Email string `json:"mail"` | ||
Roles []string `json:"roles"` | ||
Permissions []string `json:"permissions"` | ||
} | ||
|
||
// 用户列表信息响应, 字段含义见models.SysUser | ||
type UserListResp struct { | ||
Id uint `json:"id"` | ||
Username string `json:"username"` | ||
Mobile string `json:"mobile"` | ||
Avatar string `json:"avatar"` | ||
Name string `json:"name"` | ||
Email string `json:"mail"` | ||
Status *bool `json:"status"` | ||
RoleId uint `json:"roleId"` | ||
Creator string `json:"creator"` | ||
CreatedAt models.LocalTime `json:"createdAt"` | ||
UpdatedAt models.LocalTime `json:"updatedAt"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package service | ||
|
||
import ( | ||
"anew-server/common" | ||
"anew-server/dto/request" | ||
"anew-server/dto/response" | ||
"anew-server/models" | ||
"anew-server/utils" | ||
) | ||
|
||
// 用户是否存在 | ||
func CheckUser(username string) int { | ||
var users models.SysUser | ||
common.Mysql.Select("id").Where("username = ?", username).First(&users) | ||
if users.Id > 0 { | ||
return response.ERROR_USERNAME_USED | ||
} | ||
return response.SUCCSE | ||
} | ||
|
||
// 创建用户 | ||
func CreateUser(req *request.CreateUserReq) int { | ||
var user models.SysUser | ||
utils.Struct2StructByJson(req, &user) | ||
// 将密码转为密文 | ||
user.Password = utils.GenPwd(req.Password) | ||
err := common.Mysql.Create(&user) | ||
if err != nil { | ||
return response.FAILURE | ||
} | ||
return response.SUCCSE | ||
} | ||
|
||
// 用户列表 | ||
func GetUsers(pageSize int, pageNum int) []models.SysUser { | ||
var users []models.SysUser | ||
err := common.Mysql.Limit(pageSize).Offset((pageNum - 1) * pageSize).Find(&users).Error | ||
if err != nil { | ||
return nil | ||
} | ||
return users | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.