Lesson.php 5.5 KB

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