百度小程序自动提交搜索

背景:新上线了小程序,使用了小程序自带的搜索组件,需要提交一些文章到搜索上才能在小程序里搜索出来,但是官方给出的接口一次只能提交50次,所以自己写了一个循环进行整个栏目的提交.也是为了做一个备份

使用的后台:

我使用的建站是pbootcms,如果你是其他的cms系统,可以适当修改

后台html:

新增一个按钮用于触发请求:

<button type="submit" name="submit" value="bdapp_search_all" class="layui-btn layui-btn-sm">百度小程序搜索全提交</button>

截图如下:

 

 

后台方法:

\apps\admin\controller\content\ContentController.php

其他的cms需要到对应的php下了

在这里找到对应的方法,添加case分支,pb的可以搜baiduzz这个关键词,这个是pb自带的百度推送的分支处理.

复制代码
case 'bdapp_search_all':
                    $list = post('list');
                    $urls = post('urls');
                    $mcode = post('mcode', 'var');
                    
//这个是小程序id,获取小程序的流程,可能会不一样的,我这里是保存到数据库中了
                    $client_id = $this->config('xcx_client_id');
                    //查询数据库获取所有的id,status=1的
                    $ids_list = $this->model->getList_search_ids($mcode);
                    
                    $ids = '';
                    foreach($ids_list as $key => $ids_list){
                        
                        if(($key % 9) != 0 || $key== 0){
                            
                            $ids .= ($ids_list->id . ",");
                            
                        }else{
                            $ids = trim($ids,',');
                            
                            
                            $in_sql = 'a.id in (' . $ids . ')';
                            
                            $post_list = $this->model->getList($mcode, $in_sql, false);
                            
                            $post_data = array();
//开始封装数据
                            foreach( $post_list as $key => $value ){
                                $tags = explode(',',$value->tags);
                                $tags = array_splice($tags,3);
                                $post_data[] = array(
                                    'path'  =>  '/pages/article/view?id=' . $value->id,//小程序的路径
                                    'jump_app_key'  =>  $client_id,
                                    'title' =>  $value->title,
                                    'mapp_type' => 2001,//这是内容类型,一般填这个就是
                                    'schema' => json_encode([
                                        'datatype'=>2,
                                        'desc'=>$value->description,
                                        'cate_name'=>$tags,
                                        'detail'=>[
                                            'params'=>[
                                                'title'=>$value->title,
                                                'desc'=>$value->description,
                                                //'image'=>$value->ico,//这里如果需要图片,需要用http://xxx/xx.jpg 这样的格式,不能用相对路径
                                                'author'=>$value->author,
                                                'tags'=>$tags,
                                                'publishTime'=> substr($value->date,0,16),
                                                'app_key'=>$client_id,
                                                'jump_url'=>'/pages/article/view?id=' . $value->id,
                                                'jump_type'=>0
                                            ]
                                        ]
                                    ],JSON_UNESCAPED_UNICODE)
                                );
                            }
                            $post_data = json_encode($post_data,JSON_UNESCAPED_UNICODE);
//获取小程序的token
                            $access_token = $this->getAccessToken();
                            
                            $api = "https://openapi.baidu.com/rest/2.0/smartapp/search/submit/schema?access_token=".$access_token;
                            $header = array('Content-Type:application/json');
                            $result = post_baiduapp($api, $post_data, $header);
                            
                            if ( $result->errno==0 ) {
                                //$result->msg('成功推送小程序搜索'.ids);
                            } else {
                                $err_msg .= '推送发生错误:'. $ids;
                                
                            }
                            $ids="";//重新初始化
                        }
                        
                    }
                     alert_back($err_msg);
                    
                    exit();
                    break;
View Code
复制代码

在 model中添加一个数据请求 \apps\admin\model\content\ContentModel.php.

复制代码
// 获取文章列表
    public function getList_search_ids($mcode)
    {
        $field = array(
           'a.id'
        );
       $join = array(
        array(
            'ay_content_sort b',
            'a.scode=b.scode',
            'LEFT'
        ),
        array(
            'ay_model d',
            'b.mcode=d.mcode',
            'LEFT'
        )
        );
        return parent::table('ay_content a')->field($field)
            ->where("a.status='1'")
            ->where("b.mcode='$mcode'")
            ->join($join)
            ->order('a.id DESC')
            ->select();
    }
复制代码

好了主要就是修改以上的三个地方.不过需要注意的是如果一次性提交的数量比较多的话,可能会报请求超时,不过数据提交是没用问题的.

效果展示:

 

 

以上内容来源于百科书,可以关注我了解更多.

posted @   玄之又玄众妙之门  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了

哦哦查

研学服务

知芽网

醉学网

点击右上角即可分享
微信分享提示