:art:用户接口处理完毕
This commit is contained in:
@@ -2,7 +2,10 @@ package repository
|
||||
|
||||
import (
|
||||
"wireguard-dashboard/client"
|
||||
"wireguard-dashboard/http/param"
|
||||
"wireguard-dashboard/model/entity"
|
||||
"wireguard-dashboard/model/vo"
|
||||
"wireguard-dashboard/utils"
|
||||
)
|
||||
|
||||
type user struct{}
|
||||
@@ -11,6 +14,21 @@ func User() user {
|
||||
return user{}
|
||||
}
|
||||
|
||||
// List
|
||||
// @description: 用户列表
|
||||
// @receiver r
|
||||
// @param p
|
||||
// @return data
|
||||
// @return total
|
||||
// @return err
|
||||
func (r user) List(p param.UserList) (data []vo.User, total int64, err error) {
|
||||
err = client.DB.Model(&entity.User{}).Scopes(utils.Page(p.Current, p.Size)).
|
||||
Select("id", "created_at", "updated_at", "avatar", "name", "account", "is_admin", "status").Order("created_at DESC").
|
||||
Find(&data).Offset(-1).Limit(-1).Count(&total).Error
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// GetUserById
|
||||
// @description: 根据id获取用户信息
|
||||
// @receiver r
|
||||
@@ -32,3 +50,56 @@ func (r user) GetUserByAccount(account string) (data *entity.User, err error) {
|
||||
err = client.DB.Where("account = ?", account).First(&data).Error
|
||||
return
|
||||
}
|
||||
|
||||
// Save
|
||||
// @description: 创建/更新用户
|
||||
// @receiver r
|
||||
// @param ent
|
||||
// @return err
|
||||
func (r user) Save(ent *entity.User) (err error) {
|
||||
// 更新
|
||||
if ent.Id != "" {
|
||||
updates := map[string]any{
|
||||
"name": ent.Name,
|
||||
"avatar": ent.Avatar,
|
||||
"email": ent.Email,
|
||||
"is_admin": ent.IsAdmin,
|
||||
"status": ent.Status,
|
||||
}
|
||||
|
||||
return client.DB.Model(&entity.User{}).Where("id = ?", ent.Id).Updates(&updates).Error
|
||||
}
|
||||
|
||||
defaultPassword := utils.Password().GenerateHashPassword("admin123")
|
||||
if ent.Password == "" { // 没有密码给一个默认密码
|
||||
ent.Password = defaultPassword
|
||||
}
|
||||
|
||||
// 没有头像就生成一个头像
|
||||
if ent.Avatar == "" {
|
||||
ent.Avatar, _ = utils.Avatar().GenerateAvatar()
|
||||
}
|
||||
|
||||
// 创建
|
||||
return client.DB.Create(&ent).Error
|
||||
}
|
||||
|
||||
// ChangePassword
|
||||
// @description: 变更密码
|
||||
// @receiver r
|
||||
// @param p
|
||||
// @param userId
|
||||
// @return err
|
||||
func (r user) ChangePassword(p param.ChangePassword, userId string) (err error) {
|
||||
password := utils.Password().GenerateHashPassword(p.NewPassword)
|
||||
return client.DB.Model(&entity.User{}).Where("id = ?", userId).Update("password", password).Error
|
||||
}
|
||||
|
||||
// ChangeUserState
|
||||
// @description: 变更用户状态
|
||||
// @receiver r
|
||||
// @param p
|
||||
// @return err
|
||||
func (r user) ChangeUserState(p param.ChangeUserState) (err error) {
|
||||
return client.DB.Model(&entity.User{}).Where("id = ?", p.ID).Update("status", p.Status).Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user