Commit 22a47a6e authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/optimize' into 'master'

Feature/optimize See merge request !307
parents c3327d47 e2915984
......@@ -3,6 +3,7 @@
namespace api\controllers;
use common\business\ExchangeBusiness;
use common\models\psources\CoinSupportedChain;
use Yii;
use api\base\BaseController;
use common\models\psources\WalletRecommendCoin;
......@@ -34,12 +35,12 @@ class RecommendCoinController extends BaseController
$recommend_coin = WalletRecommendCoin::find()->where(['category_id' => $category['id']])->all();
$items = [];
foreach ($recommend_coin as $coin) {
$temp['id'] = $coin->coin['id'];
$temp['icon'] = $coin->coin['icon'];
$temp['name'] = $coin->coin['name'];
$temp['platform'] = $coin->coin['platform'];
$temp['chain'] = $coin->coin['chain'];
$temp['treaty'] = $coin->coin['treaty'];
$temp['exchange_id'] = $coin->coin['exchange_id'];
$nickname = json_decode($coin->coin['nickname'], true);
$temp['nickname'] = isset($nickname[$this->lang]) ? $nickname[$this->lang] : '';
$introduce = json_decode($coin->coin['introduce'], true);
......@@ -92,14 +93,15 @@ class RecommendCoinController extends BaseController
$category['name'] = isset($name[$this->lang]) ? $name[$this->lang] : '';
$recommend_coin = WalletRecommendCoin::find()->where(['category_id' => $category['id']])->all();
unset($category['id']);
$items = [];
foreach ($recommend_coin as $coin) {
$temp['id'] = $coin->coin['id'];
$temp['icon'] = $coin->coin['icon'];
$temp['name'] = $coin->coin['name'];
$temp['platform'] = $coin->coin['platform'];
$temp['chain'] = $coin->coin['chain'];
$temp['treaty'] = $coin->coin['treaty'];
$temp['exchange_id'] = $coin->coin['exchange_id'];
$nickname = json_decode($coin->coin['nickname'], true);
$temp['nickname'] = isset($nickname[$this->lang]) ? $nickname[$this->lang] : '';
$introduce = json_decode($coin->coin['introduce'], true);
......@@ -108,8 +110,27 @@ class RecommendCoinController extends BaseController
}
$category['items'] = $items;
}
$this->data = $recommend_category;
$supported_chain_model = CoinSupportedChain::find()->where(['platform_id' => $platform_id])->all();
$data = [];
if (false != $supported_chain_model) {
foreach ($supported_chain_model as $key => &$val) {
$data[$key]['id'] = isset($val->coin->id) ? $val->coin->id : '';
$data[$key]['icon'] = isset($val->coin->icon) ? $val->coin->icon : '';
$data[$key]['name'] = isset($val->coin->name) ? $val->coin->name : '';
$data[$key]['platform'] = isset($val->coin->platform) ? $val->coin->platform : '';
$data[$key]['chain'] = isset($val->coin->chain) ? $val->coin->chain : '';
$data[$key]['treaty'] = isset($val->coin->treaty) ? $val->coin->treaty : '';
$data[$key]['nickname'] = isset($val->coin->nickname[$this->lang]) ? $val->coin->nickname[$this->lang] : '';
$data[$key]['introduce'] = isset($val->coin->introduce[$this->lang]) ? $val->coin->introduce[$this->lang] : '';
}
$supported_chain = [
'name' => '主链',
'items' => $data
];
array_unshift($this->data, $supported_chain);
}
doEnd :
return ['code' => $this->code, 'data' => $this->data, 'msg' => $this->msg];
}
......
<?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
......@@ -33,15 +33,10 @@ class SupportedChainController extends BaseController
$data[$key]['name'] = isset($val->coin->name) ? $val->coin->name : '';
$data[$key]['icon'] = isset($val->coin->icon) ? $val->coin->icon : '';
$data[$key]['name'] = isset($val->coin->name) ? $val->coin->name : '';
$data[$key]['sid'] = isset($val->coin->sid) ? $val->coin->sid : '';
$data[$key]['nickname'] = isset($val->coin->nickname[$this->lang]) ? $val->coin->nickname[$this->lang] : '';
$data[$key]['chain'] = isset($val->coin->chain) ? $val->coin->chain : '';
$data[$key]['platform'] = isset($val->coin->platform) ? $val->coin->platform : '';
$data[$key]['sort'] = isset($val->coin->sort) ? $val->coin->sort : 0;
$data[$key]['treaty'] = isset($val->coin->treaty) ? $val->coin->treaty : '';
$data[$key]['optional_name'] = isset($val->coin->optional_name) ? $val->coin->optional_name : '';
$data[$key]['rmb'] = 0;
$data[$key]['usd'] = 0;
......
......@@ -49,7 +49,7 @@ class WalletCoinController extends BaseController
foreach ($condition as $val) {
if (is_array($val)) {
$model = WalletCoin::find()->select('id,sid,icon,name,nickname,introduce,platform,chain,address as contract_address,treaty')
$model = WalletCoin::find()->select('id,icon,name,nickname,introduce,platform,chain,address as contract_address,treaty')
->where(['name' => $val[0], 'platform' => $val[1]])
->andWhere(['in', 'id', $relation_ids])
->asArray()
......@@ -57,7 +57,7 @@ class WalletCoinController extends BaseController
}
if (is_string($val)) {
$model = WalletCoin::find()->select('id,sid,icon,name,nickname,introduce,platform,chain,address as contract_address,treaty')
$model = WalletCoin::find()->select('id,icon,name,nickname,introduce,platform,chain,address as contract_address,treaty')
->where(['name' => $val])
->andWhere(['in', 'id', $relation_ids])
->asArray()
......@@ -136,7 +136,7 @@ class WalletCoinController extends BaseController
$relation_ids = array_column($relation_model, 'coin_id');
$query = WalletCoin::find()
->select('id, sid, icon, name, nickname, platform, chain, address as contract_address, treaty')
->select('id, icon, name, nickname, platform, chain, address as contract_address, treaty')
->where(['in', 'id', $relation_ids])
->orderBy('id');
......@@ -188,7 +188,7 @@ class WalletCoinController extends BaseController
goto doEnd;
}
$relation = WalletCoinRelation::find()->where(['coin_id' => $id, 'platform_id' => (int)$platform_id])->one();
if (empty($relation)){
if (empty($relation)) {
goto doEnd;
}
......
......@@ -46,7 +46,7 @@ class CoinSupportedChain extends BaseActiveRecord
public function getCoin()
{
return $this->hasOne(Coin::className(), ['id' => 'coin_id'])
->select('id, sid, icon, name, introduce, optional_name, nickname, platform, chain, treaty');
return $this->hasOne(WalletCoin::className(), ['id' => 'coin_id'])
->select('id, icon, name, introduce, nickname, platform, chain, treaty');
}
}
......@@ -19,6 +19,6 @@ class WalletRecommendCoin extends BaseActiveRecord
public function getCoin()
{
return $this->hasOne(WalletCoin::className(), ['id' => 'cid'])->select(['id', 'icon', 'name', 'introduce', 'nickname', 'platform', 'chain', 'treaty', 'exchange_id'])->asArray();
return $this->hasOne(WalletCoin::className(), ['id' => 'cid'])->select(['id', 'icon', 'name', 'introduce', 'nickname', 'platform', 'chain', 'treaty'])->asArray();
}
}
\ 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) {
$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