Appindexdes.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 Appindexdes extends Backend
  13. {
  14. /**
  15. * Appindexdes模型对象
  16. * @var \app\admin\model\Appindexdes
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\Appindexdes;
  23. }
  24. public function index($ids=null)
  25. {
  26. $ids=input('ids');
  27. if(empty($ids)){
  28. $ids=session('despid');
  29. }else{
  30. session('despid',$ids);
  31. }
  32. //设置过滤方法
  33. $this->request->filter(['strip_tags', 'trim']);
  34. if (false === $this->request->isAjax()) {
  35. return $this->view->fetch();
  36. }
  37. //如果发送的来源是 Selectpage,则转发到 Selectpage
  38. if ($this->request->request('keyField')) {
  39. return $this->selectpage();
  40. }
  41. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  42. $dts['pid'] =$ids;
  43. $list = $this->model
  44. ->where($where)
  45. ->where($dts)
  46. ->order($sort, $order)
  47. ->paginate($limit);
  48. $result = ['total' => $list->total(), 'rows' => $list->items()];
  49. return json($result);
  50. }
  51. /**
  52. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  53. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  54. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  55. */
  56. /**
  57. * 添加
  58. *
  59. * @return string
  60. * @throws \think\Exception
  61. */
  62. public function add()
  63. {
  64. if (false === $this->request->isPost()) {
  65. return $this->view->fetch();
  66. }
  67. $params = $this->request->post('row/a');
  68. if (empty($params)) {
  69. $this->error(__('Parameter %s can not be empty', ''));
  70. }
  71. $ra_id=session('despid');
  72. $params['pid']=$ra_id;
  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()->validate($validate);
  85. }
  86. $result = $this->model->allowField(true)->save($params);
  87. Db::commit();
  88. } catch (ValidateException|PDOException|Exception $e) {
  89. Db::rollback();
  90. $this->error($e->getMessage());
  91. }
  92. if ($result === false) {
  93. $this->error(__('No rows were inserted'));
  94. }
  95. $this->success();
  96. }
  97. }