作者 郭盛
1 个管道 的构建 失败 耗费 8 秒

修改后台授权

<?php
namespace app\admin\controller;
use app\common\controller\Backend;
use think\Db;
/**
* 授权记录
*
* @icon fa fa-circle-o
*/
class Impower extends Backend
{
/**
* Impower模型对象
* @var \app\admin\model\Impower
*/
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\Impower;
}
/**
* 默认生成的控制器所继承的父类中有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', '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
->with(['user','farm','project'])
->where($where)
->order($sort, $order)
->count();
$list = $this->model
->with(['user','farm','project'])
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();
foreach ($list as $row) {
$row->getRelation('user')->visible(['nickname']);
$row->getRelation('farm')->visible(['name']);
$row->getRelation('project')->visible(['project_name']);
}
$list = collection($list)->toArray();
$result = array("total" => $total, "rows" => $list);
return json($result);
}
return $this->view->fetch();
}
/**
* 删除
*/
public function del($ids = "")
{
if ($ids) {
$pk = $this->model->getPk();
$adminIds = $this->getDataLimitAdminIds();
if (is_array($adminIds)) {
$this->model->where($this->dataLimitField, 'in', $adminIds);
}
$list = $this->model->where($pk, 'in', $ids)->select();
$count = 0;
//接收传递过来的ID 将对应授权人员的身份变为游客
$user = Db::name('impower')->where('id',$ids)->find();
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());
}
$user_id = $user['user_id'];
$data = Db::name('impower')->where('user_id',$user_id)->find();
if(empty($data)){
Db::name('user')->where('id',$user['user_id'])->update(['identity'=>1]);
}
if ($count) {
$this->success();
} else {
$this->error(__('No rows were deleted'));
}
}
$this->error(__('Parameter %s can not be empty', 'ids'));
}
}
... ...
<?php
return [
'Id' => 'ID',
'User_id' => '用户ID',
'Farm_id' => '农场ID',
'Project_id' => '项目ID',
'User.nickname' => '昵称',
'Farm.name' => '农场名称',
'Project.project_name' => '项目名称',
'Createtime' =>'授权时间',
];
... ...
<?php
namespace app\admin\model;
use think\Model;
class Impower extends Model
{
// 表名
protected $name = 'impower';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = false;
protected $deleteTime = false;
// 追加属性
protected $append = [
];
public function user()
{
return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function farm()
{
return $this->belongsTo('Farm', 'farm_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
public function project()
{
return $this->belongsTo('Project', 'project_id', 'id', [], 'LEFT')->setEagerlyType(0);
}
}
... ...
<?php
namespace app\admin\validate;
use think\Validate;
class Impower 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">
<label class="control-label col-xs-12 col-sm-2">{:__('User_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-user_id" data-rule="required" data-source="user/user/index" data-field="nickname" class="form-control selectpage" name="row[user_id]" type="text" value="">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Farm_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-farm_id" data-rule="required" data-source="farm/index" class="form-control selectpage" name="row[farm_id]" type="text" value="">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Project_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-project_id" data-rule="required" data-source="project/index" class="form-control selectpage" name="row[project_id]" type="text" value="">
</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">{:__('User_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-user_id" data-rule="required" data-source="user/user/index" data-field="nickname" class="form-control selectpage" name="row[user_id]" type="text" value="{$row.user_id|htmlentities}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Farm_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-farm_id" data-rule="required" data-source="farm/index" class="form-control selectpage" name="row[farm_id]" type="text" value="{$row.farm_id|htmlentities}">
</div>
</div>
<div class="form-group">
<label class="control-label col-xs-12 col-sm-2">{:__('Project_id')}:</label>
<div class="col-xs-12 col-sm-8">
<input id="c-project_id" data-rule="required" data-source="project/index" class="form-control selectpage" name="row[project_id]" type="text" value="{$row.project_id|htmlentities}">
</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('impower/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('impower/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('impower/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>-->
<!--<a href="javascript:;" class="btn btn-danger btn-import {:$auth->check('impower/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('impower/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('impower/edit')}"
data-operate-del="{:$auth->check('impower/del')}"
width="100%">
</table>
</div>
</div>
</div>
</div>
</div>
... ...
... ... @@ -41,6 +41,21 @@ class Project extends Api
public function farm()
{
$user_id = $this->getUserId();
$user = Db::name('user')->where('id',$user_id)->find();
if($user['identity'] == 5){
//查出授权人员的农场ID
$f = Db::name('impower')->where('user_id',$user_id)->find();
//通过农场ID 查出农场的信息
$farm = Db::name('farm')
->alias('a')
->join('user r','a.user_id = r.id')
->where('a.id',$f['farm_id'])
->field('a.id,a.name,r.avatar,r.score')
->find();
$this->success('success',$farm);
}
$farm = Db::name('farm')
->alias('a')
->join('user r','a.user_id = r.id')
... ... @@ -101,7 +116,7 @@ class Project extends Api
}else{
$user_id = $this->getUserId();
$shen = Db::name('user')->where('id',$user_id)->field('id,identity')->find();
if($shen['identity'] == 1 || $shen['identity'] == 2 || $shen['identity'] == 5){
if($shen['identity'] == 1 || $shen['identity'] == 2){
$page = $this->request->param('page',1,'intval');
$pageNum = $this->request->param('pageNum',10,'intval');
$farm_id = $this->request->param('farm_id');
... ... @@ -120,6 +135,26 @@ class Project extends Api
$v['createtime'] = date('Y-m-d',$v['createtime']);
}
$this->success('success',$data);
}elseif($shen['identity'] == 5){
$page = $this->request->param('page',1,'intval');
$pageNum = $this->request->param('pageNum',10,'intval');
$farm_id = $this->request->param('farm_id');
if(empty($farm_id)){
$this->error('缺少必要参数');
}
$impower = Db::name('impower')->where('user_id',$user_id)->where('farm_id',$farm_id)->column('project_id');
$data = Db::name('project')
->whereIn('id',$impower)
->field('id,project_name,image,content,look_num,good_num,status,createtime')
->order('id desc')
->page($page,$pageNum)
->select();
foreach ($data as &$v){
$v['shou'] = 0;
$v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
$v['createtime'] = date('Y-m-d',$v['createtime']);
}
$this->success('success',$data);
}else{
$page = $this->request->param('page',1,'intval');
$pageNum = $this->request->param('pageNum',10,'intval');
... ... @@ -1260,4 +1295,34 @@ class Project extends Api
$this->success('success',$data);
}
/**
* @ApiTitle (扫描授权码绑定下级)
* @ApiSummary (扫描授权码绑定下级)
* @ApiMethod (POST)
* @ApiRoute (/api/project/bang)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
*
* @ApiParams (name="project_id", type="int", required=false, description="项目id")
* @ApiParams (name="farm_id", type="int", required=false, description="农场id")
*
* @ApiReturn({
"code": 1,
"msg": "SUCCESS",
"time": "1553839125",
"data": {
"id"://项目id
"pic"://项目授权码
}
})
*/
public function bang()
{
$param['user_id'] = $this->getUserId();
$param['project_id'] = $this->request->param('project_id');
$param['farm_id'] = $this->request->param('farm_id');
$param['createtime'] = time();
Db::name('user')->where('id',$param['user_id'])->update(['identity'=>5]);
$data = Db::name('impower')->insertGetId($param);
}
}
\ No newline at end of file
... ...
define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
var Controller = {
index: function () {
// 初始化表格参数配置
Table.api.init({
extend: {
index_url: 'impower/index' + location.search,
add_url: 'impower/add',
edit_url: 'impower/edit',
del_url: 'impower/del',
multi_url: 'impower/multi',
table: 'impower',
}
});
var table = $("#table");
// 初始化表格
table.bootstrapTable({
url: $.fn.bootstrapTable.defaults.extend.index_url,
pk: 'id',
sortName: 'id',
columns: [
[
{checkbox: true},
{field: 'id', title: __('Id'), operate:false},
{field: 'user.nickname', title: __('User.nickname'), operate:false},
{field: 'farm.name', title: __('Farm.name'), operate:false},
{field: 'project.project_name', title: __('Project.project_name'), operate:false},
{field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
var that = $.extend({}, this);
var table = $(that.table).clone(true);
$(table).data("operate-edit", null);
that.table = table;
return Table.api.formatter.operate.call(that, value, row, index);
}}
]
]
});
// 为表格绑定事件
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
... ...