Trylesson.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 Trylesson extends Backend
  15. {
  16. /**
  17. * Trylesson模型对象
  18. * @var \app\admin\model\Trylesson
  19. */
  20. protected $model = null;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\Trylesson;
  25. $this->view->assign("isShowList", $this->model->getIsShowList());
  26. }
  27. /**
  28. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  29. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  30. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  31. */
  32. /**
  33. * 查看
  34. */
  35. public function index()
  36. {
  37. //当前是否为关联查询
  38. $this->relationSearch = true;
  39. //设置过滤方法
  40. $this->request->filter(['strip_tags', 'trim']);
  41. if ($this->request->isAjax()) {
  42. //如果发送的来源是Selectpage,则转发到Selectpage
  43. if ($this->request->request('keyField')) {
  44. return $this->selectpage();
  45. }
  46. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  47. $list = $this->model
  48. ->with(['lesson','cate'])
  49. ->where($where)
  50. ->order($sort, $order)
  51. ->paginate($limit);
  52. foreach ($list as $row) {
  53. $row->getRelation('lesson')->visible(['name','name_en']);
  54. $row->getRelation('cate')->visible(['name','name_en']);
  55. }
  56. $result = array("total" => $list->total(), "rows" => $list->items());
  57. return json($result);
  58. }
  59. return $this->view->fetch();
  60. }
  61. /**
  62. * 编辑
  63. *
  64. * @param $ids
  65. * @return string
  66. * @throws DbException
  67. * @throws \think\Exception
  68. */
  69. public function edit($ids = null)
  70. {
  71. $row = $this->model->get($ids);
  72. if (!$row) {
  73. $this->error(__('No Results were found'));
  74. }
  75. $adminIds = $this->getDataLimitAdminIds();
  76. if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
  77. $this->error(__('You have no permission'));
  78. }
  79. if (false === $this->request->isPost()) {
  80. $this->view->assign('row', $row);
  81. return $this->view->fetch();
  82. }
  83. $params = $this->request->post('row/a');
  84. if (empty($params)) {
  85. $this->error(__('Parameter %s can not be empty', ''));
  86. }
  87. $params = $this->preExcludeFields($params);
  88. $result = false;
  89. Db::startTrans();
  90. try {
  91. //是否采用模型验证
  92. if ($this->modelValidate) {
  93. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  94. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  95. $row->validateFailException()->validate($validate);
  96. }
  97. //修改所有试课订单
  98. $rs_up = Db::name('trylesson_order')->where('trylesson_id',$ids)->update(['lesson_ids'=>$params['lesson_ids']]);
  99. if($rs_up === false){
  100. Db::rollback();
  101. abort(500,'修改失败');
  102. }
  103. //
  104. $result = $row->allowField(true)->save($params);
  105. Db::commit();
  106. } catch (ValidateException|PDOException|Exception $e) {
  107. Db::rollback();
  108. $this->error($e->getMessage());
  109. }
  110. if (false === $result) {
  111. $this->error(__('No rows were updated'));
  112. }
  113. $this->success();
  114. }
  115. }