Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
projecttwo
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wangtao
projecttwo
Commits
2f4d6ada
Commit
2f4d6ada
authored
Jun 23, 2025
by
wangzhengwen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
exp
parent
99ff3f55
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
144 additions
and
1 deletion
+144
-1
Search.php
app/api/controller/Search.php
+1
-1
middleware.php
app/api/middleware.php
+2
-0
AddExperience.php
app/api/middleware/AddExperience.php
+141
-0
No files found.
app/api/controller/Search.php
View file @
2f4d6ada
...
...
@@ -243,6 +243,6 @@ class Search extends BaseController
public
function
addSearchKey
()
{
}
}
\ No newline at end of file
app/api/middleware.php
View file @
2f4d6ada
...
...
@@ -3,4 +3,6 @@
return
[
// 全局中间件
// \app\api\middleware\Auth::class,
\app\api\middleware\AddExperience
::
class
,
];
\ No newline at end of file
app/api/middleware/AddExperience.php
0 → 100644
View file @
2f4d6ada
<?php
namespace
app\api\middleware
;
use
think\facade\Db
;
use
think\facade\Log
;
class
AddExperience
{
public
function
handle
(
$request
,
\Closure
$next
)
{
$response
=
$next
(
$request
);
try
{
// 获取当前控制器和方法(格式如:user/login)
$fullAction
=
strtolower
(
$request
->
controller
()
.
'/'
.
$request
->
action
());
// 检查是否是登录方法
$isLoginAction
=
(
$fullAction
===
'user/login'
);
// 获取经验规则
$rule
=
Db
::
name
(
'user_exp_rule'
)
->
where
(
'action'
,
$fullAction
)
->
where
(
'is_open'
,
1
)
->
where
(
'delete_time'
,
null
)
->
find
();
if
(
!
$rule
)
{
return
$response
;
}
// 特殊处理登录方法
if
(
$isLoginAction
)
{
$this
->
handleLoginAction
(
$response
,
$rule
);
}
else
{
$this
->
handleNormalAction
(
$request
,
$rule
);
}
}
catch
(
\Exception
$e
)
{
Log
::
error
(
'经验系统异常:'
.
$e
->
getMessage
());
}
return
$response
;
}
/**
* 处理登录方法
*/
private
function
handleLoginAction
(
$response
,
$rule
)
{
try
{
$responseData
=
json_decode
(
$response
->
getContent
(),
true
);
$token
=
$responseData
[
'data'
][
'token'
]
??
null
;
if
(
!
$token
)
{
return
;
}
$userId
=
Db
::
name
(
'user'
)
->
where
(
'token'
,
$token
)
->
value
(
'id'
);
if
(
$userId
)
{
$this
->
addExperience
(
$userId
,
$rule
);
}
}
catch
(
\Exception
$e
)
{
Log
::
error
(
'登录经验处理失败:'
.
$e
->
getMessage
());
}
}
/**
* 处理普通方法
*/
private
function
handleNormalAction
(
$request
,
$rule
)
{
$token
=
$request
->
header
(
'token'
);
if
(
!
$token
)
return
;
$userId
=
Db
::
name
(
'user'
)
->
where
(
'token'
,
$token
)
->
where
(
'delete_time'
,
null
)
->
value
(
'id'
);
if
(
$userId
)
{
$this
->
addExperience
(
$userId
,
$rule
);
}
}
/**
* 统一添加经验
*/
private
function
addExperience
(
$userId
,
$rule
)
{
// 检查今日是否已达到上限
$today
=
strtotime
(
date
(
'Y-m-d'
));
$expToday
=
Db
::
name
(
'user_exp_log'
)
->
where
(
'user_id'
,
$userId
)
->
where
(
'rule_id'
,
$rule
[
'id'
])
->
where
(
'create_time'
,
'>='
,
$today
)
->
sum
(
'exp'
);
if
(
$rule
[
'daily_limit'
]
>
0
&&
$expToday
>=
$rule
[
'daily_limit'
])
{
return
;
}
// 2. 检查是否在间隔时间内(interval_limit > 0 时才检查)
if
(
$rule
[
'interval_limit'
]
>
0
)
{
$lastRecord
=
Db
::
name
(
'user_exp_log'
)
->
where
(
'user_id'
,
$userId
)
->
where
(
'rule_id'
,
$rule
[
'id'
])
->
order
(
'create_time'
,
'desc'
)
->
find
();
if
(
$lastRecord
&&
(
time
()
-
$lastRecord
[
'create_time'
])
<
$rule
[
'interval_limit'
])
{
return
;
}
}
Db
::
startTrans
();
try
{
// 增加用户经验
Db
::
name
(
'user'
)
->
where
(
'id'
,
$userId
)
->
inc
(
'experience'
,
$rule
[
'exp'
])
->
update
();
// 记录日志
Db
::
name
(
'user_exp_log'
)
->
insert
([
'user_id'
=>
$userId
,
'rule_id'
=>
$rule
[
'id'
],
'exp'
=>
$rule
[
'exp'
],
'create_time'
=>
time
(),
'ip'
=>
request
()
->
ip
()
]);
Db
::
commit
();
}
catch
(
\Exception
$e
)
{
Db
::
rollback
();
Log
::
error
(
"经验添加失败:
{
$userId
}
-
{
$rule
[
'id'
]
}
,msg:
{
}{$e->getMessage()
}
"
);
}
}
}
\ No newline at end of file
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