作者 曹东东
  1 +<?php
  2 +
  3 +namespace app\school\controller\station;
  4 +
  5 +use app\common\controller\DoubleBase;
  6 +use think\Db;
  7 +use think\Exception;
  8 +use think\exception\PDOException;
  9 +
  10 +/**
  11 + * 工位管理
  12 + *
  13 + * @icon fa fa-circle-o
  14 + */
  15 +class Station extends DoubleBase
  16 +{
  17 +
  18 + /**
  19 + * Station模型对象
  20 + * @var \app\school\model\station\Station
  21 + */
  22 + protected $noNeedRight = ['*'];
  23 + protected $model = null;
  24 +
  25 + public function _initialize()
  26 + {
  27 + parent::_initialize();
  28 + $this->model = new \app\school\model\station\Station;
  29 + $type = [
  30 + '1' => '个人',
  31 + '2' => '会议室',
  32 + ];
  33 + $this->assign('type',$type);
  34 + $this->assignconfig('type',$type);
  35 + }
  36 +
  37 + public function import()
  38 + {
  39 + parent::import();
  40 + }
  41 +
  42 + /**
  43 + * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  44 + * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  45 + * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  46 + */
  47 + public function index()
  48 + {
  49 + //设置过滤方法
  50 + $this->request->filter(['strip_tags', 'trim']);
  51 + if ($this->request->isAjax()) {
  52 + list($where, $sort, $order, $offset, $limit) = $this->buildparams(null,true);
  53 + $list = $this->model
  54 + ->where($where)
  55 + ->order('createtime', $order)
  56 + ->paginate($limit);
  57 + $result = array("total" => $list->total(), "rows" => $list->items());
  58 + return json($result);
  59 + }
  60 + return $this->view->fetch();
  61 + }
  62 +
  63 + /**
  64 + * 添加
  65 + */
  66 + public function add()
  67 + {
  68 + if ($this->request->isPost()) {
  69 + $params = $this->request->post("row/a");
  70 + if ($params) {
  71 + $params = $this->preExcludeFields($params);
  72 + $params['school_id'] = SCHOOL_ID;
  73 + if (!$params['capacity']){
  74 + $this->error("该工位可容纳人数应大于0");
  75 + }
  76 + Db::startTrans();
  77 + try {
  78 + $result = $this->model->allowField(true)->save($params);
  79 + Db::commit();
  80 + } catch (Exception $e) {
  81 + Db::rollback();
  82 + $this->error($e->getMessage());
  83 + }
  84 + $this->success();
  85 + }
  86 + $this->error(__('Parameter %s can not be empty', ''));
  87 + }
  88 + return $this->view->fetch();
  89 + }
  90 + /**
  91 + * 编辑
  92 + */
  93 + public function edit($ids = null)
  94 + {
  95 + if(empty($ids)){
  96 + $ids = $this->request->param('id');
  97 + }
  98 + $row = $this->model->get($ids);
  99 + if (!$row) {
  100 + $this->error(__('No Results were found'));
  101 + }
  102 + if ($this->request->isPost()) {
  103 +
  104 + $params = $this->request->post("row/a");
  105 + if ($params) {
  106 + $params = $this->preExcludeFields($params);
  107 + $params['school_id'] = SCHOOL_ID;
  108 + if (!$params['capacity']){
  109 + $this->error("该工位可容纳人数应大于0");
  110 + }
  111 + Db::startTrans();
  112 + try {
  113 + $result = $row->allowField(true)->save($params);
  114 + Db::commit();
  115 + } catch (Exception $e) {
  116 + Db::rollback();
  117 + $this->error($e->getMessage());
  118 + }
  119 + $this->success();
  120 + }
  121 + $this->error(__('Parameter %s can not be empty', ''));
  122 + }
  123 +
  124 + $this->view->assign("row", $row); //选中的数据
  125 + $this->assignconfig('id',$ids);
  126 + return $this->view->fetch();
  127 + }
  128 +
  129 + /**
  130 + * 删除
  131 + */
  132 + public function del($ids = "")
  133 + {
  134 + if ($ids) {
  135 + $pk = $this->model->getPk();
  136 +
  137 + if($this->shopIdAutoCondition){
  138 + $this->model->where(array('school_id'=>SCHOOL_ID));
  139 + }
  140 + $list = $this->model->where($pk, 'in', $ids)->select();
  141 +
  142 + $count = 0;
  143 + Db::startTrans();
  144 + try {
  145 + foreach ($list as $k => $v) {
  146 + $count += $v->delete();
  147 + }
  148 + Db::commit();
  149 + } catch (PDOException $e) {
  150 + Db::rollback();
  151 + $this->error($e->getMessage());
  152 + } catch (Exception $e) {
  153 + Db::rollback();
  154 + $this->error($e->getMessage());
  155 + }
  156 + if ($count) {
  157 + $this->success();
  158 + } else {
  159 + $this->error(__('No rows were deleted'));
  160 + }
  161 + }
  162 + $this->error(__('Parameter %s can not be empty', 'ids'));
  163 + }
  164 +}
  1 +<?php
  2 +
  3 +return [
  4 + 'Id' => '主键ID',
  5 + 'Station_name' => '工位名称',
  6 + 'Type' => '类型',// 0:个人 1:会议室
  7 + 'Status' => '状态',// 0:不开放 1:开放
  8 + 'Capacity' => '容纳人数',
  9 + 'Describe' => '描述'
  10 +];
  1 +<?php
  2 +
  3 +namespace app\school\model\station;
  4 +
  5 +use think\Model;
  6 +use traits\model\SoftDelete;
  7 +
  8 +class Station extends Model
  9 +{
  10 + use SoftDelete;
  11 + // 表名
  12 + protected $name = 'station';
  13 +
  14 + // 自动写入时间戳字段
  15 + protected $autoWriteTimestamp = 'int';
  16 +
  17 + // 定义时间戳字段名
  18 + protected $createTime = 'createtime';
  19 + protected $updateTime = 'updatetime';
  20 + protected $deleteTime = 'deletetime';
  21 +
  22 + // 追加属性
  23 + protected $append = [
  24 + 'status_text',
  25 + ];
  26 +
  27 + public function getStatusTextAttr($value,$data)
  28 + {
  29 + return isset($data['status']) ? ($data['status'] == 1 ? '开放' : '不开放') : '';
  30 + }
  31 +
  32 +
  33 +
  34 +
  35 +
  36 +
  37 +
  38 +
  39 +}
  1 +<?php
  2 +
  3 +namespace app\school\validate\station;
  4 +
  5 +use think\Validate;
  6 +
  7 +class Station extends Validate
  8 +{
  9 + /**
  10 + * 验证规则
  11 + */
  12 + protected $rule = [
  13 + ];
  14 + /**
  15 + * 提示消息
  16 + */
  17 + protected $message = [
  18 + ];
  19 + /**
  20 + * 验证场景
  21 + */
  22 + protected $scene = [
  23 + 'add' => [],
  24 + 'edit' => [],
  25 + ];
  26 +
  27 +}
  1 +<form id="add-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
  2 +
  3 + <div class="form-group" style="margin-top: 30px;">
  4 + <label class="control-label col-xs-12 col-sm-2">{:__('Station_name')}:</label>
  5 + <div class="col-xs-12 col-sm-8">
  6 + <input id="c-station_name" class="form-control" name="row[station_name]" type="text">
  7 + </div>
  8 + </div>
  9 + <div class="form-group">
  10 + <label class="control-label col-xs-12 col-sm-2">{:__('Type')}:</label>
  11 + <div class="col-xs-12 col-sm-8">
  12 + <select id="c-type" data-rule="required" class="form-control selectpicker" name="row[type]">
  13 + {foreach name="type" item="vo"}
  14 + <option value="{$key}" >{$vo}</option>
  15 + {/foreach}
  16 + </select>
  17 + </div>
  18 + </div>
  19 + <div class="form-group">
  20 + <label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
  21 + <div class="col-xs-12 col-sm-8">
  22 + <label class="radio-inline">
  23 + <input type="radio" name="row[status]" class="status" value="0" checked> 不开放
  24 + </label>
  25 + <label class="radio-inline">
  26 + <input type="radio" name="row[status]" class="status" value="1"> 开放
  27 + </label>
  28 + </div>
  29 + </div>
  30 + <div class="form-group">
  31 + <label class="control-label col-xs-12 col-sm-2">{:__('Capacity')}:</label>
  32 + <div class="col-xs-12 col-sm-8">
  33 + <input id="c-capacity" class="form-control" name="row[capacity]" type="number" value="" oninput="javascript:this.value=this.value.replace(/[^\d]/g,'')">
  34 + </div>
  35 + </div>
  36 + <div class="form-group">
  37 + <label class="control-label col-xs-12 col-sm-2">{:__('Describe')}:</label>
  38 + <div class="col-xs-12 col-sm-8">
  39 + <textarea id="c-describe" class="form-control" rows="5" name="row[describe]" cols="50"></textarea>
  40 + </div>
  41 + </div>
  42 + <div class="form-group layer-footer">
  43 + <label class="control-label col-xs-12 col-sm-2"></label>
  44 + <div class="col-xs-12 col-sm-8">
  45 + <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
  46 + <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
  47 + </div>
  48 + </div>
  49 +</form>
  1 +<form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
  2 +
  3 + <div class="form-group" style="margin-top: 30px;">
  4 + <label class="control-label col-xs-12 col-sm-2">{:__('Station_name')}:</label>
  5 + <div class="col-xs-12 col-sm-8">
  6 + <input id="c-station_name" class="form-control" name="row[station_name]" type="text" value="{$row.station_name|htmlentities}">
  7 + </div>
  8 + </div>
  9 + <div class="form-group">
  10 + <label class="control-label col-xs-12 col-sm-2">{:__('Type')}:</label>
  11 + <div class="col-xs-12 col-sm-8">
  12 + <select id="c-type" data-rule="required" class="form-control selectpicker" name="row[type]">
  13 + {foreach name="type" item="vo"}
  14 + <option value="{$key}" {if $row['type'] == $key} selected {/if}>{$vo}</option>
  15 + {/foreach}
  16 + </select>
  17 + </div>
  18 + </div>
  19 + <div class="form-group">
  20 + <label class="control-label col-xs-12 col-sm-2">{:__('Status')}:</label>
  21 + <div class="col-xs-12 col-sm-8">
  22 + <label class="radio-inline">
  23 + <input type="radio" name="row[status]" class="status" value="0" {if $row['status'] == 0} checked {/if}> 不开放
  24 + </label>
  25 + <label class="radio-inline">
  26 + <input type="radio" name="row[status]" class="status" value="1" {if $row['status'] == 1} checked {/if}> 开放
  27 + </label>
  28 + </div>
  29 + </div>
  30 + <div class="form-group">
  31 + <label class="control-label col-xs-12 col-sm-2">{:__('Capacity')}:</label>
  32 + <div class="col-xs-12 col-sm-8">
  33 + <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>
  34 + </div>
  35 + </div>
  36 + <div class="form-group">
  37 + <label class="control-label col-xs-12 col-sm-2">{:__('Describe')}:</label>
  38 + <div class="col-xs-12 col-sm-8">
  39 + <textarea id="c-describe" class="form-control" rows="5" name="row[describe]" cols="50">{$row.describe|htmlentities}</textarea>
  40 + </div>
  41 + </div>
  42 + <div class="form-group layer-footer">
  43 + <label class="control-label col-xs-12 col-sm-2"></label>
  44 + <div class="col-xs-12 col-sm-8">
  45 + <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
  46 + <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
  47 + </div>
  48 + </div>
  49 +</form>
  1 +<div class="panel panel-default panel-intro">
  2 + {:build_heading()}
  3 +
  4 + <div class="panel-body">
  5 + <div id="myTabContent" class="tab-content">
  6 + <div class="tab-pane fade active in" id="one">
  7 + <div class="widget-body no-padding">
  8 + <div id="toolbar" class="toolbar">
  9 + <a href="javascript:;" class="btn btn-primary btn-refresh" title="{:__('Refresh')}" ><i class="fa fa-refresh"></i> </a>
  10 + <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>
  11 +<!-- <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>-->
  12 +<!-- <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>-->
  13 +<!-- <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>-->
  14 +
  15 + <div class="dropdown btn-group {:$auth->check('station/station/multi')?'':'hide'}">
  16 +<!-- <a class="btn btn-primary btn-more dropdown-toggle btn-disabled disabled" data-toggle="dropdown"><i class="fa fa-cog"></i> {:__('More')}</a>-->
  17 + <ul class="dropdown-menu text-left" role="menu">
  18 + <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>
  19 + <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>
  20 + </ul>
  21 + </div>
  22 +
  23 +
  24 + </div>
  25 + <table id="table" class="table table-striped table-bordered table-hover table-nowrap"
  26 + data-operate-edit="{:$auth->check('station/station/edit')}"
  27 + data-operate-del="{:$auth->check('station/station/del')}"
  28 + width="100%">
  29 + </table>
  30 + </div>
  31 + </div>
  32 +
  33 + </div>
  34 + </div>
  35 +</div>
  1 +define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
  2 +
  3 + var Controller = {
  4 + index: function () {
  5 + // 初始化表格参数配置
  6 + Table.api.init({
  7 + extend: {
  8 + index_url: 'station/station/index' + location.search,
  9 + add_url: 'station/station/add',
  10 + edit_url: 'station/station/edit',
  11 + del_url: 'station/station/del',
  12 + multi_url: 'station/station/multi',
  13 + import_url: 'station/station/import',
  14 + table: 'station',
  15 + }
  16 + });
  17 +
  18 + var table = $("#table");
  19 +
  20 + // 初始化表格
  21 + table.bootstrapTable({
  22 + url: $.fn.bootstrapTable.defaults.extend.index_url,
  23 + pk: 'id',
  24 + sortName: 'id',
  25 + //禁用默认搜索
  26 + search: false,
  27 + //启用普通表单搜索
  28 + // commonSearch: false,
  29 + //可以控制是否默认显示搜索单表,false则隐藏,默认为false
  30 + // searchFormVisible: false,
  31 +
  32 + columns: [
  33 + [
  34 + {checkbox: true},
  35 + {field: 'id', title: __('Id'),visible:false, operate: false},
  36 + {field: 'station_name', title: __('Station_name'), operate: 'LIKE'},
  37 + {field: 'type', title: __('Type'),searchList:Config.type, formatter: Table.api.formatter.normal},
  38 + {field: 'status', title: __('Status'),searchList:{"0":__("不开放"),"1":__("开放")}, formatter: Table.api.formatter.normal},
  39 + {field: 'capacity', title: __('Capacity'), operate: false},
  40 + {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
  41 + ]
  42 + ]
  43 + });
  44 +
  45 + // 为表格绑定事件
  46 + Table.api.bindevent(table);
  47 + },
  48 + add: function () {
  49 + Controller.api.bindevent();
  50 + },
  51 + edit: function () {
  52 + Controller.api.bindevent();
  53 + },
  54 + api: {
  55 + bindevent: function () {
  56 + Form.api.bindevent($("form[role=form]"));
  57 + }
  58 + }
  59 + };
  60 + return Controller;
  61 +});