model = new \app\admin\model\Video; $tags=\app\admin\model\Tag::column('id,name'); $this->assign('tags',$tags); } public function import() { parent::import(); } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //当前是否为关联查询 $this->relationSearch = false; //设置过滤方法 $this->request->filter(['strip_tags', 'trim']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model ->join('video_tag','video_tag.video_id=video.id','left') ->join('tag','tag.id=video_tag.tag_id','left') ->group('video.id') ->where($where) ->order($sort, $order) ->field(['video.*',Db::raw('group_concat(tag.name SEPARATOR \',\') as tag')]) ->paginate($limit); foreach ($list as $row) { $row->visible(['id','bg','src','play_num','comment_num','like_num','fav_num','created_at','weight','title','tag']); } $result = array("total" => $list->total(), "rows" => $list->items()); return json($result); } return $this->view->fetch(); } public function add() { $video=new \app\admin\model\Video(); if($this->request->isGet()){ return parent::add(); } Db::startTrans(); $data=input('row/a'); $this->validate($data,[ 'title|标题'=>['require'], 'bg'=>['require'], 'src'=>['require'], 'guest_id'=>['require'], ]); $video->guest_id=$data['guest_id']; $video->bg=$data['bg']; $video->src=$data['src']; $video->is_open=$data['is_open']; $video->title=$data['title']; if(!$video->save()){ $this->error('保存失败'); } $tags=array_filter($data['tag']??[]); if(!empty($tags)){ $video->tag()->sync($data['tag']); } foreach ($data['point']??[] as $item){ $keyAll=$item['key']??[]; if($item['type']=='choose'&&empty($keyAll)){ $this->error('答案必选'); } $this->validate($item,[ 'second|卡点时间点'=>['require','integer','gt:0'], 'title|卡点标题'=>['max:15'], 'title_img|卡点标题图片'=>['url'], ]); if(empty($item['title']) && empty($item['title_img'])){ $this->error('卡点标题和标题图片不能同时为空'); } if(in_array($item['type'],['choose','vote'])){ $item['items']=$item['item_type']=='txt'?$item['txt']['items']??[]:$item['image']['items']??[]; unset($item['txt'],$item['image']); } if(in_array($item['type'],['choose','vote'])){ $a=[]; $idx=0; $key=[]; foreach ($item['items'] as $_k=>$temp){ if($item['item_type']=='txt') { $this->validate(compact('temp'), [ 'temp|选项' => ['require', 'max:15'] ]); } $a[$newKey=chr($idx+65)]=$temp; if($item['type']=='choose' && isset($keyAll[$_k])){ $key[]=$newKey; } $idx++; } $item['items']=$a; if($item['type']=='choose'){ $item['key']=implode(',',$key); } } $video->point()->save($item); } Db::commit(); $this->success(); } public function edit($ids=null){ $video=$this->model->find($ids); if($this->request->isGet()){ $this->assign('row',$video); $this->assign('checked_tags',$video->tag()->select()->column('id')); return $this->view->fetch(); }else{ $data=input('row/a'); Db::startTrans(); $video['guest_id']=$data['guest_id']; $video['bg']=$data['bg']; $video['src']=$data['src']; $video['title']=$data['title']; $video['is_open']=$data['is_open']; $video->save(); $video->tag()->sync(array_filter($data['tag'])); Db::commit(); $this->success(); } } }