Commit fc15d2de authored by rlgy's avatar rlgy

update

parent 7f2e58c0
<?php
/**
* Created By Sublime Text 3
*
* @author rlgyzhcn <rlgyzhcn@qq.com>
*/
namespace backend\controllers;
use common\business\Chain33Business;
use Yii;
use backend\models\coin\CoinPublishForm;
use common\models\psources\CoinPublish;
......@@ -17,16 +19,23 @@ class CoinPublishController extends BaseController
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json';
$get = Yii::$app->request->get();
$page = $get['page'] ?? 1;
$limit = $get['limit'] ?? 10;
$auth = Yii::$app->authManager;
$role_self = current($auth->getRolesByUser(Yii::$app->user->id));
$child_roles = $auth->getChildRoles($role_self->name);
$userful_roles = array_keys($child_roles);
// var_dump($userful_roles);die();
$condition[] = ['in', 'owner', $userful_roles];
$data = CoinPublish::getList($page, $limit, $condition);
$get = Yii::$app->request->get();
$page = $get['page'] ?? 1;
$limit = $get['limit'] ?? 10;
$auth = Yii::$app->authManager;
$role_self = current($auth->getRolesByUser(Yii::$app->user->id));
$child_roles = $auth->getChildRoles($role_self->name);
$userful_roles = array_keys($child_roles);
$condition[] = ['in', 'owner', $userful_roles];
$data = CoinPublish::getList($page, $limit, $condition);
//获取热钱包数量
if ($data['code'] == 0 && !empty($data['data'])) {
$list = &$data['data'];
$amount = Chain33Business::getMulAccountTokenAssets($list);
foreach ($list as $k => &$v) {
$v['amount'] = $amount[$v['id']];
}
}
return $data;
}
return $this->render('list');
......@@ -36,21 +45,21 @@ class CoinPublishController extends BaseController
{
if (Yii::$app->request->isGet) {
$this->layout = false;
$id = Yii::$app->request->get('id', null);
$id = Yii::$app->request->get('id', null);
if ($id) {
$model = CoinPublish::findOne($id);
} else {
$model = new CoinPublishForm();
}
$auth = Yii::$app->authManager;
$role = current($auth->getRolesByUser(Yii::$app->user->id));
$auth = Yii::$app->authManager;
$role = current($auth->getRolesByUser(Yii::$app->user->id));
$childRoles = array_keys($auth->getChildRoles($role->name));
// $coins = Coin::getAllByPlatformId(Yii::$app->user->identity->platform_id, 0, 999);
return $this->render('form', ['model' => $model, 'roles' => $childRoles]);
} elseif (Yii::$app->request->isPost) {
Yii::$app->response->format = 'json';
$post = Yii::$app->request->post();
$model = new CoinPublishForm();
$post = Yii::$app->request->post();
$model = new CoinPublishForm();
if (isset($post['id']) && !empty($post['id'])) {
$model->scenario = CoinPublishForm::SCENARIOS_UPDATE;
} else {
......@@ -65,9 +74,9 @@ class CoinPublishController extends BaseController
$CoinPublish->company_name = $model->company_name;
$CoinPublish->coin_name_zh = $model->coin_name_zh;
$CoinPublish->coin_name_en = $model->coin_name_en;
$CoinPublish->address = $model->address;
$CoinPublish->owner = $model->owner;
$CoinPublish->platform_id = Yii::$app->user->identity->platform_id;
$CoinPublish->address = $model->address;
$CoinPublish->owner = $model->owner;
$CoinPublish->platform_id = Yii::$app->user->identity->platform_id;
if ($CoinPublish->save(false)) {
return ['code' => 0, 'msg' => 'succeed', 'data' => ''];
} else {
......@@ -82,7 +91,7 @@ class CoinPublishController extends BaseController
public function actionDel()
{
Yii::$app->response->format = 'json';
$id = Yii::$app->request->get('id', null);
$id = Yii::$app->request->get('id', null);
if ($id) {
$model = CoinPublish::findOne(['id' => $id]);
if ($model->delete()) {
......
......@@ -7,11 +7,12 @@
namespace backend\controllers;
use Yii;
use backend\jobs\FreezeJob;
use common\models\psources\CoinPublish;
use common\models\psources\CoinPublishRule;
use common\models\psources\CoinReleaseMember;
use Yii;
use common\business\Chain33Business;
class CoinPublishRuleController extends BaseController
{
......@@ -78,11 +79,20 @@ class CoinPublishRuleController extends BaseController
$data['data'] = $models;
return $data;
}
$coin = CoinPublish::findOne($pid);
$coin = CoinPublish::findOne($pid);
$amount = Chain33Business::getAccountTokenAssets($coin->address, $coin->coin_name_en);
// var_dump($amount);die();
if ($amount['code'] == 0) {
$amount = $amount['amount'];
} else {
$amount = 'error';
}
//获取热钱包数量
return $this->render('list',
[
'coin_name' => $coin->coin_name_en ?? '',
'amount' => 100090000,
'amount' => $amount,
'release_amount' => CoinPublishRule::getReleaseAmountThisMonthById($pid),
'address' => $coin->address ?? '',
]);
......@@ -106,12 +116,12 @@ class CoinPublishRuleController extends BaseController
// 编号
$prefix = date('Ymd', time());
$form_database = CoinPublishRule::find()
->where(['like', 'sid', $prefix])
->orderBy(['sid' => SORT_DESC])
->asArray()
->One();
->where(['like', 'sid', $prefix])
->orderBy(['sid' => SORT_DESC])
->asArray()
->One();
if ($form_database) {
$sid = (int) $form_database['sid'] + 1;
$sid = (int)$form_database['sid'] + 1;
} else {
$sid = $prefix . '001';
}
......@@ -120,11 +130,11 @@ class CoinPublishRuleController extends BaseController
}
if ($model->load($post) && $model->validate()) {
if (CoinPublishRule::SCENARIOS_ADD == $model->scenario) {
$model->sid = (string) $sid;
$model->sid = (string)$sid;
}
$model->release_time = date_create_from_format(self::DATE_FORMAT[$model->repeat],
$model->release_time)->format('Y-m-d H:i:s');
$model->pid = $this->getPid();
$model->pid = $this->getPid();
if ($model->save()) {
if ($model->qid) {
//删除原有的任务
......@@ -176,6 +186,7 @@ class CoinPublishRuleController extends BaseController
/**
* 获取id
*
* @return int
*/
private function getPid()
......
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-9-18
* Time: 下午2:50
*/
namespace backend\jobs;
use Yii;
use yii\queue\JobInterface;
use yii\queue\Queue;
use yii\base\BaseObject;
use common\models\psources\CoinReleaseCheck;
use common\models\psources\CoinReleaseMember;
use common\models\psources\CoinPublishRule;
use common\models\psources\CoinPublish;
use common\business\Chain33Business;
/**
* Class ExtractTokenJob
* 提币任务
*
* @package backend\jobs
*/
class ExtractTokenJob extends BaseObject implements JobInterface
{
/**
* 提币申请id
*
* @var int $cid
*/
public $cid = 0;
/**
* @param Queue $queue which pushed and is handling the job
* @return integer
*/
public function execute($queue)
{
//获取申请
$apply = CoinReleaseCheck::findOne($this->cid);
if ($apply) {
$assets = CoinReleaseMember::findOne($apply->uid);
if ($assets) {
$release = $assets->release;
if ($release >= $apply->amount) {
//提币
$amount = $apply->amount / 1e8;
//获取提币地址
$from = '';
$coin_publish_rule = CoinPublishRule::findOne($assets->rule_id);
if ($coin_publish_rule) {
$coin_publish = CoinPublish::findOne($coin_publish_rule->pid);
if ($coin_publish) {
$from = $coin_publish->address;
}
}
if (!empty($from)) {
$result = Chain33Business::extractToken($from, $apply->to_address, $amount, '', true,
$apply->coin);
if ($result['code'] == 0) {
$assets->release -= $amount;
$assets->output += $amount;
if ($assets->save()) {
//提币成功
$apply->status = CoinReleaseCheck::STATUS_PADDING;//提币中
if ($apply->save()) {
Yii::info("提币成功: [申请id]: $this->cid", __CLASS__);
}
Yii::warning("提币申请状态修改失败: [申请id]: $this->cid", __CLASS__);
} else {
Yii::warning("减少用户资产失败: [申请id]: $this->cid, [资产]: $amount", __CLASS__);
}
} else {
var_dump($result);
Yii::info("提币失败: {$result['msg']}! [申请id]: $this->cid", __CLASS__);
}
} else {
Yii::info("提币失败: 提币地址不存在! [申请id]: $this->cid", __CLASS__);
}
} else {
Yii::info("提币失败: 用户资产不足! [申请id]: $this->cid", __CLASS__);
}
} else {
Yii::info("提币失败: 用户资产信息不存在! [申请id]: $this->cid", __CLASS__);
}
} else {
Yii::info("提币失败: 提币申请不存在! [申请id]: $this->cid", __CLASS__);
}
return 0;
}
}
\ No newline at end of file
......@@ -6,6 +6,7 @@
*/
use backend\assets\coinPublishRule\ListAsset;
ListAsset::register($this);
?>
......@@ -21,7 +22,7 @@ ListAsset::register($this);
<tbody>
<tr>
<td><?= $coin_name ?></td>
<td><?= $amount ?></td>
<td><?= $amount / 1e8 ?></td>
<td><?= $release_amount ?></td>
<td><?= $address ?></td>
</tr>
......
var table = layui.table;
table.render({
elem: "#table1",
url: '/admin/coin-publish/list',
limit: 10,
page: 1,
loading: true,
cols: [[
{field: 'id', title: 'ID'},
{field: 'company_name', title: '公司名称'},
{field: 'coin_name_zh', title: '币种中文名'},
{field: 'coin_name_en', title: '币种英文名'},
elem: "#table1",
url: '/admin/coin-publish/list',
limit: 10,
page: 1,
loading: true,
cols: [[
{field: 'id', title: 'ID'},
{field: 'company_name', title: '公司名称'},
{field: 'coin_name_zh', title: '币种中文名'},
{field: 'coin_name_en', title: '币种英文名'},
{field: 'address', title: '热钱包地址'},
{field: 'amount', title: '热钱包数量'},
{toolbar: '#toolbar', title: '操作'}
]],
{
field: 'amount', title: '热钱包数量', templet: function (d) {
return d.amount / 1e8;
}
},
{toolbar: '#toolbar', title: '操作'}
]],
});
table.on('tool(table1)', function(obj){
var event = obj.event;
var data = obj.data;
if ('update' == event) {
$.get('/admin/coin-publish/add', {id: data.id}, function (str) {
var index = layer.open({
type: 1,
title: '编辑',
id: 'add-one',
skin: 'layui-layer-lan',
area: ['320px', 'auto'],
content: str,
btn: ['确认', '取消'],
btn1: function () {
var company_name = $('#addData input[name="company_name"]').val();
var coin_name_zh = $('#addData input[name="coin_name_zh"]').val();
var coin_name_en = $('#addData input[name="coin_name_en"]').val();
if (company_name !== "" && company_name !== "" && coin_name_en !== "") {
$btn = $('.layui-layer-btn0').button('loading');
$.post('/admin/coin-publish/add', $("#addData").serialize(), function (rev) {
layer.msg(rev.msg);
if (rev.code == 0) {
layer.close(index);
table.reload("table1", {});
}
});
$btn.button('reset');
} else {
layer.msg("请输入完整!");
}
return false;
}
});
layui.form.render();
});
}else if('del' == event){
layer.confirm('确定删除此币种监控吗?', {title: '删除币种'}, function(index){
$.get('/admin/coin-publish/del', {id: data.id}, function(rev) {
if (rev.code == 0) {
table.on('tool(table1)', function (obj) {
var event = obj.event;
var data = obj.data;
if ('update' == event) {
$.get('/admin/coin-publish/add', {id: data.id}, function (str) {
var index = layer.open({
type: 1,
title: '编辑',
id: 'add-one',
skin: 'layui-layer-lan',
area: ['320px', 'auto'],
content: str,
btn: ['确认', '取消'],
btn1: function () {
var company_name = $('#addData input[name="company_name"]').val();
var coin_name_zh = $('#addData input[name="coin_name_zh"]').val();
var coin_name_en = $('#addData input[name="coin_name_en"]').val();
if (company_name !== "" && company_name !== "" && coin_name_en !== "") {
$.post('/admin/coin-publish/add', $("#addData").serialize(), function (rev) {
layer.msg(rev.msg);
if (rev.code == 0) {
layer.close(index);
table.reload("table1", {});
}
});
} else {
layer.msg("请输入完整!");
}
return false;
}
});
layui.form.render();
});
} else if ('del' == event) {
layer.confirm('确定删除此币种监控吗?', {title: '删除币种'}, function (index) {
$.get('/admin/coin-publish/del', {id: data.id}, function (rev) {
if (rev.code == 0) {
obj.del();
layer.close(index);
}
});
});
}
layer.close(index);
}
});
});
}
});
$('#add').click(function () {
//打开弹窗
......
<?php
/**
* Created By Sublime Text 3
*
* @date 2018-09-18 10:21:53
* @authors rlgy <rlgyzhcn@qq.com>
*/
namespace common\business;
use common\service\chain33\Chain33Service;
/**
* chain33 区块链接口
*/
class Chain33Business
{
/**
* 查询地址下的token合约下的token资产
*
* @param string $token
* @param string $address
* @return array
*/
public static function getAccountTokenAssets($address, $token)
{
$service = new Chain33Service();
$result = $service->getAccountTokenAssets($address, $token);
if ($result['code'] == 0) {
$result = $result['result'];
if (isset($result['tokenAssets'])) {
$tokenAssets = $result['tokenAssets'];
$tokenAssets = array_column($tokenAssets, null, 'symbol');
if (isset($tokenAssets[$token])) {
return ['code' => 0, 'amount' => $tokenAssets[$token]['account']['balance']];
}
return ['code' => -1, 'msg' => "$token 不存在"];
}
return ['code' => -1, 'msg' => '资产信息不存在'];
}
return $result;
}
/**
* 批量获取token资产
*
* @param array $list
* @return array
*/
public static function getMulAccountTokenAssets($list)
{
foreach ($list as $k => &$v) {
$temp = self::getAccountTokenAssets($v['address'], $v['coin_name_en']);
if ($temp['code'] == 0) {
$v['amount'] = $temp['amount'];
} else {
$v['amount'] = 0;
}
}
return array_column($list, 'amount', 'id');
}
/**
* 提币接口
*
* @param $from
* @param $to
* @param $amount
* @param string $note
* @param bool $isToken
* @param string $tokenSymbol
* @return array|string
*/
public static function extractToken($from, $to, $amount, $note = "", $isToken = true, $tokenSymbol = "")
{
$service = new Chain33Service();
$result = $service->extractToken($from, $to, $amount, $note, $isToken, $tokenSymbol);
if ($result['code'] == 0) {
return $result['result']['hash'];
}
return ['code' => -1, 'msg' => $result['msg']];
}
/**
* 转账接口
*
* @param $from
* @param $to
* @param $amount
* @param string $note
* @param bool $isToken
* @param string $tokenSymbol
* @return array|string
*/
public static function transToken($from, $to, $amount, $note = "", $isToken = true, $tokenSymbol = "")
{
$service = new Chain33Service();
$result = $service->extractToken($from, $to, $amount, $note, $isToken, $tokenSymbol);
if ($result['code'] == 0) {
return $result['result']['hash'];
}
return ['code' => -1, 'msg' => $result['msg']];
}
}
......@@ -7,4 +7,9 @@
*/
return [
];
\ No newline at end of file
'chain33' => [
'scheme' => 'http',
'host' => '114.55.101.159',
'port' => 8801,
],
];
<?php
$chain_url = "121.196.205.182:45656";
return [
'adminEmail' => 'admin@example.com',
'supportEmail' => 'support@example.com',
......@@ -47,6 +45,6 @@ return [
'http_proxy' => [
'host' => '127.0.0.1',
'port' => '8118',
'use' => true
'use' => true
]
];
......@@ -14,16 +14,17 @@ use Yii;
/**
* @property integer $id
* @property integer $uid
* @property string $mobile
* @property string $coin
* @property string $to_address
* @property string $mobile
* @property string $coin
* @property double $amount
* @property string $to_address
* @property integer $check_first_status
* @property integer $check_first_uid
* @property integer $check_second_status
* @property integer $check_second_uid
* @property integer $status
* @property srting $create_time
* @property srting $update_time
* @property string $create_time
* @property string $update_time
*/
class CoinReleaseCheck extends BaseActiveRecord
{
......@@ -86,18 +87,46 @@ class CoinReleaseCheck extends BaseActiveRecord
{
return [
self::SCENARIOS_CHECK => ['coin', 'start_time', 'end_time', 'mobile'],
self::SCENARIOS_SEARCH => ['status', 'coin', 'check_first_uid', 'check_second_uid', 'start_time', 'end_time', 'mobile'],
self::SCENARIOS_SEARCH => [
'status',
'coin',
'check_first_uid',
'check_second_uid',
'start_time',
'end_time',
'mobile'
],
];
}
public function rules()
{
return [
[['id', 'uid', 'check_first_status', 'check_first_uid', 'check_second_status', 'check_second_uid', 'status'], 'integer'],
[['mobile', 'coin', 'to_address', 'create_time', 'update_time', 'start_time', 'end_time'], 'string', 'max' => 255],
[
[
'id',
'uid',
'check_first_status',
'check_first_uid',
'check_second_status',
'check_second_uid',
'status'
],
'integer'
],
[
['mobile', 'coin', 'to_address', 'create_time', 'update_time', 'start_time', 'end_time'],
'string',
'max' => 255
],
[['end_time'], 'compare', 'compareAttribute' => 'start_time', 'operator' => '>=', 'skipOnEmpty' => true],
[['coin', 'start_time', 'end_time', 'mobile'], 'default', 'value' => '', 'on' => self::SCENARIOS_CHECK],
[['status', 'coin', 'check_first_uid', 'check_second_uid', 'start_time', 'end_time', 'mobile'], 'default', 'value' => '', 'on' => self::SCENARIOS_SEARCH],
[
['status', 'coin', 'check_first_uid', 'check_second_uid', 'start_time', 'end_time', 'mobile'],
'default',
'value' => '',
'on' => self::SCENARIOS_SEARCH
],
];
}
}
......@@ -12,15 +12,17 @@ use Yii;
/**
* CoinReleaseMember
* @property int $id
* @property int $rule_id
* @property int $user_id
* @property int $amount 币种总数
* @property int $release 币种释放总数
* @property int $freeze 币种冻结总数
* @property int $platform_id 平台id
* @property string $mobile 手机号码
* @property string $coin 币种
*
* @property int $id
* @property int $rule_id
* @property int $user_id
* @property int $amount 币种总数
* @property int $release 币种释放总数
* @property int $freeze 币种冻结总数
* @property int $output 币种提取总数
* @property int $platform_id 平台id
* @property string $mobile 手机号码
* @property string $coin 币种
*/
class CoinReleaseMember extends BaseActiveRecord
{
......@@ -76,9 +78,10 @@ class CoinReleaseMember extends BaseActiveRecord
/**
* 导入数据
* @param int $datas[0] mobile
* @param string $datas[1] coin
* @param int $datas[2] amount
*
* @param int $datas [0] mobile
* @param string $datas [1] coin
* @param int $datas [2] amount
* @return boolean
*/
public static function implode(array $datas)
......@@ -88,9 +91,10 @@ class CoinReleaseMember extends BaseActiveRecord
}
try {
$result = self::getDb()
->createCommand()
->batchInsert(self::tableName(), ['rule_id', 'mobile', 'coin', 'amount', 'release', 'freeze'], $datas)
->execute();
->createCommand()
->batchInsert(self::tableName(), ['rule_id', 'mobile', 'coin', 'amount', 'release', 'freeze'],
$datas)
->execute();
return $result;
} catch (\Exception $e) {
}
......@@ -100,7 +104,7 @@ class CoinReleaseMember extends BaseActiveRecord
/**
* 批量获取锁仓规则下的人数
*
* @param array $ids 规则id
* @param array $ids 规则id
* @return array|boolean 返回id为键,数量为值的数组
*/
public static function getCountByRuleIds($ids)
......@@ -109,7 +113,7 @@ class CoinReleaseMember extends BaseActiveRecord
$ids = [$ids];
}
$query = CoinReleaseMember::find()->select(['rule_id', 'COUNT(id) as count'])
->groupBy('rule_id')->where(['in', 'rule_id', $ids]);
->groupBy('rule_id')->where(['in', 'rule_id', $ids]);
// return $query->createCommand()->getRawSql();
$datas = $query->asArray()->all();
return array_column($datas, 'count', 'rule_id');
......
<?php
/**
* Created By Sublime Text 3
*
* @date 2018-09-18 10:28:23
* @authors rlgy <rlgyzhcn@qq.com>
*/
namespace common\service\chain33;
use Yii;
use common\helpers\Curl;
/**
* chain33 区块链接口
*/
class Chain33Service
{
public static function urlBuild()
{
$config = Yii::$app->params['chain33'];
$scheme = $config['scheme'] ?? 'http';
$host = $config['host'] ?? '127.0.0.1';
$port = (string)$config['port'] ?? '';
if ($port) {
return $scheme . '://' . $host . ':' . $port;
} else {
return $scheme . '://' . $host;
}
}
public static function jsonRpcBuild($params = [], $method = 'Chain33.Query')
{
$data = [
'jsonrpc' => '2.0',
'id' => 0,
'method' => $method,
'params' => [$params],
];
return json_encode($data);
}
public function send($params = [], $method = 'Chain33.Query')
{
$ch = new Curl();
$jsonrpc = self::jsonRpcBuild($params, $method);
echo $jsonrpc;
$ch->setHeader('Content-Type', 'application/json');
$ch->setRawPostData($jsonrpc);
$result = $ch->post(self::urlBuild(), false);
if (!$result) {
return ['code' => -1, 'msg' => '请求失败'];
}
if (0 == $result['id'] && empty($result['error'])) {
$result['code'] = $result['id'];
unset($result['id']);
return $result;
} else {
return ['code' => -1, 'msg' => $result['error']];
}
}
/**
* 获取地址下的所有token资产
*
* @param string $address
* @param string $symbol
* @return array
*/
public function getAccountTokenAssets($address, $symbol)
{
$params = [
"execer" => "token",
"funcName" => "GetAccountTokenAssets",
"payload" => [
"address" => $address,
"execer" => "token",
],
];
return $this->send($params);
}
/**
* 提币
*
* @param $from
* @param $to
* @param $amount
* @param string $note
* @param bool $isToken
* @param string $tokenSymbol
* @return array
*/
public function extractToken($from, $to, $amount, $note = '', $isToken = true, $tokenSymbol = '')
{
$params = [
"from" => $from,
"to" => $to,
"amount" => -$amount,
"note" => $note,
"isToken" => $isToken ? true : false,
"tokenSymbol" => strtoupper($tokenSymbol)
];
return $this->send($params, 'Chain33.SendToAddress');
}
/**
* token 转账
*
* @param $from
* @param $to
* @param $amount
* @param string $note
* @param bool $isToken
* @param string $tokenSymbol
* @return array
*/
public function transToken($from, $to, $amount, $note = '', $isToken = true, $tokenSymbol = '')
{
$params = [
"from" => $from,
"to" => $to,
"amount" => $amount,
"note" => $note,
"isToken" => $isToken ? true : false,
"tokenSymbol" => strtoupper($tokenSymbol)
];
return $this->send($params, 'Chain33.SendToAddress');
}
}
......@@ -16,16 +16,19 @@ use yii\queue\Queue;
use yii\console\Controller;
use common\models\psources\CoinPublishRule;
use common\models\psources\CoinReleaseMember;
use backend\jobs\ExtractTokenJob;
/**
* Class ExchangeController
* 调用交易所api更新数据保存到redis
*
* @package console\controllers
*/
class DevController extends Controller
{
/**
* 解冻币任务
*
* @return [type] [description]
*/
public function actionFreeze1()
......@@ -33,4 +36,10 @@ class DevController extends Controller
$qid = Yii::$app->queue->push(new FreezeJob(['id' => 43]));
var_dump($qid);
}
public function actionExtractToken()
{
$id = Yii::$app->queue->push(new ExtractTokenJob(['cid' => 1]));
var_dump($id);
}
}
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