SampleApply.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace app\admin\controller\books;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\exception\DbException;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use think\response\Json;
  9. /**
  10. * 样书申请管理
  11. *
  12. * @icon fa fa-circle-o
  13. */
  14. class SampleApply extends Backend
  15. {
  16. /**
  17. * SampleApply模型对象
  18. * @var \app\admin\model\books\SampleApply
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\books\SampleApply;
  25. $this->view->assign("useToList", $this->model->getUseToList());
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. *
  36. * @return string|Json
  37. * @throws \think\Exception
  38. * @throws DbException
  39. */
  40. public function index()
  41. {
  42. //设置过滤方法
  43. $this->request->filter(['strip_tags', 'trim']);
  44. if (false === $this->request->isAjax()) {
  45. return $this->view->fetch();
  46. }
  47. //如果发送的来源是 Selectpage,则转发到 Selectpage
  48. if ($this->request->request('keyField')) {
  49. return $this->selectpage();
  50. }
  51. [$where, $sort, $order, $offset, $limit] = $this->buildparams();
  52. $list = $this->model
  53. ->where($where)
  54. ->with(['user','book'])
  55. ->order($sort, $order)
  56. ->paginate($limit);
  57. $result = ['total' => $list->total(), 'rows' => $list->items()];
  58. return json($result);
  59. }
  60. /**
  61. * 修改状态
  62. *
  63. * @param $ids
  64. * @return string
  65. * @throws DbException
  66. * @throws \think\Exception
  67. */
  68. public function update_status($ids = null){
  69. $row = $this->model->get($ids);
  70. if (!$row) {
  71. $this->error(__('No Results were found'));
  72. }
  73. $adminIds = $this->getDataLimitAdminIds();
  74. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  75. $this->error(__('You have no permission'));
  76. }
  77. if (false === $this->request->isPost()) {
  78. $this->view->assign('row', $row);
  79. return $this->view->fetch();
  80. }
  81. $params = $this->request->post();
  82. // $params = $this->request->post('row/a');
  83. // if (empty($params)) {
  84. // $this->error(__('Parameter %s can not be empty', ''));
  85. // }
  86. // $params = $this->preExcludeFields($params);
  87. $result = false;
  88. Db::startTrans();
  89. try {
  90. //是否采用模型验证
  91. $result = $row->allowField(true)->save($params);
  92. Db::commit();
  93. } catch (ValidateException|PDOException|Exception $e) {
  94. Db::rollback();
  95. $this->error($e->getMessage());
  96. }
  97. if (false === $result) {
  98. $this->error(__('No rows were updated'));
  99. }
  100. $this->success();
  101. }
  102. }