Commit 35a7af46 authored by rlgy's avatar rlgy

空投类型

parent 7c8ae3b7
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-13
* Time: 下午3:22
*/
namespace backend\controllers;
use Yii;
use common\models\pwallet\InvestmentType;
class InvestmentController extends BaseController
{
public function actionIndex()
{
return $this->render('index');
}
public function actionAddOne()
{
$this->layout = false;
return $this->render('form_add_one');
}
public function actionType()
{
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = 'json';
return InvestmentType::getList(1, 999, []);
}
return $this->render('type');
}
public function actionTypeAdd()
{
$this->layout = false;
if (Yii::$app->request->isGet) {
return $this->render('type-add');
} elseif (Yii::$app->request->isPost) {
Yii::$app->response->format = 'json';
$name = Yii::$app->request->post('name', '');
if ($name) {
if (!InvestmentType::isExists($name)) {
$model = new InvestmentType();
$model->name = $name;
$model->create_at = date('Y-m-d H:i:s');
$model->save();
} else {
return ['code' => 2, 'msg' => '类型已经存在'];
}
} else {
return ['code' => 1, 'msg' => '名称不能为空'];
}
return ['code' => 0, 'msg' => 'succeed'];
}
}
public function actionTypeDelete($id)
{
Yii::$app->response->format = 'json';
$model = InvestmentType::findOne(['id' => $id]);
if ($model) {
try {
$model->delete();
} catch (\Exception|\Throwable $e) {
return ['code' => $e->getCode(), 'msg' => $e->getMessage()];
}
} else {
return ['code' => 1, 'msg' => '类型不存在'];
}
return ['code' => 0, 'msg' => 'succeed'];
}
public function actionTypeUpdate()
{
if (Yii::$app->request->isGet) {
$id = Yii::$app->request->get('id', 0);
$model = InvestmentType::findOne(['id' => $id]);
if ($model) {
$this->layout = false;
return $this->render('type-update', ['model' => $model]);
} else {
Yii::$app->response->format = 'json';
return ['code' => 1, 'msg' => '类型不存在'];
}
} elseif (Yii::$app->request->isPost) {
Yii::$app->response->format = 'json';
$id = Yii::$app->request->post('id', 0);
$name = Yii::$app->request->post('name', 0);
$model = InvestmentType::findOne(['id' => $id]);
if ($model) {
$model->name = $name;
$model->save();
} else {
return ['code' => 1, 'msg' => '类型不存在'];
}
return ['code' => 0, 'msg' => 'succeed'];
}
}
}
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-20
* Time: 下午3:09
*/
?>
<div style="padding: 5px;">
<form id="type-add" class="layui-form">
<input type="hidden" name="_csrf" value="<?= Yii::$app->request->getCsrfToken() ?>">
<div class="layui-form-item">
<label class="layui-form-label">名称</label>
<div class="layui-input-inline">
<input name="name" class="layui-input">
</div>
</div>
</form>
</div>
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-20
* Time: 下午4:27
*/
?>
<div style="padding: 5px;">
<form id="type-update" class="layui-form">
<input type="hidden" name="_csrf" value="<?= Yii::$app->request->getCsrfToken() ?>">
<input type="hidden" name="id" value="<?= $model->id ?>">
<div class="layui-form-item">
<label class="layui-form-label">名称</label>
<div class="layui-input-inline">
<input name="name" class="layui-input" value="<?= $model->name ?>">
</div>
</div>
</form>
</div>
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-20
* Time: 下午2:46
*/
/**
* @var \yii\web\View $this
*/
$this->registerJsFile('@web/js/investment-type/index.js');
?>
<h4>空投类型管理</h4>
<div class="layui-row">
<button class="layui-btn" id="add">添加类型</button>
</div>
<div class="layui-row">
<div class="layui-col-md6">
<table class="layui-table" id="table1" lay-filter="table1"></table>
</div>
</div>
<script type="text/html" id="operationTpl">
<button class="layui-btn layui-btn-sm" lay-event="update">修改</button>
<button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="delete">删除</button>
</script>
/**
* 空投类型js
* @author rlgyzhcn@qq.com
*/
var table = layui.table;
table.render({
elem: "#table1",
url: '/admin/investment/type',
cols: [[
{field: 'id', title: "ID"},
{field: 'name', title: "类型"},
{title: '操作', templet: "#operationTpl"}
]],
});
$("#add").click(function () {
$.get('/admin/investment/type-add', {}, function (str) {
var index = layer.open({
type: 1,
title: '添加类型',
id: 'addType',
skin: 'layui-layer-lan',
area: ['320px', 'auto'],
content: str,
btn: ['确认', '取消'],
btn1: function () {
var name = $("#type-add input[name='name']").val();
var _csrf = $("#type-add input[name='_csrf']").val();
$.post('/admin/investment/type-add', {_csrf: _csrf, name: name}, function (res) {
layer.msg(res.msg);
if (res.code == 0) {
layer.close(index);
table.reload('table1', {curr: 1});
}
});
}
});
});
});
table.on('tool(table1)', function (obj) {
var data = obj.data;
var event = obj.event;
if (event == 'delete') {
var confirm = layer.confirm('删除?', {icon: 3, title: '删除'}, function (index) {
$.get('/admin/investment/type-delete', {id: data.id}, function (res) {
layer.close(confirm);
table.reload('table1', {curr: 1});
});
});
} else if (event == 'update') {
$.get('/admin/investment/type-update', {id: data.id}, function (res) {
if (typeof res != 'string' && res.code != 0) {
layer.msg(res.msg);
} else {
var index = layer.open({
type: 1,
title: '修改',
id: 'updateType',
skin: 'layui-layer-lan',
area: ['320px', 'auto'],
content: res,
btn: ['确认', '取消'],
btn1: function () {
var name = $("#type-update input[name='name']").val();
var _csrf = $("#type-update input[name='_csrf']").val();
$.post('/admin/investment/type-update', {
_csrf: _csrf,
name: name,
id: data.id
}, function (res) {
layer.msg(res.msg);
if (res.code == 0) {
layer.close(index);
table.reload('table1', {curr: 1});
}
});
}
});
}
})
}
});
\ No newline at end of file
<?php
/**
* Created by PhpStorm.
* User: rlgyzhcn
* Date: 18-6-20
* Time: 下午3:28
*/
namespace common\models\pwallet;
use Yii;
use common\core\BaseActiveRecord;
/**
* Class InvestmentType
* @property integer $id
* @property string $name
* @property string $create_at
* @package common\models\pwallet
*/
class InvestmentType extends BaseActiveRecord
{
/**
* @return null|object|\yii\db\Connection
* @throws \yii\base\InvalidConfigException
*/
public static function getDb()
{
return Yii::$app->get('db_pwallet');
}
public static function isExists($name)
{
return (bool)self::findOne(['name' => $name]);
}
}
\ 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