My Computer · 2025/10/05 0

苹果cms 增加支持豆瓣ID精准搜索

用户在搜索框输入:

  • 2294255(5–10 位数字)→ 控制器识别为豆瓣ID;
    • 若命中 1 条:直接跳到视频详情页 /voddetail/{id}.html
    • 多/0 条:进入搜索页,模板显示“搜索 2294255…”,列表/分页返回 vod_douban_id=2294255 的结果;分类统计也按该 ID 过滤。
  • 笑傲江湖(文本)→ 正常关键词流程;模板显示“搜索 笑傲江湖…”,翻页/分类统计都维持原有逻辑。

演示:如搜索笑傲江湖,结果较多不能精确定位到想要的视频

改为搜索豆瓣ID方式,如想看的是笑傲江湖1996版,对应的豆瓣ID是2294255,搜索 2294255只有两个结果

如果搜索的豆瓣ID结果只有一个,则不显示搜索结果页直接跳转到该视频的详情页。以下为大概的实现步骤,供参考。

一、数据库层(一次性)

目标:让按 vod_douban_id 查询足够快、可维护。
修改

  • mac_vod 表确保存在字段与索引:
    -- 字段(如已存在可跳过)
    ALTER TABLE mac_vod MODIFY COLUMN vod_douban_id VARCHAR(16) NOT NULL DEFAULT '' COMMENT '豆瓣ID';
    -- 索引(如已存在可跳过)
    CREATE INDEX idx_vod_douban_id ON mac_vod (vod_douban_id);

二、控制器(入口分流 + 透传参数)

文件application/index/controller/Vod.php
方法search()

  1. 解析参数:若 ?douban_id= 存在,直接用;否则当 wd 满足 5–10 位纯数字 时,将其判定为豆瓣ID。
  2. 唯一命中:查库命中 1 条时,直接 302 跳转到短链 /voddetail/{id}.html(避免走 index/vod/detail 导致“非法请求”,根据实际url重写规则进行修改)。
  3. 多条/0条:把 douban_id 放回请求参数($_GET['douban_id']$param['douban_id']),并清空 wd,随后 assign('param',$param) 进入搜索模板。

核心片段(思路,非强制一字不差)

    public function search()
    {
        $param = mac_param_url();
    
        // 解析 douban_id:显式 ?douban_id=xxxx 或 wd 为 5~10 位纯数字
        $doubanId = '';
        if (isset($param['douban_id'])) {
            $doubanId = trim((string)$param['douban_id']);
        }
        if ($doubanId === '') {
            $kw = isset($param['wd']) ? trim((string)$param['wd']) : '';
            if ($kw !== '' && preg_match('/^\d{5,10}$/', $kw)) {
                $doubanId = $kw;
            }
        }
        if ($doubanId !== '' && !preg_match('/^\d{5,10}$/', $doubanId)) {
            $doubanId = '';
        }
    
        if ($doubanId !== '') {
            $rows = db('vod')
                ->where(['vod_status' => 1, 'vod_douban_id' => $doubanId])
                ->field('vod_id')
                ->limit(2)
                ->select();
    
            $cnt = is_array($rows) ? count($rows) : 0;
            if ($cnt === 1) {
                $vodId = (int)$rows[0]['vod_id'];
                // 直接跳短链
                return $this->redirect('/voddetail/' . $vodId . '.html');
            }
    
            // 多条或0条:保留 douban_id,避免 wd 干扰
            $_GET['douban_id'] = $doubanId;
            unset($param['wd']);
            $param['douban_id'] = $doubanId;
            $param['wd'] = '';
        }
    
        // ★ 关键:把 param 透传到模板(后续模板/label 都用它)
        $this->assign('param', $param);
    
        return $this->label_fetch('vod/search');
    }

Ajax 搜索(与 search 同步支持 douban_id)

public function ajax_search()
    {
        $this->check_ajax();

        $param = mac_param_url();

        $doubanId = '';
        if (isset($param['douban_id'])) {
            $doubanId = trim((string)$param['douban_id']);
        }
        if ($doubanId === '') {
            $kw = isset($param['wd']) ? trim((string)$param['wd']) : '';
            if ($kw !== '' && preg_match('/^\d{5,10}$/', $kw)) {
                $doubanId = $kw;
            }
        }
        if ($doubanId !== '' && !preg_match('/^\d{5,10}$/', $doubanId)) {
            $doubanId = '';
        }

        if ($doubanId !== '') {
            $_GET['douban_id'] = $doubanId;
            if (isset($_GET['wd'])) unset($_GET['wd']);
            $param['douban_id'] = $doubanId;
            $param['wd'] = '';
            $this->assign('param', $param);
        }

        return $this->label_fetch('vod/ajax_search');
    }

