Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
陈曦
sub2api
Commits
4dab18a9
"vscode:/vscode.git/clone" did not exist on "89c1a413058b64ad29fb91d1b44daeecc01506a3"
Commit
4dab18a9
authored
Dec 29, 2025
by
yangjianbo
Browse files
fix(用户): 修复删除用户软删除钩子
避免软删除钩子类型断言失败导致 500\n删除无记录时返回未找到错误并记录日志
parent
5584709a
Changes
3
Show whitespace changes
Inline
Side-by-side
backend/ent/schema/mixins/soft_delete.go
View file @
4dab18a9
...
@@ -112,9 +112,6 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook {
...
@@ -112,9 +112,6 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook {
// 类型断言,获取 mutation 的扩展接口
// 类型断言,获取 mutation 的扩展接口
mx
,
ok
:=
m
.
(
interface
{
mx
,
ok
:=
m
.
(
interface
{
SetOp
(
ent
.
Op
)
SetOp
(
ent
.
Op
)
Client
()
interface
{
Mutate
(
context
.
Context
,
ent
.
Mutation
)
(
ent
.
Value
,
error
)
}
SetDeletedAt
(
time
.
Time
)
SetDeletedAt
(
time
.
Time
)
WhereP
(
...
func
(
*
sql
.
Selector
))
WhereP
(
...
func
(
*
sql
.
Selector
))
})
})
...
@@ -127,7 +124,7 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook {
...
@@ -127,7 +124,7 @@ func (d SoftDeleteMixin) Hooks() []ent.Hook {
mx
.
SetOp
(
ent
.
OpUpdate
)
mx
.
SetOp
(
ent
.
OpUpdate
)
// 设置删除时间为当前时间
// 设置删除时间为当前时间
mx
.
SetDeletedAt
(
time
.
Now
())
mx
.
SetDeletedAt
(
time
.
Now
())
return
mx
.
Client
()
.
Mutate
(
ctx
,
m
)
return
next
.
Mutate
(
ctx
,
m
)
})
})
},
},
}
}
...
...
backend/internal/repository/user_repo.go
View file @
4dab18a9
...
@@ -169,8 +169,14 @@ func (r *userRepository) Update(ctx context.Context, userIn *service.User) error
...
@@ -169,8 +169,14 @@ func (r *userRepository) Update(ctx context.Context, userIn *service.User) error
}
}
func
(
r
*
userRepository
)
Delete
(
ctx
context
.
Context
,
id
int64
)
error
{
func
(
r
*
userRepository
)
Delete
(
ctx
context
.
Context
,
id
int64
)
error
{
_
,
err
:=
r
.
client
.
User
.
Delete
()
.
Where
(
dbuser
.
IDEQ
(
id
))
.
Exec
(
ctx
)
affected
,
err
:=
r
.
client
.
User
.
Delete
()
.
Where
(
dbuser
.
IDEQ
(
id
))
.
Exec
(
ctx
)
return
err
if
err
!=
nil
{
return
translatePersistenceError
(
err
,
service
.
ErrUserNotFound
,
nil
)
}
if
affected
==
0
{
return
service
.
ErrUserNotFound
}
return
nil
}
}
func
(
r
*
userRepository
)
List
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
)
([]
service
.
User
,
*
pagination
.
PaginationResult
,
error
)
{
func
(
r
*
userRepository
)
List
(
ctx
context
.
Context
,
params
pagination
.
PaginationParams
)
([]
service
.
User
,
*
pagination
.
PaginationResult
,
error
)
{
...
...
backend/internal/service/admin_service.go
View file @
4dab18a9
...
@@ -366,7 +366,11 @@ func (s *adminServiceImpl) DeleteUser(ctx context.Context, id int64) error {
...
@@ -366,7 +366,11 @@ func (s *adminServiceImpl) DeleteUser(ctx context.Context, id int64) error {
if
user
.
Role
==
"admin"
{
if
user
.
Role
==
"admin"
{
return
errors
.
New
(
"cannot delete admin user"
)
return
errors
.
New
(
"cannot delete admin user"
)
}
}
return
s
.
userRepo
.
Delete
(
ctx
,
id
)
if
err
:=
s
.
userRepo
.
Delete
(
ctx
,
id
);
err
!=
nil
{
log
.
Printf
(
"delete user failed: user_id=%d err=%v"
,
id
,
err
)
return
err
}
return
nil
}
}
func
(
s
*
adminServiceImpl
)
UpdateUserBalance
(
ctx
context
.
Context
,
userID
int64
,
balance
float64
,
operation
string
,
notes
string
)
(
*
User
,
error
)
{
func
(
s
*
adminServiceImpl
)
UpdateUserBalance
(
ctx
context
.
Context
,
userID
int64
,
balance
float64
,
operation
string
,
notes
string
)
(
*
User
,
error
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment