作者 金洪杰

工位管理

<?php
namespace app\school\controller\station;
use app\common\controller\DoubleBase;
use think\Db;
use think\Exception;
use think\exception\PDOException;
/**
* 工位管理
*
* @icon fa fa-circle-o
*/
class Station extends DoubleBase
{
/**
* Station模型对象
* @var \app\school\model\station\Station
*/
protected $noNeedRight = ['*'];
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\school\model\station\Station;
$type = [
'1' => '个人',
'2' => '会议室',
];
$this->assign('type',$type);
$this->assignconfig('type',$type);
}
public function import()
{
parent::import();
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
public function index()
{
//设置过滤方法
$this->request->filter(['strip_tags', 'trim']);
if ($this->request->isAjax()) {
list($where, $sort, $order, $offset, $limit) = $this->buildparams(null,true);
$list = $this->model
->where($where)
->order('createtime', $order)
->paginate($limit);
$result = array("total" => $list->total(), "rows" => $list->items());
return json($result);
}
return $this->view->fetch();
}
/**
* 添加
*/
public function add()
{
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
$params['school_id'] = SCHOOL_ID;
Db::startTrans();
try {
$result = $this->model->allowField(true)->save($params);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success();
}
$this->error(__('Parameter %s can not be empty', ''));
}
return $this->view->fetch();
}
/**
* 编辑
*/
public function edit($ids = null)
{
if(empty($ids)){
$ids = $this->request->param('id');
}
$row = $this->model->get($ids);
if (!$row) {
$this->error(__('No Results were found'));
}
if ($this->request->isPost()) {
$params = $this->request->post("row/a");
if ($params) {
$params = $this->preExcludeFields($params);
$params['school_id'] = SCHOOL_ID;
Db::startTrans();
try {
$result = $row->allowField(true)->save($params);
Db::commit();
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
$this->success();
}
$this->error(__('Parameter %s can not be empty', ''));
}
$this->view->assign("row", $row); //选中的数据
$this->assignconfig('id',$ids);
return $this->view->fetch();
}
/**
* 删除
*/
public function del($ids = "")
{
if ($ids) {
$pk = $this->model->getPk();
if($this->shopIdAutoCondition){
$this->model->where(array('school_id'=>SCHOOL_ID));
}
$list = $this->model->where($pk, 'in', $ids)->select();
$count = 0;
Db::startTrans();
try {
foreach ($list as $k => $v) {
$count += $v->delete();
}
Db::commit();
} catch (PDOException $e) {
Db::rollback();
$this->error($e->getMessage());
} catch (Exception $e) {
Db::rollback();
$this->error($e->getMessage());
}
if ($count) {
$this->success();
} else {
$this->error(__('No rows were deleted'));
}
}
$this->error(__('Parameter %s can not be empty', 'ids'));
}
}
... ...
<?php
return [
'Id' => '主键ID',
'Station_name' => '工位名称',
'Type' => '类型',// 0:个人 1:会议室
'Status' => '状态',// 0:不开放 1:开放
'Capacity' => '容纳人数',
'Describe' => '描述'
];
... ...
<?php
namespace app\school\model\station;
use think\Model;
use traits\model\SoftDelete;
class Station extends Model
{
use SoftDelete;
// 表名
protected $name = 'station';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
// 追加属性
protected $append = [
'status_text',
];
public function getStatusTextAttr($value,$data)
{
return isset($data['status']) ? ($data['status'] == 1 ? '开放' : '不开放') : '';
}
}
... ...
<?php
namespace app\school\validate\station;
use think\Validate;
class Station extends Validate
{
/**
* 验证规则
*/
protected $rule = [
];
/**
* 提示消息
*/
protected $message = [
];
/**
* 验证场景
*/
protected $scene = [
'add' => [],
'edit' => [],
];
}
... ...
<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
<div class="form-group" style="margin-top: 30px;">
<label class="control-label col-xs-12 col-sm-2">{:__('Station_name')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-station_name" class="form-control" name="row[station_name]" type="text">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Type')}:</label>
<div class="col-xs-12 col-sm-8">
<select id="c-type" data-rule="required" class="form-control selectpicker" name="row[type]">
{foreach name="type" item="vo"}
<option value="{$key}" >{$vo}</option>
{/foreach}
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
<div class="col-xs-12 col-sm-8">
<label class="radio-inline">
<input type="radio" name="row[status]" class="status" value="0" checked> 不开放
</label>
<label class="radio-inline">
<input type="radio" name="row[status]" class="status" value="1"> 开放
</label>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Capacity')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-capacity" class="form-control" name="row[capacity]" type="number" value="" oninput="javascript:this.value=this.value.replace(/[^\d]/g,'')">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Describe')}:</label>
<div class="col-xs-12 col-sm-8">
<textarea id="c-describe" class="form-control" rows="5" name="row[describe]" cols="50"></textarea>
</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" style="margin-top: 30px;">
<label class="control-label col-xs-12 col-sm-2">{:__('Station_name')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-station_name" class="form-control" name="row[station_name]" type="text" value="{$row.station_name|htmlentities}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Type')}:</label>
<div class="col-xs-12 col-sm-8">
<select id="c-type" data-rule="required" class="form-control selectpicker" name="row[type]">
{foreach name="type" item="vo"}
<option value="{$key}" {if $row['type'] == $key} selected {/if}>{$vo}</option>
{/foreach}
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
<div class="col-xs-12 col-sm-8">
<label class="radio-inline">
<input type="radio" name="row[status]" class="status" value="0" {if $row['status'] == 0} checked {/if}> 不开放
</label>
<label class="radio-inline">
<input type="radio" name="row[status]" class="status" value="1" {if $row['status'] == 1} checked {/if}> 开放
</label>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Capacity')}:</label>
<div class="col-xs-12 col-sm-8">
<div class='control-relative'><input id="c-capacity" class="form-control" data-toggle="city-picker" name="row[capacity]" type="text" value="{$row.capacity|htmlentities}" oninput="javascript:this.value=this.value.replace(/[^\d]/g,'')"></div>
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Describe')}:</label>
<div class="col-xs-12 col-sm-8">
<textarea id="c-describe" class="form-control" rows="5" name="row[describe]" cols="50">{$row.describe|htmlentities}</textarea>
</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('station/station/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('station/station/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('station/station/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>-->
<!-- <a href="javascript:;" class="btn btn-danger btn-import {:$auth->check('station/station/import')?'':'hide'}" title="{:__('Import')}" id="btn-import-file" data-url="ajax/upload" data-mimetype="csv,xls,xlsx" data-multiple="false"><i class="fa fa-upload"></i> {:__('Import')}</a>-->
<div class="dropdown btn-group {:$auth->check('station/station/multi')?'':'hide'}">
<!-- <a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>-->
<ul class="dropdown-menu text-left" role="menu">
<li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=normal"><i class="fa fa-eye"></i> {:__('Set to normal')}</a></li>
<li><a class="btn btn-link btn-multi btn-disabled disabled" href="javascript:;" data-params="status=hidden"><i class="fa fa-eye-slash"></i> {:__('Set to hidden')}</a></li>
</ul>
</div>
</div>
<table id="table" class="table table-striped table-bordered table-hover table-nowrap"
data-operate-edit="{:$auth->check('station/station/edit')}"
data-operate-del="{:$auth->check('station/station/del')}"
width="100%">
</table>
</div>
</div>
</div>
</div>
</div>
... ...
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'station/station/index' + location.search,
add_url: 'station/station/add',
edit_url: 'station/station/edit',
del_url: 'station/station/del',
multi_url: 'station/station/multi',
import_url: 'station/station/import',
table: 'station',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
//禁用默认搜索
search: false,
//启用普通表单搜索
// commonSearch: false,
//可以控制是否默认显示搜索单表,false则隐藏,默认为false
// searchFormVisible: false,
columns: [
[
{checkbox: true},
{field: 'id', title: __('Id'),visible:false, operate: false},
{field: 'station_name', title: __('Station_name'), operate: 'LIKE'},
{field: 'type', title: __('Type'),searchList:Config.type, formatter: Table.api.formatter.normal},
{field: 'status', title: __('Status'),searchList:{"0":__("不开放"),"1":__("开放")}, formatter: Table.api.formatter.normal},
{field: 'capacity', title: __('Capacity'), operate: false},
{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 () {
Form.api.bindevent($("form[role=form]"));
}
}
};
return Controller;
});
\ No newline at end of file
... ...