三、模型(列表查询按豆瓣ID精确过滤)

文件application/common/model/Vod.php
方法listCacheData($lp)

  1. 读取分页参数阶段的 $param = mac_param_url(); 之后,新增一段:
    • 若存在 param['douban_id'] 且为 5–10 位纯数字:
      • where['vod_douban_id'] = param['douban_id']
      • 同时将 $wd/$name/$tag/$class/$actor/$director 置空,避免触发关键词模糊搜索与 VodSearch 优化逻辑。
  2. 其它 where、缓存、排序逻辑不改

目的

  • 任何走到列表/分页/AJAX 的地方,都会“精准命中豆瓣ID”,且不受关键词干扰。
public function listCacheData($lp)
    {
        if(!is_array($lp)){
            $lp = json_decode($lp,true);
        }

        $order = $lp['order'];
        $by = $lp['by'];
        $type = $lp['type'];
        $ids = $lp['ids'];
        $rel = $lp['rel'];
        $paging = $lp['paging'];
        $pageurl = $lp['pageurl'];
        $level = $lp['level'];
        $area = $lp['area'];
        $lang = $lp['lang'];
        $state = $lp['state'];
        $wd = $lp['wd'];
        $tag = $lp['tag'];
        $class = $lp['class'];
        $letter = $lp['letter'];
        $actor = $lp['actor'];
        $director = $lp['director'];
        $version = $lp['version'];
        $year = $lp['year'];
        $start = intval(abs($lp['start']));
        $num = intval(abs($lp['num']));
        $half = intval(abs($lp['half']));
        $weekday = $lp['weekday'];
        $tv = $lp['tv'];
        $timeadd = $lp['timeadd'];
        $timehits = $lp['timehits'];
        $time = $lp['time'];
        $hitsmonth = $lp['hitsmonth'];
        $hitsweek = $lp['hitsweek'];
        $hitsday = $lp['hitsday'];
        $hits = $lp['hits'];
        $not = $lp['not'];
        $cachetime = $lp['cachetime'];
        $isend = $lp['isend'];
        $plot = $lp['plot'];
        $typenot = $lp['typenot'];
        $name = $lp['name'];

        $page = 1;
        $where=[];
        $totalshow = 0;

        if(empty($num)){
            $num = 20;
        }
        if($start>1){
            $start--;
        }
        if($paging=='yes'){
            $param = mac_param_url();
            $param = mac_search_len_check($param);
            $totalshow = 1;
            if(!empty($param['id'])){
                //$type = intval($param['id']);
            }
            if(!empty($param['level'])){
                $level = $param['level'];
            }
            if(!empty($param['ids'])){
                $ids = $param['ids'];
            }
            if(!empty($param['tid'])) {
                $tid = intval($param['tid']);
            }
            if(!empty($param['year'])){
                if(strlen($param['year'])==4){
                    $year = $param['year'];
                }
                else{
                    $year = $param['year'];
                }
            }
            if(!empty($param['area'])){
                $area = $param['area'];
            }
            if(!empty($param['lang'])){
                $lang = $param['lang'];
            }
            if(!empty($param['state'])){
                $state = $param['state'];
            }
            if(!empty($param['tag'])){
                $tag = $param['tag'];
            }
            if(!empty($param['class'])){
                $class = $param['class'];
            }
            if(!empty($param['letter'])){
                $letter = $param['letter'];
            }
            if(!empty($param['by'])){
                $by = $param['by'];
            }
            if(!empty($param['order'])){
                $order = $param['order'];
            }
            if(!empty($param['level'])){
                $level = $param['level'];
            }
            if(!empty($param['wd'])){
                $wd = $param['wd'];
            }
            if(!empty($param['name'])){
                $name = $param['name'];
            }
            if(!empty($param['actor'])){
                $actor = $param['actor'];
            }
            if(!empty($param['director'])){
                $director = $param['director'];
            }
            if(!empty($param['plot'])){
                $plot = $param['plot'];
            }
            if(!empty($param['isend'])){
                $isend = $param['isend'];
            }
            if(!empty($param['page'])){
                $page = intval($param['page']);
            }
            if(!empty($param['size'])){
                $num = intval($param['size']);
            }
            if(!empty($param['tid'])){
                $pageurl = 'vod/type';
                $type = intval($param['tid']);
                $type_info = model('Type')->getCacheInfo($type);
                $flag='type';
                if(!empty($param['class'])){ $flag.='_'.$param['class']; }
                if($by){ $flag.='_'.$by; }
                if($actorset){ $flag.='_'.$actorset; }
                $pageurl = mac_url_type($type_info,$param,$flag);
            }
            else{
                $pageurl = mac_url($pageurl,$param);
            }

            // === [新增] 豆瓣ID精确筛选:若携带 douban_id,则追加 where 并清空关键词相关变量,避免触发模糊搜索与 VodSearch 优化 ===
            if (!empty($param['douban_id']) && preg_match('/^\d{5,10}$/', (string)$param['douban_id'])) {
                $where['vod_douban_id'] = (string)$param['douban_id'];
                $wd = '';
                $name = '';
                $tag = '';
                $class = '';
                $actor = '';
                $director = '';
            }
            // === [新增结束] ===
        }

        $where['vod_status'] = ['eq',1];
        if(!empty($ids)) {
            if($ids!='all'){
                $where['vod_id'] = ['in',explode(',',$ids)];
            }
        }
        if(!empty($not)){
            $where['vod_id'] = ['not in',explode(',',$not)];
        }
        if(!empty($rel)){
            $tmp = explode(',',$rel);
            if(is_numeric($rel) || mac_array_check_num($tmp)==true  ){
                $where['vod_id'] = ['in',$tmp];
            }
            else{
                $where['vod_rel_vod'] = ['like', mac_like_arr($rel),'OR'];
            }
        }
        if(!empty($level)) {
            if($level=='all'){
                $level = '1,2,3,4,5,6,7,8,9';
            }
            $where['vod_level'] = ['in',explode(',',$level)];
        }
        if(!empty($year)) {
            $where['vod_year'] = ['in',explode(',',$year)];
        }
        if(!empty($area)) {
            $where['vod_area'] = ['in',explode(',',$area)];
        }
        if(!empty($lang)) {
            $where['vod_lang'] = ['in',explode(',',$lang)];
        }
        if(!empty($state)) {
            $where['vod_state'] = ['in',explode(',',$state)];
        }
        if(!empty($weekday)){
            $where['vod_weekday'] = ['in',explode(',',$weekday)];
        }
        if(!empty($tv)){
            $where['vod_tv'] = ['in',explode(',',$tv)];
        }
        if(!empty($timeadd)){
            $s = intval(strtotime($timeadd));
            $where['vod_time_add'] =['gt',$s];
        }
        if(!empty($timehits)){
            $s = intval(strtotime($timehits));
            $where['vod_time_hits'] =['gt',$s];
        }
        if(!empty($time)){
            $s = intval(strtotime($time));
            $where['vod_time'] =['gt',$s];
        }
        if(!empty($letter)){
            if(substr($letter,0,1)=='0' && substr($letter,2,1)=='9'){
                $letter='0,1,2,3,4,5,6,7,8,9';
            }
            $where['vod_letter'] = ['in',explode(',',$letter)];
        }
        if(!empty($type)) {
            if($type=='current'){
                $type = intval( $GLOBALS['type_id'] );
            }
            if($type!='all') {
                $tmp_arr = explode(',',$type);
                $type_list = model('Type')->getCache('type_list');
                $type = [];
                foreach($type_list as $k2=>$v2){
                    if(in_array($v2['type_id'],$tmp_arr) || in_array($v2['type_pid'],$tmp_arr)){
                        $type[] = $v2['type_id'];
                    }
                }
                $type = array_unique($type);
                if(!empty($type)){
                    $where['type_id'] = ['in',join(',',$type)];
                }
            }
        }
        if(!empty($typenot)) {
            $tmp_arr = explode(',',$typenot);
            $type_list = model('Type')->getCache('type_list');
            $type = [];
            foreach($type_list as $k2=>$v2){
                if(in_array($v2['type_id'],$tmp_arr) || in_array($v2['type_pid'],$tmp_arr)){
                    $type[] = $v2['type_id'];
                }
            }
            $type = array_unique($type);
            if(!empty($type)){
                $where['type_id'] = ['not in',join(',',$type)];
            }
        }

        // —— 与关键字相关的高性能搜索缓存(VodSearch) ——
        $vod_search = model('VodSearch');
        $vod_search_enabled = $vod_search->isFrontendEnabled();
        $max_id_count = $vod_search->maxIdCount;
        if ($vod_search_enabled) {
            $search_id_list = [];
            if(!empty($wd)) {
                $role = 'vod_name';
                if(!empty($GLOBALS['config']['app']['search_vod_rule'])){
                    $role .= '|' . $GLOBALS['config']['app']['search_vod_rule'];
                }
                $where[$role] = ['like', '%' . $wd . '%'];
                if (count($search_id_list_tmp = $vod_search->getResultIdList($wd, $role)) <= $max_id_count) {
                    $search_id_list += $search_id_list_tmp;
                    unset($where[$role]);
                }
            }
            if(!empty($name)) {
                $where['vod_name'] = ['like',mac_like_arr($name),'OR'];
                if (count($search_id_list_tmp = $vod_search->getResultIdList($name, 'vod_name')) <= $max_id_count) {
                    $search_id_list += $search_id_list_tmp;
                    unset($where['vod_name']);
                }
            }
            if(!empty($tag)) {
                $where['vod_tag'] = ['like',mac_like_arr($tag),'OR'];
                if (count($search_id_list_tmp = $vod_search->getResultIdList($tag, 'vod_tag', true)) <= $max_id_count) {
                    $search_id_list += $search_id_list_tmp;
                    unset($where['vod_tag']);
                }
            }
            if(!empty($class)) {
                $where['vod_class'] = ['like',mac_like_arr($class), 'OR'];
                if (count($search_id_list_tmp = $vod_search->getResultIdList($class, 'vod_class', true)) <= $max_id_count) {
                    $search_id_list += $search_id_list_tmp;
                    unset($where['vod_class']);
                }
            }
            if(!empty($actor)) {
                $where['vod_actor'] = ['like', mac_like_arr($actor), 'OR'];
                if (count($search_id_list_tmp = $vod_search->getResultIdList($actor, 'vod_actor', true)) <= $max_id_count) {
                    $search_id_list += $search_id_list_tmp;
                    unset($where['vod_actor']);
                }
            }
            if(!empty($director)) {
                $where['vod_director'] = ['like',mac_like_arr($director),'OR'];
                if (count($search_id_list_tmp = $vod_search->getResultIdList($director, 'vod_director', true)) <= $max_id_count) {
                    $search_id_list += $search_id_list_tmp;
                    unset($where['vod_director']);
                }
            }
            $search_id_list = array_unique($search_id_list);
            if (!empty($search_id_list)) {
                $where['_string'] = "vod_id IN (" . join(',', $search_id_list) . ")";
            }
        }

        if(in_array($plot,['0','1'])){
            $where['vod_plot'] = $plot;
        }

        if(defined('ENTRANCE') && ENTRANCE == 'index' && $GLOBALS['config']['app']['search'] !='0' && ($GLOBALS['config']['app']['search'] == 'close' || $GLOBALS['config']['app']['vod_search'] == 'close')){
            $this->error = lang('model/search_close_err');
            return ['code'=>1001,'msg'=>$this->error ];
        }

        $use_rand = false;
        if($by == 'rnd'){
            $use_rand = true;
            $by = 'time';
            $order = 'desc';
        }

        if(!in_array($by, ['id', 'time','time_add','score','hits_day','hits_week','hits_month','up','down','level','rnd'])) {
            $by = 'time';
        }
        if(!in_array($order, ['asc', 'desc'])) {
            $order = 'desc';
        }
        $order= 'vod_'.$by .' ' . $order;
        $where_cache = $where;
        if($use_rand){
            unset($where_cache['vod_id']);
            $where_cache['order'] = 'rnd';
        }

        $cach_name = $GLOBALS['config']['app']['cache_flag']. '_vodlist_' . md5(json_encode($where_cache)).'_'.$order.'_'.$page.'_'.$num.'_'.$start.'_'.$pageurl;
        $res = Cache::get($cach_name);
        if(empty($cachetime)){
            $cachetime = $GLOBALS['config']['app']['cache_time'];
        }
        if($GLOBALS['config']['app']['cache_core']==0 || empty($res)) {
            $res = $this->listData($where, $order, $page, $num, $start,'*',1, $totalshow);
            if($GLOBALS['config']['app']['cache_core']==1) {
                Cache::set($cach_name, $res, $cachetime);
            }
        }
        $res['pageurl'] = $pageurl;
        $res['half'] = $half;

        return $res;
    }

四、搜索页模板

模板可能不同,这里仅供参考。

文件/template/mxpro/html/vod/search.html

修改点

  1. 根节点透出 douban_id(用于前端 AJAX/统计沿用):
<div id="search-context"
     ...
     data-type1="{$param.type_id_1|default=''}"
     data-douban-id="{$param.douban_id|default=''}"> <!-- 新增 -->

2. 头部关键词显示:在原有“搜索…找到 X 部”上方插入一个 {php} 计算块,然后只替换关键词那一小段输出,其余字样和布局不变:

{php}
  $__search_kw = '';
  if (!empty($param['douban_id']))      $__search_kw = $param['douban_id'];
  elseif (!empty($param['wd']))         $__search_kw = $param['wd'];
  elseif (!empty($param['name']))       $__search_kw = $param['name'];
  else $__search_kw = ($param['actor'] ?? '') . ($param['director'] ?? '') . ($param['area'] ?? '') .
                      ($param['lang'] ?? '') . ($param['year'] ?? '') . ($param['class'] ?? '');
{/php}

3. 标题里原本 <strong>…</strong> 的搜索结果长拼接替换为:

<strong><?php echo htmlspecialchars($__search_kw, ENT_QUOTES, 'UTF-8'); ?></strong>