Commit 10d8c5b2 authored by shajiaiming's avatar shajiaiming

订单相关接口

parent 1c9462b2
<?php
namespace api\controllers;
use Yii;
use yii\data\Pagination;
use api\base\BaseController;
use common\models\psources\CoinCTocTransfer;
class OrderController extends BaseController
{
public function actionIndex()
{
$code = -1;
$request = Yii::$app->request;
if (!$request->isPost) {
$msg = '请求错误!';
goto doEnd;
}
$post = $request->post();
if (2 != count($post['txs'])) {
$msg = '交易笔数错误!';
goto doEnd;
}
$data['is_sell'] = (false == $post['isSell']) ? 0 : 1;
$data['type'] = $post['type'];
$data['address'] = $post['address'];
$data['token_name'] = $post['token_name'];
$data['market_name'] = $post['market_name'];
$data['price'] = $post['price'];
$data['amount'] = $post['amount'];
$data['transfer_number'] = date('YmdHis') . self::getrandnums();
$redis = Yii::$app->redis;
if ($redis->exists('C2C_' . $post['address'] . '_' . $data['is_sell'])) {
$msg = '尚存在一笔未完成的交易!';
goto doEnd;
}
$redis->set('C2C_' . $post['address'] . '_' . $data['is_sell'], time());
foreach ($post['txs'] as $key => $val) {
$model = new CoinCTocTransfer();
$data['txhex'] = $val['tx'];
$data['transfer_url'] = $val['url'];
$data['step'] = $val['step'];
$model->load($data, '');
$model->save();
}
$code = 0;
$msg = 'success';
doEnd :
return ['code' => $code, 'msg' => $msg];
}
public function actionFixOrder()
{
$code = -1;
$request = Yii::$app->request;
if (!$request->isPost) {
$msg = '请求错误!';
goto doEnd;
}
$post = $request->post();
$data['is_sell'] = (false == $post['isSell']) ? 0 : 1;
$data['type'] = $post['type'];
$data['address'] = $post['address'];
foreach ($post['txs'] as $key => $val) {
$model = CoinCTocTransfer::find()->where(['is_sell' => $data['is_sell']])
->andWhere(['type' => $data['type']])
->andWhere(['address' => $data['address']])
->andWhere(['step' => $val['step']])
->andWhere(['<>', 'msg', 'success'])
->one();
$model->txhex = $val['tx'];
$model->transfer_url = $val['url'];
$model->send_result = 0;
$model->query_result = 0;
$model->msg = 0;
$model->save();
continue;
}
$redis = Yii::$app->redis;
$redis->set('C2C_' . $post['address'] . '_' . $data['is_sell'], time());
$code = 0;
$msg = 'success';
doEnd :
return ['code' => $code, 'msg' => $msg];
}
public function actionOrderList()
{
$address = Yii::$app->request->get('address', '');
$is_sell = Yii::$app->request->get('isSell', -1);
$type = Yii::$app->request->get('type', '');
$page = Yii::$app->request->get('page', 1);
if(empty($address)){
$msg = '请求参数错误';
$code = -1;
$data = null;
goto doEnd;
}
$query = CoinCTocTransfer::find()
->where('address= :address',[':address' => $address])
->orderBy('create_time desc')
->groupBy('address, is_sell');
if ($is_sell > -1) {
$query->andWhere('is_sell= :is_sell',[':is_sell' => $is_sell]);
}
if (false != $type) {
$query->andWhere('type= :type',[':type' => $type]);
}
$data = $query->offset(($page - 1) * 20)->limit(20)->asArray()->all();
$countQuery = clone $query;
$pages = new Pagination(['totalCount' => $countQuery->count(), 'pageSize' => 20]);
$data = [
'list' => $data,
'page' => [
'pageCount' => $pages->pageCount,
'pageSize' => 20,
'currentPage' => $page,
]
];
$code = 1;
$msg = 'success';
doEnd :
return ['code' => $code, 'msg' => $msg, 'data' => $data];
}
public static function getrandnums()
{
$arr = array();
while (count($arr) < 6) {
$arr[] = rand(0, 9);
$arr = array_unique($arr);
}
return implode("", $arr);
}
}
\ No newline at end of file
<?php
namespace common\models\psources;
use Yii;
use common\core\BaseActiveRecord;
class CoinCTocTransfer extends BaseActiveRecord
{
const TYPE_SELL = 1; //卖单
const TYPE_BUY = 0; //买单
const TYPE_ORDER = 1; //挂单
const TYPE_TRANSFER = 2; //交易
const TYPE_REVOKE = 3; //撤消
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public static function tableName()
{
return '{{%coin_ctoc_transfer}}';
}
//定义场景
const SCENARIOS_CREATE = 'create';
public function rules()
{
return [
[['is_sell', 'type', 'address', 'token_name', 'market_name', 'price', 'amount', 'step', 'txhex', 'transfer_url', 'transfer_number'], 'required'],
[['send_result', 'query_result', 'msg'], 'safe']
];
}
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['is_sell', 'type', 'address', 'token_name', 'market_name', 'price', 'amount', 'step', 'txhex', 'transfer_url', 'transfer_number'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
}
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