审查视图

application/admin/controller/Video.php 19.4 KB
郭盛 authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<?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;
郭盛 authored
21
    protected $searchFields = 'title,number';
郭盛 authored
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

    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();
62
            $words = new \app\admin\model\Words();
郭盛 authored
63
            foreach ($list as &$v){
64 65 66 67
                $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);
郭盛 authored
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
                $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);
87
88
                //截取提取码
89 90
                if(!empty($params['two_url'])){
                    $params['two_code'] = substr($params['two_url'],-4);
91 92
                }else{
                    $params['two_code'] = '';
93 94 95
                }
                if(!empty($params['four_url'])){
                    $params['four_code'] = substr($params['four_url'],-4);
96 97
                }else{
                    $params['four_code'] = '';
98 99 100
                }
                if(!empty($params['eight_url'])){
                    $params['eight_code'] = substr($params['eight_url'],-4);
101 102
                }else{
                    $params['eight_code'] = '';
103
                }
104
105
                $a = $params['one'];
106
                //判断分辨率
107 108
                if(!empty($params['two'])){
                    $a .= ','.$params['two'];
109 110 111
                }else{
                    $params['two'] = '';
                    $a .= '';
112 113 114
                }
                if(!empty($params['four'])){
                    $a .= ','.$params['four'];
115 116 117
                }else{
                    $params['four'] = '';
                    $a .= '';
118 119 120
                }
                if(!empty($params['eight'])){
                    $a .= ','.$params['eight'];
121 122 123 124 125 126 127 128
                }else{
                    $params['eight'] = '';
                    $a .= '';
                }

                //判断价格
                if(empty($params['price'])){
                    $params['price'] = 0;
129
                }
130 131 132 133 134 135 136
                if(empty($params['four_price'])){
                    $params['four_price'] = 0;
                }
                if(empty($params['eight_price'])){
                    $params['eight_price'] = 0;
                }
137
                $params['content'] = $a;
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
                //如果内容关键字和地名关键字都为空
                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;
157 158 159 160 161
                if($count>=3){
                    true;
                }else{
                    $this->error('至少选择三个关键字标签');
                }
郭盛 authored
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
                $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");
215 216 217 218

            //截取提取码
            if(!empty($params['two_url'])){
                $params['two_code'] = substr($params['two_url'],-4);
219 220
            }else{
                $params['two_code'] = '';
221 222 223
            }
            if(!empty($params['four_url'])){
                $params['four_code'] = substr($params['four_url'],-4);
224 225
            }else{
                $params['four_code'] = '';
226 227 228
            }
            if(!empty($params['eight_url'])){
                $params['eight_code'] = substr($params['eight_url'],-4);
229 230
            }else{
                $params['eight_code'] = '';
231 232
            }
233
            $a = $params['one'];
234
            //判断分辨率
235 236
            if(!empty($params['two'])){
                $a .= ','.$params['two'];
237 238 239
            }else{
                $params['two'] = '';
                $a .= '';
240 241 242
            }
            if(!empty($params['four'])){
                $a .= ','.$params['four'];
243 244 245
            }else{
                $params['four'] = '';
                $a .= '';
246 247 248
            }
            if(!empty($params['eight'])){
                $a .= ','.$params['eight'];
249 250 251 252 253 254 255 256 257 258 259
            }else{
                $params['eight'] = '';
                $a .= '';
            }

            //判断价格
            if(empty($params['price'])){
                $params['price'] = 0;
            }
            if(empty($params['four_price'])){
                $params['four_price'] = 0;
260
            }
261 262 263 264
            if(empty($params['eight_price'])){
                $params['eight_price'] = 0;
            }
265
            $params['content'] = $a;
郭盛 authored
266
            $params['type_ids'] =','.$params['type_ids'].',';
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
            //如果内容关键字和地名关键字都为空
            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;
286 287 288 289 290
            if($count>=3){
                true;
            }else{
                $this->error('至少选择三个关键字标签');
            }
郭盛 authored
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
            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();
    }
郭盛 authored
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
    /**
     * 删除
     */
    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'));
    }


郭盛 authored
366 367 368 369 370 371
    public function type(){
        $res = Db::name('type')->field('id,area_name')->select();
        $arr = [];
        foreach($res as $key=>$value){
            $arr[$value['id']] = $value['area_name'];
        }
372 373 374
        return json($arr);
    }
375 376 377 378 379 380 381 382 383 384 385 386
    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();
387 388 389 390
        $arr = [];
        foreach($res as $key=>$value){
            $arr[$value['id']] = $value['name'];
        }
郭盛 authored
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
        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];
    }



}