Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
taobao-mini-template
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
2
Issues
2
List
Board
Labels
Milestones
Merge Requests
1
Merge Requests
1
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
qinhaitao
taobao-mini-template
Commits
ff6eea1d
Commit
ff6eea1d
authored
Nov 11, 2020
by
qinhaitao
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 限流
parent
f7119984
Changes
9
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
128 additions
and
5 deletions
+128
-5
preUpdate.ts
v2.0/src/decorator/common/preUpdate.ts
+1
-1
requestCountLimit.ts
v2.0/src/decorator/common/requestCountLimit.ts
+65
-0
requestRateLimit.ts
v2.0/src/decorator/common/requestRateLimit.ts
+53
-0
checkHelpRecord.ts
v2.0/src/utils/common/check/checkHelpRecord.ts
+1
-1
checkInviteId.ts
v2.0/src/utils/common/check/checkInviteId.ts
+1
-1
checkJoinId.ts
v2.0/src/utils/common/check/checkJoinId.ts
+1
-1
checkValidPrize.ts
v2.0/src/utils/common/check/checkValidPrize.ts
+1
-1
mongodb.ts
v2.0/src/utils/common/mongodb.ts
+0
-0
sql.ts
v2.0/src/utils/common/sql.ts
+5
-0
No files found.
v2.0/src/decorator/common/preUpdate.ts
View file @
ff6eea1d
...
@@ -5,7 +5,7 @@ import { recordErrorLog } from '../../utils/common/logger'
...
@@ -5,7 +5,7 @@ import { recordErrorLog } from '../../utils/common/logger'
import
{
resultsModel
}
from
'../../sdk'
import
{
resultsModel
}
from
'../../sdk'
import
{
CODE_TYPES
}
from
'../../errorCode'
import
{
CODE_TYPES
}
from
'../../errorCode'
import
{
formatUpdatedDataByProjection
,
formatUpdateUserProjection
}
from
'../../utils/common/format'
import
{
formatUpdatedDataByProjection
,
formatUpdateUserProjection
}
from
'../../utils/common/format'
import
{
dbUpdate
}
from
'../../utils/common/db'
import
{
dbUpdate
}
from
'../../utils/common/
mongo
db'
import
{
USER_DB_NAME
}
from
'../../db'
import
{
USER_DB_NAME
}
from
'../../db'
// 检验参数是否存在
// 检验参数是否存在
...
...
v2.0/src/decorator/common/requestCountLimit.ts
0 → 100644
View file @
ff6eea1d
/** @format */
import
{
resultsModel
}
from
'../../sdk'
import
{
CODE_TYPES
}
from
'../../constants'
import
{
logFn
,
recordErrorLog
}
from
'../../utils'
/**
* 限制单位时间接口请求次数
*
* @export
* @param {number} limitCount 限制次数
* @param {number} limitTime 限制的单位时间
* @return {*}
*/
export
default
function
requestCountLimit
(
limitCount
:
number
,
limitTime
:
number
)
{
return
function
(
target
:
Object
,
name
:
string
,
descriptor
:
PropertyDescriptor
)
{
const
method
=
descriptor
.
value
descriptor
.
value
=
async
function
(...
args
:
any
[])
{
const
recordTableName
=
'c_test'
const
[
context
,
baseInfos
=
{},
services
=
[],
preCheckData
=
{}]
=
args
const
{
activityId
}
=
context
.
data
const
{
openId
}
=
context
const
handler
=
context
?.
cloud
?.
dataspace
?.
context
?.
handler
const
now
=
Date
.
now
()
try
{
const
data
=
await
context
.
cloud
.
dataspace
.
executeSql
(
`SELECT gmt_modified, expired_time, count FROM
${
recordTableName
}
where activity_id = ? and open_id = ? and handler = ?`
,
[
activityId
,
openId
,
handler
]
)
const
handlerRecord
=
data
?.[
0
]
if
(
!
handlerRecord
)
{
context
.
cloud
.
dataspace
.
executeSql
(
`insert into
${
recordTableName
}
(activity_id,open_id,handler,count, expired_time) values(?,?)`
,
[
activityId
,
openId
,
handler
,
1
,
now
+
limitTime
]
)
}
else
{
if
(
now
<=
handlerRecord
.
expired_time
)
{
if
(
handlerRecord
?.
count
<
limitCount
)
{
await
context
.
cloud
.
dataspace
.
executeSql
(
`update
${
recordTableName
}
set count = ? where activity_id = ? and open_id = ? and handler = ?`
,
[
handlerRecord
?.
count
+
1
,
activityId
,
openId
,
handler
]
)
}
else
{
return
resultsModel
.
error
(
CODE_TYPES
.
SYSTEM_ERROR
,
'请求过于频繁,请稍后再试~'
)
}
}
if
(
now
>
handlerRecord
.
expired_time
)
{
await
context
.
cloud
.
dataspace
.
executeSql
(
`update
${
recordTableName
}
set count = ? expired_time = ? where activity_id = ? and open_id = ? and handler = ?`
,
[
1
,
now
+
limitTime
,
activityId
,
openId
,
handler
]
)
}
}
}
catch
(
error
)
{
console
.
log
(
'error'
,
error
)
recordErrorLog
(
context
,
baseInfos
,
error
.
toString
(),
error
.
stack
)
return
resultsModel
.
error
(
CODE_TYPES
.
SYSTEM_ERROR
)
}
return
method
.
apply
(
target
,
[
context
,
baseInfos
,
services
,
preCheckData
])
}
}
}
v2.0/src/decorator/common/requestRateLimit.ts
0 → 100644
View file @
ff6eea1d
/** @format */
import
{
resultsModel
}
from
'../../sdk'
import
{
CODE_TYPES
}
from
'../../constants'
import
{
logFn
,
recordErrorLog
}
from
'../../utils'
// 限制请求频率 rate单位 ms
export
default
function
requestRateLimit
(
rate
=
500
)
{
return
function
(
target
:
Object
,
name
:
string
,
descriptor
:
PropertyDescriptor
)
{
const
method
=
descriptor
.
value
descriptor
.
value
=
async
function
(...
args
:
any
[])
{
const
recordTableName
=
'c_test'
const
[
context
,
baseInfos
=
{},
services
=
[],
preCheckData
=
{}]
=
args
const
{
activityId
}
=
context
.
data
const
{
openId
}
=
context
const
handler
=
context
?.
cloud
?.
dataspace
?.
context
?.
handler
try
{
const
data
=
await
context
.
cloud
.
dataspace
.
executeSql
(
`SELECT gmt_modified FROM
${
recordTableName
}
where activity_id = ? and open_id = ? and handler = ?`
,
[
activityId
,
openId
,
handler
]
)
const
handlerRecord
=
data
?.[
0
]
if
(
!
handlerRecord
)
{
context
.
cloud
.
dataspace
.
executeSql
(
`insert into
${
recordTableName
}
(activity_id,open_id,handler,count) values(?,?)`
,
[
activityId
,
openId
,
handler
,
1
]
)
}
else
{
const
lastRequestTime
=
new
Date
(
handlerRecord
.
gmt_modified
).
getTime
()
const
now
=
Date
.
now
()
if
(
now
-
lastRequestTime
<
rate
)
{
return
resultsModel
.
error
(
CODE_TYPES
.
SYSTEM_ERROR
,
'请求过于频繁,请稍后再试~'
)
}
else
{
await
context
.
cloud
.
dataspace
.
executeSql
(
`update
${
recordTableName
}
set gmt_modified = ? where activity_id = ? and open_id = ? and handler = ?`
,
[
Date
.
now
(),
activityId
,
openId
,
handler
]
)
}
}
}
catch
(
error
)
{
console
.
log
(
'error'
,
error
)
recordErrorLog
(
context
,
baseInfos
,
error
.
toString
(),
error
.
stack
)
return
resultsModel
.
error
(
CODE_TYPES
.
SYSTEM_ERROR
)
}
return
method
.
apply
(
target
,
[
context
,
baseInfos
,
services
,
preCheckData
])
}
}
}
v2.0/src/utils/common/check/checkHelpRecord.ts
View file @
ff6eea1d
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
import
{
resultsModel
}
from
'../../../sdk'
import
{
resultsModel
}
from
'../../../sdk'
import
{
CODE_TYPES
}
from
'../../../errorCode'
import
{
CODE_TYPES
}
from
'../../../errorCode'
import
{
dbCount
}
from
'../../common/db'
import
{
dbCount
}
from
'../../common/
mongo
db'
import
{
STAT_DB_NAME
}
from
'../../../db'
import
{
STAT_DB_NAME
}
from
'../../../db'
import
{
STAT_TYPE
}
from
'../../../constants'
import
{
STAT_TYPE
}
from
'../../../constants'
...
...
v2.0/src/utils/common/check/checkInviteId.ts
View file @
ff6eea1d
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
import
{
resultsModel
}
from
'../../../sdk'
import
{
resultsModel
}
from
'../../../sdk'
import
{
CODE_TYPES
}
from
'../../../errorCode'
import
{
CODE_TYPES
}
from
'../../../errorCode'
import
{
dbFindOne
}
from
'../../common/db'
import
{
dbFindOne
}
from
'../../common/
mongo
db'
import
{
USER_DB_NAME
}
from
'../../../db'
import
{
USER_DB_NAME
}
from
'../../../db'
export
default
async
function
checkInviteId
(
context
:
IContext
<
{
activityId
:
string
;
inviteId
:
string
}
>
)
{
export
default
async
function
checkInviteId
(
context
:
IContext
<
{
activityId
:
string
;
inviteId
:
string
}
>
)
{
...
...
v2.0/src/utils/common/check/checkJoinId.ts
View file @
ff6eea1d
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
import
{
resultsModel
}
from
'../../../sdk'
import
{
resultsModel
}
from
'../../../sdk'
import
{
CODE_TYPES
}
from
'../../../errorCode'
import
{
CODE_TYPES
}
from
'../../../errorCode'
import
{
dbFindOne
}
from
'../db'
import
{
dbFindOne
}
from
'../
mongo
db'
import
{
JOIN_DB_NAME
}
from
'../../../db'
import
{
JOIN_DB_NAME
}
from
'../../../db'
export
default
async
function
checkJoinId
(
context
:
IContext
<
IParams
>
)
{
export
default
async
function
checkJoinId
(
context
:
IContext
<
IParams
>
)
{
...
...
v2.0/src/utils/common/check/checkValidPrize.ts
View file @
ff6eea1d
...
@@ -3,7 +3,7 @@
...
@@ -3,7 +3,7 @@
import
{
resultsModel
}
from
'../../../sdk'
import
{
resultsModel
}
from
'../../../sdk'
import
{
CODE_TYPES
}
from
'../../../errorCode'
import
{
CODE_TYPES
}
from
'../../../errorCode'
import
{
DRAW_STATUS
,
PRIZE_TYPE
}
from
'../../../constants'
import
{
DRAW_STATUS
,
PRIZE_TYPE
}
from
'../../../constants'
import
{
dbFindOne
}
from
'../db'
import
{
dbFindOne
}
from
'../
mongo
db'
import
{
AWARDS_DB_NAME
}
from
'../../../db'
import
{
AWARDS_DB_NAME
}
from
'../../../db'
export
default
async
function
checkValidPrize
(
prizeTypes
:
number
[])
{
export
default
async
function
checkValidPrize
(
prizeTypes
:
number
[])
{
...
...
v2.0/src/utils/common/db.ts
→
v2.0/src/utils/common/
mongo
db.ts
View file @
ff6eea1d
File moved
v2.0/src/utils/common/sql.ts
0 → 100644
View file @
ff6eea1d
/** @format */
export
async
function
sqlSelect
<
T
>
(
context
:
IContext
<
IParams
>
,
tableName
:
string
,
que
)
{
return
context
.
cloud
.
dataspace
.
executeSql
(
`SELECT * FROM
${
tableName
}
where id = ? and `
,
[
context
.
data
.
id
])
}
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