Protableinfo.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\PDOException;
  6. use think\exception\ValidateException;
  7. /**
  8. * 报修详情
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Protableinfo extends Backend
  13. {
  14. /**
  15. * Protableinfo模型对象
  16. * @var \app\admin\model\Protableinfo
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Protableinfo;
  23. }
  24. public function import()
  25. {
  26. parent::import();
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //当前是否为关联查询
  39. $this->relationSearch = true;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. $ids = $this->request->param('ids');
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $list = $this->model
  50. ->with(['protable'])
  51. ->where('p_id',1)
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->paginate($limit);
  55. foreach ($list as $row) {
  56. $row->getRelation('protable')->visible(['name']);
  57. }
  58. $result = array("total" => $list->total(), "rows" => $list->items());
  59. return json($result);
  60. }
  61. $ids = $this->request->param('ids');
  62. $this->assignconfig('ids',$ids);
  63. return $this->view->fetch();
  64. }
  65. /**
  66. * 添加
  67. */
  68. public function add()
  69. {
  70. if ($this->request->isPost()) {
  71. $params = $this->request->post("row/a");
  72. if ($params) {
  73. $params = $this->preExcludeFields($params);
  74. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  75. $params[$this->dataLimitField] = $this->auth->id;
  76. }
  77. $result = false;
  78. Db::startTrans();
  79. try {
  80. //是否采用模型验证
  81. if ($this->modelValidate) {
  82. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  83. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  84. $this->model->validateFailException(true)->validate($validate);
  85. }
  86. $result = $this->model->allowField(true)->save($params);
  87. Db::commit();
  88. } catch (ValidateException $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. } catch (PDOException $e) {
  92. Db::rollback();
  93. $this->error($e->getMessage());
  94. } catch (Exception $e) {
  95. Db::rollback();
  96. $this->error($e->getMessage());
  97. }
  98. if ($result !== false) {
  99. $this->success();
  100. } else {
  101. $this->error(__('No rows were inserted'));
  102. }
  103. }
  104. $this->error(__('Parameter %s can not be empty', ''));
  105. }
  106. $ids = $this->request->param('ids');
  107. $this->assign('ids',$ids);
  108. return $this->view->fetch();
  109. }
  110. /**
  111. * 编辑
  112. */
  113. public function edit($ids = null)
  114. {
  115. $row = $this->model->get($ids);
  116. if (!$row) {
  117. $this->error(__('No Results were found'));
  118. }
  119. $adminIds = $this->getDataLimitAdminIds();
  120. if (is_array($adminIds)) {
  121. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  122. $this->error(__('You have no permission'));
  123. }
  124. }
  125. if ($this->request->isPost()) {
  126. $params = $this->request->post("row/a");
  127. if ($params) {
  128. $params = $this->preExcludeFields($params);
  129. $result = false;
  130. Db::startTrans();
  131. try {
  132. //是否采用模型验证
  133. if ($this->modelValidate) {
  134. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  135. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  136. $row->validateFailException(true)->validate($validate);
  137. }
  138. $result = $row->allowField(true)->save($params);
  139. Db::commit();
  140. } catch (ValidateException $e) {
  141. Db::rollback();
  142. $this->error($e->getMessage());
  143. } catch (PDOException $e) {
  144. Db::rollback();
  145. $this->error($e->getMessage());
  146. } catch (Exception $e) {
  147. Db::rollback();
  148. $this->error($e->getMessage());
  149. }
  150. if ($result !== false) {
  151. $this->success();
  152. } else {
  153. $this->error(__('No rows were updated'));
  154. }
  155. }
  156. $this->error(__('Parameter %s can not be empty', ''));
  157. }
  158. $this->view->assign("row", $row);
  159. return $this->view->fetch();
  160. }
  161. }