go-platform/controllers/platform_admin_user.go
2026-04-01 15:15:21 +08:00

303 lines
7.9 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package controllers
import (
"encoding/json"
"io"
"strconv"
"strings"
"server/models"
beego "github.com/beego/beego/v2/server/web"
)
// PlatformAdminUserController 平台管理员用户管理yz_system_admin_user
type PlatformAdminUserController struct {
beego.Controller
}
type adminUserDTO struct {
ID uint64 `json:"id"`
Account string `json:"account"`
Name *string `json:"name"`
Phone *string `json:"phone"`
Email *string `json:"email"`
Qq *string `json:"qq"`
Sex uint8 `json:"sex"`
Avatar *string `json:"avatar"`
Rid uint64 `json:"rid"`
LoginCount uint64 `json:"login_count"`
LastLoginIP *string `json:"last_login_ip"`
Status uint8 `json:"status"`
CreateTime string `json:"create_time"`
UpdateTime *string `json:"update_time"`
}
func toAdminUserDTO(u models.AdminUser) adminUserDTO {
var updateTime *string
if u.UpdateTime != nil {
s := u.UpdateTime.Format("2006-01-02 15:04:05")
updateTime = &s
}
return adminUserDTO{
ID: u.ID,
Account: u.Account,
Name: u.Name,
Phone: u.Phone,
Email: u.Email,
Qq: u.Qq,
Sex: u.Sex,
Avatar: u.Avatar,
Rid: u.RoleID,
LoginCount: u.LoginCount,
LastLoginIP: u.LastLoginIP,
Status: u.Status,
CreateTime: u.CreateTime.Format("2006-01-02 15:04:05"),
UpdateTime: updateTime,
}
}
// GetAllUsers 获取全部平台管理员用户
// GET /platform/getAllUsers
func (c *PlatformAdminUserController) GetAllUsers() {
rows, total, err := models.ListAdminUsers()
if err != nil {
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "查询失败"}
_ = c.ServeJSON()
return
}
list := make([]adminUserDTO, 0, len(rows))
for _, u := range rows {
list = append(list, toAdminUserDTO(u))
}
c.Data["json"] = map[string]interface{}{
"code": 200,
"msg": "success",
"data": map[string]interface{}{"list": list, "total": total},
}
_ = c.ServeJSON()
}
// GetUserInfo 获取用户详情
// GET /platform/getUserInfo/:id
func (c *PlatformAdminUserController) GetUserInfo() {
idStr := c.Ctx.Input.Param(":id")
id, _ := strconv.ParseUint(idStr, 10, 64)
if id == 0 {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "id 不能为空"}
_ = c.ServeJSON()
return
}
u, err := models.GetAdminUserByID(id)
if err != nil {
c.Data["json"] = map[string]interface{}{"code": 404, "msg": "用户不存在"}
_ = c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"code": 200,
"msg": "success",
"data": toAdminUserDTO(*u),
}
_ = c.ServeJSON()
}
type adminAddUserPayload struct {
Account string `json:"account"`
Password string `json:"password"`
Name *string `json:"name"`
Phone *string `json:"phone"`
Email *string `json:"email"`
Qq *string `json:"qq"`
Sex *uint8 `json:"sex"`
Avatar *string `json:"avatar"`
Rid *uint64 `json:"rid"`
Status *uint8 `json:"status"`
}
// AddUser 添加平台管理员用户(仅写 yz_system_admin_user不处理 tid
// POST /platform/addUser
func (c *PlatformAdminUserController) AddUser() {
var p adminAddUserPayload
raw, _ := io.ReadAll(c.Ctx.Request.Body)
if err := json.Unmarshal(raw, &p); err != nil {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
_ = c.ServeJSON()
return
}
p.Account = strings.TrimSpace(p.Account)
p.Password = strings.TrimSpace(p.Password)
if p.Account == "" {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "account 不能为空"}
_ = c.ServeJSON()
return
}
if p.Password == "" {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "password 不能为空"}
_ = c.ServeJSON()
return
}
status := uint8(1)
if p.Status != nil {
status = *p.Status
}
sex := uint8(0)
if p.Sex != nil {
sex = *p.Sex
}
roleID := uint64(1)
if p.Rid != nil && *p.Rid != 0 {
roleID = *p.Rid
}
id, err := models.CreateAdminUser(p.Account, p.Password, p.Name, p.Phone, p.Email, p.Qq, p.Avatar, sex, roleID, status)
if err != nil {
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "添加失败"}
_ = c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{
"code": 200,
"msg": "success",
"data": map[string]interface{}{"id": id},
}
_ = c.ServeJSON()
}
type editUserPayload struct {
Account *string `json:"account"`
Password *string `json:"password"`
Name *string `json:"name"`
Phone *string `json:"phone"`
Email *string `json:"email"`
Qq *string `json:"qq"`
Sex *uint8 `json:"sex"`
Avatar *string `json:"avatar"`
Rid *uint64 `json:"rid"`
Status *uint8 `json:"status"`
}
// EditUser 编辑用户信息password 可选,存在则修改)
// POST /platform/editUser/:id
func (c *PlatformAdminUserController) EditUser() {
idStr := c.Ctx.Input.Param(":id")
id, _ := strconv.ParseUint(idStr, 10, 64)
if id == 0 {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "id 不能为空"}
_ = c.ServeJSON()
return
}
var p editUserPayload
raw, _ := io.ReadAll(c.Ctx.Request.Body)
if err := json.Unmarshal(raw, &p); err != nil {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
_ = c.ServeJSON()
return
}
fields := map[string]interface{}{}
if p.Account != nil {
acc := strings.TrimSpace(*p.Account)
if acc != "" {
fields["account"] = acc
}
}
if p.Name != nil {
fields["name"] = *p.Name
}
if p.Phone != nil {
fields["phone"] = *p.Phone
}
if p.Email != nil {
fields["email"] = *p.Email
}
if p.Qq != nil {
fields["qq"] = *p.Qq
}
if p.Sex != nil {
fields["sex"] = *p.Sex
}
if p.Avatar != nil {
fields["avatar"] = *p.Avatar
}
if p.Rid != nil && *p.Rid != 0 {
fields["role_id"] = *p.Rid
}
if p.Status != nil {
fields["status"] = *p.Status
}
if len(fields) > 0 {
if err := models.UpdateAdminUser(id, fields); err != nil {
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "编辑失败"}
_ = c.ServeJSON()
return
}
}
if p.Password != nil && strings.TrimSpace(*p.Password) != "" {
if err := models.ChangeAdminUserPassword(id, *p.Password); err != nil {
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "密码修改失败"}
_ = c.ServeJSON()
return
}
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success"}
_ = c.ServeJSON()
}
// DeleteUser 删除用户
// DELETE /platform/deleteUser/:id
func (c *PlatformAdminUserController) DeleteUser() {
idStr := c.Ctx.Input.Param(":id")
id, _ := strconv.ParseUint(idStr, 10, 64)
if id == 0 {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "id 不能为空"}
_ = c.ServeJSON()
return
}
if err := models.DeleteAdminUser(id); err != nil {
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "删除失败"}
_ = c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "success"}
_ = c.ServeJSON()
}
type changePasswordPayload struct {
ID uint64 `json:"id"`
Password string `json:"password"`
}
// ChangePassword 修改密码
// POST /platform/changePassword
func (c *PlatformAdminUserController) ChangePassword() {
var p changePasswordPayload
raw, _ := io.ReadAll(c.Ctx.Request.Body)
if err := json.Unmarshal(raw, &p); err != nil {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "参数错误"}
_ = c.ServeJSON()
return
}
if p.ID == 0 {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "id 不能为空"}
_ = c.ServeJSON()
return
}
if strings.TrimSpace(p.Password) == "" {
c.Data["json"] = map[string]interface{}{"code": 400, "msg": "password 不能为空"}
_ = c.ServeJSON()
return
}
if err := models.ChangeAdminUserPassword(p.ID, p.Password); err != nil {
c.Data["json"] = map[string]interface{}{"code": 500, "msg": "修改失败"}
_ = c.ServeJSON()
return
}
c.Data["json"] = map[string]interface{}{"code": 200, "msg": "修改成功"}
_ = c.ServeJSON()
}