Commit b33a2b1b authored by rlgyzhcn's avatar rlgyzhcn

币种代扣

parent 57a6b7be
<?php
/**
* Created By Sublime Text 3
*
* @date 2018-09-12 16:10:28
* @authors rlgy <rlgyzhcn@qq.com>
*/
namespace backend\controllers;
use common\models\psources\Coin;
use common\models\psources\CoinPlatform;
use common\models\psources\CoinWithhold;
use common\models\psources\CoinWithholdToken;
use Yii;
use yii\web\Response;
class CoinWithholdController extends BaseController
{
public function actionIndex()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
$get = Yii::$app->request->get();
$page = $get['page'] ?? 1;
$limit = $get['limit'] ?? 10;
if (Yii::$app->params['admin'] == Yii::$app->user->id) {
$datas = CoinWithhold::getList2($page, $limit, []);
} else {
$datas = CoinWithhold::getList2($page, $limit, ['platform_id' => Yii::$app->user->identity->platform_id]);
}
$withholds = &$datas['data'];
$coin_ids = array_column($withholds, 'cid');
$coin_infos = Coin::getCoinInfoByIds($coin_ids, ['id', 'name'], 'id');
$platforms = CoinPlatform::find()->asArray()->all();
$platforms = array_column($platforms, 'name', 'id');
foreach ($withholds as $key => &$value) {
$value['name'] = $coin_infos[$value['cid']]['name'];
$value['platform'] = $platforms[$value['platform_id']];
}
return $datas;
return ['code' => 0, 'msg' => 'null'];
}
return $this->render('index');
}
public function actionSet()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
$coin = strtoupper(Yii::$app->request->get('coin', ''));
if (CoinWithhold::isExists($coin, Yii::$app->user->identity->platform_id)) {
return ['code' => -1, 'msg' => '代扣币种已经存在'];
}
try {
if (CoinWithhold::saveByName($coin, 1, '', '', Yii::$app->user->identity->platform_id)) {
return ['code' => 0, 'msg' => '保存成功'];
}
} catch (\Exception $e) {
return ['code' => $e->getCode(), 'msg' => $e->getMessage()];
}
return ['code' => -1, 'msg' => '保存失败'];
}
}
public function actionDel()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
$id = Yii::$app->request->get('id', 0);
if ($id) {
//判断是否有代扣token
$count = CoinWithholdToken::find()->where(['wid' => $id])->count();
if ($count > 0) {
return ['code' => -1, 'msg' => '存在代扣Token,无法删除'];
}
if (CoinWithhold::findOne($id)->delete()) {
return ['code' => 0, 'msg' => '删除成功'];
}
}
return ['code' => -1, 'msg' => '删除失败'];
}
}
public function actionDetial()
{
$wid = Yii::$app->request->get('wid', 1);
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
$get = Yii::$app->request->get();
$page = $get['page'] ?? 1;
$limit = $get['limit'] ?? 10;
$datas = CoinWithholdToken::getList2($page, $limit, ['wid' => $wid]);
return $datas;
}
return $this->render('detial', ['wid' => $wid]);
}
public function actionAddToken()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
$get = Yii::$app->request->get();
if (isset($get['id']) && !empty($get['id'])) {
$model = CoinWithholdToken::findOne($get['id']);
$model->scenario = CoinWithholdToken::SCENARIOS_UPDATE;
} else {
$model = new CoinWithholdToken();
$model->scenario = CoinWithholdToken::SCENARIOS_ADD;
}
if ($model->load($get) && $model->validate()) {
$model->token = strtoupper($model->token);
if ($model->save()) {
return ['code' => 0, 'msg' => '保存成功'];
}
} else {
return ['code' => -1, 'msg' => current($model->firstErrors)];
}
return ['code' => -1, 'msg' => '保存失败'];
}
}
public function actionDelToken()
{
Yii::$app->response->format = Response::FORMAT_JSON;
if (Yii::$app->request->isAjax) {
$id = Yii::$app->request->get('id', 0);
if (ctype_digit($id)) {
$model = CoinWithholdToken::findOne($id);
if ($model && $model->delete()) {
return ['code' => 0, 'msg' => '删除成功'];
}
}
return ['code' => -1, 'msg' => '删除失败'];
}
}
}
......@@ -160,7 +160,7 @@ table.on('toolbar(table1)', function(obj) {
type: 1,
zindex: 1,
content: $("#_form"),
btn: ['添加', '取消'],
btn: ['保存', '取消'],
scrollbar: false,
success: function() {
form.val("form1", {
......@@ -201,7 +201,7 @@ table.on('tool(table1)', function(obj) {
type: 1,
zindex: 1,
content: $("#_form"),
btn: ['添加', '取消'],
btn: ['保存', '取消'],
scrollbar: false,
success: function() {
form.val("form1", {
......
<?php
/**
* Created By Sublime Text 3
*
* @date 2018-09-12 17:59:17
* @authors rlgy <rlgyzhcn@qq.com>
*/
?>
<style type="text/css">
.layui-form-label{
width: 100px;
}
</style>
<form class="layui-form" id="_form_hidden">
<input type="hidden" name="wid" value="<?= $wid ?>">
</form>
<div class="layui-col-md8">
<table class="layui-table" id="table1" lay-filter="table1"></table>
</div>
<div style="display: none; padding: 5px 5px;" id="_form_add">
<form class="layui-form" id="form_add" lay-filter="form_add">
<input type="hidden" name="id" value="">
<div class="layui-form-item">
<label class="layui-form-label">Token名称</label>
<div class="layui-input-block">
<input name="token" class="layui-input" type="text">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">代扣地址</label>
<div class="layui-input-block">
<input name="address" class="layui-input" type="text">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">代扣状态</label>
<div class="layui-input-block">
<select name="status" class="layui-select">
<option value="1">启用</option>
<option value="2">禁用</option>
</select>
</div>
</div>
</form>
</div>
<script type="text/html" id="toolBarTop">
<div class="layui-btn-container">
<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add">添加</button>
<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="set">为所有币种设置代扣地址</button>
</div>
</script>
<script type="text/html" id="toolBar">
<button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">编辑</button>
<button class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</button>
</script>
<script type="text/html" id="status_tpl">
{{# if(1 == d.status){ }}
启用
{{# } else if(2 == d.status){ }}
禁用
{{# } }}
</script>
<script type="text/javascript">
var table = layui.table;
var form = layui.form;
form.render();
table.render({
elem: '#table1',
page: true,
limit: 10,
url: '/admin/coin-withhold/detial',
where: {wid: $("#_form_hidden input[name='wid']").val()},
toolbar: '#toolBarTop',
cols: [[
{field: 'id',title: 'ID'},
{field: 'token',title: 'Token名字'},
{field: 'status',title: '代扣状态', templet:'#status_tpl'},
{field: 'address',title: '代扣币种地址'},
{title: '操作',toolbar: '#toolBar'}
]]
});
table.on('toolbar(table1)', function(obj) {
var event = obj.event;
if (event == 'add') {
var index = layer.open({
type: 1,
title: '添加代扣token',
content: $("#_form_add"),
btn: ['保存', '取消'],
btn1: function(argument) {
$.get('/admin/coin-withhold/add-token', $('#form_add').serialize() + '&' + $('#_form_hidden').serialize(), function(rev) {
layer.msg(rev.msg);
if (rev.code == 0) {
layer.close(index);
table.reload('table1', {
page: {
curr: 1
}
});
$('#_form_add').css('display', 'none');
}
});
},
btn2: function() {
$('#_form_add').css('display', 'none');
}
});
} else if (event == 'del') {
layer.confirm('确认删除?',{icon:3,'title':'删除'}, function() {
$.get('/admin/coin-withhold/del',{id:obj.data.id},function (rev) {
// body...
});
})
}
});
table.on('tool(table1)', function(obj) {
var event = obj.event;
var data = obj.data;
if (event == 'edit') {
var index = layer.open({
type: 1,
title: '添加代扣token',
content: $("#_form_add"),
btn: ['保存', '取消'],
success: function() {
form.val('form_add', data);
},
btn1: function(argument) {
$.get('/admin/coin-withhold/add-token', $('#form_add').serialize() + '&' + $('#_form_hidden').serialize(), function(rev) {
layer.msg(rev.msg);
if (rev.code == 0) {
layer.close(index);
table.reload('table1', {
page: {
curr: 1
}
});
$('#_form_add').css('display', 'none');
}
});
},
btn2: function() {
$('#_form_add').css('display', 'none');
}
});
} else if (event == 'del') {
layer.confirm('确认删除?', {
icon: 3,
'title': '删除'
}, function() {
$.get('/admin/coin-withhold/del-token', {
id: obj.data.id
}, function(rev) {
layer.msg(rev.msg);
if (rev.code == 0) {
table.reload('table1', {
page: {
curr: 1
}
});
}
});
})
}
});
</script>
\ No newline at end of file
<?php
/**
* Created By Sublime Text 3
*
* @date 2018-09-12 16:14:15
* @authors rlgy <rlgyzhcn@qq.com>
*/
;?>
<div class="layui-col-md8">
<table class="layui-table" id="table1" lay-filter="table1"></table>
</div>
<script type="text/html" id="status_tpl">
{{# if(d.status==1){ }}
{{# }else{ }}
{{# } }}
</script>
<script type="text/html" id="toolBarTop">
<button class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add">添加</button>
</script>
<script type="text/html" id="toolBar">
<a class="layui-btn layui-btn-xs layui-btn-normal" href="/admin/coin-withhold/detial?wid={{d.id}}">查看详情</a>
<button class="layui-btn layui-btn-xs layui-btn-normal" lay-event="del">删除</button>
</script>
<script type="text/javascript">
var table = layui.table;
table.render({
elem: '#table1',
page: true,
limit: 10,
url: '/admin/coin-withhold/index',
toolbar: '#toolBarTop',
cols: [[
{field: 'id',title: 'ID'},
{field: 'name',title: '代扣币种'},
{field: 'status',title: '是否开启全局代扣', templet:'#status_tpl'},
{field: 'token_name',title: '代扣币种'},
{field: 'address',title: '代扣币种地址'},
{field: 'platform',title: '平台'},
{title: '操作',toolbar: '#toolBar'}
]]
});
table.on('toolbar(table1)', function(obj) {
var event = obj.event;
if ('add' == event) {
layer.prompt({
formType: 0,
value: '',
maxlength: 255,
title: '请输入值'
}, function(value, index, elem) {
$.get('/admin/coin-withhold/set', {
coin: value
}, function(rev) {
layer.msg(rev.msg);
if (rev.code == 0) {
layer.close(index);
table.reload('table1', {
page: {
curr: 1
}
});
}
});
});
}
});
table.on('tool(table1)', function(obj) {
var event = obj.event;
var data = obj.data;
if (event == 'del') {
layer.confirm('确定删除吗?', {
icon: 3,
title: '删除'
}, function() {
$.get('/admin/coin-withhold/del', {
id: data.id
}, function(rev) {
layer.msg(rev.msg);
if (rev.code == 0) {
table.reload('table1', {
page: {
curr: 1
}
});
}
});
});
}
});
</script>
......@@ -37,6 +37,8 @@ class BaseActiveRecord extends \yii\db\ActiveRecord
if (!empty($order_by)) {
# code...
$data = $data->orderby($order_by)->asArray()->all();
} else {
$data = $data->asArray()->all();
}
// $sql = $query->createCommand()->getSql();
$data = ['count' => $count, 'data' => $data];
......
<?php
/**
* Created By Sublime Text 3
*
* @date 2018-09-12 16:48:22
* @authors rlgy <rlgyzhcn@qq.com>
*/
namespace common\models\psources;
class CoinWithhold extends BaseActiveRecord
{
public static function isExists($name, $platform_id)
{
$coin = Coin::findOne(['name' => $name]);
if ($coin) {
return (bool) self::findOne(['cid' => $coin->id, 'platform_id' => $platform_id]);
}
return false;
}
public static function saveByName($name, $status = 1, $token_name = '', $address = '', $platform_id = 1)
{
$coin = Coin::findOne(['name' => $name]);
if ($coin) {
$withhold = new self();
$withhold->cid = $coin->id;
$withhold->status = $status;
$withhold->token_name = $token_name;
$withhold->address = $address;
$withhold->platform_id = $platform_id;
return $withhold->save();
}
throw new \Exception('币种不存在', -1);
}
}
<?php
/**
* Created By Sublime Text 3
*
* @date 2018-09-12 18:10:27
* @authors rlgy <rlgyzhcn@qq.com>
*/
namespace common\models\psources;
class CoinWithholdToken extends BaseActiveRecord
{
const SCENARIOS_ADD = 'add';
const SCENARIOS_UPDATE = 'update';
public static function tableName()
{
return '{{%coin_withhold_token}}';
}
public function formName()
{
return '';
}
public function attributeLabels()
{
return [
'id' => 'ID',
'address' => '代扣地址',
'status' => '代扣状态',
'token' => 'Token名称',
'wid' => '代扣币种ID',
];
}
public function rules()
{
return [
[['id', 'status', 'wid'], 'integer'],
[['address', 'token'], 'string', 'max' => 255],
[['id', 'wid', 'status', 'token', 'address'], 'required', 'on' => self::SCENARIOS_UPDATE],
[['wid', 'status', 'token', 'address'], 'required', 'on' => self::SCENARIOS_ADD],
];
}
public function scenarios()
{
return [
self::SCENARIOS_ADD => ['token', 'address', 'status', 'wid'],
self::SCENARIOS_UPDATE => ['token', 'address', 'status', 'wid', '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