ClassDict.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace app\admin\controller\train;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Exception;
  6. use think\exception\PDOException;
  7. /**
  8. * 培训-题目分类
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class ClassDict extends Backend
  13. {
  14. /**
  15. * 无需鉴权的方法,但需要登录
  16. * @var array
  17. */
  18. protected $noNeedRight = ['classselectlist'];
  19. /**
  20. * ClassDictModel模型对象
  21. * @var \app\admin\model\train\ClassDictModel
  22. */
  23. protected $model = null;
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. $this->model = new \app\admin\model\train\ClassDictModel;
  28. }
  29. /**
  30. * 查看
  31. */
  32. public function index()
  33. {
  34. //设置过滤方法
  35. $this->request->filter(['strip_tags']);
  36. if ($this->request->isAjax()) {
  37. //如果发送的来源是Selectpage,则转发到Selectpage
  38. if ($this->request->request('keyField')) {
  39. return $this->selectpage();
  40. }
  41. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  42. $del_where = ['is_del' => 0];
  43. $total = $this->model
  44. ->where($where)
  45. ->where($del_where)
  46. ->order($sort, $order)
  47. ->count();
  48. $list = $this->model
  49. ->where($where)
  50. ->where($del_where)
  51. ->order($sort, $order)
  52. ->limit($offset, $limit)
  53. ->select();
  54. $list = collection($list)->toArray();
  55. $result = array("total" => $total, "rows" => $list);
  56. return json($result);
  57. }
  58. return $this->view->fetch();
  59. }
  60. public function classSelectList()
  61. {
  62. $data = $this->model
  63. ->field('id, title as name')
  64. ->select();
  65. $data = ['searchlist' => $data];
  66. $this->success('success', null, $data);
  67. }
  68. public function del($ids = "")
  69. {
  70. if ($ids) {
  71. $pk = $this->model->getPk();
  72. $adminIds = $this->getDataLimitAdminIds();
  73. if (is_array($adminIds)) {
  74. $this->model->where($this->dataLimitField, 'in', $adminIds);
  75. }
  76. Db::startTrans();
  77. try {
  78. $count = $this->model->where($pk, 'in', $ids)
  79. ->update([
  80. 'is_del' => 1
  81. ]);
  82. } catch (PDOException $e) {
  83. Db::rollback();
  84. $this->error($e->getMessage());
  85. } catch (Exception $e) {
  86. Db::rollback();
  87. $this->error($e->getMessage());
  88. }
  89. if ($count) {
  90. $this->success();
  91. } else {
  92. $this->error(__('No rows were deleted'));
  93. }
  94. }
  95. $this->error(__('Parameter %s can not be empty', 'ids'));
  96. }
  97. }