2023-06-19 18:23:09 +08:00
|
|
|
|
package admin
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"chatplus/core"
|
2023-06-19 21:53:07 +08:00
|
|
|
|
"chatplus/core/types"
|
2023-06-19 18:23:09 +08:00
|
|
|
|
"chatplus/handler"
|
|
|
|
|
"chatplus/store/model"
|
|
|
|
|
"chatplus/store/vo"
|
|
|
|
|
"chatplus/utils"
|
|
|
|
|
"chatplus/utils/resp"
|
2024-01-07 10:49:36 +08:00
|
|
|
|
"fmt"
|
2024-03-23 16:21:37 +08:00
|
|
|
|
"time"
|
2024-01-07 10:49:36 +08:00
|
|
|
|
|
2023-06-19 18:23:09 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type UserHandler struct {
|
|
|
|
|
handler.BaseHandler
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewUserHandler(app *core.AppServer, db *gorm.DB) *UserHandler {
|
2024-03-19 18:25:01 +08:00
|
|
|
|
return &UserHandler{BaseHandler: handler.BaseHandler{App: app, DB: db}}
|
2023-06-19 18:23:09 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// List 用户列表
|
|
|
|
|
func (h *UserHandler) List(c *gin.Context) {
|
|
|
|
|
page := h.GetInt(c, "page", 1)
|
|
|
|
|
pageSize := h.GetInt(c, "page_size", 20)
|
2024-01-05 18:17:11 +08:00
|
|
|
|
username := h.GetTrim(c, "username")
|
2023-07-26 15:22:11 +08:00
|
|
|
|
|
2023-06-19 18:23:09 +08:00
|
|
|
|
offset := (page - 1) * pageSize
|
|
|
|
|
var items []model.User
|
|
|
|
|
var users = make([]vo.User, 0)
|
|
|
|
|
var total int64
|
2023-07-26 15:22:11 +08:00
|
|
|
|
|
2024-03-19 18:25:01 +08:00
|
|
|
|
session := h.DB.Session(&gorm.Session{})
|
2024-01-05 18:17:11 +08:00
|
|
|
|
if username != "" {
|
|
|
|
|
session = session.Where("username LIKE ?", "%"+username+"%")
|
2023-07-26 15:22:11 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session.Model(&model.User{}).Count(&total)
|
|
|
|
|
res := session.Offset(offset).Limit(pageSize).Find(&items)
|
2023-06-19 18:23:09 +08:00
|
|
|
|
if res.Error == nil {
|
|
|
|
|
for _, item := range items {
|
|
|
|
|
var user vo.User
|
|
|
|
|
err := utils.CopyObject(item, &user)
|
|
|
|
|
if err == nil {
|
|
|
|
|
user.Id = item.Id
|
|
|
|
|
user.CreatedAt = item.CreatedAt.Unix()
|
|
|
|
|
user.UpdatedAt = item.UpdatedAt.Unix()
|
|
|
|
|
users = append(users, user)
|
|
|
|
|
} else {
|
|
|
|
|
logger.Error(err)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pageVo := vo.NewPage(total, page, pageSize, users)
|
|
|
|
|
resp.SUCCESS(c, pageVo)
|
|
|
|
|
}
|
2023-06-19 21:53:07 +08:00
|
|
|
|
|
2023-08-01 16:02:49 +08:00
|
|
|
|
func (h *UserHandler) Save(c *gin.Context) {
|
2023-06-19 21:53:07 +08:00
|
|
|
|
var data struct {
|
|
|
|
|
Id uint `json:"id"`
|
2023-08-01 16:02:49 +08:00
|
|
|
|
Password string `json:"password"`
|
2024-01-07 10:49:36 +08:00
|
|
|
|
Username string `json:"username"`
|
2023-06-19 21:53:07 +08:00
|
|
|
|
ChatRoles []string `json:"chat_roles"`
|
2024-03-18 15:37:46 +08:00
|
|
|
|
ChatModels []int `json:"chat_models"`
|
2023-06-19 21:53:07 +08:00
|
|
|
|
ExpiredTime string `json:"expired_time"`
|
|
|
|
|
Status bool `json:"status"`
|
2023-11-22 11:30:58 +08:00
|
|
|
|
Vip bool `json:"vip"`
|
2024-03-23 16:21:37 +08:00
|
|
|
|
Power int `json:"power"`
|
2023-06-19 21:53:07 +08:00
|
|
|
|
}
|
|
|
|
|
if err := c.ShouldBindJSON(&data); err != nil {
|
|
|
|
|
resp.ERROR(c, types.InvalidArgs)
|
|
|
|
|
return
|
|
|
|
|
}
|
2023-06-20 07:08:37 +08:00
|
|
|
|
var user = model.User{}
|
2023-08-01 16:02:49 +08:00
|
|
|
|
var res *gorm.DB
|
|
|
|
|
var userVo vo.User
|
|
|
|
|
if data.Id > 0 { // 更新
|
2024-03-23 16:21:37 +08:00
|
|
|
|
res = h.DB.Where("id", data.Id).First(&user)
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
resp.ERROR(c, "user not found")
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-25 11:40:03 +08:00
|
|
|
|
var oldPower = user.Power
|
2024-03-23 16:21:37 +08:00
|
|
|
|
user.Username = data.Username
|
|
|
|
|
user.Status = data.Status
|
|
|
|
|
user.Vip = data.Vip
|
|
|
|
|
user.Power = data.Power
|
|
|
|
|
user.ChatRoles = utils.JsonEncode(data.ChatRoles)
|
|
|
|
|
user.ChatModels = utils.JsonEncode(data.ChatModels)
|
|
|
|
|
user.ExpiredTime = utils.Str2stamp(data.ExpiredTime)
|
|
|
|
|
|
2024-03-25 11:40:03 +08:00
|
|
|
|
res = h.DB.Select("username", "status", "vip", "power", "chat_roles_json", "chat_models_json", "expired_time").Updates(&user)
|
2024-03-23 16:21:37 +08:00
|
|
|
|
if res.Error != nil {
|
|
|
|
|
resp.ERROR(c, "更新数据库失败!")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
// 记录算力日志
|
2024-03-25 11:40:03 +08:00
|
|
|
|
if oldPower != user.Power {
|
|
|
|
|
mark := types.PowerAdd
|
|
|
|
|
amount := user.Power - oldPower
|
|
|
|
|
if oldPower > user.Power {
|
|
|
|
|
mark = types.PowerSub
|
|
|
|
|
amount = oldPower - user.Power
|
|
|
|
|
}
|
2024-03-23 16:21:37 +08:00
|
|
|
|
h.DB.Create(&model.PowerLog{
|
|
|
|
|
UserId: user.Id,
|
|
|
|
|
Username: user.Username,
|
|
|
|
|
Type: types.PowerGift,
|
2024-03-25 11:40:03 +08:00
|
|
|
|
Amount: amount,
|
2024-03-23 16:21:37 +08:00
|
|
|
|
Balance: user.Power,
|
2024-03-25 11:40:03 +08:00
|
|
|
|
Mark: mark,
|
2024-03-23 16:21:37 +08:00
|
|
|
|
Model: "管理员",
|
2024-03-25 11:40:03 +08:00
|
|
|
|
Remark: fmt.Sprintf("后台管理员强制修改用户算力,修改前:%d,修改后:%d, 管理员ID:%d", oldPower, user.Power, h.GetLoginUserId(c)),
|
2024-03-23 16:21:37 +08:00
|
|
|
|
CreatedAt: time.Now(),
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-08-01 16:02:49 +08:00
|
|
|
|
} else {
|
|
|
|
|
salt := utils.RandString(8)
|
|
|
|
|
u := model.User{
|
2024-01-07 10:49:36 +08:00
|
|
|
|
Username: data.Username,
|
|
|
|
|
Nickname: fmt.Sprintf("极客学长@%d", utils.RandomNumber(6)),
|
2023-08-01 16:02:49 +08:00
|
|
|
|
Password: utils.GenPassword(data.Password, salt),
|
|
|
|
|
Avatar: "/images/avatar/user.png",
|
|
|
|
|
Salt: salt,
|
2024-03-23 16:21:37 +08:00
|
|
|
|
Power: data.Power,
|
2023-08-01 16:02:49 +08:00
|
|
|
|
Status: true,
|
|
|
|
|
ChatRoles: utils.JsonEncode(data.ChatRoles),
|
2023-10-26 13:41:49 +08:00
|
|
|
|
ChatModels: utils.JsonEncode(data.ChatModels),
|
2023-08-01 16:02:49 +08:00
|
|
|
|
ExpiredTime: utils.Str2stamp(data.ExpiredTime),
|
|
|
|
|
}
|
2024-03-19 18:25:01 +08:00
|
|
|
|
res = h.DB.Create(&u)
|
2023-08-01 16:02:49 +08:00
|
|
|
|
_ = utils.CopyObject(u, &userVo)
|
|
|
|
|
userVo.Id = u.Id
|
|
|
|
|
userVo.CreatedAt = u.CreatedAt.Unix()
|
|
|
|
|
userVo.UpdatedAt = u.UpdatedAt.Unix()
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-19 21:53:07 +08:00
|
|
|
|
if res.Error != nil {
|
|
|
|
|
resp.ERROR(c, "更新数据库失败")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-01 16:02:49 +08:00
|
|
|
|
resp.SUCCESS(c, userVo)
|
2023-06-19 21:53:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-26 15:22:11 +08:00
|
|
|
|
// ResetPass 重置密码
|
|
|
|
|
func (h *UserHandler) ResetPass(c *gin.Context) {
|
|
|
|
|
var data struct {
|
|
|
|
|
Id uint
|
|
|
|
|
Password string
|
|
|
|
|
}
|
|
|
|
|
if err := c.ShouldBindJSON(&data); err != nil {
|
|
|
|
|
resp.ERROR(c, types.InvalidArgs)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var user model.User
|
2024-03-19 18:25:01 +08:00
|
|
|
|
res := h.DB.First(&user, data.Id)
|
2023-07-26 15:22:11 +08:00
|
|
|
|
if res.Error != nil {
|
|
|
|
|
resp.ERROR(c, "No user found")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
password := utils.GenPassword(data.Password, user.Salt)
|
|
|
|
|
user.Password = password
|
2024-03-19 18:25:01 +08:00
|
|
|
|
res = h.DB.Updates(&user)
|
2023-07-26 15:22:11 +08:00
|
|
|
|
if res.Error != nil {
|
|
|
|
|
resp.ERROR(c)
|
|
|
|
|
} else {
|
|
|
|
|
resp.SUCCESS(c)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-20 07:08:37 +08:00
|
|
|
|
func (h *UserHandler) Remove(c *gin.Context) {
|
2024-03-19 18:25:01 +08:00
|
|
|
|
id := h.GetInt(c, "id", 0)
|
|
|
|
|
if id <= 0 {
|
2024-03-13 14:40:38 +08:00
|
|
|
|
resp.ERROR(c, types.InvalidArgs)
|
|
|
|
|
return
|
|
|
|
|
}
|
2024-03-19 18:25:01 +08:00
|
|
|
|
// 删除用户
|
|
|
|
|
res := h.DB.Where("id = ?", id).Delete(&model.User{})
|
|
|
|
|
if res.Error != nil {
|
|
|
|
|
resp.ERROR(c, "删除失败")
|
|
|
|
|
return
|
2023-06-20 07:08:37 +08:00
|
|
|
|
}
|
2024-03-19 18:25:01 +08:00
|
|
|
|
|
|
|
|
|
// 删除聊天记录
|
|
|
|
|
h.DB.Where("user_id = ?", id).Delete(&model.ChatItem{})
|
|
|
|
|
// 删除聊天历史记录
|
|
|
|
|
h.DB.Where("user_id = ?", id).Delete(&model.ChatMessage{})
|
|
|
|
|
// 删除登录日志
|
|
|
|
|
h.DB.Where("user_id = ?", id).Delete(&model.UserLoginLog{})
|
|
|
|
|
// 删除算力日志
|
|
|
|
|
h.DB.Where("user_id = ?", id).Delete(&model.PowerLog{})
|
|
|
|
|
// 删除众筹日志
|
|
|
|
|
h.DB.Where("user_id = ?", id).Delete(&model.Reward{})
|
|
|
|
|
// 删除绘图任务
|
|
|
|
|
h.DB.Where("user_id = ?", id).Delete(&model.MidJourneyJob{})
|
|
|
|
|
h.DB.Where("user_id = ?", id).Delete(&model.SdJob{})
|
|
|
|
|
// 删除订单
|
|
|
|
|
h.DB.Where("user_id = ?", id).Delete(&model.Order{})
|
2023-06-20 07:08:37 +08:00
|
|
|
|
resp.SUCCESS(c)
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 06:53:41 +08:00
|
|
|
|
func (h *UserHandler) LoginLog(c *gin.Context) {
|
|
|
|
|
page := h.GetInt(c, "page", 1)
|
|
|
|
|
pageSize := h.GetInt(c, "page_size", 20)
|
|
|
|
|
var total int64
|
2024-03-19 18:25:01 +08:00
|
|
|
|
h.DB.Model(&model.UserLoginLog{}).Count(&total)
|
2023-06-21 06:53:41 +08:00
|
|
|
|
offset := (page - 1) * pageSize
|
|
|
|
|
var items []model.UserLoginLog
|
2024-03-19 18:25:01 +08:00
|
|
|
|
res := h.DB.Offset(offset).Limit(pageSize).Order("id DESC").Find(&items)
|
2023-06-21 06:53:41 +08:00
|
|
|
|
if res.Error != nil {
|
|
|
|
|
resp.ERROR(c, "获取数据失败")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var logs []vo.UserLoginLog
|
|
|
|
|
for _, v := range items {
|
|
|
|
|
var log vo.UserLoginLog
|
|
|
|
|
err := utils.CopyObject(v, &log)
|
|
|
|
|
if err == nil {
|
|
|
|
|
log.Id = v.Id
|
|
|
|
|
log.CreatedAt = v.CreatedAt.Unix()
|
|
|
|
|
logs = append(logs, log)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
resp.SUCCESS(c, vo.NewPage(total, page, pageSize, logs))
|
|
|
|
|
}
|