238 lines
14 KiB
PHP
238 lines
14 KiB
PHP
<?php
|
||
/**
|
||
* Created by PhpStorm.
|
||
* User: lock
|
||
* Date: 2020/11/9
|
||
* Time: 4:56 PM
|
||
*/
|
||
|
||
namespace app\index\controller;
|
||
|
||
use app\index\model\AnalysisError;
|
||
use app\index\model\search;
|
||
use app\index\model\SearchContent;
|
||
use app\index\model\SearchRelation;
|
||
use app\index\model\Task;
|
||
use app\index\model\TaskContentRelation;
|
||
|
||
class MessageSearch extends Common
|
||
{
|
||
|
||
/**
|
||
* lists
|
||
* @return \think\response\Json
|
||
*/
|
||
public function lists()
|
||
{
|
||
if(self::$result[self::$error_code_name] == 0)
|
||
{
|
||
$search_model = new Search();
|
||
$field_arr = array(
|
||
array('field_name' => $search_model::KEYWORD,'rule' => ''),
|
||
array('field_name' => 'page','rule' => 'require|number'),
|
||
array('field_name' => 'pageSize','rule' => 'require|number')
|
||
);
|
||
$check_arr = $this->checkHave($field_arr);
|
||
if($check_arr[self::$error_code_name] == 0)
|
||
{
|
||
$where = self::returnWhere($field_arr,$check_arr[self::$data_name]);
|
||
unset($where['page']);
|
||
unset($where['pageSize']);
|
||
if (array_key_exists($search_model::KEYWORD,$check_arr[self::$data_name]))
|
||
{
|
||
unset($where[$search_model::KEYWORD]);
|
||
$where[$search_model::KEYWORD_MD5] = md5($check_arr[self::$data_name][$search_model::KEYWORD]);
|
||
}
|
||
$lists = $search_model->modelLists($search_model,$check_arr[self::$data_name]['page'],$check_arr[self::$data_name]['pageSize'],$where);
|
||
$tmp_lists = array();
|
||
if (!empty($lists))
|
||
{
|
||
$id_arr = array();
|
||
foreach ($lists as $each_k => $each_v)
|
||
{
|
||
$id_arr[] = $each_v[$search_model::AUTO_ID];
|
||
}
|
||
$search_relation_model = new SearchRelation();
|
||
$search_relation_where = array();
|
||
$search_relation_where[$search_relation_model::KEYWORD_ID] = array('in',$id_arr);
|
||
$search_relation_list = $search_relation_model->modelAllLists($search_relation_model,$search_relation_where);
|
||
$content_id_arr = array();
|
||
if (!empty($search_relation_list))
|
||
{
|
||
foreach ($search_relation_list as $search_each_v)
|
||
{
|
||
$content_id_arr[] = $search_each_v[$search_relation_model::CONTENT_ID];
|
||
}
|
||
}
|
||
$search_content_model = new SearchContent();
|
||
$search_content_where = array();
|
||
$search_content_where[$search_content_model::AUTO_ID] = array('in',$content_id_arr);
|
||
$tmp_lists = $search_content_model->modelAllLists($search_content_model,$search_content_where);
|
||
}
|
||
self::$result['lists'] = $tmp_lists;
|
||
}else{
|
||
if($check_arr[self::$error_code_name] == 1)
|
||
{
|
||
self::$result[self::$error_code_name] = 10004;
|
||
self::$result[self::$error_message] = $check_arr[self::$error_message];
|
||
}else{
|
||
self::$result[self::$error_code_name] = 10001;
|
||
}
|
||
}
|
||
}
|
||
return json(self::$result);
|
||
}
|
||
|
||
/**
|
||
* addData
|
||
* @return \think\response\Json
|
||
*/
|
||
public function addData()
|
||
{
|
||
if(self::$result[self::$error_code_name] == 0)
|
||
{
|
||
$search_model = new Search();
|
||
$field_arr = array(
|
||
array('field_name' => 'type','rule' => 'number'),
|
||
array('field_name' => 'keyword','rule' => ''),
|
||
array('field_name' => 'results','rule' => 'require'),
|
||
array('field_name' => 'task_id','rule' => 'number')
|
||
);
|
||
$check_arr = $this->checkHave($field_arr);
|
||
if($check_arr[self::$error_code_name] == 0)
|
||
{
|
||
$results = $check_arr[self::$data_name]['results'];
|
||
//$check_arr[self::$data_name]['results'] = $check_arr[self::$data_name]['results'] = '{"Data": {"tag2": 0,"tag3": 20,"tag4": {"continueFlag":1,"cookies":{"box_offset":0,"businessType":2,"cookies_buffer":"EgpuZXdzc2VhcmNo","doc_offset":0,"dup_bf":"","isHomepage":0,"page_cnt":0,"query":"iphone12","scene":22},"data":[{"boxID":"eb022b6a-5e5f-41f7-9695-8e746c6cadf4_0","count":20,"items":[{"date":1603635111,"desc":"经销商王绍在朋友圈写道。 他告诉「wise财经」,系列虽然骂的人居多,但...","docID":"7085246093426012770","doc_url":"http://mp.weixin.qq.com/s?subscene=22&__biz=MjM5MDYxMDA0MQ==&mid=2751292094&idx=5&sn=01f9feb3bb1af853cd2b5689acf1f23b&chksm=804be5a8b73c6cbe362f0b732f4e03bf8129011c4447fbd8d116076961ca9c62560ea6658708&scene=7#rd","insertFlag":0,"news_type":0,"source":{"dateTime":"2020-10-25","title":"好机友"},"srcUserName":"gh_e80203d95064","src_type":49,"thumbUrl":"http://mmbiz.qpic.cn/sz_mmbiz_jpg/6OrVdXH1Eibv3YzT5oodR2juuD78UuKzgkFEDZ0J8sl0wxX8QicbCIjwR4De2HE30mzzbTttmL4V5OIlDibgIXWoA/300","title":"被抢到涨价,真的是因为5G吗"}],"resultType":0,"subType":0,"title":"相关文章","totalCount":254,"type":2}],"direction":2,"filter":{"boxID":"eb022b6a-5e5f-41f7-9695-8e746c6cadf4_1","isHold":1,"tabs":[{"items":[{"paramKey":"docSuperType","paramValue":"0","selected":1,"tip":"不限"},{"paramKey":"docSuperType","paramValue":"3","tip":"最近读过"},{"paramKey":"docSuperType","paramValue":"1","tip":"由已关注的公众号发布"},{"paramKey":"docSuperType","paramValue":"2","tip":"朋友分享过"}],"title":"不限","type":1},{"items":[{"paramKey":"docSortType","paramValue":"0","tip":"综合排序"},{"paramKey":"docSortType","paramValue":"2","selected":1,"tip":"按发布时间排序"},{"paramKey":"docSortType","paramValue":"4","tip":"按阅读量排序"}],"title":"按发布时间排序","type":1}],"type":7},"isDivide":0,"isHomePage":0,"lang":"zh_CN","offset":20,"query":"iphone12","resultType":0,"ret":0,"searchID":"12886422554149847187"}},"wxid": null,"Success": true,"Code": "0","Message": "成功"}';
|
||
$results = $this->Handling_special_characters($results);
|
||
$json_arr = json_decode($results,true);
|
||
$search_add_rs = 0;
|
||
if (array_key_exists('keyword',$check_arr[self::$data_name]) && array_key_exists('type',$check_arr[self::$data_name]))
|
||
{
|
||
$md5_keyword = md5($check_arr[self::$data_name]['keyword']);
|
||
$search_where = array();
|
||
$search_where[$search_model::KEYWORD_MD5] = $md5_keyword;
|
||
$search_info = $search_model->getOneById($search_model,$search_where);
|
||
if (empty($search_info))
|
||
{
|
||
$search_data = array();
|
||
$search_data[$search_model::TYPE] = $check_arr[self::$data_name][$search_model::TYPE];
|
||
$search_data[$search_model::KEYWORD] = $check_arr[self::$data_name][$search_model::KEYWORD];
|
||
$search_data[$search_model::KEYWORD_MD5] = md5($check_arr[self::$data_name][$search_model::KEYWORD]);
|
||
$search_add_rs = $search_model->addInsertData($search_model,$search_data);
|
||
}else
|
||
{
|
||
$search_add_rs = $search_info[$search_model::AUTO_ID];
|
||
}
|
||
}
|
||
if (!empty($json_arr))
|
||
{
|
||
if ($json_arr['Success'])
|
||
{
|
||
$tmp_json_arr = $json_arr['Data']['tag4']['data'][0]['items'];
|
||
$content_id_arr = array();
|
||
foreach ($tmp_json_arr as $json_each_v)
|
||
{
|
||
$search_content_model = new SearchContent();
|
||
$search_content_where = array();
|
||
$search_content_where[$search_content_model::ARTICLE_ID] = $json_each_v['docID'];
|
||
$search_content_info = $search_content_model->getOneById($search_content_model,$search_content_where);
|
||
$content_id = 0;
|
||
if (!empty($search_content_info))
|
||
{
|
||
$content_id = $search_content_info[$search_content_model::AUTO_ID];
|
||
$search_content_where = array();
|
||
$search_content_where[$search_content_model::AUTO_ID] = $search_content_info[$search_content_model::AUTO_ID];
|
||
$search_content_data = array();
|
||
$search_content_data[$search_content_model::CONTENT] = json_encode($json_each_v);
|
||
$search_content_model->updateByWhere($search_content_model,$search_content_where,$search_content_data);
|
||
}else
|
||
{
|
||
$search_content_data = array();
|
||
$search_content_data[$search_content_model::ARTICLE_ID] = $json_each_v['docID'];
|
||
$search_content_data[$search_content_model::CONTENT] = json_encode($json_each_v);
|
||
$content_id = $search_content_model->addInsertData($search_content_model,$search_content_data);
|
||
}
|
||
$content_id_arr[] = $content_id;
|
||
if ($search_add_rs > 0)
|
||
{
|
||
$search_relation_model = new SearchRelation();
|
||
$search_relation_data = array();
|
||
$search_relation_where = array();
|
||
$search_relation_where[$search_relation_model::KEYWORD_ID] = $search_add_rs;
|
||
$search_relation_where[$search_relation_model::CONTENT_ID] = $content_id;
|
||
$search_relation_info = $search_relation_model->getOneById($search_relation_model,$search_relation_where);
|
||
if (empty($search_relation_info))
|
||
{
|
||
$search_relation_data[$search_relation_model::KEYWORD_ID] = $search_add_rs;
|
||
$search_relation_data[$search_relation_model::CONTENT_ID] = $content_id;
|
||
$search_relation_model->addInsertData($search_relation_model,$search_relation_data);
|
||
}
|
||
}
|
||
}
|
||
if (array_key_exists('task_id',$check_arr[self::$data_name]) && !empty($content_id_arr))
|
||
{
|
||
$task_id = $check_arr[self::$data_name]['task_id'];
|
||
$task_model = new Task();
|
||
$task_where = array();
|
||
$task_where[$task_model::AUTO_ID] = $task_id;
|
||
$task_info = $task_model->getOneById($task_model,$task_where);
|
||
if (!empty($task_info))
|
||
{
|
||
if (in_array($task_info[$task_model::TYPE],[6,7]))
|
||
{
|
||
$task_content_relation_model = new TaskContentRelation();
|
||
$more_data = array();
|
||
foreach ($content_id_arr as $content_each_v)
|
||
{
|
||
$tmp_arr = array();
|
||
$tmp_arr[$task_content_relation_model::TASK_ID] = $task_id;
|
||
$tmp_arr[$task_content_relation_model::CONTENT_ID] = $content_each_v;
|
||
$more_data[] = $tmp_arr;
|
||
}
|
||
$task_content_relation_model->addMoreData($task_content_relation_model,$more_data);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}else
|
||
{
|
||
//error
|
||
$analysis_error_model = new AnalysisError();
|
||
$analysis_error_data = array();
|
||
$analysis_error_data[$analysis_error_model::CONTENT] = $results;
|
||
$analysis_error_model->addInsertData($analysis_error_model,$analysis_error_data);
|
||
}
|
||
}else{
|
||
if($check_arr[self::$error_code_name] == 1)
|
||
{
|
||
self::$result[self::$error_code_name] = 10004;
|
||
self::$result[self::$error_message] = $check_arr[self::$error_message];
|
||
}else{
|
||
self::$result[self::$error_code_name] = 10001;
|
||
}
|
||
}
|
||
}
|
||
return json(self::$result);
|
||
}
|
||
|
||
|
||
/**
|
||
* Handling_special_characters
|
||
* @param $json_str
|
||
* @return mixed
|
||
*/
|
||
public function Handling_special_characters($json_str)
|
||
{
|
||
$json_str = str_replace('\\n','',$json_str);
|
||
$json_str = str_replace('\\\\n\\','',$json_str);
|
||
$json_str = str_replace('\\','',$json_str);
|
||
$json_str = str_replace('"{','{',$json_str);
|
||
$json_str = str_replace('}"','}',$json_str);
|
||
$json_str = str_replace('="','=',$json_str);
|
||
$json_str = str_replace('">','>',$json_str);
|
||
$json_str = stripslashes($json_str);
|
||
return $json_str;
|
||
}
|
||
|
||
|
||
} |