Commit 42b1c4d3 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/airdrop' into 'master'

Feature/airdrop See merge request !337
parents 0d102990 d527b88d
......@@ -3,6 +3,7 @@
namespace api\controllers;
use Yii;
use common\components\Tools;
use api\base\BaseController;
use common\models\psources\AirDrop;
use common\service\chain33\Chain33Service;
......@@ -12,6 +13,29 @@ use common\models\psources\AirDropApplyRecord;
class AirDropController extends BaseController
{
public function actionValidateIdentifier()
{
$data = Yii::$app->request->get();
$identifier = $data['identifier'] ?? null;
if (false == $identifier) {
$this->code = -1;
$this->msg = '设备号错误,请重新输入.';
goto doEnd;
}
$exist = AirDropRulePool::find()->where(['identifier' => $identifier])->one();
if (false == $exist || false == $exist->rule) {
$this->code = -1;
$this->msg = '设备号错误,请重新输入.';
goto doEnd;
}
doEnd :
return ['code' => $this->code, 'msg' => $this->msg, 'data' => $this->data];
}
/**
* 参于空投申请/空投申请列表
* @param identifier 树莓派编号
......@@ -46,6 +70,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 +113,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();
......@@ -124,6 +165,31 @@ class AirDropController extends BaseController
}
/**
* 验证今日是否可领取
* @param identifier 树莓派编号
* @return array
*/
public function actionValidate()
{
$data = Yii::$app->request->get();
$identifier = $data['identifier'] ?? '';
if (false == $identifier) {
$this->code = -1;
$this->msg = 'Validation failed.';
goto doEnd;
}
$redis = Yii::$app->redis_app;
if (true == $redis->exists($identifier)) {
$this->data = false;
goto doEnd;
}
$this->data = true;
doEnd :
return ['code' => $this->code, 'msg' => $this->msg, 'data' => $this->data];
}
/**
* 空投领取情况
* @param identifier 树莓派编号
* @param miner_address 矿工地址
......@@ -146,7 +212,13 @@ class AirDropController extends BaseController
}
$redis = Yii::$app->redis_app;
list($reach, $reached_times, $draw_success, $income, $un_draw) = $redis->hmget('airdrop:' . $identifier, 'reach', 'reached_times', 'draw_success', 'income', 'un_draw');
list($reached_times, $draw_success, $income, $un_draw) = $redis->hmget('airdrop:' . $identifier, 'reached_times', 'draw_success', 'income', 'un_draw');
$exist = AirDropRulePool::find()->where(['identifier' => $identifier])->one();
if (false == $exist) {
$reach = 0;
} else {
$reach = $exist->rule->duration;
}
$this->data = [
'reach' => empty($reach) ? 0 : (int)$reach,
'reached_times' => empty($reached_times) ? 0 : (int)$reached_times,
......@@ -202,6 +274,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 +289,90 @@ 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->wallet_address) || empty($model->record)) {
$this->code = -1;
$this->msg = '设备初始化中,请稍后再试.';
goto doEnd;
}
$expiry_date = date("Y-m-d", strtotime("+1 day"));
$record = AirDropApplyRecord::find()
->where(['apply_id' => $model->id, 'draw_status' => AirDropApplyRecord::STATUS_UNDRAW])
->andWhere(['<', 'create_time', $expiry_date])
->orderBy('id')
->limit(1)
->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