Commit 689ef567 authored by shajiaiming's avatar shajiaiming

Merge branch 'feature/issue_coin' into 'master'

Feature/issue coin See merge request !295
parents 2f1c0fce ded7b59c
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
namespace api\controllers; namespace api\controllers;
use common\models\psources\Coin;
use common\models\psources\CoinPlatformWithHold;
use common\service\chain33\Chain33Service; use common\service\chain33\Chain33Service;
use Yii; use Yii;
use api\base\BaseController; use api\base\BaseController;
...@@ -35,8 +37,8 @@ class WalletChainController extends BaseController ...@@ -35,8 +37,8 @@ class WalletChainController extends BaseController
$page = Yii::$app->request->get('page', 1); $page = Yii::$app->request->get('page', 1);
$size = Yii::$app->request->get('size', 10); $size = Yii::$app->request->get('size', 10);
$query = WalletChain::find() $query = CoinPlatformWithHold::find()
->select('id, token, platform, create_time, status') ->select('id, token, platform, status, create_time')
->where(['wallet_address' => $wallet_address]) ->where(['wallet_address' => $wallet_address])
->orderBy('id'); ->orderBy('id');
...@@ -75,14 +77,11 @@ class WalletChainController extends BaseController ...@@ -75,14 +77,11 @@ class WalletChainController extends BaseController
public function actionChain() public function actionChain()
{ {
$request = Yii::$app->request; $request = Yii::$app->request;
if (!$request->isPost) {
$this->code = -1;
$this->msg = '请求方式错误!';
goto doEnd;
}
$post = $request->post(); $post = $request->post();
$model = new WalletChain();
$model->setScenario(WalletChain::SCENARIOS_CREATE); if ($request->isPost) {
$model = new CoinPlatformWithHold();
$model->setScenario(CoinPlatformWithHold::SCENARIOS_CREATE);
$params = [ $params = [
'platform' => isset($post['platform']) ? $post['platform'] : '', 'platform' => isset($post['platform']) ? $post['platform'] : '',
'address' => isset($post['address']) ? $post['address'] : '', 'address' => isset($post['address']) ? $post['address'] : '',
...@@ -91,24 +90,21 @@ class WalletChainController extends BaseController ...@@ -91,24 +90,21 @@ class WalletChainController extends BaseController
'host' => isset($post['host']) ? $post['host'] : '', 'host' => isset($post['host']) ? $post['host'] : '',
'port' => isset($post['port']) ? $post['port'] : '', 'port' => isset($post['port']) ? $post['port'] : '',
'wallet_address' => isset($post['wallet_address']) ? $post['wallet_address'] : '', 'wallet_address' => isset($post['wallet_address']) ? $post['wallet_address'] : '',
'status' => WalletChain::STATUS_NO, 'status' => CoinPlatformWithHold::STATUS_UNPAID,
'origin' => WalletChain::ORIGIN_MANAGE, 'origin' => CoinPlatformWithHold::ORIGIN_USER,
'fee' => isset($post['fee']) ? $post['fee'] : '', 'fee' => isset($post['fee']) ? $post['fee'] : '',
'hash' => isset($post['hash']) ? $post['hash'] : '' 'exer' => 'user.p.' . (isset($post['platform']) ? $post['platform'] : '')
]; ];
if ($model->load($params, '') && !$model->save()) { if ($model->load($params, '') && !$model->validate()) {
$this->msg = $model->errors; $errors = '';
foreach ($model->errors as $error) {
$errors .= $error[0];
}
$this->msg = $errors;
$this->code = -1; $this->code = -1;
goto doEnd; goto doEnd;
} }
$id = Yii::$app->p_sources->getLastInsertID();
// $params = [
// 'platform' => 'BZCHAIN',
// 'host' => '112.74.59.221',
// 'port' => 1235,
// ];
$node_params = Yii::$app->params['para']; $node_params = Yii::$app->params['para'];
$service = new Chain33Service($node_params); $service = new Chain33Service($node_params);
...@@ -119,11 +115,45 @@ class WalletChainController extends BaseController ...@@ -119,11 +115,45 @@ class WalletChainController extends BaseController
goto doEnd; goto doEnd;
} }
$chain_update = WalletChain::find()->where(['id' => $id])->one(); $model->save();
$chain_update->setScenario(WalletChain::SCENARIOS_UPDATE); $this->data = (int)Yii::$app->p_sources->getLastInsertID();
$chain_update->status = WalletChain::STATUS_YES; goto doEnd;
$chain_update->save(); }
if ($request->isPut) {
$id = isset($post['id']) ? $post['id'] : 0;
$hash = isset($post['hash']) ? $post['hash'] : 0;
if (false == $id || false == $hash) {
$this->msg = '缺少必要的参数';
$this->code = -1;
goto doEnd;
}
$model = CoinPlatformWithHold::findOne($id);
if (empty($model)) {
$this->msg = '数据不存在';
$this->code = -1;
goto doEnd;
}
$model->setScenario(CoinPlatformWithHold::SCENARIOS_UPDATE);
$params = [
'id' => $id,
'hash' => $hash,
'status' => CoinPlatformWithHold::STATUS_CREATEING
];
if ($model->load($params, '') && !$model->save()) {
$errors = '';
foreach ($model->errors as $error) {
$errors .= $error[0];
}
$this->msg = $errors;
$this->code = -1;
goto doEnd;
}
$this->syncCoin(['platform' => $model['platform'], 'token' => $model['token']]);
}
doEnd : doEnd :
return ['code' => $this->code, 'data' => $this->data, 'msg' => $this->msg]; return ['code' => $this->code, 'data' => $this->data, 'msg' => $this->msg];
...@@ -152,7 +182,7 @@ class WalletChainController extends BaseController ...@@ -152,7 +182,7 @@ class WalletChainController extends BaseController
} }
$model = WalletChain::find()->select('platform, token, address, private_key, fee, host, port, hash, status')->where(['id' => (int)$id])->asArray()->one(); $model = CoinPlatformWithHold::find()->select('platform, token, address, private_key, fee, host, port, hash, status, create_time')->where(['id' => (int)$id])->asArray()->one();
if (empty($model)) { if (empty($model)) {
goto doEnd; goto doEnd;
} }
...@@ -165,4 +195,15 @@ class WalletChainController extends BaseController ...@@ -165,4 +195,15 @@ class WalletChainController extends BaseController
doEnd : doEnd :
return ['code' => $this->code, 'data' => $this->data, 'msg' => $this->msg]; return ['code' => $this->code, 'data' => $this->data, 'msg' => $this->msg];
} }
public function syncCoin($params = [])
{
$model = new Coin();
$model->nickname = ['ja' => '', 'en-US' => '', 'zh-CN' => ''];
$model->name = $params['token'];
$model->platform = $params['platform'];
$model->chain = 'BTY';
$model->platform_id = 1;
$model->save();
}
} }
\ No newline at end of file
...@@ -10,6 +10,71 @@ namespace common\models\psources; ...@@ -10,6 +10,71 @@ namespace common\models\psources;
class CoinPlatformWithHold extends BaseActiveRecord class CoinPlatformWithHold extends BaseActiveRecord
{ {
//定义场景
const SCENARIOS_CREATE = 'create';
const SCENARIOS_UPDATE = 'update';
const SCENARIOS_CONSOLE = 'console';
const STATUS_NO = 0; //创建失败
const STATUS_YES = 1; //创建成功
const STATUS_CREATEING = 2; //生效中
const STATUS_UNPAID = 3; //尚未支付
const ORIGIN_MANAGE = 1; //管理员创建
const ORIGIN_USER = 2; //h5用户创建
public function attributeLabels()
{
return [
'id' => 'ID',
'platform' => '平行链名称',
'address' => '代扣地址',
'private_key' => '代扣私钥',
'execer' => '执行器',
'fee' => '手续费',
'brower_url' => '浏览器链接',
'token' => '手续费(基础代币)',
'host' => '钱包服务IP',
'port' => '钱包服务端口',
'wallet_address' => '钱包地址',
'status' => '申请状态',
'origin' => '创建人',
'hash' => '申请费用hash'
];
}
public function rules()
{
return [
[['platform', 'address', 'private_key', 'exer', 'brower_url', 'token', 'host', 'wallet_address', 'status', 'port', 'fee', 'origin'], 'required'],
[['platform', 'address', 'private_key', 'exer', 'brower_url', 'token', 'host', 'wallet_address', 'hash'], 'string'],
['address','unique','message'=>'地址已存在'],
['platform','unique','message'=>'平行链已存在'],
[['platform'], 'string', 'length' => [1, 30]],
[['token'], 'string', 'length' => [1, 10]],
[['port', 'status', 'origin'], 'integer'],
['host', 'verfiyIp']
];
}
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['platform', 'token', 'address', 'private_key', 'fee', 'host', 'port', 'wallet_address', 'status', 'origin', 'hash', 'exer'],
self:: SCENARIOS_UPDATE => ['id', 'hash', 'status'],
self:: SCENARIOS_CONSOLE => ['status'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
public function verfiyIp($attribute, $params)
{
if (!preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $this->host)) {
$this->addError($attribute, '错误的Ip地址');
return false;
}
}
public static function tableName() public static function tableName()
{ {
return 'coin_platform_withhold'; return 'coin_platform_withhold';
......
...@@ -52,6 +52,7 @@ class WalletChain extends BaseActiveRecord ...@@ -52,6 +52,7 @@ class WalletChain extends BaseActiveRecord
return [ return [
[['platform', 'address', 'private_key', 'execer', 'brower_url', 'token', 'host', 'wallet_address', 'status', 'port', 'fee', 'origin', 'hash'], 'required'], [['platform', 'address', 'private_key', 'execer', 'brower_url', 'token', 'host', 'wallet_address', 'status', 'port', 'fee', 'origin', 'hash'], 'required'],
[['platform', 'address', 'private_key', 'execer', 'brower_url', 'token', 'host', 'wallet_address', 'hash'], 'string'], [['platform', 'address', 'private_key', 'execer', 'brower_url', 'token', 'host', 'wallet_address', 'hash'], 'string'],
['address','unique','message'=>'地址已存在'],
[['platform'], 'string', 'length' => [1, 30]], [['platform'], 'string', 'length' => [1, 30]],
[['token'], 'string', 'length' => [1, 10]], [['token'], 'string', 'length' => [1, 10]],
[['port', 'status', 'origin'], 'integer'], [['port', 'status', 'origin'], 'integer'],
......
...@@ -413,7 +413,7 @@ class Chain33Service ...@@ -413,7 +413,7 @@ class Chain33Service
'payload' => [ 'payload' => [
'status' => 1, 'status' => 1,
'queryAll' => true, 'queryAll' => true,
'symbolOnly' => true //'symbolOnly' => true
] ]
]; ];
return $this->send($params, 'Chain33.Query'); return $this->send($params, 'Chain33.Query');
......
...@@ -2,8 +2,10 @@ ...@@ -2,8 +2,10 @@
namespace console\controllers; namespace console\controllers;
use common\components\Tools;
use common\models\psources\CoinIssueRevokeRecord; use common\models\psources\CoinIssueRevokeRecord;
use common\models\psources\CoinPlatformWithHold; use common\models\psources\CoinPlatformWithHold;
use common\models\psources\WalletChain;
use Yii; use Yii;
use yii\console\Controller; use yii\console\Controller;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
...@@ -368,6 +370,65 @@ class IssueChainTransferController extends Controller ...@@ -368,6 +370,65 @@ class IssueChainTransferController extends Controller
return 0; return 0;
} }
public function actionUpdateChain()
{
$current_time = date('Y-m-d H:i:s',strtotime('-10 minute'));
$chains = CoinPlatformWithHold::find()
->where(['status' => CoinPlatformWithHold::STATUS_CREATEING, 'origin' => CoinPlatformWithHold::ORIGIN_USER])
->andWhere(['<', 'create_time', $current_time])
->all();
if (empty($chains)) {
echo date('Y-m-d H:i:s') . '无需更新的链' . PHP_EOL;
return 0;
}
foreach ($chains as $chain) {
$chain->setScenario(CoinPlatformWithHold::SCENARIOS_CONSOLE);
$chain->status = CoinPlatformWithHold::STATUS_YES;
$chain->save();
}
echo date('Y-m-d H:i:s') . '更新成功' . PHP_EOL;
return 0;
}
public function actionAsyncCoin()
{
$chains = CoinPlatformWithHold::find()->select('host, platform')
->where(['status' => 1, 'origin' => CoinPlatformWithHold::ORIGIN_USER])
->asArray()
->all();
foreach ($chains as $chain){
$node = [
'scheme' => 'http',
'host' => $chain['host'],
'port' => 8901
];
$chain_service = new Chain33Service($node);
$result = $chain_service->getTokens('token', 'GetTokens');
if (null != $result['error']) continue;
$tokens = isset($result['result']['tokens']) ? $result['result']['tokens'] : null;
if (null == $tokens) continue;
foreach ($tokens as $key => $token) {
$model_coin = Coin::find()->where(['name' => $token['symbol'], 'platform' => $chain['platform'], 'chain' => 'BTY'])->one();
if (empty($model_coin)) {
$model = new Coin();
$model->name = $token['symbol'];
$model->sid = $token['name'];
$model->nickname = ['ja' => '', 'en-US' => '', 'zh-CN' => ''];
$model->introduce = ['ja' => '', 'en-US' => '', 'zh-CN' => $token['introduction']];
$model->platform = $chain['platform'];
$model->publish_count = $token['total'];
$model->chain = 'BTY';
$model->platform_id = 1;
$model->treaty = 1;
$model->save();
}
}
}
}
public function syncCoin($params = []) public function syncCoin($params = [])
{ {
......
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