Commit 573eb440 authored by shajiaiming's avatar shajiaiming

issue coin

parent 4596440c
<?php
namespace api\controllers;
use Yii;
class IssueCoinController extends BaseController
{
/**
* landing
* @return array
* @throws \yii\base\Exception
* @throws \yii\base\InvalidConfigException
*/
public function actionList()
{
$platform_id = Yii::$app->request->getPlatformId();
if (1 === $platform_id) {
$platforms = CoinPlatform::find()->select('id, name')->asArray()->all();
} else {
$platforms = CoinPlatform::find()->select('id, name')->where(['id' => $platform_id])->asArray()->all();
}
return ['code' => 0, 'msg' => 'ok', 'data' => $platforms];
}
public function actionWalletBallance()
{
$current_platform_id = Yii::$app->request->getPlatformId();
if(1 === $current_platform_id) {
$platform_id = Yii::$app->request->get('platform_id', 1);
$platform_id = empty($platform_id) ? 1 : $platform_id;
} else {
$platform_id = Yii::$app->request->getPlatformId();
}
if(!isset(Yii::$app->params['trusteeship']['node_'. $platform_id])){
return ['code' => -1, 'data' => [], 'msg' => '此钱包节点尚未开通'];
}
$node_params = Yii::$app->params['trusteeship']['node_'. $platform_id];
$type = Yii::$app->request->get('type', 1);
$page = Yii::$app->request->get('page', 1);
$size = Yii::$app->request->get('size', 15);
$currency = Yii::$app->request->get('currency ', '');
$params = [
'type' => $type,
'page' => $page,
'size' => $size,
'currency' => $currency
];
$time = time();
$appKey = isset($node_params['appKey']) ? $node_params['appKey'] : null;
$appSecret = isset($node_params['appSecret']) ? $node_params['appSecret'] : null;
$signature = self::getSign($params, $appKey, $appSecret, $time);
$headers = [
'FZM-Wallet-Signature' => $signature,
'FZM-Wallet-Timestamp' => $time,
'FZM-Wallet-AppKey' => $appKey,
'FZM-Wallet-AppIp' => Yii::$app->request->userIP
];
$service = new TrusteeShipService($node_params, $headers);
$result = $service->getWalletBalance($params);
if (200 !== $result['code']) {
return ['code' => $result['code'], 'data' => [], 'msg' => $result['msg']];
}
return ['code' => 1, 'data' => $result['msg'], 'msg' => 'success'];
}
public function actionUserAsset()
{
$platform_id = Yii::$app->request->getPlatformId();
$node_params = Yii::$app->params['trusteeship']['node_'. $platform_id];
$uid = Yii::$app->request->get('uid', '');
$params = [
'uid' => $uid
];
$time = time();
$appKey = isset($node_params['appKey']) ? $node_params['appKey'] : null;
$appSecret = isset($node_params['appSecret']) ? $node_params['appSecret'] : null;
$signature = self::getSign($params, $appKey, $appSecret, $time);
$headers = [
'FZM-Wallet-Signature' => $signature,
'FZM-Wallet-Timestamp' => $time,
'FZM-Wallet-AppKey' => $appKey,
'FZM-Wallet-AppIp' => Yii::$app->request->userIP
];
$service = new TrusteeShipService($node_params, $headers);
$result = $service->getUserAsset($params);
if (200 !== $result['code']) {
return ['code' => $result['code'], 'data' => [], 'msg' => $result['msg']];
}
return ['code' => 1, 'data' => $result['msg'], 'msg' => 'success'];
}
}
\ No newline at end of file
<?php
namespace common\models;
use Yii;
use yii\db\Expression;
class CoinIssueCoin extends CommonActiveRecord
{
const STATUS_SUCCESS = 1; //发行成功
const STATUS_FAIL = 0; //发行失败
public static function getDb()
{
return Yii::$app->get('p_sources');
}
public function fields()
{
return [
'name',
'symbol',
'total',
'owner',
'introduction',
'category',
'chain_id',
];
}
public static function tableName()
{
return '{{%coin_issue_coin}}';
}
public function attributeLabels()
{
return [
'name' => 'Token简称',
'symbol' => 'Token全称',
'total' => '发行数量',
'owner' => '接收地址',
'introduction' => 'Token简介',
'category' => '是否增加发',
'chain_id' => '平行链名称',
'msg' =>'失败原因'
];
}
/**
* 获取状态数组
* @return array
*/
public static function getAgentStatus()
{
return [
self::STATUS_SUCCESS => '发行成功',
self::STATUS_FAIL => '发行失败',
];
}
}
\ No newline at end of file
<?php
namespace common\models;
use yii\base\Model;
class CoinIssueCoinForm extends Model
{
public $name;
public $symbol;
public $total;
public $owner;
public $introduction;
public $category;
public $platform_id;
public $chain_id;
private $_issue_coin_list;
//定义场景
const SCENARIOS_CREATE = 'create';
const SCENARIOS_UPDATE = 'update';
public function rules()
{
return [
[['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'platform_id', 'chain_id'], 'required'],
[['total', 'category', 'platform_id', 'chain_id'], 'integer'],
];
}
public function scenarios()
{
$scenarios = [
self:: SCENARIOS_CREATE => ['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'platform_id', 'chain_id'],
self:: SCENARIOS_UPDATE => ['name', 'symbol', 'total', 'owner', 'introduction', 'category', 'platform_id', 'chain_id'],
];
return array_merge(parent:: scenarios(), $scenarios);
}
public function attributeLabels()
{
return [
'name' => 'Token简称',
'symbol' => 'Token全称',
'total' => '发行数量',
'owner' => '接收地址',
'introduction' => 'Token简介',
'category' => '是否增加发',
'chain_id' => '平行链名称'
];
}
public function getIsNewRecord()
{
return $this->_issue_coin_list === null;
}
public function getIssueList($id)
{
$this->_issue_coin_list = CoinIssueCoinForm::find()
->where('agent_id = :agent_id', [':agent_id' => $id])
->one();
if ($this->_issue_coin_list) {
$this->agent_id = $this->_issue_coin_list->agent_id;
$this->title = $this->_issue_coin_list->title;
$this->etitle = $this->_issue_coin_list->etitle;
$this->description = $this->_issue_coin_list->description;
$this->requirement = $this->_issue_coin_list->requirement;
$this->status = $this->_issue_coin_list->status;
}
return $this->_issue_coin_list;
}
/**
* 保存代理人
* @return boolean
*/
public function save()
{
if ($this->validate()) {
if ($this->getIsNewRecord()) {
$this->_issue_coin_list = new Agent();
$isNew = true;
} else {
$isNew = false;
}
$this->_issue_coin_list->agent_id = $this->agent_id;
$this->_issue_coin_list->title = $this->title;
$this->_issue_coin_list->etitle = $this->etitle;
$this->_issue_coin_list->description = $this->description;
$this->_issue_coin_list->requirement = $this->requirement;
$this->_issue_coin_list->status = $this->status;
return $this->_issue_coin_list->save();
} else {
return false;
}
}
}
\ No newline at end of file
<?php
namespace common\models;
use Yii;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;
use yii\web\Linkable;
use yii\web\Link;
class CommonActiveRecord extends ActiveRecord
{
public $cacheKeyStorage = []; //缓存key寄存器
public function toArray(array $fields = [], array $expand = [], $recursive = true)
{
$data = [];
$cachePrefix = __CLASS__;
$cache = Yii::$app->cache;
foreach ($this->resolveFields($fields, $expand) as $field => $definition) {
if (strpos($field, '.')) {
$fieldChunks = explode('.', $field);
$primaryKey = is_array($this->primaryKey) ? implode('-', $this->primaryKey) : $this->primaryKey;
$uniqueRelationCacheKey = "{$cachePrefix}_{$primaryKey}_{$fieldChunks[0]}";
$this->addCacheKeyToStorage($uniqueRelationCacheKey);
$relation = $cache->get($uniqueRelationCacheKey);
if (!$relation) {
$relation = $this->{$fieldChunks[0]};
$cache->set($uniqueRelationCacheKey, $relation);
}
if (is_array($relation)) {
foreach ($relation as $relatedField => $relatedObject) {
if (!is_object($relatedObject)) {
continue;
}
$data[$fieldChunks[0]][$relatedField][$fieldChunks[1]] = $this->getRelationField($relatedObject, $fieldChunks[1]);
}
} else {
if (!is_object($relation)) {
continue;
}
$data[$fieldChunks[0]][$fieldChunks[1]] = $this->getRelationField($relation, $fieldChunks[1]);
}
} else {
$data[$field] = is_string($definition) ? $this->$definition : call_user_func($definition, $this, $field);
}
}
$this->deleteStorageCaches();
if ($this instanceof Linkable) {
$data['_links'] = Link::serialize($this->getLinks());
}
return $recursive ? ArrayHelper::toArray($data) : $data;
}
/**
* This method also will check relations which are declared in [[extraFields()]]
* to determine which related fields can be returned.
* @inheritdoc
*/
protected function resolveFields(array $fields, array $expand)
{
$result = [];
foreach ($this->fields() as $field => $definition) {
if (is_integer($field)) {
$field = $definition;
}
if (empty($fields) || in_array($field, $fields, true)) {
$result[$field] = $definition;
}
}
if (empty($expand)) {
return $result;
}
$extraFieldsKeys = array_keys($this->extraFields());
foreach($expand as $expandedAttribute) {
if(in_array(explode('.',$expandedAttribute)[0], $this->extraFields())) {
$result[$expandedAttribute] = $expandedAttribute;
}else if(in_array($expandedAttribute, $extraFieldsKeys)) {
$result[$expandedAttribute] = $this->extraFields()[$expandedAttribute];
}
}
return $result;
}
/**
* Additional method to check the related model has specified field
*/
private function getRelationField($relatedRecord, $field)
{
if (!$relatedRecord->hasAttribute($field) && !$relatedRecord->isRelationPopulated($field)) {
throw new \yii\web\ServerErrorHttpException(sprintf(
"Related record '%s' does not have attribute '%s'",
get_class($relatedRecord), $field)
);
}
if($relatedRecord->hasAttribute($field)) {
return ArrayHelper::toArray($relatedRecord)[$field];
} else {
return $relatedRecord->{$field};
}
}
public static function quoteDbName($dbname)
{
return '`' . $dbname . '`';
}
/**
* 添加cache key到寄存器
* @param type $cacheKey
*/
public function addCacheKeyToStorage($cacheKey)
{
if(!in_array($cacheKey, $this->cacheKeyStorage)) {
$this->cacheKeyStorage[] = $cacheKey;
}
}
/**
* 删除寄存器里所有的cache
*/
public function deleteStorageCaches()
{
foreach($this->cacheKeyStorage as $cacheKey) {
Yii::$app->cache->delete($cacheKey);
}
}
}
\ No newline at end of file
<?php
namespace wallet\controllers;
use Yii;
use common\models\Admin;
class IssueCoinController extends BaseController
{
/**
* landing
* @return array
* @throws \yii\base\Exception
* @throws \yii\base\InvalidConfigException
*/
public function actionList()
{
$platform_id = Yii::$app->request->getPlatformId();
if (1 === $platform_id) {
$platforms = CoinPlatform::find()->select('id, name')->asArray()->all();
} else {
$platforms = CoinPlatform::find()->select('id, name')->where(['id' => $platform_id])->asArray()->all();
}
return ['code' => 0, 'msg' => 'ok', 'data' => $platforms];
}
public function actionWalletBallance()
{
$current_platform_id = Yii::$app->request->getPlatformId();
if(1 === $current_platform_id) {
$platform_id = Yii::$app->request->get('platform_id', 1);
$platform_id = empty($platform_id) ? 1 : $platform_id;
} else {
$platform_id = Yii::$app->request->getPlatformId();
}
if(!isset(Yii::$app->params['trusteeship']['node_'. $platform_id])){
return ['code' => -1, 'data' => [], 'msg' => '此钱包节点尚未开通'];
}
$node_params = Yii::$app->params['trusteeship']['node_'. $platform_id];
$type = Yii::$app->request->get('type', 1);
$page = Yii::$app->request->get('page', 1);
$size = Yii::$app->request->get('size', 15);
$currency = Yii::$app->request->get('currency ', '');
$params = [
'type' => $type,
'page' => $page,
'size' => $size,
'currency' => $currency
];
$time = time();
$appKey = isset($node_params['appKey']) ? $node_params['appKey'] : null;
$appSecret = isset($node_params['appSecret']) ? $node_params['appSecret'] : null;
$signature = self::getSign($params, $appKey, $appSecret, $time);
$headers = [
'FZM-Wallet-Signature' => $signature,
'FZM-Wallet-Timestamp' => $time,
'FZM-Wallet-AppKey' => $appKey,
'FZM-Wallet-AppIp' => Yii::$app->request->userIP
];
$service = new TrusteeShipService($node_params, $headers);
$result = $service->getWalletBalance($params);
if (200 !== $result['code']) {
return ['code' => $result['code'], 'data' => [], 'msg' => $result['msg']];
}
return ['code' => 1, 'data' => $result['msg'], 'msg' => 'success'];
}
public function actionUserAsset()
{
$platform_id = Yii::$app->request->getPlatformId();
$node_params = Yii::$app->params['trusteeship']['node_'. $platform_id];
$uid = Yii::$app->request->get('uid', '');
$params = [
'uid' => $uid
];
$time = time();
$appKey = isset($node_params['appKey']) ? $node_params['appKey'] : null;
$appSecret = isset($node_params['appSecret']) ? $node_params['appSecret'] : null;
$signature = self::getSign($params, $appKey, $appSecret, $time);
$headers = [
'FZM-Wallet-Signature' => $signature,
'FZM-Wallet-Timestamp' => $time,
'FZM-Wallet-AppKey' => $appKey,
'FZM-Wallet-AppIp' => Yii::$app->request->userIP
];
$service = new TrusteeShipService($node_params, $headers);
$result = $service->getUserAsset($params);
if (200 !== $result['code']) {
return ['code' => $result['code'], 'data' => [], 'msg' => $result['msg']];
}
return ['code' => 1, 'data' => $result['msg'], 'msg' => 'success'];
}
}
\ 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