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

修改后台授权

  1 +<?php
  2 +
  3 +namespace app\admin\controller;
  4 +
  5 +use app\common\controller\Backend;
  6 +use think\Db;
  7 +
  8 +/**
  9 + * 授权记录
  10 + *
  11 + * @icon fa fa-circle-o
  12 + */
  13 +class Impower extends Backend
  14 +{
  15 +
  16 + /**
  17 + * Impower模型对象
  18 + * @var \app\admin\model\Impower
  19 + */
  20 + protected $model = null;
  21 +
  22 + public function _initialize()
  23 + {
  24 + parent::_initialize();
  25 + $this->model = new \app\admin\model\Impower;
  26 +
  27 + }
  28 +
  29 + /**
  30 + * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  31 + * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  32 + * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  33 + */
  34 +
  35 +
  36 + /**
  37 + * 查看
  38 + */
  39 + public function index()
  40 + {
  41 + //当前是否为关联查询
  42 + $this->relationSearch = true;
  43 + //设置过滤方法
  44 + $this->request->filter(['strip_tags', 'trim']);
  45 + if ($this->request->isAjax())
  46 + {
  47 + //如果发送的来源是Selectpage,则转发到Selectpage
  48 + if ($this->request->request('keyField'))
  49 + {
  50 + return $this->selectpage();
  51 + }
  52 + list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  53 + $total = $this->model
  54 + ->with(['user','farm','project'])
  55 + ->where($where)
  56 + ->order($sort, $order)
  57 + ->count();
  58 +
  59 + $list = $this->model
  60 + ->with(['user','farm','project'])
  61 + ->where($where)
  62 + ->order($sort, $order)
  63 + ->limit($offset, $limit)
  64 + ->select();
  65 +
  66 + foreach ($list as $row) {
  67 +
  68 + $row->getRelation('user')->visible(['nickname']);
  69 + $row->getRelation('farm')->visible(['name']);
  70 + $row->getRelation('project')->visible(['project_name']);
  71 + }
  72 + $list = collection($list)->toArray();
  73 + $result = array("total" => $total, "rows" => $list);
  74 +
  75 + return json($result);
  76 + }
  77 + return $this->view->fetch();
  78 + }
  79 +
  80 + /**
  81 + * 删除
  82 + */
  83 + public function del($ids = "")
  84 + {
  85 + if ($ids) {
  86 + $pk = $this->model->getPk();
  87 + $adminIds = $this->getDataLimitAdminIds();
  88 + if (is_array($adminIds)) {
  89 + $this->model->where($this->dataLimitField, 'in', $adminIds);
  90 + }
  91 + $list = $this->model->where($pk, 'in', $ids)->select();
  92 +
  93 + $count = 0;
  94 +
  95 + //接收传递过来的ID 将对应授权人员的身份变为游客
  96 + $user = Db::name('impower')->where('id',$ids)->find();
  97 +
  98 + Db::startTrans();
  99 + try {
  100 + foreach ($list as $k => $v) {
  101 + $count += $v->delete();
  102 + }
  103 + Db::commit();
  104 + } catch (PDOException $e) {
  105 + Db::rollback();
  106 + $this->error($e->getMessage());
  107 + } catch (Exception $e) {
  108 + Db::rollback();
  109 + $this->error($e->getMessage());
  110 + }
  111 +
  112 + $user_id = $user['user_id'];
  113 + $data = Db::name('impower')->where('user_id',$user_id)->find();
  114 + if(empty($data)){
  115 + Db::name('user')->where('id',$user['user_id'])->update(['identity'=>1]);
  116 + }
  117 +
  118 + if ($count) {
  119 + $this->success();
  120 + } else {
  121 + $this->error(__('No rows were deleted'));
  122 + }
  123 + }
  124 + $this->error(__('Parameter %s can not be empty', 'ids'));
  125 + }
  126 +}
  1 +<?php
  2 +
  3 +return [
  4 + 'Id' => 'ID',
  5 + 'User_id' => '用户ID',
  6 + 'Farm_id' => '农场ID',
  7 + 'Project_id' => '项目ID',
  8 + 'User.nickname' => '昵称',
  9 + 'Farm.name' => '农场名称',
  10 + 'Project.project_name' => '项目名称',
  11 + 'Createtime' =>'授权时间',
  12 +];
  1 +<?php
  2 +
  3 +namespace app\admin\model;
  4 +
  5 +use think\Model;
  6 +
  7 +
  8 +class Impower extends Model
  9 +{
  10 +
  11 +
  12 +
  13 +
  14 +
  15 + // 表名
  16 + protected $name = 'impower';
  17 +
  18 + // 自动写入时间戳字段
  19 + protected $autoWriteTimestamp = 'int';
  20 +
  21 + // 定义时间戳字段名
  22 + protected $createTime = 'createtime';
  23 + protected $updateTime = false;
  24 + protected $deleteTime = false;
  25 +
  26 + // 追加属性
  27 + protected $append = [
  28 +
  29 + ];
  30 +
  31 +
  32 +
  33 +
  34 +
  35 +
  36 +
  37 +
  38 +
  39 +
  40 + public function user()
  41 + {
  42 + return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
  43 + }
  44 +
  45 +
  46 + public function farm()
  47 + {
  48 + return $this->belongsTo('Farm', 'farm_id', 'id', [], 'LEFT')->setEagerlyType(0);
  49 + }
  50 +
  51 +
  52 + public function project()
  53 + {
  54 + return $this->belongsTo('Project', 'project_id', 'id', [], 'LEFT')->setEagerlyType(0);
  55 + }
  56 +}
  1 +<?php
  2 +
  3 +namespace app\admin\validate;
  4 +
  5 +use think\Validate;
  6 +
  7 +class Impower 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">
  4 + <label class="control-label col-xs-12 col-sm-2">{:__('User_id')}:</label>
  5 + <div class="col-xs-12 col-sm-8">
  6 + <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="">
  7 + </div>
  8 + </div>
  9 + <div class="form-group">
  10 + <label class="control-label col-xs-12 col-sm-2">{:__('Farm_id')}:</label>
  11 + <div class="col-xs-12 col-sm-8">
  12 + <input id="c-farm_id" data-rule="required" data-source="farm/index" class="form-control selectpage" name="row[farm_id]" type="text" value="">
  13 + </div>
  14 + </div>
  15 + <div class="form-group">
  16 + <label class="control-label col-xs-12 col-sm-2">{:__('Project_id')}:</label>
  17 + <div class="col-xs-12 col-sm-8">
  18 + <input id="c-project_id" data-rule="required" data-source="project/index" class="form-control selectpage" name="row[project_id]" type="text" value="">
  19 + </div>
  20 + </div>
  21 + <div class="form-group layer-footer">
  22 + <label class="control-label col-xs-12 col-sm-2"></label>
  23 + <div class="col-xs-12 col-sm-8">
  24 + <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
  25 + <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
  26 + </div>
  27 + </div>
  28 +</form>
  1 +<form id="edit-form" class="form-horizontal" role="form" data-toggle="validator" method="POST" action="">
  2 +
  3 + <div class="form-group">
  4 + <label class="control-label col-xs-12 col-sm-2">{:__('User_id')}:</label>
  5 + <div class="col-xs-12 col-sm-8">
  6 + <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}">
  7 + </div>
  8 + </div>
  9 + <div class="form-group">
  10 + <label class="control-label col-xs-12 col-sm-2">{:__('Farm_id')}:</label>
  11 + <div class="col-xs-12 col-sm-8">
  12 + <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}">
  13 + </div>
  14 + </div>
  15 + <div class="form-group">
  16 + <label class="control-label col-xs-12 col-sm-2">{:__('Project_id')}:</label>
  17 + <div class="col-xs-12 col-sm-8">
  18 + <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}">
  19 + </div>
  20 + </div>
  21 + <div class="form-group layer-footer">
  22 + <label class="control-label col-xs-12 col-sm-2"></label>
  23 + <div class="col-xs-12 col-sm-8">
  24 + <button type="submit" class="btn btn-success btn-embossed disabled">{:__('OK')}</button>
  25 + <button type="reset" class="btn btn-default btn-embossed">{:__('Reset')}</button>
  26 + </div>
  27 + </div>
  28 +</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('impower/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('impower/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('impower/del')?'':'hide'}" title="{:__('Delete')}" ><i class="fa fa-trash"></i> {:__('Delete')}</a>-->
  13 + <!--<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>-->
  14 +
  15 + <div class="dropdown btn-group {:$auth->check('impower/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('impower/edit')}"
  27 + data-operate-del="{:$auth->check('impower/del')}"
  28 + width="100%">
  29 + </table>
  30 + </div>
  31 + </div>
  32 +
  33 + </div>
  34 + </div>
  35 +</div>
