feat: user items

This commit is contained in:
zzw
2026-08-24 15:57:08 +08:00
parent b7769ece4c
commit 132fb7c357
29 changed files with 1599 additions and 2031 deletions
+31
View File
@@ -79,3 +79,34 @@ type UserLoginSession struct {
AppId uint32 `json:"app_id"`
LastTime utils.CustomTime `json:"last_time"`
}
type UserListRow struct {
Id int64 `gorm:"column:id" json:"id"`
Username string `gorm:"column:username" json:"username"`
Nickname string `gorm:"column:nickname" json:"nickname"`
Avatar string `gorm:"column:avatar" json:"avatar"`
Mobile string `gorm:"column:mobile" json:"mobile"`
OnTrialNum int `gorm:"column:on_trial_num" json:"on_trial_num"`
Gender int `gorm:"column:gender" json:"gender"`
Birthday string `gorm:"column:birthday" json:"birthday"`
CreatedAt utils.CustomTime `gorm:"column:created_at" json:"created_at"`
UpdatedAt utils.CustomTime `gorm:"column:updated_at" json:"updated_at"`
}
type UserListItem struct {
Id int64 `json:"id"`
Username string `json:"username"`
Nickname string `json:"nickname"`
HeadPortrait string `json:"head_portrait"`
Mobile string `json:"mobile"`
OnTrialNum int `json:"on_trial_num"`
Gender int `json:"gender"`
Birthday string `json:"birthday"`
CreatedAt utils.CustomTime `json:"created_at"`
UpdatedAt utils.CustomTime `json:"updated_at"`
}
type UserItemsData struct {
Count int64 `json:"count"`
Items []UserListItem `json:"items"`
}
@@ -0,0 +1,95 @@
package logic
import (
"context"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/pkg/validate"
user "lone-services/rpc/user/pb"
"lone-services/services/user/internal/dao"
"lone-services/services/user/internal/model"
"lone-services/services/user/internal/svc"
"lone-services/services/user/validator"
"github.com/zeromicro/go-zero/core/logx"
)
type UserItemsLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUserItemsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserItemsLogic {
return &UserItemsLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UserItemsLogic) UserItems(in *user.UserItemsReq) (*user.Response, error) {
var v validator.UserItemsValidator
if msg := validate.ValidateFromProto(in, &v); msg != utils.StringEmpty {
return outResponse(utils.ErrorParams, msg), nil
}
adminInfo := utils.GetUserFromCtx(l.ctx)
if adminInfo.ID < utils.NumberOne {
return failResponse(utils.ErrorNoLoginInfo), nil
}
page := int(v.Page)
size := int(v.Size)
if page < modelbase.DefaultPage {
page = modelbase.DefaultPage
}
if size < utils.NumberOne {
size = modelbase.DefaultSize
}
where := map[string]string{}
if v.Mobile != utils.StringEmpty {
mobile, cErr := utils.Crypto{}.AESEncryptECB(v.Mobile)
if cErr != nil {
l.Errorf("user items encrypt mobile: %v", cErr)
return failResponse(utils.ErrorEncryptAesError), nil
}
where["mobile"] = mobile
}
var list []dao.UserListRow
result, err := model.UserModel{}.Init().Page(modelbase.Params{
Eq: where,
Order: "id DESC",
Page: page,
Size: size,
}, &list)
if err != nil {
l.Errorf("user items: %v", err)
return failResponse(utils.Fail), nil
}
items := make([]dao.UserListItem, 0, len(list))
for _, row := range list {
headPortrait, _ := utils.BuildImageURL(row.Avatar).(string)
items = append(items, dao.UserListItem{
Id: row.Id,
Username: row.Username,
Nickname: row.Nickname,
HeadPortrait: headPortrait,
Mobile: utils.DecryptMobile(row.Mobile),
OnTrialNum: row.OnTrialNum,
Gender: row.Gender,
Birthday: row.Birthday,
CreatedAt: row.CreatedAt,
UpdatedAt: row.UpdatedAt,
})
}
return okResponse(dao.UserItemsData{
Count: result.Count,
Items: items,
}), nil
}
@@ -0,0 +1,56 @@
package logic
import (
"context"
"strconv"
"lone-services/pkg/modelbase"
"lone-services/pkg/utils"
"lone-services/pkg/validate"
user "lone-services/rpc/user/pb"
"lone-services/services/user/internal/model"
"lone-services/services/user/internal/svc"
"lone-services/services/user/validator"
"github.com/zeromicro/go-zero/core/logx"
)
type UserStatusLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUserStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserStatusLogic {
return &UserStatusLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UserStatusLogic) UserStatus(in *user.UserStatusReq) (*user.Response, error) {
var v validator.UserStatusValidator
if msg := validate.ValidateFromProto(in, &v); msg != utils.StringEmpty {
return outResponse(utils.ErrorParams, msg), nil
}
adminInfo := utils.GetUserFromCtx(l.ctx)
if adminInfo.ID < utils.NumberOne {
return failResponse(utils.ErrorNoLoginInfo), nil
}
_, err := model.UserModel{}.Init().Edit(modelbase.Params{
Eq: map[string]string{
"id": strconv.FormatInt(v.Id, utils.NumberTen),
},
}, map[string]interface{}{
"status": v.Status,
})
if err != nil {
l.Errorf("user status: %v", err)
return failResponse(utils.Fail), nil
}
return okResponse(nil), nil
}
+12 -1
View File
@@ -6,7 +6,8 @@ package server
import (
"context"
user "lone-services/rpc/user/pb"
"lone-services/rpc/user/pb"
"lone-services/services/user/internal/logic"
"lone-services/services/user/internal/svc"
)
@@ -36,3 +37,13 @@ func (s *UserServer) Login(ctx context.Context, in *user.LoginReq) (*user.Respon
l := logic.NewLoginLogic(ctx, s.svcCtx)
return l.Login(in)
}
func (s *UserServer) UserItems(ctx context.Context, in *user.UserItemsReq) (*user.Response, error) {
l := logic.NewUserItemsLogic(ctx, s.svcCtx)
return l.UserItems(in)
}
func (s *UserServer) UserStatus(ctx context.Context, in *user.UserStatusReq) (*user.Response, error) {
l := logic.NewUserStatusLogic(ctx, s.svcCtx)
return l.UserStatus(in)
}