页面JS路径:public/assets/js/backend/invitationlimits.js
页面模型路径:application/admin/model/Invitationlimits.php
页面视图路径:application/admin/view/invitationlimits
控制器路径:application/admin/controller/Invitationlimits.php
上面是关于后台页面的相关路径
//JS配置信息
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'invitationlimits/index',//主页视图url
add_url: 'invitationlimits/add',//新增视图url
edit_url: 'invitationlimits/edit',//编辑视图url
del_url: 'invitationlimits/del',//删除视图url
multi_url: 'invitationlimits/multi',//这里应该是表头 不确定
table: 'invitationlimits',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
columns: [
[
{ checkbox: true },
{ field: 'id', title: __('Id') },
{ field: 'total_limit', title: __('Total_limit') },
{ field: 'created_time', title: __('Created_time'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
{ field: 'notes', title: __('Notes') },
{ field: 'admin.nickname', title: '用户名称' },
{ field: 'created.nickname', title: '操作人' },
// 下面这里是右侧的操作列
// {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;
});
//模型解释 model
<?php
namespace app\admin\model;
use think\Model;
class Invitationlimits extends Model
{
// 表名
protected $name = 'invitationlimits';
// 自动写入时间戳字段
protected $autoWriteTimestamp = false;
// 定义时间戳字段名
protected $createTime = false;
protected $updateTime = false;
// 追加属性
protected $append = [
'created_time_text'
];
public function getCreatedTimeTextAttr($value, $data)
{
$value = $value ? $value : (isset($data['created_time']) ? $data['created_time'] : '');
return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
}
protected function setCreatedTimeAttr($value)
{
return $value && !is_numeric($value) ? strtotime($value) : $value;
}
public function admin()
{
return $this->belongsTo('Admin', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
// 自定义关联模型
public function created()
{
// 关联到 created_by 字段
return $this->belongsTo('Admin', 'created_by', 'id', [], 'LEFT')->setEagerlyType(0);
}
}
//控制器解释 controller
<?php
namespace appadmincontroller;
use appcommoncontrollerBackend;
/**
- 邀请人管理
* - @icon fa fa-circle-o
*/
class Invitationlimits extends Backend
{
/**
* Invitationlimits模型对象
* @var \app\admin\model\Invitationlimits
*/
protected $model = null;
protected $isSuperAdmin = false;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\Invitationlimits;
// 调用后台鉴权的模型 用来判断当前用户是否为超级管理员
$g = new \app\admin\library\Auth();
if (!$g->isSuperAdmin()) {
// 当前用户不是超级管理员
$this->isSuperAdmin = false;
// 不是超级管理员 只允许查看当前用户相关的数据
} else {
// 当前用户为超级管理员
$this->isSuperAdmin = true;
}
}
/**
* 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
* 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
* 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
*/
/**
* 查看
*/
public function index()
{
//当前是否为关联查询
$this->relationSearch = true;
//设置过滤方法
$this->request->filter(['strip_tags']);
if ($this->request->isAjax()) {
//如果发送的来源是Selectpage,则转发到Selectpage
if ($this->request->request('keyField')) {
return $this->selectpage();
}
list($where, $sort, $order, $offset, $limit) = $this->buildparams();
$whe = [];
if (!$this->isSuperAdmin) {
$whe['user_id'] = $this->auth->id;
}
$total = $this->model
// 这里的with就是在调用模型里面的关模型关联方法 数组形式
->with(['admin', 'created'])
->where($where)
->where($whe)
->order($sort, $order)
->count();
$list = $this->model
->with(['admin', 'created'])
->where($where)
->where($whe)
->order($sort, $order)
->limit($offset, $limit)
->select();
foreach ($list as $row) {
// 要显示的字段列
$row->visible(['id', 'total_limit', 'created_time', 'notes']);
// 设置admin和created关联模型的结果可见
$row->visible(['admin', 'created']);
// 获取关联结果 这里根据admin 的关联模型取出了nickname
$row->getRelation('admin')->visible(['nickname']);
// 获取关联结果 这里根据created 的关联模型取出了nickname
// created就是自定义关联模型的结果
$row->getRelation('created')->visible(['nickname']);
}
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
// 传递给视图容器一个isSuperAdmin变量 告诉视图容器 当前用户是否为超级管理员
$this->view->assign('isSuperAdmin', $this->isSuperAdmin);
return $this->view->fetch();
}
}
视图容器
<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>
<!-- 下面这里的if 判断了控制器传来的isSupperAdmin是否为超级管理员 -->
<!-- 如果是就显示添加按钮 否则就隐藏添加按钮 -->
{if $isSuperAdmin}
<a href="javascript:;"
class="btn btn-success btn-add {:$auth->check('invitationlimits/add')?'':'hide'}"
title="{:__('Add')}"><i class="fa fa-plus"></i> {:__('Add')}</a>
{/if}
<!-- 编辑 -->
<a href="javascript:;"
class="btn btn-success btn-edit btn-disabled disabled {:$auth->check('invitationlimits/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('invitationlimits/del')?'':'hide'}"
title="{:__('Delete')}"><i class="fa fa-trash"></i> {:__('Delete')}</a>
<!-- 导入 -->
<a href="javascript:;"
class="btn btn-danger btn-import {:$auth->check('invitationlimits/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('invitationlimits/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('invitationlimits/edit')}"
data-operate-del="{:$auth->check('invitationlimits/del')}" width="100%">
</table>
</div>
</div>
</div>
</div>
</div>