@@ -41,6 +41,21 @@ class Project extends Api @@ -41,6 +41,21 @@ class Project extends Api
41 public function farm() 41 public function farm()
42 { 42 {
43 $user_id = $this->getUserId(); 43 $user_id = $this->getUserId();
  44 + $user = Db::name('user')->where('id',$user_id)->find();
  45 + if($user['identity'] == 5){
  46 + //查出授权人员的农场ID
  47 + $f = Db::name('impower')->where('user_id',$user_id)->find();
  48 +
  49 + //通过农场ID 查出农场的信息
  50 + $farm = Db::name('farm')
  51 + ->alias('a')
  52 + ->join('user r','a.user_id = r.id')
  53 + ->where('a.id',$f['farm_id'])
  54 + ->field('a.id,a.name,r.avatar,r.score')
  55 + ->find();
  56 + $this->success('success',$farm);
  57 + }
  58 +
44 $farm = Db::name('farm') 59 $farm = Db::name('farm')
45 ->alias('a') 60 ->alias('a')
46 ->join('user r','a.user_id = r.id') 61 ->join('user r','a.user_id = r.id')
@@ -101,7 +116,7 @@ class Project extends Api @@ -101,7 +116,7 @@ class Project extends Api
101 }else{ 116 }else{
102 $user_id = $this->getUserId(); 117 $user_id = $this->getUserId();
103 $shen = Db::name('user')->where('id',$user_id)->field('id,identity')->find(); 118 $shen = Db::name('user')->where('id',$user_id)->field('id,identity')->find();
104 - if($shen['identity'] == 1 || $shen['identity'] == 2 || $shen['identity'] == 5){ 119 + if($shen['identity'] == 1 || $shen['identity'] == 2){
105 $page = $this->request->param('page',1,'intval'); 120 $page = $this->request->param('page',1,'intval');
106 $pageNum = $this->request->param('pageNum',10,'intval'); 121 $pageNum = $this->request->param('pageNum',10,'intval');
107 $farm_id = $this->request->param('farm_id'); 122 $farm_id = $this->request->param('farm_id');
@@ -120,6 +135,26 @@ class Project extends Api @@ -120,6 +135,26 @@ class Project extends Api
120 $v['createtime'] = date('Y-m-d',$v['createtime']); 135 $v['createtime'] = date('Y-m-d',$v['createtime']);
121 } 136 }
122 $this->success('success',$data); 137 $this->success('success',$data);
  138 + }elseif($shen['identity'] == 5){
  139 + $page = $this->request->param('page',1,'intval');
  140 + $pageNum = $this->request->param('pageNum',10,'intval');
  141 + $farm_id = $this->request->param('farm_id');
  142 + if(empty($farm_id)){
  143 + $this->error('缺少必要参数');
  144 + }
  145 + $impower = Db::name('impower')->where('user_id',$user_id)->where('farm_id',$farm_id)->column('project_id');
  146 + $data = Db::name('project')
  147 + ->whereIn('id',$impower)
  148 + ->field('id,project_name,image,content,look_num,good_num,status,createtime')
  149 + ->order('id desc')
  150 + ->page($page,$pageNum)
  151 + ->select();
  152 + foreach ($data as &$v){
  153 + $v['shou'] = 0;
  154 + $v['image'] = 'http://q2ugvq3qf.bkt.clouddn.com'.$v['image'];
  155 + $v['createtime'] = date('Y-m-d',$v['createtime']);
  156 + }
  157 + $this->success('success',$data);
123 }else{ 158 }else{
124 $page = $this->request->param('page',1,'intval'); 159 $page = $this->request->param('page',1,'intval');
125 $pageNum = $this->request->param('pageNum',10,'intval'); 160 $pageNum = $this->request->param('pageNum',10,'intval');
@@ -1260,4 +1295,34 @@ class Project extends Api @@ -1260,4 +1295,34 @@ class Project extends Api
1260 $this->success('success',$data); 1295 $this->success('success',$data);
1261 } 1296 }
1262 1297
  1298 + /**
  1299 + * @ApiTitle (扫描授权码绑定下级)
  1300 + * @ApiSummary (扫描授权码绑定下级)
  1301 + * @ApiMethod (POST)
  1302 + * @ApiRoute (/api/project/bang)
  1303 + * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
  1304 + *
  1305 + * @ApiParams (name="project_id", type="int", required=false, description="项目id")
  1306 + * @ApiParams (name="farm_id", type="int", required=false, description="农场id")
  1307 + *
  1308 + * @ApiReturn({
  1309 + "code": 1,
  1310 + "msg": "SUCCESS",
  1311 + "time": "1553839125",
  1312 + "data": {
  1313 + "id"://项目id
  1314 + "pic"://项目授权码
  1315 + }
  1316 + })
  1317 + */
  1318 + public function bang()
  1319 + {
  1320 + $param['user_id'] = $this->getUserId();
  1321 + $param['project_id'] = $this->request->param('project_id');
  1322 + $param['farm_id'] = $this->request->param('farm_id');
  1323 + $param['createtime'] = time();
  1324 + Db::name('user')->where('id',$param['user_id'])->update(['identity'=>5]);
  1325 + $data = Db::name('impower')->insertGetId($param);
  1326 + }
  1327 +
