You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
information-system/application/third_party/tpmanage/controllers/webgetbokun.php

410 lines
18 KiB
PHTML

<?php
/**
* 网前用的抓取程序,主要是单独处理抓取的单个产品
*/
defined('BASEPATH') or exit('No direct script access allowed');
class webgetbokun extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->library("Bokun_lib");
$this->load->model("tpBokun_model");
$this->load->helper("text");
$this->load->model("InfoBokunData_model");
}
public function index()
{
}
/**
* @description: 检查是否是新的bokun产品根据infobokundata表的activityid记录
* @param {type}
* @return:
* @Date Changed:
*/
function checkActivityId($activityid)
{
$result = $this->tpBokun_model->checkActivityId($activityid);
// print_r($result);
echo $result;
return $result;
}
/**
* @description: 前端根据activityid抓取具体的产品信息并根据传递过来pag_code保存数据到信息平台
* @param string $PassParam 传递过来的参数base64
* @return:
* @Date Changed:
*/
function addActivity($PassParam)
{
$ParamData = json_decode(base64_decode($PassParam));
//print_r($ParamData);
$activityid = $ParamData->activityid;
$PAG_Code = $ParamData->PAG_Code;
$CityName = $ParamData->CityName;
$checkCode = $ParamData->checkCode;
if ($checkCode != "cits") {
return "Error"; //传递的验证code错误
} else {
//1.根据城市名称获取对应的is_id作为 父类ID
$is_id = $this->get_isid_byCity($CityName);
//2.根据$activityid 抓取接口数据
$activityDeatil = json_decode($this->bokun_lib->get_activity_detail($activityid));
$title = $activityDeatil->title;
$title_url = str_replace("'", "", strtolower($title));
$title_url = str_replace(":", "", $title_url);
$title_url = str_replace("(", " ", $title_url);
$title_url = str_replace(")", " ", $title_url);
$title_url = str_replace("/", " ", $title_url);
$title_url = str_replace("\\", " ", $title_url);
$title_url = str_replace(" ", " ", $title_url);
$title_url = str_replace(' ', '-', $title_url);
$CityName_url = str_replace("'", "", str_replace(' ', '-', strtolower($CityName)));
//3.根据抓取数据进行信息表infocontent和infostructures表的添加
$this->tpBokun_model->ic_title = $title;
$this->tpBokun_model->ic_url_title = $title;
$guideUrl = "/$CityName_url-tours/$title_url/";
$this->tpBokun_model->ic_url = $guideUrl;
//抓取数据组合成详细内容。
$arrContent = $activityDeatil->agendaItems;
$strContent = "";
foreach ($arrContent as $ContentItem) {
$strTemp = "";
if ($ContentItem->title != "") {
$strTemp .= "<h3><i class=\"fa fa-circle\" aria-hidden=\"true\"></i>" . $ContentItem->title . "</h3>";
}
if ($ContentItem->body != "") {
$strTemp .= $ContentItem->body;
}
if ($ContentItem->excerpt != "") {
$strTemp .= "<p>" . $ContentItem->excerpt . "</p>";
}
if ($strTemp != "") {
$strContent .= "<div class=\"details-list\">" . $strTemp . "</div>";
}
}
$this->tpBokun_model->ic_content = $strContent;
//简介
$temp_summary = $activityDeatil->description;
if ($activityDeatil->excerpt != "") {
$temp_summary .= "<p>" . $activityDeatil->excerpt . "</p>";
}
$this->tpBokun_model->ic_summary = $temp_summary;
//seo
$this->tpBokun_model->ic_seo_title = character_limiter($title, 100);
$this->tpBokun_model->ic_seo_description = character_limiter(strip_tags($activityDeatil->description), 200);
$tempKeywords = $activityDeatil->keywords;
if (is_array($tempKeywords) && count($tempKeywords) > 0) {
$this->tpBokun_model->ic_seo_keywords = implode(",", $tempKeywords);
}
$this->tpBokun_model->ic_status = 1;
$tempData = json_decode($this->tpBokun_model->addInfo($is_id));
if (is_array($tempData)) {
$ic_id = $tempData[0]->value->ic_id;
} else {
$result["status"] = "error";
echo json_encode($result);
}
//4.附加属性添加infometa表及新建的infoBokunData表
if ($ic_id > 0) {
//先旧的infometa表
$tempRequireFileds = $activityDeatil->requiredCustomerFields; //根据抓取的需求字段判断是否需要passport
if (is_array($tempRequireFileds)) {
if (in_array("passportId", $tempRequireFileds[0])) {
$this->tpBokun_model->addMeta($ic_id, "meta_trippest_type", "gg");
}
}
$this->tpBokun_model->addMeta($ic_id, "meta_product_code", $PAG_Code); //对应翰特编码
$this->tpBokun_model->addMeta($ic_id, "meta_facts_city", $activityDeatil->googlePlace->city); //保存一下具体城市
//然后保存新建的infobokunData表
$this->InfoBokunData_model->ibd_ic_id = $ic_id; //统一的
$this->InfoBokunData_model->ibd_ItemName = "ActivityID";
$this->InfoBokunData_model->ibd_Itemvalue = $activityid;
$ibd_id = $this->InfoBokunData_model->Add();
$this->InfoBokunData_model->ibd_parentId = $ibd_id; //设置剩余的属性都是这个activityid的子类避免每条记录都保存activityid
$this->InfoBokunData_model->ibd_ItemName = "keyPhoto"; //主要图片
$this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->keyPhoto->originalUrl;
$this->InfoBokunData_model->ibd_ItemType = "originalUrl";
$this->InfoBokunData_model->Add();
$arrKeyPhoto = $activityDeatil->keyPhoto->derived; //主要图片派生图,大中小类型
if (is_array($arrKeyPhoto)) {
foreach ($arrKeyPhoto as $kPhotoItem) {
$this->InfoBokunData_model->ibd_Itemvalue = $kPhotoItem->url;
$this->InfoBokunData_model->ibd_ItemType = $kPhotoItem->name;
$this->InfoBokunData_model->Add();
}
}
$arrProductPhoto = $activityDeatil->photos; //产品主要图片
if (is_array($arrProductPhoto)) {
foreach ($arrProductPhoto as $pPhotoItem) {
$this->InfoBokunData_model->ibd_ItemName = "ProductPhoto";
$this->InfoBokunData_model->ibd_Itemvalue = $pPhotoItem->originalUrl;
$this->InfoBokunData_model->ibd_ItemType = "originalUrl";
$this->InfoBokunData_model->Add();
$arrProductPhotoDerived = $pPhotoItem->derived; //产品主要图片的派生图,大中小类型
if (is_array($arrProductPhotoDerived)) {
foreach ($arrProductPhotoDerived as $pPhotoDerivedItem) {
$this->InfoBokunData_model->ibd_Itemvalue = $pPhotoDerivedItem->url;
$this->InfoBokunData_model->ibd_ItemType = $pPhotoDerivedItem->name;
$this->InfoBokunData_model->Add();
}
}
}
}
//一些FAQ类的属性如includedrequirements
$this->InfoBokunData_model->ibd_ItemName = "included"; //
$this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->included;
$this->InfoBokunData_model->ibd_ItemType = "";
$this->InfoBokunData_model->Add();
$this->InfoBokunData_model->ibd_ItemName = "excluded"; //
$this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->excluded;
$this->InfoBokunData_model->ibd_ItemType = "";
$this->InfoBokunData_model->Add();
$this->InfoBokunData_model->ibd_ItemName = "requirements"; //
$this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->requirements;
$this->InfoBokunData_model->ibd_ItemType = "";
$this->InfoBokunData_model->Add();
$this->InfoBokunData_model->ibd_ItemName = "attention"; //
$this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->attention;
$this->InfoBokunData_model->ibd_ItemType = "";
$this->InfoBokunData_model->Add();
$this->InfoBokunData_model->ibd_ItemName = "country"; //
$this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->googlePlace->country;
$this->InfoBokunData_model->ibd_ItemType = "";
$this->InfoBokunData_model->Add();
$this->InfoBokunData_model->ibd_ItemName = "city"; //
$this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->googlePlace->city;
$this->InfoBokunData_model->ibd_ItemType = "";
$this->InfoBokunData_model->Add();
$this->InfoBokunData_model->ibd_ItemName = "durationText"; //
$this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->durationText;
$this->InfoBokunData_model->ibd_ItemType = "";
$this->InfoBokunData_model->Add();
if ($activityDeatil->noPickupMsg != null && $activityDeatil->noPickupMsg != "") {
$this->InfoBokunData_model->ibd_ItemName = "noPickupMsg"; //
$this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->noPickupMsg;
$this->InfoBokunData_model->ibd_ItemType = "";
$this->InfoBokunData_model->Add();
}
if ($activityDeatil->ticketMsg != null && $activityDeatil->ticketMsg != "") {
$this->InfoBokunData_model->ibd_ItemName = "ticketMsg"; //
$this->InfoBokunData_model->ibd_Itemvalue = $activityDeatil->ticketMsg;
$this->InfoBokunData_model->ibd_ItemType = "";
$this->InfoBokunData_model->Add();
}
$arrAgendaItems = $activityDeatil->agendaItems; //保存产品行程里面的派生图片
if (is_array($arrAgendaItems)) {
foreach ($arrAgendaItems as $agendaItem) {
if ($agendaItem->keyPhoto != null && $agendaItem->keyPhoto != "") {
$this->InfoBokunData_model->ibd_ItemName = "AgendaPhoto";
$this->InfoBokunData_model->ibd_Itemvalue = $agendaItem->keyPhoto->originalUrl;
$this->InfoBokunData_model->ibd_ItemDescription = $$agendaItem->keyPhoto->description;
$this->InfoBokunData_model->ibd_ItemType = "originalUrl";
$this->InfoBokunData_model->Add();
}
$arrAgendaItemPhotos = $agendaItem->photos;
if (is_array($arrAgendaItemPhotos)) {
foreach ($arrAgendaItemPhotos as $agendaPhotoItem) {
$this->InfoBokunData_model->ibd_ItemName = "AgendaPhoto";
$this->InfoBokunData_model->ibd_Itemvalue = $agendaPhotoItem->originalUrl;
$this->InfoBokunData_model->ibd_ItemType = "originalUrl";
$this->InfoBokunData_model->ibd_ItemDescription = $agendaPhotoItem->description;
$this->InfoBokunData_model->Add();
}
}
}
}
} else {
$result["status"] = "error";
echo json_encode($result);
}
//5. 返回成功信息
$result["status"] = "ok";
$result["ic_id"] = $ic_id;
echo json_encode($result);
}
}
/**
* @description: 根据城市名称获取对应的is_id判断产品放到信息平台的位置
* @param {type}
* @return: int is_id :城市所在信息平台的Id
* @Date Changed:
*/
function get_isid_byCity($CityName)
{
$cityUrl = "/" . str_replace("'", "", str_replace(' ', '-', strtolower($CityName))) . "-" . "tours/";
$row = $this->tpBokun_model->getInfomationAll($cityUrl);
if (isset($row)) {
$result = $row->is_id;
} else {
$result = $this->tpBokun_model->is_topId;
}
return $result;
}
/**
* @description: 为了方便,首先循环添加所有的城市目录
* @param {type}
* @return:
* @Date Changed:
*/
function checkAllCity()
{
$str_return = "";
$result = $this->bokun_lib->get_all_product_list();
//print_r($result);
$result = json_decode($result);
if (is_array($result) && count($result) > 0) {
//productlist是所有产品按城市分类然后child里面是类别分类
foreach ($result as $pItem) {
$cityName = $pItem->title; //城市名称,用来判断信息平台是否有该城市
if (isset($cityName)) {
$infoCity = json_decode($this->doInfoCity($cityName)); //获取导入的城市名称为父类,下面的产品在信息平台里面放在这个的下面
if ($infoCity->type == "new") {
$str_return .= $infoCity->cityname . ",";
}
}
}
}
if ($str_return != "") {
echo "新城市添加:" . $str_return."\r\n";
} else {
echo "没有新的城市"."\r\n";
}
}
/**
* @description: 判断信息平台是否有该城市的记录,没有则添加。/beijing-tours/
* @param {type}
* @return: 返回info的is_id ,作为父类ID
* @Date Changed:
*/
function doInfoCity($CityName)
{
$cityUrl = "/" . str_replace("'", "", str_replace(' ', '-', strtolower($CityName))) . "-" . "tours/";
$infoDetail = $this->tpBokun_model->getInfomationAll($cityUrl);
if (isset($infoDetail)) {
//如果存在
$result["Id"] = $infoDetail->is_id;
$result["cityname"] = $infoDetail->ic_title;
$result["type"] = "old";
} else {
//如果不存在就录入
$this->tpBokun_model->ic_title = ucwords($CityName) . " Tours";
$this->tpBokun_model->ic_url_title = ucwords($CityName) . " Tours";
$this->tpBokun_model->ic_url = $cityUrl;
$tempData = json_decode($this->tpBokun_model->addInfo($this->tpBokun_model->is_topId));
if (is_array($tempData)) {
$result["Id"] = $tempData[0]->value->is_id;
$result["cityname"] = $CityName;
$result["type"] = "new";
} else {
$result["Id"] = 0;
$result["cityname"] = "";
$result["type"] = "error";
}
}
return json_encode($result);
}
/**
* @description: 网前只抓取信息平台的信息。由于产品的翰特编码都是yong bk_activityid的规则组成所以分开更新是可能的。这样可以保证不需要理会本地和网前的处理速度
* @param sring $city : 城市名称 。用来单独更新某个城市
* @return:
* @Date Changed:
*/
function onlyUpdateInfo($city = "")
{
//先进行对应城市的更新
$this->checkAllCity();
//循环接口开始
$result = $this->bokun_lib->get_all_product_list();
//print_r($result);
$result = json_decode($result);
if (is_array($result) && count($result) > 0) {
//productlist是所有产品按城市分类然后child里面是类别分类
foreach ($result as $pItem) {
$cityName = str_replace("'", "", $pItem->title); //城市名称,用来判断信息平台是否有该城市
if (isset($cityName)) {
if ($city != "" && strtolower($city) != strtolower($cityName)) {
continue; //如果设置了指定抓取城市
} else {
$list_id = $pItem->id;
$pchild_Detail = $this->bokun_lib->get_product_list_detail($list_id);
$data_pchild_Detail = json_decode($pchild_Detail); //类别详细
foreach ($data_pchild_Detail->items as $pcdItem) { //循环类别
$activityData = $pcdItem->activity; //产品详细内容
//解析后获取需要的参数
$activityId = $activityData->id; //产品的activityId可以获取对应的产品详细内容。
//先判断是否是新产品
$checkNew = json_decode($this->tpBokun_model->checkActivityId($activityId));
if ($checkNew->return == "yes") {
continue;
}
//提交网前接口
$PAGCode = "bk_" . $activityId;
$checkCode = "cits"; //用来防止参数被破解
$PassParam["activityid"] = $activityId;
$PassParam["PAG_Code"] = $PAGCode;
$PassParam["CityName"] = $cityName;
$PassParam["checkCode"] = $checkCode;
$strPass = base64_encode(json_encode($PassParam));
$this->addActivity($strPass);
echo "\r\n" ;
}
}
}
}
echo ("抓取完成!\r\n");
} else {
echo "无接口数据!\r\n";
}
}
}
/* End of file webgetbokun.php */