fix: rebuild MES as compilable go-zero+ent backend (renamed bj_power_mes), restore 3 workstation projects from pristine original, align naming; all Go projects go build clean
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/common/errorx"
|
||||
xhttp "bj_power_mes/common/httpx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/internal/auth"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ChangePasswordLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 修改密码
|
||||
func NewChangePasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ChangePasswordLogic {
|
||||
return &ChangePasswordLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ChangePasswordLogic) ChangePassword(req *types.SetUserPasswordReq) (err error) {
|
||||
userId := xhttp.GetUidFromCtx(l.ctx)
|
||||
|
||||
u, err := l.svcCtx.EntClient.User.Get(l.ctx, int(userId))
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
return errorx.New(errorx.UserNotFound)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
if !auth.CheckPassword(req.OldPassword, u.Password) {
|
||||
return errorx.New(errorx.UserPasswordNotMatch)
|
||||
}
|
||||
|
||||
hashed, err := auth.HashPassword(req.NewPassword)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = u.Update().SetPassword(hashed).Save(l.ctx)
|
||||
if err != nil {
|
||||
l.Error(err)
|
||||
return errorx.Wrap(err, errorx.DbError, "update password failed")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jinzhu/copier"
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/department"
|
||||
"bj_power_mes/ent/role"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type CreateUserLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 创建用户
|
||||
func NewCreateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateUserLogic {
|
||||
return &CreateUserLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *CreateUserLogic) CreateUser(req *types.CreateUserReq) (resp *types.UserInfoReply, err error) {
|
||||
l.Info("创建用户请求:", req)
|
||||
|
||||
user, err := l.svcCtx.EntClient.User.Create().
|
||||
SetName(req.Name).
|
||||
SetUsername(req.Username).
|
||||
SetMobile(req.Mobile).
|
||||
SetGender(int(req.Gender)).
|
||||
SetPosition(req.Position).
|
||||
SetRoleID(int(req.RoleId)).
|
||||
SetDeptID(req.DeptId).
|
||||
SetStatus(req.Status).
|
||||
SetPassword(req.Password).
|
||||
Save(l.ctx)
|
||||
|
||||
if err != nil {
|
||||
l.Error("创建用户时出错:", err)
|
||||
if ent.IsValidationError(err) {
|
||||
l.Error(err)
|
||||
return nil, err
|
||||
} else {
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) && pgErr.Code == "23505" {
|
||||
l.Error("用户已存在", err)
|
||||
return nil, errorx.New(errorx.UserAlreadyExists)
|
||||
}
|
||||
return nil, errorx.New(errorx.DbError)
|
||||
}
|
||||
}
|
||||
|
||||
dept, err := l.svcCtx.EntClient.Department.Query().
|
||||
Where(department.ID(req.DeptId)).
|
||||
Only(l.ctx)
|
||||
if err != nil {
|
||||
l.Error("获取部门时出错:", err)
|
||||
return nil, errorx.New(errorx.DbError)
|
||||
}
|
||||
|
||||
role, err := l.svcCtx.EntClient.Role.Query().
|
||||
Where(role.ID(int(req.RoleId))).
|
||||
Only(l.ctx)
|
||||
if err != nil {
|
||||
l.Error("获取角色时出错:", err)
|
||||
return nil, errorx.New(errorx.DbError)
|
||||
}
|
||||
|
||||
var userInfo types.UserInfoReply
|
||||
_ = copier.Copy(&userInfo, user)
|
||||
userInfo.CreatedAt = user.CreatedAt.Unix()
|
||||
userInfo.UpdatedAt = user.UpdatedAt.Unix()
|
||||
if role != nil {
|
||||
userInfo.RoleId = role.ID
|
||||
userInfo.RoleName = role.Name
|
||||
}
|
||||
if dept != nil {
|
||||
userInfo.DeptId = dept.ID
|
||||
userInfo.DeptName = dept.Name
|
||||
}
|
||||
return &userInfo, nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DeleteUserLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 删除用户
|
||||
func NewDeleteUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteUserLogic {
|
||||
return &DeleteUserLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DeleteUserLogic) DeleteUser(req *types.PathId) error {
|
||||
|
||||
err := l.svcCtx.EntClient.User.DeleteOneID(req.Id).Exec(l.ctx)
|
||||
|
||||
if ent.IsNotFound(err) {
|
||||
l.Logger.Errorf("User not found with ID: %d", req.Id)
|
||||
return errorx.New(errorx.UserNotFound)
|
||||
} else if err != nil {
|
||||
l.Logger.Errorf("Failed to delete user with ID: %d, error: %v", req.Id, err)
|
||||
return err
|
||||
}
|
||||
|
||||
l.Logger.Infof("User deleted successfully with ID: %d", req.Id)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type DisableUserLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 禁用用户
|
||||
func NewDisableUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DisableUserLogic {
|
||||
return &DisableUserLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *DisableUserLogic) DisableUser(req *types.SetUserStateReq) error {
|
||||
err := l.svcCtx.EntClient.User.UpdateOneID(req.Id).
|
||||
SetStatus(req.Status).
|
||||
SetName(req.Name).
|
||||
Exec(l.ctx)
|
||||
if ent.IsNotFound(err) {
|
||||
l.Logger.Errorf("User not found with ID: %d", req.Id)
|
||||
return errorx.New(errorx.UserNotFound)
|
||||
} else if err != nil {
|
||||
l.Logger.Errorf("Failed to disable user with ID: %d, error: %v", req.Id, err)
|
||||
return err
|
||||
}
|
||||
|
||||
l.Logger.Infof("User disabled successfully with ID: %d", req.Id)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type EnableUserLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 启用用户
|
||||
func NewEnableUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EnableUserLogic {
|
||||
return &EnableUserLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *EnableUserLogic) EnableUser(req *types.SetUserStateReq) error {
|
||||
err := l.svcCtx.EntClient.User.UpdateOneID(req.Id).
|
||||
SetStatus(req.Status).
|
||||
SetName(req.Name).
|
||||
Exec(l.ctx)
|
||||
if ent.IsNotFound(err) {
|
||||
l.Logger.Errorf("User not found with ID: %d", req.Id)
|
||||
return errorx.New(errorx.UserNotFound)
|
||||
} else if err != nil {
|
||||
l.Logger.Errorf("Failed to enable user with ID: %d, error: %v", req.Id, err)
|
||||
return err
|
||||
}
|
||||
|
||||
l.Logger.Infof("User enable successfully with ID: %d", req.Id)
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/common/errorx"
|
||||
"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 GetUserLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 通过ID查询用户
|
||||
func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserLogic {
|
||||
return &GetUserLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetUserLogic) GetUser(req *types.PathId) (resp *types.UserInfoReply, err error) {
|
||||
query := l.svcCtx.EntClient.User.Query().Where(user.ID(req.Id)).WithDept().WithRole()
|
||||
u, err := query.Only(l.ctx)
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
return nil, errorx.New(errorx.UserNotFound)
|
||||
}
|
||||
l.Error(err)
|
||||
return nil, errorx.New(errorx.DbError)
|
||||
}
|
||||
|
||||
var reply types.UserInfoReply
|
||||
_ = copier.Copy(&reply, u)
|
||||
reply.CreatedAt = u.CreatedAt.Unix()
|
||||
reply.UpdatedAt = u.UpdatedAt.Unix()
|
||||
|
||||
if u.Edges.Dept != nil {
|
||||
reply.DeptName = u.Edges.Dept.Name
|
||||
reply.DeptId = u.Edges.Dept.ID
|
||||
} else {
|
||||
reply.DeptName = ""
|
||||
}
|
||||
|
||||
if u.Edges.Role != nil {
|
||||
reply.RoleName = u.Edges.Role.Name
|
||||
reply.RoleId = u.Edges.Role.ID
|
||||
} else {
|
||||
reply.RoleName = ""
|
||||
}
|
||||
|
||||
return &reply, nil
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/iancoleman/strcase"
|
||||
"bj_power_mes/ent/user"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"entgo.io/ent/dialect/sql"
|
||||
"github.com/jinzhu/copier"
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type QueryUsersLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 查询用户列表
|
||||
func NewQueryUsersLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryUsersLogic {
|
||||
return &QueryUsersLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *QueryUsersLogic) QueryUsers(req *types.QueryUsersReq) (resp *types.QueryUsersReply, err error) {
|
||||
query := l.svcCtx.EntClient.User.Query()
|
||||
|
||||
if req.Keyword != "" {
|
||||
query.Where(user.Or(user.NameContainsFold(req.Keyword)))
|
||||
}
|
||||
|
||||
// 关联查询部门和角色信息
|
||||
query = query.WithDept().WithRole()
|
||||
|
||||
total, err := query.Count(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if req.Sort != "" {
|
||||
col := strcase.ToSnake(req.Sort) // 转换为数据库字段名
|
||||
if user.ValidColumn(col) { // 检查字段是否存在
|
||||
op := sql.OrderAsc() // 默认为升序
|
||||
if strings.HasPrefix(req.Order, "desc") {
|
||||
op = sql.OrderDesc()
|
||||
}
|
||||
query.Order(sql.OrderByField(col, op).ToFunc())
|
||||
}
|
||||
} else {
|
||||
query.Order(user.ByCreatedAt(sql.OrderDesc()))
|
||||
}
|
||||
|
||||
query = query.Limit(req.Limit).Offset((req.Page - 1) * req.Limit)
|
||||
|
||||
users, err := query.All(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp = new(types.QueryUsersReply)
|
||||
resp.Total = total
|
||||
resp.Page = req.Page
|
||||
resp.Limit = req.Limit
|
||||
resp.Data = make([]types.UserInfoReply, 0)
|
||||
|
||||
var tempUserInfo types.UserInfoReply
|
||||
|
||||
for _, u := range users {
|
||||
_ = copier.Copy(&tempUserInfo, u)
|
||||
|
||||
if u.Edges.Dept != nil {
|
||||
tempUserInfo.DeptId = u.Edges.Dept.ID
|
||||
tempUserInfo.DeptName = u.Edges.Dept.Name
|
||||
}
|
||||
|
||||
if u.Edges.Role != nil {
|
||||
tempUserInfo.RoleName = u.Edges.Role.Name
|
||||
tempUserInfo.RoleId = u.Edges.Role.ID
|
||||
}
|
||||
|
||||
tempUserInfo.CreatedAt = u.CreatedAt.Unix()
|
||||
tempUserInfo.UpdatedAt = u.UpdatedAt.Unix()
|
||||
tempUserInfo.Status = u.Status
|
||||
resp.Data = append(resp.Data, tempUserInfo)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/jinzhu/copier"
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/department"
|
||||
"bj_power_mes/ent/role"
|
||||
"bj_power_mes/ent/user"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type QueryUsersOfDeptLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 查询部门用户分页列表
|
||||
func NewQueryUsersOfDeptLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryUsersOfDeptLogic {
|
||||
return &QueryUsersOfDeptLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *QueryUsersOfDeptLogic) QueryUsersOfDept(req *types.QueryDeptUsersReq) (resp *types.QueryUsersReply, err error) {
|
||||
// 构建查询条件
|
||||
query := l.svcCtx.EntClient.User.Query().Where(user.DeptId(req.PathId.Id))
|
||||
query = query.Limit(req.Limit).Offset((req.Page - 1) * req.Limit)
|
||||
|
||||
// 执行查询
|
||||
users, err := query.All(l.ctx)
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
return nil, errorx.New(errorx.UserNotFound)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 初始化部门和角色映射
|
||||
deptMap := make(map[int]string)
|
||||
roleMap := make(map[int]string)
|
||||
|
||||
// 收集部门ID
|
||||
deptIDs := make([]int, len(users))
|
||||
for i, u := range users {
|
||||
deptIDs[i] = u.DeptId
|
||||
}
|
||||
|
||||
// 查询部门信息
|
||||
depts, err := l.svcCtx.EntClient.Department.Query().Where(department.IDIn(deptIDs...)).All(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, d := range depts {
|
||||
deptMap[d.ID] = d.Name
|
||||
}
|
||||
|
||||
// 收集角色ID
|
||||
roleIDs := make([]int, len(users))
|
||||
for i, u := range users {
|
||||
roleIDs[i] = u.RoleId
|
||||
}
|
||||
|
||||
// 查询角色信息
|
||||
roles, err := l.svcCtx.EntClient.Role.Query().Where(role.IDIn(roleIDs...)).All(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, r := range roles {
|
||||
roleMap[r.ID] = r.Name
|
||||
}
|
||||
|
||||
// 构建响应对象
|
||||
resp = &types.QueryUsersReply{
|
||||
PageReply: types.PageReply{
|
||||
Page: req.Page,
|
||||
Limit: req.Limit,
|
||||
},
|
||||
Data: make([]types.UserInfoReply, len(users)),
|
||||
}
|
||||
|
||||
// 填充用户信息
|
||||
for i, u := range users {
|
||||
var reply types.UserInfoReply
|
||||
_ = copier.Copy(&reply, u)
|
||||
reply.CreatedAt = u.CreatedAt.Unix()
|
||||
reply.UpdatedAt = u.UpdatedAt.Unix()
|
||||
reply.DeptName = deptMap[u.DeptId]
|
||||
reply.RoleName = roleMap[u.RoleId]
|
||||
reply.Status = u.Status
|
||||
resp.Data[i] = reply
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/jinzhu/copier"
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type QueryUsersOfRoleLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 查询角色用户分页列表
|
||||
func NewQueryUsersOfRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryUsersOfRoleLogic {
|
||||
return &QueryUsersOfRoleLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *QueryUsersOfRoleLogic) QueryUsersOfRole(req *types.QueryRoleUsersReq) (resp *types.QueryUsersReply, err error) {
|
||||
r, err := l.svcCtx.EntClient.Role.Get(l.ctx, req.PathId.Id)
|
||||
if ent.IsNotFound(err) {
|
||||
return nil, errorx.New(errorx.RoleNotFound)
|
||||
} else if err != nil {
|
||||
l.WithContext(l.ctx).Error(err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
query := r.QueryUsers()
|
||||
|
||||
total, err := query.Count(l.ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
query.WithRole().WithDept().Limit(req.Limit).Offset((req.Page - 1) * req.Limit)
|
||||
|
||||
users, err := query.All(l.ctx)
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
return nil, errorx.New(errorx.UserNotFound)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp = new(types.QueryUsersReply)
|
||||
resp.Total = total
|
||||
resp.Limit = req.Limit
|
||||
resp.Page = req.Page
|
||||
resp.Data = make([]types.UserInfoReply, 0)
|
||||
|
||||
for _, u := range users {
|
||||
var userInfo types.UserInfoReply
|
||||
_ = copier.Copy(&userInfo, u)
|
||||
userInfo.CreatedAt = u.CreatedAt.Unix()
|
||||
userInfo.UpdatedAt = u.UpdatedAt.Unix()
|
||||
if u.Edges.Dept != nil {
|
||||
userInfo.DeptName = u.Edges.Dept.Name
|
||||
userInfo.DeptId = u.Edges.Dept.ID
|
||||
}
|
||||
if u.Edges.Role != nil {
|
||||
userInfo.RoleName = u.Edges.Role.Name
|
||||
userInfo.RoleId = u.Edges.Role.ID
|
||||
}
|
||||
resp.Data = append(resp.Data, userInfo)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/internal/auth"
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type ResetPasswordLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 重置密码
|
||||
func NewResetPasswordLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ResetPasswordLogic {
|
||||
return &ResetPasswordLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *ResetPasswordLogic) ResetPassword(req *types.ResetUserPasswordReq) error {
|
||||
//userId := ctxdata.GetUidFromCtx(l.ctx)
|
||||
//
|
||||
//query := l.svcCtx.EntClient.User.Query().Where(user.ID(int(userId))).WithRole()
|
||||
//u, err := query.Only(l.ctx)
|
||||
//if err != nil {
|
||||
// if ent.IsNotFound(err) {
|
||||
// return errorx.New(errorx.UserNotFound)
|
||||
// }
|
||||
//}
|
||||
//
|
||||
//if u.Edges.Role.Name != "admin" {
|
||||
// return errorx.New(errorx.UserNotAllow)
|
||||
//}
|
||||
|
||||
targetUserId := int(req.UserId)
|
||||
targetUser, err := l.svcCtx.EntClient.User.Get(l.ctx, targetUserId)
|
||||
if err != nil {
|
||||
if ent.IsNotFound(err) {
|
||||
return errorx.New(errorx.UserNotFound)
|
||||
}
|
||||
}
|
||||
// base64加密
|
||||
initialPassword := "000000"
|
||||
hashed, err := auth.HashPassword(initialPassword)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = targetUser.Update().SetPassword(hashed).Save(l.ctx)
|
||||
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SetUserRolesLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 添加用户角色
|
||||
func NewSetUserRolesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SetUserRolesLogic {
|
||||
return &SetUserRolesLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *SetUserRolesLogic) SetUserRoles(req *types.SetUserRolesReq) error {
|
||||
// todo: add your logic here and delete this line
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/jinzhu/copier"
|
||||
"bj_power_mes/common/errorx"
|
||||
"bj_power_mes/ent"
|
||||
"bj_power_mes/ent/department"
|
||||
"bj_power_mes/ent/role"
|
||||
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type UpdateUserLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
// 编辑用户
|
||||
func NewUpdateUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateUserLogic {
|
||||
return &UpdateUserLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *UpdateUserLogic) UpdateUser(req *types.UpdateUserReq) (resp *types.UserInfoReply, err error) {
|
||||
l.Info("编辑用户请求:", req)
|
||||
|
||||
user, err := l.svcCtx.EntClient.User.UpdateOneID(req.Id).
|
||||
SetName(req.Name).
|
||||
SetUsername(req.Username).
|
||||
SetMobile(req.Mobile).
|
||||
SetGender(int(req.Gender)).
|
||||
SetPosition(req.Position).
|
||||
SetRoleId(int(req.RoleId)).
|
||||
SetDeptId(req.DepartmentId).
|
||||
SetStatus(req.State).
|
||||
Save(l.ctx)
|
||||
|
||||
if err != nil {
|
||||
l.Error("编辑用户时出错:", err)
|
||||
if ent.IsValidationError(err) {
|
||||
l.Error(err)
|
||||
return nil, err
|
||||
} else if ent.IsNotFound(err) {
|
||||
return nil, errorx.New(errorx.UserNotFound)
|
||||
} else {
|
||||
return nil, errorx.New(errorx.DbError)
|
||||
}
|
||||
}
|
||||
|
||||
dept, err := l.svcCtx.EntClient.Department.Query().
|
||||
Where(department.ID(user.DeptId)).
|
||||
Only(l.ctx)
|
||||
if err != nil {
|
||||
l.Error("获取部门时出错:", err)
|
||||
return nil, errorx.New(errorx.DbError)
|
||||
}
|
||||
|
||||
role, err := l.svcCtx.EntClient.Role.Query().
|
||||
Where(role.ID(user.RoleId)).
|
||||
Only(l.ctx)
|
||||
if err != nil {
|
||||
l.Error("获取角色时出错:", err)
|
||||
return nil, errorx.New(errorx.DbError)
|
||||
}
|
||||
|
||||
var userInfo types.UserInfoReply
|
||||
_ = copier.Copy(&userInfo, user)
|
||||
userInfo.CreatedAt = user.CreatedAt.Unix()
|
||||
userInfo.UpdatedAt = user.UpdatedAt.Unix()
|
||||
if role != nil {
|
||||
userInfo.RoleId = role.ID
|
||||
userInfo.RoleName = role.Name
|
||||
}
|
||||
if dept != nil {
|
||||
userInfo.DeptId = dept.ID
|
||||
userInfo.DeptName = dept.Name
|
||||
}
|
||||
return &userInfo, nil
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
"bj_power_mes/common/errorx"
|
||||
xhttp "bj_power_mes/common/httpx"
|
||||
"bj_power_mes/ent"
|
||||
@@ -11,8 +12,6 @@ import (
|
||||
"bj_power_mes/internal/svc"
|
||||
"bj_power_mes/internal/types"
|
||||
|
||||
"github.com/jinzhu/copier"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
@@ -43,9 +42,7 @@ func (l *UserinfoLogic) Userinfo() (resp *types.UserInfoReply, err error) {
|
||||
}
|
||||
|
||||
resp = new(types.UserInfoReply)
|
||||
if err := copier.Copy(resp, u); err != nil {
|
||||
l.Errorf("用户信息拷贝失败: %v", err)
|
||||
}
|
||||
_ = copier.Copy(resp, u)
|
||||
|
||||
resp.CreatedAt = u.CreatedAt.Unix()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user