Commit d0181bff authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/airdrop' into develop

parents f50245cb 7dfdd6c1
......@@ -2,6 +2,7 @@
namespace api\controllers;
use common\components\Tools;
use Yii;
use api\base\BaseController;
use common\models\psources\AirDrop;
......@@ -46,6 +47,24 @@ class AirDropController extends BaseController
goto doEnd;
}
$model->save();
$apply_id = $model->id;
$time = $exist->rule->duration - 1;
$create_time = date('Y-m-d');
$finish_time = date('Y-m-d', strtotime("+$time day"));
$expiry_date = Tools::getDatesFromRange($create_time, $finish_time);
$apply_record_model = new AirDropApplyRecord();
foreach ($expiry_date as $key => $val) {
$apply_record_model->setIsNewRecord(true);
$apply_record_model->apply_id = $apply_id;
$apply_record_model->reach = AirDropApplyRecord::REACH_NO;
$apply_record_model->amount = $exist->rule->amount;
$apply_record_model->token = $exist->rule->token;
$apply_record_model->draw_status = AirDropApplyRecord::STATUS_UNDRAW;
$apply_record_model->create_time = $val;
$apply_record_model->update_time = $val;
$apply_record_model->save() && $apply_record_model->id = 0;;
}
$redis = Yii::$app->redis_app;
$redis->hmset('airdrop:' . $data['identifier'], 'reach', 0, 'draw_success', 0, 'income', 0, 'un_draw', 0);
......@@ -71,17 +90,16 @@ class AirDropController extends BaseController
}
$query = AirDropApplyRecord::find()
->select('id, reach, amount, token,draw_status, create_time')
->select('id, reach, amount, token,draw_status, create_time, update_time')
->where(['apply_id' => $model['id']]);
if (!empty($apply_ids)) {
$apply_ids = rtrim($apply_ids, ',');
$apply_id_arr = explode(',', $apply_ids);
$query->andWhere(['in', 'id', $apply_id_arr]);
}
$expiry_date = date("Y-m-d", strtotime("+1 day"));
$query->andWhere(['<', 'create_time', $expiry_date]);
$query->andWhere(['>', 'draw_status', AirDropApplyRecord::STATUS_UNDRAW]);
$items = $query->offset(($page - 1) * $size)->orderBy('create_time desc')->limit($size)->all();
$items = $query->offset(($page - 1) * $size)->orderBy('update_time desc')->limit($size)->all();
$countQuery = clone $query;
$total = (int)$countQuery->count();
......@@ -202,6 +220,12 @@ class AirDropController extends BaseController
return ['code' => $this->code, 'msg' => $this->msg, 'data' => $this->data];
}
/**
* 领取某日空投
* @param identifier 树莓派编号
* @param miner_address 矿工地址
* @return array
*/
public function actionDraw()
{
if (!Yii::$app->request->isPost) {
......@@ -211,6 +235,85 @@ class AirDropController extends BaseController
}
$data = Yii::$app->request->post();
$identifier = $data['identifier'] ?? '';
$miner_address = $data['miner_address'] ?? '';
if (false == $identifier || false == $miner_address) {
$this->code = -1;
$this->msg = 'Validation failed.';
goto doEnd;
}
$model = AirDrop::find()->select('id, wallet_address')->where(['identifier' => $identifier, 'miner_address' => $miner_address])->one();
if (empty($model) || empty($model->record)) {
$this->code = -1;
$this->msg = '暂无符合条件的空投申请记录';
goto doEnd;
}
$record = AirDropApplyRecord::find()->where(['apply_id' => $model->id, 'draw_status' => AirDropApplyRecord::STATUS_UNDRAW])->limit(1)->orderBy('id')->one();
if (empty($record)) {
$this->code = -1;
$this->msg = '暂无符合条件的领取记录';
goto doEnd;
}
$redis = Yii::$app->redis_app;
$value = date('Y-m-d H:i:s');
$is_locked = $redis->setnx($data['identifier'], $value);
if (!$is_locked) {
$this->msg = '已达今日领取上限,请明天再来!';
$this->code = -1;
goto doEnd;
}
$service = new Chain33Service();
$execer = 'ticket';
$result = $service->getBalance($model->wallet_address, $execer);
if (0 == $result['code']) {
$balance = $result['result'][0]['balance'] ?? 0;
$frozen = $result['result'][0]['frozen'] ?? 0;
if (($balance + $frozen) / 1e8 < 3000) {
$this->code = -1;
$this->msg = '未达标,请确认矿机正在挖矿后重新尝试';
$redis->del($data['identifier']);
goto doEnd;
}
}
$record->reach = AirDropApplyRecord::REACH_YES;
$record->draw_status = AirDropApplyRecord::STATUS_DRAWING;
$record->update_time = date('Y-m-d');
if (false == $record->save()) {
$this->code = -1;
$this->msg = $record->errors;
goto doEnd;
}
$redis->setex($data['identifier'], strtotime('23:59:59') - time(), $value);
doEnd :
return ['code' => $this->code, 'msg' => $this->msg, 'data' => $this->data];
}
/**
* 领取某日空投 (暂停用)
* @param id 某日id
* @param identifier 树莓派编号
* @param miner_address 矿工地址
* @return array
*/
public function actionDrawBak()
{
if (!Yii::$app->request->isPost) {
$this->code = -1;
$this->msg = '请求方式错误!';
goto doEnd;
}
$data = Yii::$app->request->post();
$id = $data['id'] ?? '';
$identifier = $data['identifier'] ?? '';
$miner_address = $data['miner_address'] ?? '';
......
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