Video.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 Video extends Backend
  11. {
  12. /**
  13. * Video模型对象
  14. * @var \app\admin\model\Video
  15. */
  16. protected $model = null;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new \app\admin\model\Video;
  21. $tags=\app\admin\model\Tag::column('id,name');
  22. $this->assign('tags',$tags);
  23. }
  24. public function import()
  25. {
  26. parent::import();
  27. }
  28. /**
  29. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  30. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  31. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  32. */
  33. /**
  34. * 查看
  35. */
  36. public function index()
  37. {
  38. //当前是否为关联查询
  39. $this->relationSearch = false;
  40. //设置过滤方法
  41. $this->request->filter(['strip_tags', 'trim']);
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('keyField')) {
  45. return $this->selectpage();
  46. }
  47. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  48. $list = $this->model
  49. ->join('video_tag','video_tag.video_id=video.id','left')
  50. ->join('tag','tag.id=video_tag.tag_id','left')
  51. ->group('video.id')
  52. ->where($where)
  53. ->order($sort, $order)
  54. ->field(['video.*',Db::raw('group_concat(tag.name SEPARATOR \',\') as tag')])
  55. ->paginate($limit);
  56. foreach ($list as $row) {
  57. $row->visible(['id','bg','src','play_num','comment_num','like_num','fav_num','created_at','weight','title','tag']);
  58. }
  59. $result = array("total" => $list->total(), "rows" => $list->items());
  60. return json($result);
  61. }
  62. return $this->view->fetch();
  63. }
  64. public function add()
  65. {
  66. $video=new \app\admin\model\Video();
  67. if($this->request->isGet()){
  68. return parent::add();
  69. }
  70. Db::startTrans();
  71. $data=input('row/a');
  72. $this->validate($data,[
  73. 'title|标题'=>['require'],
  74. 'bg'=>['require'],
  75. 'src'=>['require'],
  76. 'guest_id'=>['require'],
  77. ]);
  78. $video->guest_id=$data['guest_id'];
  79. $video->bg=$data['bg'];
  80. $video->src=$data['src'];
  81. $video->is_open=$data['is_open'];
  82. $video->title=$data['title'];
  83. if(!$video->save()){
  84. $this->error('保存失败');
  85. }
  86. $tags=array_filter($data['tag']??[]);
  87. if(!empty($tags)){
  88. $video->tag()->sync($data['tag']);
  89. }
  90. foreach ($data['point']??[] as $item){
  91. $keyAll=$item['key']??[];
  92. if($item['type']=='choose'&&empty($keyAll)){
  93. $this->error('答案必选');
  94. }
  95. $this->validate($item,[
  96. 'second|卡点时间点'=>['require','integer','gt:0'],
  97. 'title|卡点标题'=>['max:15'],
  98. 'title_img|卡点标题图片'=>['url'],
  99. ]);
  100. if(empty($item['title']) && empty($item['title_img'])){
  101. $this->error('卡点标题和标题图片不能同时为空');
  102. }
  103. if(in_array($item['type'],['choose','vote'])){
  104. $item['items']=$item['item_type']=='txt'?$item['txt']['items']??[]:$item['image']['items']??[];
  105. unset($item['txt'],$item['image']);
  106. }
  107. if(in_array($item['type'],['choose','vote'])){
  108. $a=[];
  109. $idx=0;
  110. $key=[];
  111. foreach ($item['items'] as $_k=>$temp){
  112. if($item['item_type']=='txt') {
  113. $this->validate(compact('temp'), [
  114. 'temp|选项' => ['require', 'max:15']
  115. ]);
  116. }
  117. $a[$newKey=chr($idx+65)]=$temp;
  118. if($item['type']=='choose' && isset($keyAll[$_k])){
  119. $key[]=$newKey;
  120. }
  121. $idx++;
  122. }
  123. $item['items']=$a;
  124. if($item['type']=='choose'){
  125. $item['key']=implode(',',$key);
  126. }
  127. }
  128. $video->point()->save($item);
  129. }
  130. Db::commit();
  131. $this->success();
  132. }
  133. public function edit($ids=null){
  134. $video=$this->model->find($ids);
  135. if($this->request->isGet()){
  136. $this->assign('row',$video);
  137. $this->assign('checked_tags',$video->tag()->select()->column('id'));
  138. return $this->view->fetch();
  139. }else{
  140. $data=input('row/a');
  141. Db::startTrans();
  142. $video['guest_id']=$data['guest_id'];
  143. $video['bg']=$data['bg'];
  144. $video['src']=$data['src'];
  145. $video['title']=$data['title'];
  146. $video['is_open']=$data['is_open'];
  147. $video->save();
  148. $video->tag()->sync(array_filter($data['tag']));
  149. Db::commit();
  150. $this->success();
  151. }
  152. }
  153. }