作者 开飞机的舒克

后台功能修改

  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 Campus extends Backend
  14 +{
  15 +
  16 + /**
  17 + * Campus模型对象
  18 + * @var \app\admin\model\Campus
  19 + */
  20 + protected $model = null;
  21 +
  22 + public function _initialize()
  23 + {
  24 + parent::_initialize();
  25 + $this->model = new \app\admin\model\Campus;
  26 +
  27 + }
  28 +
  29 +
  30 +
  31 + /**
  32 + * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  33 + * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  34 + * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  35 + */
  36 +
  37 +
  38 + /**
  39 + * 查看
  40 + */
  41 + public function index()
  42 + {
  43 + //当前是否为关联查询
  44 + $this->relationSearch = true;
  45 + //设置过滤方法
  46 + $this->request->filter(['strip_tags', 'trim']);
  47 + if ($this->request->isAjax()) {
  48 + //如果发送的来源是Selectpage,则转发到Selectpage
  49 + if ($this->request->request('keyField')) {
  50 + return $this->selectpage();
  51 + }
  52 + list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  53 +
  54 + $list = $this->model
  55 + ->with(['school','activity'])
  56 + ->where($where)
  57 + ->order($sort, $order)
  58 + ->paginate($limit);
  59 +
  60 + foreach ($list as $row) {
  61 + $row->visible(['id','title','school_id','activity_id','date']);
  62 + $row->visible(['school']);
  63 + $row->getRelation('school')->visible(['title']);
  64 + $row->visible(['activity']);
  65 + $row->getRelation('activity')->visible(['title']);
  66 + }
  67 +
  68 + $result = array("total" => $list->total(), "rows" => $list->items());
  69 +
  70 + return json($result);
  71 + }
  72 + return $this->view->fetch();
  73 + }
  74 +
  75 + /**
  76 + * 添加
  77 + *
  78 + * @return string
  79 + * @throws \think\Exception
  80 + */
  81 + public function add()
  82 + {
  83 + if (false === $this->request->isPost()) {
  84 + return $this->view->fetch();
  85 + }
  86 + $params = $this->request->post('row/a');
  87 + if (empty($params)) {
  88 + $this->error(__('Parameter %s can not be empty', ''));
  89 + }
  90 + $params = $this->preExcludeFields($params);
  91 +
  92 + if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  93 + $params[$this->dataLimitField] = $this->auth->id;
  94 + }
  95 + $result = false;
  96 + Db::startTrans();
  97 + try {
  98 + //是否采用模型验证
  99 + if ($this->modelValidate) {
  100 + $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  101 + $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  102 + $this->model->validateFailException()->validate($validate);
  103 + }
  104 + $result = $this->model->allowField(true)->save($params);
  105 + $id = $this->model->id;
  106 + $school_id = $this->model->school_id;
  107 + $activity_id = $this->model->activity_id;
  108 + $school_title = \db('school')->where('id',$school_id)->value('title');
  109 + $activity_title = \db('activity')->where('id',$activity_id)->value('title');
  110 + $date = $this->model->date;
  111 + $title = $date.$school_title.$activity_title;
  112 + $this->model->save(['title'=>$title],['id'=>$id]);
  113 + Db::commit();
  114 + } catch (ValidateException|PDOException|Exception $e) {
  115 + Db::rollback();
  116 + $this->error($e->getMessage());
  117 + }
  118 + if ($result === false) {
  119 + $this->error(__('No rows were inserted'));
  120 + }
  121 + $this->success();
  122 + }
  123 +
  124 +}