<?php namespace app\admin\controller; use app\common\controller\Backend; use think\Db; /** * 视频管理 * * @icon fa fa-circle-o */ class Video extends Backend { /** * Video模型对象 * @var \app\admin\model\Video */ protected $model = null; protected $searchFields = 'title,number'; public function _initialize() { parent::_initialize(); $this->model = new \app\admin\model\Video; } /** * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法 * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑 * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改 */ /** * 查看 */ public function index() { //设置过滤方法 $this->request->filter(['strip_tags']); if ($this->request->isAjax()) { //如果发送的来源是Selectpage,则转发到Selectpage if ($this->request->request('keyField')) { return $this->selectpage(); } list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $total = $this->model ->where($where) ->order($sort, $order) ->count(); $list = $this->model ->where($where) ->order($sort, $order) ->limit($offset, $limit) ->select(); $list = collection($list)->toArray(); $type = new \app\admin\model\Type(); $words = new \app\admin\model\Words(); foreach ($list as &$v){ $text_ids = $words->whereIn('id',$v['text_ids'])->column('name'); $v['text_ids'] = implode(',',$text_ids); $address_ids = $words->whereIn('id',$v['address_ids'])->column('name'); $v['address_ids'] = implode(',',$address_ids); $type_ids = $type->whereIn('id',$v['type_ids'])->column('area_name'); $v['type_ids'] = implode(',',$type_ids); } $result = array("total" => $total, "rows" => $list); return json($result); } return $this->view->fetch(); } /** * 添加 */ public function add() { if ($this->request->isPost()) { $params = $this->request->post("row/a"); if ($params) { $params = $this->preExcludeFields($params); //截取提取码 if(!empty($params['two_url'])){ $params['two_code'] = substr($params['two_url'],-4); }else{ $params['two_code'] = ''; } if(!empty($params['four_url'])){ $params['four_code'] = substr($params['four_url'],-4); }else{ $params['four_code'] = ''; } if(!empty($params['eight_url'])){ $params['eight_code'] = substr($params['eight_url'],-4); }else{ $params['eight_code'] = ''; } $a = $params['one']; //判断分辨率 if(!empty($params['two'])){ $a .= ','.$params['two']; }else{ $params['two'] = ''; $a .= ''; } if(!empty($params['four'])){ $a .= ','.$params['four']; }else{ $params['four'] = ''; $a .= ''; } if(!empty($params['eight'])){ $a .= ','.$params['eight']; }else{ $params['eight'] = ''; $a .= ''; } //判断价格 if(empty($params['price'])){ $params['price'] = 0; } if(empty($params['four_price'])){ $params['four_price'] = 0; } if(empty($params['eight_price'])){ $params['eight_price'] = 0; } $params['content'] = $a; //如果内容关键字和地名关键字都为空 if($params['text_ids'] == '' && $params['address_ids'] == ''){ $this->error('请选择关键字标签'); } //内容关键字不为空 if($params['text_ids'] != ''){ $count_text = count(explode(',',$params['text_ids'])); $params['text_ids'] =','.$params['text_ids'].','; }else{ $count_text = 0; } //地名关键字不为空 if($params['address_ids'] != ''){ $count_address = count(explode(',',$params['address_ids'])); $params['address_ids'] =','.$params['address_ids'].','; }else{ $count_address = 0; } $count = $count_text + $count_address; if($count>=3){ true; }else{ $this->error('至少选择三个关键字标签'); } $params['type_ids'] =','.$params['type_ids'].','; if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException(true)->validate($validate); } $result = $this->model->allowField(true)->save($params); Db::commit(); } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(); } else { $this->error(__('No rows were inserted')); } } $this->error(__('Parameter %s can not be empty', '')); } return $this->view->fetch(); } /** * 编辑 */ public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { if (!in_array($row[$this->dataLimitField], $adminIds)) { $this->error(__('You have no permission')); } } if ($this->request->isPost()) { $params = $this->request->post("row/a"); //截取提取码 if(!empty($params['two_url'])){ $params['two_code'] = substr($params['two_url'],-4); }else{ $params['two_code'] = ''; } if(!empty($params['four_url'])){ $params['four_code'] = substr($params['four_url'],-4); }else{ $params['four_code'] = ''; } if(!empty($params['eight_url'])){ $params['eight_code'] = substr($params['eight_url'],-4); }else{ $params['eight_code'] = ''; } $a = $params['one']; //判断分辨率 if(!empty($params['two'])){ $a .= ','.$params['two']; }else{ $params['two'] = ''; $a .= ''; } if(!empty($params['four'])){ $a .= ','.$params['four']; }else{ $params['four'] = ''; $a .= ''; } if(!empty($params['eight'])){ $a .= ','.$params['eight']; }else{ $params['eight'] = ''; $a .= ''; } //判断价格 if(empty($params['price'])){ $params['price'] = 0; } if(empty($params['four_price'])){ $params['four_price'] = 0; } if(empty($params['eight_price'])){ $params['eight_price'] = 0; } $params['content'] = $a; $params['type_ids'] =','.$params['type_ids'].','; //如果内容关键字和地名关键字都为空 if($params['text_ids'] == '' && $params['address_ids'] == ''){ $this->error('请选择关键字标签'); } //内容关键字不为空 if($params['text_ids'] != ''){ $count_text = count(explode(',',$params['text_ids'])); $params['text_ids'] =','.$params['text_ids'].','; }else{ $count_text = 0; } //地名关键字不为空 if($params['address_ids'] != ''){ $count_address = count(explode(',',$params['address_ids'])); $params['address_ids'] =','.$params['address_ids'].','; }else{ $count_address = 0; } $count = $count_text + $count_address; if($count>=3){ true; }else{ $this->error('至少选择三个关键字标签'); } if ($params) { $params = $this->preExcludeFields($params); $result = false; Db::startTrans(); try { //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate; $row->validateFailException(true)->validate($validate); } $result = $row->allowField(true)->save($params); Db::commit(); } catch (ValidateException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result !== false) { $this->success(); } else { $this->error(__('No rows were updated')); } } $this->error(__('Parameter %s can not be empty', '')); } $this->view->assign("row", $row); return $this->view->fetch(); } /** * 删除 */ public function del($ids = "") { if ($ids) { $pk = $this->model->getPk(); $adminIds = $this->getDataLimitAdminIds(); Db::name('car')->where('video_id',$ids)->delete(); if (is_array($adminIds)) { $this->model->where($this->dataLimitField, 'in', $adminIds); } $list = $this->model->where($pk, 'in', $ids)->select(); $count = 0; Db::startTrans(); try { foreach ($list as $k => $v) { $count += $v->delete(); } Db::commit(); } catch (PDOException $e) { Db::rollback(); $this->error($e->getMessage()); } catch (Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($count) { $this->success(); } else { $this->error(__('No rows were deleted')); } } $this->error(__('Parameter %s can not be empty', 'ids')); } public function type(){ $res = Db::name('type')->field('id,area_name')->select(); $arr = []; foreach($res as $key=>$value){ $arr[$value['id']] = $value['area_name']; } return json($arr); } public function text(){ $res = Db::name('words')->where('type',2)->field('id,type,name')->select(); $arr = []; foreach($res as $key=>$value){ $arr[$value['id']] = $value['name']; } return json($arr); } public function address() { $res = Db::name('words')->where('type',1)->field('id,type,name')->select(); $arr = []; foreach($res as $key=>$value){ $arr[$value['id']] = $value['name']; } return json($arr); } /** * 生成查询所需要的条件,排序方式 * @param mixed $searchfields 快速查询的字段 * @param boolean $relationSearch 是否关联查询 * @return array */ protected function buildparams($searchfields = null, $relationSearch = null) { $searchfields = is_null($searchfields) ? $this->searchFields : $searchfields; $relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch; $search = $this->request->get("search", ''); $filter = $this->request->get("filter", ''); $op = $this->request->get("op", '', 'trim'); $sort = $this->request->get("sort", !empty($this->model) && $this->model->getPk() ? $this->model->getPk() : 'id'); $order = $this->request->get("order", "DESC"); $offset = $this->request->get("offset", 0); $limit = $this->request->get("limit", 0); $filter = (array)json_decode($filter, true); $op = (array)json_decode($op, true); $filter = $filter ? $filter : []; $where = []; $tableName = ''; if ($relationSearch) { if (!empty($this->model)) { $name = \think\Loader::parseName(basename(str_replace('\\', '/', get_class($this->model)))); $tableName = $name . '.'; } $sortArr = explode(',', $sort); foreach ($sortArr as $index => & $item) { $item = stripos($item, ".") === false ? $tableName . trim($item) : $item; } unset($item); $sort = implode(',', $sortArr); } $adminIds = $this->getDataLimitAdminIds(); if (is_array($adminIds)) { $where[] = [$tableName . $this->dataLimitField, 'in', $adminIds]; } if ($search) { $searcharr = is_array($searchfields) ? $searchfields : explode(',', $searchfields); foreach ($searcharr as $k => &$v) { $v = stripos($v, ".") === false ? $tableName . $v : $v; } unset($v); $where[] = [implode("|", $searcharr), "LIKE", "%{$search}%"]; } foreach ($filter as $k => $v) { $sym = isset($op[$k]) ? $op[$k] : '='; if (stripos($k, ".") === false) { $k = $tableName . $k; } $v = !is_array($v) ? trim($v) : $v; $sym = strtoupper(isset($op[$k]) ? $op[$k] : $sym); // 判断如果为地区标签,改为模糊查询 if($k == 'type_ids') { $sym = 'LIKE'; $v = $v.','; } switch ($sym) { case '=': case '<>': $where[] = [$k, $sym, (string)$v]; break; case 'LIKE': case 'NOT LIKE': case 'LIKE %...%': case 'NOT LIKE %...%': $where[] = [$k, trim(str_replace('%...%', '', $sym)), "%{$v}%"]; break; case '>': case '>=': case '<': case '<=': $where[] = [$k, $sym, intval($v)]; break; case 'FINDIN': case 'FINDINSET': case 'FIND_IN_SET': $where[] = "FIND_IN_SET('{$v}', " . ($relationSearch ? $k : '`' . str_replace('.', '`.`', $k) . '`') . ")"; break; case 'IN': case 'IN(...)': case 'NOT IN': case 'NOT IN(...)': $where[] = [$k, str_replace('(...)', '', $sym), is_array($v) ? $v : explode(',', $v)]; break; case 'BETWEEN': case 'NOT BETWEEN': $arr = array_slice(explode(',', $v), 0, 2); if (stripos($v, ',') === false || !array_filter($arr)) { continue 2; } //当出现一边为空时改变操作符 if ($arr[0] === '') { $sym = $sym == 'BETWEEN' ? '<=' : '>'; $arr = $arr[1]; } elseif ($arr[1] === '') { $sym = $sym == 'BETWEEN' ? '>=' : '<'; $arr = $arr[0]; } $where[] = [$k, $sym, $arr]; break; case 'RANGE': case 'NOT RANGE': $v = str_replace(' - ', ',', $v); $arr = array_slice(explode(',', $v), 0, 2); if (stripos($v, ',') === false || !array_filter($arr)) { continue 2; } //当出现一边为空时改变操作符 if ($arr[0] === '') { $sym = $sym == 'RANGE' ? '<=' : '>'; $arr = $arr[1]; } elseif ($arr[1] === '') { $sym = $sym == 'RANGE' ? '>=' : '<'; $arr = $arr[0]; } $where[] = [$k, str_replace('RANGE', 'BETWEEN', $sym) . ' time', $arr]; break; case 'LIKE': case 'LIKE %...%': $where[] = [$k, 'LIKE', "%{$v}%"]; break; case 'NULL': case 'IS NULL': case 'NOT NULL': case 'IS NOT NULL': $where[] = [$k, strtolower(str_replace('IS ', '', $sym))]; break; default: break; } } $where = function ($query) use ($where) { foreach ($where as $k => $v) { if (is_array($v)) { call_user_func_array([$query, 'where'], $v); } else { $query->where($v); } } }; return [$where, $sort, $order, $offset, $limit]; } }