Coach.php 6.4 KB

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