今天写了一些代码

<?php

class ApiController extends BaseController
{
protected $appId = '';
protected $actions = [
'accountRegister',
'accountUpdate',
'accountInfo',
'accountSendCode',
'accountReset',
'accountToken',
'bindGoogleAuth',
'validateGoogleAuth',

'messageList',

'walletTransfers',
'walletList',
'walletCoin',
];

protected $afterActions = [
'accountRegister' => ['walletInit','walletAddCoin'],
];

protected $beforeActions = [

];

public function __call($name, $params)
{
// TODO: Implement __call() method.
$actionName = lcfirst($name);
if(!in_array($actionName,$this->actions)) {
throw new Exception("The action not exists", 900201);
}
$params[0]['appId'] = $this->appId;

$checkRe = CheckData::check($params[0]);

$beforeRe = $this->beforeRequest($actionName,$params[0]);
$result = $resData = $afterRe = false;
if($checkRe and $beforeRe){

$config = $GLOBALS['app_conf']['bitKeepSdk'];
$bitKeepSdk = new BitKeepOpenSDK($config);
$result = $bitKeepSdk->$actionName($params[0]);
$resData = json_decode($result, true);
$afterRe = $this->afterRequest($actionName,$params[0],$resData['data']);
if(isset($resData['status']) and $resData['status'] == 0 and $afterRe){
return $resData;
}
}
l('api_log',['check'=>$checkRe,'beforeRe'=>$beforeRe,'result'=>$result,'resData'=>$resData,'afterRe'=>$afterRe,'postData'=>$params[0]],true);
throw new Exception('The request failed',900020);
}

public function beforeRequest($action,$params)
{

// if(isset($this->beforeActions[$action])){
// $res = [];
// foreach ($this->beforeActions[$action] as $item){
// $res[$action] = call_user_func_array([$this,$item],$params);
// }
// l('before_request',['res'=>$res],true);
// return in_array(false,$res) ? false : true;
// }
return true;
}

public function afterRequest($action,$params,$result)
{
if(isset($this->afterActions[$action])){
$res = [];
foreach ($this->afterActions[$action] as $item){
$res[$item] = call_user_func_array([$this,$item],[$result]);
}
l('after_request',['res'=>$res],true);
return $res;
}
return true;
}

/**
* 注册后初始化钱包
* @param $result
* @return mixed
*/
public function walletInit($result)
{
$postData = [
'userId' => $result['id'],
'appId' => $this->appId
];
$config = $GLOBALS['app_conf']['bitKeepSdk'];
$bitKeepSdk = new BitKeepOpenSDK($config);
$bitRe = $bitKeepSdk->walletInit($postData);
return $bitRe;
}

/**
* 添加BTC USDT两种货币
* @param $result
* @return array
*/
public function walletAddCoin($result){
$postData = [
'userId' => $result['id'],
'appId' => $this->appId
];

$coins = [
['coin' => 'BTC','mc' => 'BTC'],
['coin' => 'USDT','mc' => 'ETH']
];
$config = $GLOBALS['app_conf']['bitKeepSdk'];
$bitKeepSdk = new BitKeepOpenSDK($config);
$bitRe = [];
foreach ($coins as $item){
$postData['coin'] = $item['coin'];
$postData['mc'] = $item['mc'];
$bitRe[] = $bitKeepSdk->walletAddCoin($postData);
}
return $bitRe;
}
}

猜你喜欢

转载自www.cnblogs.com/huobiao/p/11970769.html