Lesson.php 4.0 KB

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