1263 } 1328 }
  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: 'impower/index' + location.search,
  9 + add_url: 'impower/add',
  10 + edit_url: 'impower/edit',
  11 + del_url: 'impower/del',
  12 + multi_url: 'impower/multi',
  13 + table: 'impower',
  14 + }
  15 + });
  16 +
  17 + var table = $("#table");
  18 +
  19 + // 初始化表格
  20 + table.bootstrapTable({
  21 + url: $.fn.bootstrapTable.defaults.extend.index_url,
  22 + pk: 'id',
  23 + sortName: 'id',
  24 + columns: [
  25 + [
  26 + {checkbox: true},
  27 + {field: 'id', title: __('Id'), operate:false},
  28 + {field: 'user.nickname', title: __('User.nickname'), operate:false},
  29 + {field: 'farm.name', title: __('Farm.name'), operate:false},
  30 + {field: 'project.project_name', title: __('Project.project_name'), operate:false},
  31 + {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
  32 + {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (value, row, index) {
  33 + var that = $.extend({}, this);
  34 + var table = $(that.table).clone(true);
  35 + $(table).data("operate-edit", null);
  36 + that.table = table;
  37 + return Table.api.formatter.operate.call(that, value, row, index);
  38 + }}
  39 + ]
  40 + ]
  41 + });
  42 +
  43 + // 为表格绑定事件
  44 + Table.api.bindevent(table);
  45 + },
  46 + add: function () {
  47 + Controller.api.bindevent();
  48 + },
  49 + edit: function () {
  50 + Controller.api.bindevent();
  51 + },
  52 + api: {
  53 + bindevent: function () {
  54 + Form.api.bindevent($("form[role=form]"));
  55 + }
  56 + }
  57 + };
  58 + return Controller;
  59 +});