Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
T
token
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
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
wallet
token
Commits
252dbf55
Commit
252dbf55
authored
Mar 24, 2020
by
shajiaiming
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix
parent
2f71423e
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
193 additions
and
0 deletions
+193
-0
SecKillController.php
api/controllers/SecKillController.php
+89
-0
BaseController.php
wallet/base/BaseController.php
+4
-0
CoinRelationController.php
wallet/controllers/CoinRelationController.php
+100
-0
No files found.
api/controllers/SecKillController.php
0 → 100644
View file @
252dbf55
<?php
namespace
api\controllers
;
use
common\models\psources\CoinExtract
;
use
Yii
;
use
api\base\BaseController
;
class
SecKillController
extends
BaseController
{
public
function
actionIndex
()
{
$num
=
$this
->
randomFloat
(
1
,
3
);
$expire
=
5
;
$nowTime
=
time
();
//新的过期时间
$expTime
=
$nowTime
+
$expire
;
$redis
=
\Yii
::
$app
->
redis_es
;
list
(
$good_id
,
$limit_min_amount
,
$limit_max_amount
)
=
$redis
->
hmget
(
'productInfo:16'
,
'good_id'
,
'limit_min_amount'
,
'limit_max_amount'
);
if
(
$num
<
$limit_min_amount
||
$num
>
$limit_max_amount
)
{
$this
->
msg
=
'err amount'
;
$this
->
code
=
-
1
;
goto
doEnd
;
}
$lock_key
=
'lock:'
.
$good_id
;
//var_dump($redis->del($lock_key));exit;
$stock
=
$redis
->
get
(
'stock:'
.
$good_id
);
if
(
0
==
$stock
)
{
$this
->
msg
=
'weclome again'
;
goto
doEnd
;
}
$is_locked
=
$redis
->
setnx
(
$lock_key
,
$expTime
);
if
(
!
$is_locked
)
{
$this
->
msg
=
'ahaaa, locked'
;
$this
->
code
=
-
1
;
goto
doEnd
;
}
$redis
->
setex
(
$lock_key
,
5
,
$expTime
);
$redis
->
incrbyfloat
(
'stock:'
.
$good_id
,
-
$num
);
$order_info
=
[
'good_id'
=>
$good_id
,
'amount'
=>
$num
,
'time'
=>
date
(
'Y-m-d H:i:s'
)
];
$redis
->
lpush
(
'order_info'
,
json_encode
(
$order_info
));
//开始其他业务逻辑
//下单。。。。。。
//用户信息写入redis,禁止重复下单
if
(
$redis
->
get
(
'stock:'
.
$good_id
)
<=
$limit_min_amount
)
{
//下架操作。。。。。。
$redis
->
set
(
'stock:'
.
$good_id
,
0
);
}
$redis
->
del
(
$lock_key
);
$expireTime
=
$redis
->
get
(
'lock:'
.
$good_id
);
if
(
$expireTime
<
$nowTime
)
{
}
doEnd
:
return
[
'code'
=>
$this
->
code
,
'msg'
=>
$this
->
msg
];
}
function
actionTest
()
{
$model
=
CoinExtract
::
find
()
->
where
([
'id'
=>
8
])
->
one
();
if
(
!
$model
->
total
>
0
)
{
$this
->
msg
=
'weclome again'
;
goto
doEnd
;
}
$model
->
total
=
$model
->
total
-
1
;
$model
->
save
();
doEnd
:
return
[
'code'
=>
$this
->
code
,
'msg'
=>
$this
->
msg
];
}
function
randomFloat
(
$min
=
0
,
$max
=
1
)
{
return
$min
+
mt_rand
()
/
mt_getrandmax
()
*
(
$max
-
$min
);
}
}
\ No newline at end of file
wallet/base/BaseController.php
View file @
252dbf55
...
@@ -17,6 +17,10 @@ use yii\web\BadRequestHttpException;
...
@@ -17,6 +17,10 @@ use yii\web\BadRequestHttpException;
class
BaseController
extends
Controller
class
BaseController
extends
Controller
{
{
public
$code
=
0
;
public
$msg
=
'success'
;
public
$data
=
null
;
public
function
behaviors
()
public
function
behaviors
()
{
{
$request_controller
=
Yii
::
$app
->
controller
->
id
;
$request_controller
=
Yii
::
$app
->
controller
->
id
;
...
...
wallet/controllers/CoinRelationController.php
0 → 100644
View file @
252dbf55
<?php
namespace
wallet\controllers
;
use
Yii
;
use
wallet\base\BaseController
;
use
common\models\psources\WalletCoin
;
class
CoinRelationController
extends
BaseController
{
public
function
actionIndex
()
{
if
(
Yii
::
$app
->
request
->
isGet
)
{
$page
=
\Yii
::
$app
->
request
->
get
(
'page'
,
1
);
$size
=
\Yii
::
$app
->
request
->
get
(
'size'
,
50
);
$name
=
\Yii
::
$app
->
request
->
get
(
'name'
,
''
);
$chain
=
\Yii
::
$app
->
request
->
get
(
'chain'
,
''
);
$platform
=
\Yii
::
$app
->
request
->
get
(
'platform'
,
''
);
$platform_id
=
\Yii
::
$app
->
request
->
get
(
'platform_id'
,
''
);
$query
=
WalletCoin
::
find
()
->
select
(
'id, name, nickname, icon, introduce, official, paper, platform, chain, release, area_search, publish_count, circulate_count, decimals, address, treaty, platform_id'
)
->
where
([
'>'
,
"find_in_set(
$platform_id
, platform_id)"
,
0
]);
if
(
false
!=
$name
)
{
$query
->
andWhere
([
'name'
=>
$name
]);
}
if
(
false
!=
$chain
)
{
$query
->
andWhere
([
'chain'
=>
$chain
]);
}
if
(
false
!=
$platform
)
{
$query
->
andWhere
([
'platform'
=>
$platform
]);
}
$model
=
$query
->
offset
((
$page
-
1
)
*
$size
)
->
orderBy
(
'id desc'
)
->
limit
(
$size
)
->
all
();
$countQuery
=
clone
$query
;
$this
->
data
=
[
'items'
=>
$model
,
'total'
=>
$countQuery
->
count
()
];
goto
doEnd
;
}
if
(
Yii
::
$app
->
request
->
isPost
)
{
$params
=
Yii
::
$app
->
request
->
post
();
$coin_id
=
isset
(
$params
[
'coin_id'
])
?
$params
[
'coin_id'
]
:
''
;
$platform_id
=
isset
(
$params
[
'platform_id'
])
?
(
int
)
$params
[
'platform_id'
]
:
0
;
if
(
false
==
$coin_id
||
false
==
$platform_id
)
{
$this
->
code
=
20001
;
$this
->
msg
=
'Validation failed.'
;
goto
doEnd
;
}
$coin_id_arr
=
explode
(
','
,
$coin_id
);
foreach
(
$coin_id_arr
as
$coin_id
)
{
$coin
=
WalletCoin
::
find
()
->
where
([
'id'
=>
$coin_id
])
->
one
();
if
(
empty
(
$coin
))
{
continue
;
}
if
(
strpos
(
$coin
->
platform_id
,
",
$platform_id
,"
)
!==
false
)
{
goto
doEnd
;
}
$coin
->
platform_id
=
$coin
->
platform_id
.
",
$platform_id
"
;
$coin
->
save
();
}
goto
doEnd
;
}
if
(
Yii
::
$app
->
request
->
isDelete
)
{
$coin_id
=
\Yii
::
$app
->
request
->
get
(
'coin_id'
,
0
);
$platform_id
=
\Yii
::
$app
->
request
->
get
(
'platform_id'
,
0
);
if
(
false
==
$coin_id
||
false
==
$platform_id
||
1
==
$platform_id
)
{
$this
->
code
=
20001
;
$this
->
msg
=
'Validation failed.'
;
goto
doEnd
;
}
$coin
=
WalletCoin
::
find
()
->
where
([
'id'
=>
$coin_id
])
->
one
();
if
(
empty
(
$coin
))
{
goto
doEnd
;
}
$platform_id_arr
=
explode
(
','
,
$coin
->
platform_id
);
$temp
=
''
;
foreach
(
$platform_id_arr
as
$val
){
if
(
$platform_id
==
$val
)
continue
;
$temp
.=
$val
.
','
;
}
$coin
->
platform_id
=
rtrim
(
$temp
,
','
);
$coin
->
save
();
goto
doEnd
;
}
doEnd
:
return
[
'code'
=>
$this
->
code
,
'msg'
=>
$this
->
msg
,
'data'
=>
$this
->
data
];
}
}
\ 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