mirror of
				https://github.com/yangjian102621/geekai.git
				synced 2025-11-04 16:23:42 +08:00 
			
		
		
		
	fix: fix bug for update user's power in admin page did not work
This commit is contained in:
		@@ -9,6 +9,7 @@ import (
 | 
				
			|||||||
	"chatplus/utils"
 | 
						"chatplus/utils"
 | 
				
			||||||
	"chatplus/utils/resp"
 | 
						"chatplus/utils/resp"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"github.com/gin-gonic/gin"
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
	"gorm.io/gorm"
 | 
						"gorm.io/gorm"
 | 
				
			||||||
@@ -73,6 +74,7 @@ func (h *UserHandler) Save(c *gin.Context) {
 | 
				
			|||||||
		ExpiredTime string   `json:"expired_time"`
 | 
							ExpiredTime string   `json:"expired_time"`
 | 
				
			||||||
		Status      bool     `json:"status"`
 | 
							Status      bool     `json:"status"`
 | 
				
			||||||
		Vip         bool     `json:"vip"`
 | 
							Vip         bool     `json:"vip"`
 | 
				
			||||||
 | 
							Power       int      `json:"power"`
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if err := c.ShouldBindJSON(&data); err != nil {
 | 
						if err := c.ShouldBindJSON(&data); err != nil {
 | 
				
			||||||
		resp.ERROR(c, types.InvalidArgs)
 | 
							resp.ERROR(c, types.InvalidArgs)
 | 
				
			||||||
@@ -82,16 +84,39 @@ func (h *UserHandler) Save(c *gin.Context) {
 | 
				
			|||||||
	var res *gorm.DB
 | 
						var res *gorm.DB
 | 
				
			||||||
	var userVo vo.User
 | 
						var userVo vo.User
 | 
				
			||||||
	if data.Id > 0 { // 更新
 | 
						if data.Id > 0 { // 更新
 | 
				
			||||||
		user.Id = data.Id
 | 
							res = h.DB.Where("id", data.Id).First(&user)
 | 
				
			||||||
		// 此处需要用 map 更新,用结构体无法更新 0 值
 | 
							if res.Error != nil {
 | 
				
			||||||
		res = h.DB.Model(&user).Updates(map[string]interface{}{
 | 
								resp.ERROR(c, "user not found")
 | 
				
			||||||
			"username":         data.Username,
 | 
								return
 | 
				
			||||||
			"status":           data.Status,
 | 
							}
 | 
				
			||||||
			"vip":              data.Vip,
 | 
							var changePower = user.Power != data.Power
 | 
				
			||||||
			"chat_roles_json":  utils.JsonEncode(data.ChatRoles),
 | 
							user.Username = data.Username
 | 
				
			||||||
			"chat_models_json": utils.JsonEncode(data.ChatModels),
 | 
							user.Status = data.Status
 | 
				
			||||||
			"expired_time":     utils.Str2stamp(data.ExpiredTime),
 | 
							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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							res = h.DB.Updates(&user)
 | 
				
			||||||
 | 
							if res.Error != nil {
 | 
				
			||||||
 | 
								resp.ERROR(c, "更新数据库失败!")
 | 
				
			||||||
 | 
								return
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							// 记录算力日志
 | 
				
			||||||
 | 
							if changePower {
 | 
				
			||||||
 | 
								h.DB.Create(&model.PowerLog{
 | 
				
			||||||
 | 
									UserId:    user.Id,
 | 
				
			||||||
 | 
									Username:  user.Username,
 | 
				
			||||||
 | 
									Type:      types.PowerGift,
 | 
				
			||||||
 | 
									Amount:    user.Power,
 | 
				
			||||||
 | 
									Balance:   user.Power,
 | 
				
			||||||
 | 
									Mark:      types.PowerAdd,
 | 
				
			||||||
 | 
									Model:     "管理员",
 | 
				
			||||||
 | 
									Remark:    fmt.Sprintf("后台管理员强制修改用户算力,修改值:%d, 管理员ID:%d", user.Power, h.GetLoginUserId(c)),
 | 
				
			||||||
 | 
									CreatedAt: time.Now(),
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		salt := utils.RandString(8)
 | 
							salt := utils.RandString(8)
 | 
				
			||||||
		u := model.User{
 | 
							u := model.User{
 | 
				
			||||||
@@ -100,6 +125,7 @@ func (h *UserHandler) Save(c *gin.Context) {
 | 
				
			|||||||
			Password:    utils.GenPassword(data.Password, salt),
 | 
								Password:    utils.GenPassword(data.Password, salt),
 | 
				
			||||||
			Avatar:      "/images/avatar/user.png",
 | 
								Avatar:      "/images/avatar/user.png",
 | 
				
			||||||
			Salt:        salt,
 | 
								Salt:        salt,
 | 
				
			||||||
 | 
								Power:       data.Power,
 | 
				
			||||||
			Status:      true,
 | 
								Status:      true,
 | 
				
			||||||
			ChatRoles:   utils.JsonEncode(data.ChatRoles),
 | 
								ChatRoles:   utils.JsonEncode(data.ChatRoles),
 | 
				
			||||||
			ChatModels:  utils.JsonEncode(data.ChatModels),
 | 
								ChatModels:  utils.JsonEncode(data.ChatModels),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -283,6 +283,7 @@ const saveUser = function () {
 | 
				
			|||||||
  userEditFormRef.value.validate((valid) => {
 | 
					  userEditFormRef.value.validate((valid) => {
 | 
				
			||||||
    if (valid) {
 | 
					    if (valid) {
 | 
				
			||||||
      showUserEditDialog.value = false
 | 
					      showUserEditDialog.value = false
 | 
				
			||||||
 | 
					      console.log(user.value)
 | 
				
			||||||
      httpPost('/api/admin/user/save', user.value).then((res) => {
 | 
					      httpPost('/api/admin/user/save', user.value).then((res) => {
 | 
				
			||||||
        ElMessage.success('操作成功!')
 | 
					        ElMessage.success('操作成功!')
 | 
				
			||||||
        if (add.value) {
 | 
					        if (add.value) {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user