Commit 252dbf55 authored by shajiaiming's avatar shajiaiming

fix

parent 2f71423e
<?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
......@@ -17,6 +17,10 @@ use yii\web\BadRequestHttpException;
class BaseController extends Controller
{
public $code = 0;
public $msg = 'success';
public $data = null;
public function behaviors()
{
$request_controller = Yii::$app->controller->id;
......
<?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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment