初始化v1
This commit is contained in:
295
internal/controller/admin/user.go
Normal file
295
internal/controller/admin/user.go
Normal file
@@ -0,0 +1,295 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"nl-video-api/internal/dao"
|
||||
"nl-video-api/internal/service/user"
|
||||
"nl-video-api/utility/crypto"
|
||||
"nl-video-api/utility/response"
|
||||
)
|
||||
|
||||
var User = cAdminUser{}
|
||||
|
||||
type cAdminUser struct{}
|
||||
|
||||
// AdminUserCreateReq 管理员创建用户请求
|
||||
type AdminUserCreateReq struct {
|
||||
Username string `json:"username" v:"required|length:3,20#用户名不能为空|用户名长度为3-20位"`
|
||||
Password string `json:"password" v:"required|length:6,20#密码不能为空|密码长度为6-20位"`
|
||||
Email string `json:"email" v:"email#邮箱格式不正确"`
|
||||
Phone string `json:"phone" v:"phone#手机号格式不正确"`
|
||||
Status int `json:"status" v:"in:0,1#状态值不正确"`
|
||||
}
|
||||
|
||||
// AdminUserUpdateReq 管理员更新用户请求
|
||||
type AdminUserUpdateReq struct {
|
||||
Id uint `json:"id" v:"required|min:1#用户ID不能为空"`
|
||||
Username string `json:"username" v:"length:3,20#用户名长度为3-20位"`
|
||||
Email string `json:"email" v:"email#邮箱格式不正确"`
|
||||
Phone string `json:"phone" v:"phone#手机号格式不正确"`
|
||||
Status int `json:"status" v:"in:0,1#状态值不正确"`
|
||||
}
|
||||
|
||||
// AdminUserDetailReq 管理员获取用户详情请求
|
||||
type AdminUserDetailReq struct {
|
||||
Id uint `json:"id" v:"required|min:1#用户ID不能为空"`
|
||||
}
|
||||
|
||||
// AdminUserListReq 管理员获取用户列表请求
|
||||
type AdminUserListReq struct {
|
||||
Page int `json:"page" v:"min:1#页码不能小于1"`
|
||||
Size int `json:"size" v:"min:1|max:100#每页数量不能小于1且不能大于100"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Status int `json:"status" v:"in:-1,0,1#状态值不正确"`
|
||||
}
|
||||
|
||||
// AdminUserDeleteReq 管理员删除用户请求
|
||||
type AdminUserDeleteReq struct {
|
||||
Id uint `json:"id" v:"required|min:1#用户ID不能为空"`
|
||||
}
|
||||
|
||||
// Create 管理员创建用户
|
||||
func (c *cAdminUser) Create(r *ghttp.Request) {
|
||||
var req *AdminUserCreateReq
|
||||
if err := r.Parse(&req); err != nil {
|
||||
response.Error(r, 1001, "参数解析失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 构造用户创建请求
|
||||
createReq := &user.UserCreateReq{
|
||||
Username: req.Username,
|
||||
Password: req.Password,
|
||||
Email: req.Email,
|
||||
Phone: req.Phone,
|
||||
}
|
||||
|
||||
// 调用用户服务创建用户
|
||||
_, err := user.User.Create(r.Context(), createReq)
|
||||
if err != nil {
|
||||
response.Error(r, 1002, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(r, g.Map{
|
||||
"message": "创建成功",
|
||||
})
|
||||
}
|
||||
|
||||
// Update 管理员更新用户
|
||||
func (c *cAdminUser) Update(r *ghttp.Request) {
|
||||
var req *AdminUserUpdateReq
|
||||
if err := r.Parse(&req); err != nil {
|
||||
response.Error(r, 1001, "参数解析失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 从URL路径获取ID
|
||||
if req.Id == 0 {
|
||||
idStr := r.Get("id").String()
|
||||
if id, err := strconv.Atoi(idStr); err == nil {
|
||||
req.Id = uint(id)
|
||||
}
|
||||
}
|
||||
|
||||
if req.Id == 0 {
|
||||
response.Error(r, 1001, "用户ID不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 构造用户更新请求
|
||||
updateReq := &user.UserUpdateReq{
|
||||
Id: int(req.Id),
|
||||
Username: req.Username,
|
||||
Email: req.Email,
|
||||
Phone: req.Phone,
|
||||
Status: req.Status,
|
||||
}
|
||||
|
||||
// 调用用户服务更新用户
|
||||
err := user.User.Update(r.Context(), updateReq)
|
||||
if err != nil {
|
||||
response.Error(r, 1002, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(r, g.Map{
|
||||
"message": "更新成功",
|
||||
})
|
||||
}
|
||||
|
||||
// GetDetail 管理员获取用户详情
|
||||
func (c *cAdminUser) GetDetail(r *ghttp.Request) {
|
||||
var req *AdminUserDetailReq
|
||||
if err := r.Parse(&req); err != nil {
|
||||
response.Error(r, 1001, "参数解析失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 从URL路径获取ID
|
||||
if req.Id == 0 {
|
||||
idStr := r.Get("id").String()
|
||||
if id, err := strconv.Atoi(idStr); err == nil {
|
||||
req.Id = uint(id)
|
||||
}
|
||||
}
|
||||
|
||||
if req.Id == 0 {
|
||||
response.Error(r, 1001, "用户ID不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 调用用户服务获取用户详情
|
||||
result, err := user.User.GetById(r.Context(), int(req.Id))
|
||||
if err != nil {
|
||||
response.Error(r, 1002, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(r, result)
|
||||
}
|
||||
|
||||
// GetList 管理员获取用户列表
|
||||
func (c *cAdminUser) GetList(r *ghttp.Request) {
|
||||
var req *AdminUserListReq
|
||||
if err := r.Parse(&req); err != nil {
|
||||
response.Error(r, 1001, "参数解析失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 设置默认分页参数
|
||||
if req.Page <= 0 {
|
||||
req.Page = 1
|
||||
}
|
||||
if req.Size <= 0 {
|
||||
req.Size = 10
|
||||
}
|
||||
|
||||
// 构造用户列表请求
|
||||
listReq := &dao.UserListReq{
|
||||
Page: req.Page,
|
||||
Username: req.Username,
|
||||
Email: req.Email,
|
||||
Status: req.Status,
|
||||
}
|
||||
|
||||
// 调用用户服务获取用户列表
|
||||
result, total, err := user.User.GetList(r.Context(), listReq)
|
||||
if err != nil {
|
||||
response.Error(r, 1002, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 构造响应数据
|
||||
responseData := g.Map{
|
||||
"list": result,
|
||||
"total": total,
|
||||
"page": req.Page,
|
||||
"size": req.Size,
|
||||
}
|
||||
|
||||
response.Success(r, responseData)
|
||||
}
|
||||
|
||||
// Delete 管理员删除用户
|
||||
func (c *cAdminUser) Delete(r *ghttp.Request) {
|
||||
var req *AdminUserDeleteReq
|
||||
if err := r.Parse(&req); err != nil {
|
||||
response.Error(r, 1001, "参数解析失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 从URL路径获取ID
|
||||
if req.Id == 0 {
|
||||
idStr := r.Get("id").String()
|
||||
if id, err := strconv.Atoi(idStr); err == nil {
|
||||
req.Id = uint(id)
|
||||
}
|
||||
}
|
||||
|
||||
if req.Id == 0 {
|
||||
response.Error(r, 1001, "用户ID不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 调用用户服务删除用户
|
||||
err := user.User.Delete(r.Context(), int(req.Id))
|
||||
if err != nil {
|
||||
response.Error(r, 1002, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(r, g.Map{
|
||||
"message": "删除成功",
|
||||
})
|
||||
}
|
||||
|
||||
// UpdateStatus 管理员更新用户状态
|
||||
func (c *cAdminUser) UpdateStatus(r *ghttp.Request) {
|
||||
id := r.Get("id").Uint()
|
||||
status := r.Get("status").Int()
|
||||
|
||||
if id == 0 {
|
||||
response.Error(r, 1001, "用户ID不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 构造用户更新请求
|
||||
updateReq := &user.UserUpdateReq{
|
||||
Id: int(id),
|
||||
Status: status,
|
||||
}
|
||||
|
||||
// 调用用户服务更新用户
|
||||
err := user.User.Update(r.Context(), updateReq)
|
||||
if err != nil {
|
||||
response.Error(r, 1002, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(r, g.Map{
|
||||
"message": "状态更新成功",
|
||||
})
|
||||
}
|
||||
|
||||
// ResetPassword 管理员重置用户密码
|
||||
func (c *cAdminUser) ResetPassword(r *ghttp.Request) {
|
||||
id := r.Get("id").Uint()
|
||||
newPassword := r.Get("password").String()
|
||||
|
||||
if id == 0 {
|
||||
response.Error(r, 1001, "用户ID不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
if newPassword == "" {
|
||||
response.Error(r, 1001, "新密码不能为空")
|
||||
return
|
||||
}
|
||||
|
||||
// 直接更新密码,管理员重置不需要原密码验证
|
||||
hashedPassword, err := crypto.HashPassword(newPassword)
|
||||
if err != nil {
|
||||
response.Error(r, 1002, "密码加密失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
// 更新用户密码
|
||||
_, err = g.DB().Model("nl_user").Ctx(r.Context()).Where("id", id).Data(g.Map{
|
||||
"password": hashedPassword,
|
||||
"updated_at": gtime.Now(),
|
||||
}).Update()
|
||||
if err != nil {
|
||||
response.Error(r, 1002, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
response.Success(r, g.Map{
|
||||
"message": "密码重置成功",
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user