64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"bj_power_mes/common/errorx"
|
|
xhttp "bj_power_mes/common/httpx"
|
|
"bj_power_mes/ent"
|
|
"bj_power_mes/ent/user"
|
|
"bj_power_mes/internal/svc"
|
|
"bj_power_mes/internal/types"
|
|
|
|
"github.com/jinzhu/copier"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type UserinfoLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 获取当前用户信息
|
|
func NewUserinfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserinfoLogic {
|
|
return &UserinfoLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *UserinfoLogic) Userinfo() (resp *types.UserInfoReply, err error) {
|
|
userId := xhttp.GetUidFromCtx(l.ctx)
|
|
query := l.svcCtx.EntClient.User.Query().Where(user.ID(int(userId))).WithDept().WithRole()
|
|
u, err := query.Only(l.ctx)
|
|
if err != nil {
|
|
if ent.IsNotFound(err) {
|
|
return nil, errorx.New(errorx.UserNotFound)
|
|
}
|
|
return nil, fmt.Errorf("获取用户信息失败: %w", err)
|
|
}
|
|
|
|
resp = new(types.UserInfoReply)
|
|
if err := copier.Copy(resp, u); err != nil {
|
|
l.Errorf("用户信息拷贝失败: %v", err)
|
|
}
|
|
|
|
resp.CreatedAt = u.CreatedAt.Unix()
|
|
|
|
if u.Edges.Dept != nil {
|
|
resp.DeptId = u.Edges.Dept.ID
|
|
resp.DeptName = u.Edges.Dept.Name
|
|
}
|
|
|
|
if u.Edges.Role != nil {
|
|
resp.RoleId = u.Edges.Role.ID
|
|
resp.RoleName = u.Edges.Role.Name
|
|
}
|
|
|
|
return resp, nil
|
|
}
|