Commit 00b3f2cd authored by shajiaiming's avatar shajiaiming

手续费

parent 37dfca0a
......@@ -5,9 +5,11 @@
* Date: 2018/11/14
* Time: 18:53
*/
namespace api\controllers;
use api\base\BaseController;
use common\models\psources\CoinHashMemo;
use common\models\psources\CoinTokenTransfer;
use common\service\chain33\Chain33Service;
use Yii;
......@@ -16,19 +18,40 @@ use Yii;
class TradeController extends BaseController
{
/**
* 添加本地备注
*/
* 添加本地备注
*/
public function actionAddMemo()
{
$request = Yii::$app->request;
$hash = $request->post('hash');
$memo = $request->post('memo');
if($hash && $memo){
Yii::$app->redis->hset('trade_hash_memo',$hash,$memo);
return ['code' => 0,'data' => [], 'msg' => '本地备注添加成功'];
}else{
return ['code' => 1,'data' => [],'msg' => '交易hash值或者本地备注不能为空'];
$request = Yii::$app->request;
$hash = $request->post('hash');
$memo = $request->post('memo');
if (empty($hash) || empty($memo)) {
return ['code' => 1, 'data' => [], 'msg' => '交易hash值或者本地备注不能为空'];
}
$model = new CoinHashMemo();
$model->setScenario(CoinHashMemo::SCENARIOS_CREATE);
if ($model->load(Yii::$app->request->post(), '') && $model->save()) {
return ['code' => 0, 'data' => [], 'msg' => '本地备注添加成功'];
} else {
return ['code' => 1, 'data' => [], 'msg' => current($model->firstErrors)];
}
}
public function actionUpdateMemo()
{
$request = Yii::$app->request;
$hash = $request->post('hash');
$memo = $request->post('memo');
if (empty($hash) || empty($memo)) {
return ['code' => 1, 'data' => [], 'msg' => '交易hash值或者本地备注不能为空'];
}
$model = CoinHashMemo::find()->where(['hash' => $hash])->one();
if (empty($model)) {
return ['code' => 1, 'data' => [], 'msg' => '记录不存在'];
}
CoinHashMemo::updateAll(['memo' => $memo], 'hash = :hash',[':hash'=>$hash]);
return ['code' => 0, 'data' => [], 'msg' => '本地备注更新成功'];
}
/**
......@@ -36,27 +59,57 @@ class TradeController extends BaseController
*/
public function actionGetMemo()
{
$request = Yii::$app->request;
$hash = $request->post('hash');
if($hash){
$memo = Yii::$app->redis->hget('trade_hash_memo',$hash);
return ['code' => 0,'data' => $memo];
}else{
return ['code' => 1,'data' => [],'msg' => '交易hash值不能为空'];
$request = Yii::$app->request;
$hash = $request->post('hash');
$version = $request->post('version', '');
if(empty($hash)) {
return ['code' => 1, 'data' => [], 'msg' => '交易hash值不能为空'];
}
$model = CoinHashMemo::find()->where(['hash' => $hash])->asArray()->one();
if(empty($model)) {
return ['code' => 1, 'data' => [], 'msg' => '记录不存在'];
}
if(empty($version)){
return ['code' => 0, 'data' => $model['memo']];
}
return ['code' => 0, 'data' => [
'memo' => $model['memo'],
'coins_fee' => $model['coins_fee']
]];
}
public function actionSyncToDb()
{
// $data = [];
// $result = Yii::$app->redis->HGETALL ('trade_hash_memo');
// foreach ($result as $key => $val){
// if (($key + 2) % 2 == 0){
// $data[] = [
// 'hash' => $val,
// 'memo' => $result[$key + 1]
// ];
// }
// }
// foreach ($data as $val){
// if ('0xc080c6a0937f25d1017e5e88bdcff5c3979810c8a6cc1f3a64cf50970da28fc9' == $val['hash']) continue;
// $coin_memo_hash = new CoinHashMemo();
// $coin_memo_hash->hash = $val['hash'];
// $coin_memo_hash->memo = $val['memo'];
// $coin_memo_hash->save();
// }
// exit;
}
public function actionAddressIsExist()
{
$get = Yii::$app->request->get();
$to = $get['coin_address'] ?? '';
if(empty($to)){
if (empty($to)) {
return ['code' => -1, 'msg' => '参数不能为空'];
}
$coinTokenTransfer = CoinTokenTransfer::find()->where(['coin_address' => $to, 'code' => 1])->one();
if(!$coinTokenTransfer){
if (!$coinTokenTransfer) {
return ['code' => 0, 'msg' => 'record does not exist'];
}
......@@ -68,12 +121,12 @@ class TradeController extends BaseController
{
$get = Yii::$app->request->get();
$to = $get['coin_address'] ?? '';
if(empty($to)){
if (empty($to)) {
return ['code' => -1, 'msg' => '参数不能为空'];
}
$coinTokenTransfer = CoinTokenTransfer::find()->where(['coin_address' => $to, 'code' => 1])->one();
if($coinTokenTransfer){
if ($coinTokenTransfer) {
return ['code' => -1, 'msg' => 'record already exists'];
}
......@@ -86,8 +139,8 @@ class TradeController extends BaseController
$service = new Chain33Service();
$createRawTransaction = $service->createTokenRawTransaction($to, $amount, $isToken, $tokenSymbol, $fee, $note, $execer);
if(0 != $createRawTransaction['code']){
$createRawTransaction = $service->createTokenRawTransaction($to, $amount, $isToken, $tokenSymbol, $fee, $note, $execer);
if (0 != $createRawTransaction['code']) {
$msg = $createRawTransaction['msg'];
$code = -1;
goto doEnd;
......@@ -98,7 +151,7 @@ class TradeController extends BaseController
$expire = '1m';
$signRawTx = $service->signRawTx($privkey, $txHex, $expire);
if(0 != $signRawTx['code']){
if (0 != $signRawTx['code']) {
$msg = $signRawTx['msg'];
$code = -1;
goto doEnd;
......@@ -106,7 +159,7 @@ class TradeController extends BaseController
$sign_str = $signRawTx['result'];
$result = $service->sendTransaction($sign_str);
if(0 != $result['code']){
if (0 != $result['code']) {
$msg = $result['msg'];
$code = -1;
goto doEnd;
......
<?php
namespace common\models\psources;
use Yii;
use common\core\BaseActiveRecord;
class CoinHashMemo extends BaseActiveRecord
{
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_hash_memo}}';
}
//定义场景
const SCENARIOS_CREATE = 'create';
const SCENARIOS_UPDATE = 'update';
public function rules() {
return [
[['hash','memo'], 'required'],
['coins_fee','safe']
];
}
public function scenarios() {
$scenarios = [
self:: SCENARIOS_CREATE => ['hash','memo', 'coins_fee'],
];
return array_merge( parent:: scenarios(), $scenarios);
}
}
\ 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