后台 - 列表页
```
{extend name="admin@index_layout"/}
{block name="main"}
<div class="layui-card">
<div class="layui-card-body">
<div class="layui-tab">
<ul class="layui-tab-title">
<li class="layui-this"><a href="{:url('coupon')}">优惠券列表</a></li>
<!-- <li><a data-open="{:url('add')}" data-width="900px" data-height="600px">添加平台</a></li> -->
</ul>
<!-- 表格内容区 -->
<div class="layui-tab-content">
<div class="layui-tab-item layui-show">
<table class="layui-hide" id="currentTable" lay-filter="currentTable"></table>
</div>
</div>
</div>
</div>
</div>
<script type="text/html" id="barTool">
<a data-open='coupon?ymid=add&id={{d.id}}' class="layui-btn layui-btn-xs" data-width="1200px" data-height="700px" title="编辑">编辑</a>
<a href='coupon?ymid=dele&id={{d.id}}' class="layui-btn layui-btn-danger layui-btn-xs layui-tr-del">删除</a>
</script>
{/block}
{block name="script"}
<script>
layui.use('yznTable', function() {
var table = layui.yznTable;
var init = {
table_elem: '#currentTable',
add_url: "coupon?ymid=add",
delete_url: "coupon?ymid=del",
};
table.render({
init: init,
id: 'currentTable',
elem: '#currentTable',
toolbar: ['refresh',
[{
text: '添加',
url: init.add_url,
method: 'open',
class: 'layui-btn layui-btn-normal layui-btn-sm',
icon: 'iconfont icon-add',
extend: 'data-width="1200px" data-height="700px"',
}], 'delete'
],
url: "coupon?ymid=list",
cols: [[
{ type: 'checkbox', fixed: 'left' },
{ field: 'id', width: 70, title: 'ID' },
{ field: 'title', title: '优惠券名称' },
{ fixed: 'right', width: 150, title: '操作', toolbar: '#barTool' }
]],
page: {}
});
});
</script>
{/block}
```
后台 - 新增/编辑
```
{extend name="admin@index_layout"/}
{block name="main"}
<form class="layui-form" method="post">
<div class="layui-form-item">
<label class="layui-form-label">折扣券标题</label>
<div class="layui-input-inline" style= "width: 80%">
<input type="text" name="title" autocomplete="off" placeholder="" class="layui-input" value="{$data.title}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">折扣券编号</label>
<div class="layui-input-inline" style= "width: 80%">
<input type="text" name="coupon_id" autocomplete="off" placeholder="" class="layui-input" value="{$data.coupon_id}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">折扣</label>
<div class="layui-input-inline" style= "width: 80%">
<input type="text" name="discount" autocomplete="off" placeholder="如:90 表示9折,50表示5折。" class="layui-input" value="{$data.discount}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">库存</label>
<div class="layui-input-inline" style= "width: 80%">
<input type="text" name="stock" autocomplete="off" placeholder="" class="layui-input" value="{$data.stock}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">限定模块</label>
<div class="layui-input-label" style= "width: 80%">
<input type="radio" name="limit" value="0" {if $data.limit =='0' || empty($data.limit)}checked {/if} title="不启用" lay-filter="limit">
<input type="radio" name="limit" value="1" {if $data.limit =='1'}checked {/if} title="启用" lay-filter="limit">
</div>
<div class="layui-form-mid layui-word-aux">启用后必须在下方输入允许的模块,否则将全部禁用</div>
</div>
<div class="layui-form-item layui-form-text">
<label class="layui-form-label">模块列表</label>
<div class="layui-input-block">
<textarea name="limit_mk" placeholder="允许使用的模块列表" class="layui-textarea">{$data.limit_mk}</textarea>
<div class="layui-form-mid layui-word-aux">用英文,隔开,比如:cms,abt,phpwczyxx</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">状态</label>
<div class="layui-input-label" style= "width: 80%">
<input type="radio" name="state" value="1" {if $data.state =='1' || empty($data.state)}checked {/if} title="启用" lay-filter="state">
<input type="radio" name="state" value="0" {if $data.state =='0'}checked {/if} title="停用" lay-filter="state">
</div>
</div>
<div class="layui-form-item layer-footer">
<div>
<button class="layui-btn" lay-submit lay-filter="*">立即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</form>
{/block}
{block name="script"}
<script type="text/javascript">
layui.use('yznForm', function() {
var yznForm = layui.yznForm;
yznForm.listen();
});
</script>
{/block}
```
PHP
```
<?php
// 列表
if (empty($_G['ymid'])||$_G['ymid']=='list') {
list($page, $limit, $where) = $this->buildTableParames();
// 查表
$_list = Db::name('coupon')->where($where)->page($page, $limit)->withAttr('id', function ($value, $data) {return ($value);})->select();
// 统计条数
$total = Db::name('coupon')->where($where)->count();
// 输出
$result = array("code" => 0, "count" => $total, "data" => $_list);
$htmlmbmc = "{$_W['controller']}/{$_W['action']}/list"; //模版目录
}
// 新增
if ($_G['ymid']=='add') {
if ($_G['id']){
$info = Db::name('coupon')->where(["id" => $_G['id']])->find();
$this->assign('data', $info);
}
if ($this->request->isPost()) {
$data = $this->request->post(''); //获取提交的值
if (empty($_G['id'])){
$sqjg_add = Db::name('coupon')->strict(false)->insertGetId($data);
if ($sqjg_add) {
$adddata = [
'inputtime' => time(), //创建时间
];
// 提交后同步更新参数
Db::name('coupon')->where(["id" => $sqjg_add])->update($adddata);
$this->success("创建成功", url("admin/coupon_list"));
}else{
$this -> error('创建失败');
}
}else{
$sqjg_up = Db::name('coupon')->where(["id" => $_G['id']])->update($data);
if ($sqjg_up) {
$this->success("更新成功", url("admin/coupon_list"));
}else{
$this -> error('更新失败');
}
}
}
$htmlmbmc = "{$_W['controller']}/{$_W['action']}/add"; //模版目录
}
// 单条删除
if ($_G['ymid']=='dele') {
$id = $this->request->param('id/d', 0);
if (Db::name('coupon')->where(["id" => $id])->delete()) {
$this->success("删除成功!");
} else {
$this->error("删除失败!");
}
}
// 批量删除
if ($_G['ymid']=='del') {
$id = $this->request->param('ids/a', null);
if (!is_array($id)) {
$id = array($id);
}
foreach ($id as $pid) {
$sqlczjg = Db::name('coupon')->where(array('id' => $pid))->delete();
}
if ($sqlczjg) {
$this->success("删除成功!");
}else{
$this -> error('删除失败');
}
}
```