作者 何书鹏
1 个管道 的构建 通过 耗费 0 秒

企业套餐管理

<?php
namespace app\admin\controller\mobile\package;
use app\common\controller\Backend;
use think\Db;
/**
* 企业套餐规格管理
*
* @icon fa fa-circle-o
*/
class PackageSpec extends Backend
{
/**
* PackageSpec模型对象
* @var \app\admin\model\mobile\package\PackageSpec
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\mobile\package\PackageSpec;
$this->view->assign("isTopList", $this->model->getIsTopList());
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index()
{
//当前是否为关联查询
$this->relationSearch = false;
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax())
{
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField'))
{
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$total = $this->model
->where('package_id',$this->request->request('package_id'))
->where($where)
->order($sort, $order)
->count();
$list = $this->model
->where('package_id',$this->request->request('package_id'))
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();
foreach ($list as $row) {
$row->visible(['id','name','current_price','original_price','people_num','is_top']);
}
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
return $this->view->fetch();
}
/**
* 添加
*/
public function add()
{
$package_id = $this->request->request('package_id');
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$params[$this->dataLimitField] = $this->auth->id;
}
$params['package_id'] = $package_id;
$result = false;
Db::startTrans();
try {
//是否采用模型验证
if ($this->modelValidate) {
$name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
$validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
$this->model->validateFailException(true)->validate($validate);
}
$result = $this->model->allowField(true)->save($params);
Db::commit();
} catch (ValidateException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($result !== false) {
$this->success();
} else {
$this->error(__('No rows were inserted'));
}
}
$this->error(__('Parameter %s can not be empty', ''));
}
return $this->view->fetch();
}
}
... ...
... ... @@ -3,6 +3,7 @@
namespace app\admin\controller\mobile\secret;
use app\common\controller\Backend;
use think\Db;
/**
* 密卷规格管理
... ...
<?php
return [
'Id' => 'id',
'Package_id' => '企业套餐ID',
'Name' => '规格名称',
'Current_price' => '当前价格',
'Original_price' => '原始价格',
'People_num' => '套餐限制人数',
'Is_top' => '是否顶配',
'Is_top 0' => '否',
'Is_top 1' => '是',
'Createtime' => '创建时间',
'Updatetime' => '修改时间'
];
... ...
<?php
namespace app\admin\model\mobile\package;
use think\Model;
class PackageSpec extends Model
{
// 表名
protected $name = 'mobile_package_spec';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
protected $append = [
'is_top_text'
];
public function getIsTopList()
{
return ['0' => __('Is_top 0'), '1' => __('Is_top 1')];
}
public function getIsTopTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['is_top']) ? $data['is_top'] : '');
$list = $this->getIsTopList();
return isset($list[$value]) ? $list[$value] : '';
}
}
... ...
<?php
namespace app\admin\validate\mobile\package;
use think\Validate;
class PackageSpec extends Validate
{
/**
* 验证规则
*/
protected $rule = [
];
/**
* 提示消息
*/
protected $message = [
];
/**
* 验证场景
*/
protected $scene = [
'add' => [],
'edit' => [],
];
}
... ...
... ... @@ -15,7 +15,7 @@
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Secret_ids')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-secret_ids" data-rule="required" data-field="title" data-source="mobile/secret/index" data-multiple="true" class="form-control selectpage" name="row[secret_ids]" type="text" value="">
<input id="c-secret_ids" data-rule="required" data-field="title" data-source="mobile/secret/secret/index" data-multiple="true" class="form-control selectpage" name="row[secret_ids]" type="text" value="">
</div>
</div>
<div class="form-group">
... ...
... ... @@ -15,7 +15,7 @@
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Secret_ids')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-secret_ids" data-rule="required" data-field="title" data-source="mobile/secret/index" data-multiple="true" class="form-control selectpage" name="row[secret_ids]" type="text" value="{$row.secret_ids|htmlentities}">
<input id="c-secret_ids" data-rule="required" data-field="title" data-source="mobile/secret/secret/index" data-multiple="true" class="form-control selectpage" name="row[secret_ids]" type="text" value="{$row.secret_ids|htmlentities}">
</div>
</div>
<div class="form-group">
... ...
<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<!-- <div class="form-group">-->
<!-- <label class="control-label col-xs-12 col-sm-2">{:__('Package_id')}:</label>-->
<!-- <div class="col-xs-12 col-sm-8">-->
<!-- <input id="c-package_id" data-rule="required" data-source="package/index" class="form-control selectpage" name="row[package_id]" type="text" value="">-->
<!-- </div>-->
<!-- </div>-->
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Name')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-name" data-rule="required" class="form-control" name="row[name]" type="text" value="">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Current_price')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-current_price" data-rule="required" class="form-control" step="0.01" name="row[current_price]" type="number" value="0.00">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Original_price')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-original_price" data-rule="required" class="form-control" step="0.01" name="row[original_price]" type="number" value="0.00">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('People_num')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-people_num" data-rule="required" class="form-control" name="row[people_num]" type="number" value="0">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Is_top')}:</label>
<div class="col-xs-12 col-sm-8">
<select id="c-is_top" data-rule="required" class="form-control selectpicker" name="row[is_top]">
{foreach name="isTopList" item="vo"}
<option value="{$key}" {in name="key" value="0"}selected{/in}>{$vo}</option>
{/foreach}
</select>
</div>
</div>
<div class="form-group layer-footer">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">
<button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
<button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
</div>
</div>
</form>
... ...
<form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<!-- <div class="form-group">-->
<!-- <label class="control-label col-xs-12 col-sm-2">{:__('Package_id')}:</label>-->
<!-- <div class="col-xs-12 col-sm-8">-->
<!-- <input id="c-package_id" data-rule="required" data-source="package/index" class="form-control selectpage" name="row[package_id]" type="text" value="{$row.package_id|htmlentities}">-->
<!-- </div>-->
<!-- </div>-->
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Name')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-name" data-rule="required" class="form-control" name="row[name]" type="text" value="{$row.name|htmlentities}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Current_price')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-current_price" data-rule="required" class="form-control" step="0.01" name="row[current_price]" type="number" value="{$row.current_price|htmlentities}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Original_price')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-original_price" data-rule="required" class="form-control" step="0.01" name="row[original_price]" type="number" value="{$row.original_price|htmlentities}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('People_num')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-people_num" data-rule="required" class="form-control" name="row[people_num]" type="number" value="{$row.people_num|htmlentities}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Is_top')}:</label>
<div class="col-xs-12 col-sm-8">
<select id="c-is_top" data-rule="required" class="form-control selectpicker" name="row[is_top]">
{foreach name="isTopList" item="vo"}
<option value="{$key}" {in name="key" value="$row.is_top"}selected{/in}>{$vo}</option>
{/foreach}
</select>
</div>
</div>
<div class="form-group layer-footer">
<label class="control-label col-xs-12 col-sm-2"></label>
<div class="col-xs-12 col-sm-8">
<button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
<button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
</div>
</div>
</form>
... ...
<div class="panel panel-default panel-intro">
{:build_heading()}
<div class="panel-body">
<div id="myTabContent" class="tab-content">
<div class="tab-pane fade active in" id="one">
<div class="widget-body no-padding">
<div id="toolbar" class="toolbar">
<a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
<a href="javascript:;" class="btn btn-success btn-add {:$auth->check('mobile/package/package_spec/add')?'':'hide'}" title="{:__('Add')}" ><i class="fa fa-plus"></i> {:__('Add')}</a>
<a href="javascript:;" class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('mobile/package/package_spec/edit')?'':'hide'}" title="{:__('Edit')}" ><i class="fa fa-pencil"></i> {:__('Edit')}</a>
<a href="javascript:;" class="btn btn-danger btn-del btn-disabled disabled {:$auth->check('mobile/package/package_spec/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>
</div>
<table id="table" class="table table-striped table-bordered table-hover table-nowrap"
data-operate-edit="{:$auth->check('mobile/package/package_spec/edit')}"
data-operate-del="{:$auth->check('mobile/package/package_spec/del')}"
width="100%">
</table>
</div>
</div>
</div>
</div>
</div>
... ...
... ... @@ -31,7 +31,24 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
{field: 'current_price', title: __('Current_price'), operate:'BETWEEN'},
{field: 'buy_num_virtual', title: __('Buy_num_virtual')},
{field: 'buy_num_real', title: __('Buy_num_real')},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
//操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
{
field: 'operate',
title: __('Operate'),
table: table,
events: Table.api.events.operate,
buttons: [
{
name: 'spec',
text: '规格管理',
title: '规格管理',
classname: 'btn btn-xs btn-primary btn-dialog',
icon: 'fa fa-list',
url: 'mobile/package/package_spec?package_id={id}',
}
],
formatter: Table.api.formatter.operate
}
]
]
});
... ...
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'mobile/package/package_spec/index' + location.search,
add_url: 'mobile/package/package_spec/add' + location.search,
edit_url: 'mobile/package/package_spec/edit',
del_url: 'mobile/package/package_spec/del',
multi_url: 'mobile/package/package_spec/multi',
table: 'mobile_package_spec',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
columns: [
[
{checkbox: true},
{field: 'id', title: __('Id')},
{field: 'name', title: __('Name')},
{field: 'current_price', title: __('Current_price'), operate:'BETWEEN'},
{field: 'original_price', title: __('Original_price'), operate:'BETWEEN'},
{field: 'people_num', title: __('People_num')},
{field: 'is_top', title: __('Is_top'), searchList: {"0":__('Is_top 0'),"1":__('Is_top 1')}, formatter: Table.api.formatter.normal},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
]
]
});
// 为表格绑定事件
Table.api.bindevent(table);
},
add: function () {
Controller.api.bindevent();
},
edit: function () {
Controller.api.bindevent();
},
api: {
bindevent: function () {
$(document).on("change", "#c-is_top", function () {
if($(this).val() == '1'){
$('#c-people_num').val(999999);
}
});
Form.api.bindevent($("form[role=form]"));
}
}
};
return Controller;
});
\ No newline at end of file
... ...