Coach.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 Coach extends Backend
  11. {
  12. protected $noNeedLogin = ['vue_index','color_list','coach_set'];
  13. /**
  14. * Coach模型对象
  15. * @var \app\admin\model\Coach
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. $this->model = new \app\admin\model\Coach;
  22. $this->view->assign("genderList", $this->model->getGenderList());
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. //可选色值
  25. $bgcolorlist = $this->model->getBgcolorList();
  26. $bgcolorlist_column = array_column($bgcolorlist,'val','id');
  27. $this->view->assign("bgcolorList", $bgcolorlist_column);
  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(['tag'])
  51. ->where($where)
  52. ->order($sort, $order)
  53. ->paginate($limit);
  54. foreach ($list as $row) {
  55. $row->getRelation('tag')->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. public function vue_index()
  66. {
  67. $list = Db::name('coach')->field('id,nickname,bgcolor')
  68. ->order('id desc')
  69. ->select();
  70. $bgcolorlist = $this->model->getBgcolorList();
  71. $bgcolorlist_column = array_column($bgcolorlist,'id','val');
  72. foreach($list as $key => &$val){
  73. $val['bgcolor_id'] = isset($bgcolorlist_column[$val['bgcolor']]) ? $bgcolorlist_column[$val['bgcolor']] : 0;
  74. }
  75. $this->result($list,1,'success','json');
  76. }
  77. //颜色列表
  78. public function color_list(){
  79. $color_list = $this->model->getBgcolorList();
  80. $this->result($color_list,1,'success','json');
  81. }
  82. //修改教练颜色
  83. public function coach_set(){
  84. $bgcolor = input('bgcolor','');
  85. $coach_id = input('id',0);
  86. Db::name('coach')->where('id',$coach_id)->update(['bgcolor'=>$bgcolor]);
  87. $this->result('',1,'设置成功','json');
  88. }
  89. }