作者 lyh

Merge branch 'master' of http://47.244.231.31:8099/zhl/globalso-v6 into master-server

<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2025/2/13
* Time: 16:45
*/
namespace App\Console\Commands\Inquiry;
use App\Helper\Translate;
use App\Models\Inquiry\InquiryInfo;
use App\Services\InquiryRelayService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class SyncInquiryRelay extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'sync_inquiry_relay';
/**
* The console command description.
*
* @var string
*/
protected $description = '同步询盘信息:站群询盘,';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->getInquiryForm();
$this->getInquirySzcm();
}
/**
* 获取表单系统中询盘信息
* message_sign唯一值:md5(name+email+phone+ip+time)
*/
public function getInquiryForm()
{
$service = new InquiryRelayService();
$result = $service->getInquiryRelay();
if (isset($result['status']) && $result['status'] == 200) {
$data = $result['data'] ?? [];
foreach ($data as $item) {
$this->saveDate($item, $item['source_type']);
}
}
}
/**
* 获取秋哥asp采集询盘信息
* type固定4
* message_sign唯一值:md5(name+email+phone+ip+time)
* 先查数据库最大origin_key,再获取数据
*/
public function getInquirySzcm()
{
$max_origin_key = InquiryInfo::where('type', 4)->orderBy('origin_key', 'desc')->value('origin_key');
if ($max_origin_key) {
$start_id = $max_origin_key + 1;
} else {
$start_id = 65029;
}
$service = new InquiryRelayService();
while ($start_id > 0) {
$result = $service->getInquirySzcm($start_id);
if ($result) {
$this->saveDate($result, 4);
$start_id += 1;
} else {
$start_id = 0;
}
}
}
public function saveDate($data, $type)
{
$model = new InquiryInfo();
$message_sign = md5($data['name'] . $data['email'] . $data['phone'] . $data['ip'] . $data['time']);
$inquiry_info = $model->where('message_sign', $message_sign)->first();
if (!$inquiry_info) {
//没有IP,根据submit_country获取
if (empty($data['ip']) && isset($data['submit_country']) && $data['submit_country']) {
$chinese_name = DB::table('gl_world_country_city')->where('pid', 0)->where('iso2', $data['submit_country'])->value('chinese_name');
$data['ip'] = $this->getIpData($chinese_name);
}
//获取country
$country = '';
if ($data['ip']) {
$country = file_get_contents("http://ip.globalso.com?ip=" . $data['ip']);
}
//翻译message
$message_cn = '';
$re_message_trans = Translate::translate($data['message'], 'zh');
if (isset($re_message_trans[0]['code']) && $re_message_trans[0]['code'] == 200) {
$message_cn = $re_message_trans[0]['texts'];
}
//获取页面上title和keywords
$html = curl_c($data['refer'], false);
if (empty($data['title'])) {
preg_match_all('/<title>([\w\W]*?)<\/title>/', $html, $matches);
if (!empty($matches[1])) {
$data['title'] = substr($matches[1][0], 0, 255);
}
}
$keywords = '';
preg_match_all('/<meta\s+[^>]*?name=[\'|\"]keywords[\'|\"]\s+[^>]*?content=[\'|\"]([\w\W]*?)[\'|\"]/', $html, $matches);
if (!empty($matches[1])) {
$keywords = substr($matches[1][0], 0, 255);
}
if (empty($data['origin_key'])) {
$data['origin_key'] = 0;
}
if (empty($data['image'])) {
$data['image'] = '';
}
$model->createInquiry($data['name'], $data['phone'], $data['email'], $data['ip'], $country, $data['message'], $message_cn, $type, $data['time'], $data['refer'], $data['title'], $keywords, $message_sign, $data['origin_key'], $data['image']);
}
}
/**
* 获取国家随机ip
* @param $country_name
* @return mixed|string
* @author Akun
* @date 2025/02/14 14:19
*/
public function getIpData($country_name)
{
// 有国家 通过国家查询, 如果没有获取到就随机获取
$where = [];
$country_name && $where['ip_area'] = $country_name;
$ip = DB::table('gl_xunpan_ipdata')->where($where)->inRandomOrder()->value('ip');
if (empty($ip_data)) {
$ip = DB::table('gl_xunpan_ipdata')->inRandomOrder()->value('ip');
}
return $ip;
}
}
... ...
... ... @@ -234,7 +234,7 @@ class Common
//过滤特殊字符
public static function deal_str($str){
$str = str_replace([',,'],',',$str);
return str_replace(['{','}','”','“','"'],'',$str);
return str_replace(['{','}','”','“','"', '**SEO Title:**'],'',$str);
}
/**
... ...
... ... @@ -12,6 +12,12 @@ use App\Models\Base;
*/
class InquiryInfo extends Base
{
/**
* @var string 数据表
*/
protected $table = 'gl_inquiry_info';
// TODO 之前的状态, 留下可以找到之前的程序
const STATUS_ZERO = 0;//未转发
const STATUS_ONE = 1;//无效
... ... @@ -21,5 +27,87 @@ class InquiryInfo extends Base
const STATUS_FOUR = 4;//延时转发中
protected $table = 'gl_inquiry_info';
const STATUS_INIT = 0;
const STATUS_FINISH = 1;
const STATUS_INVALID = 2;
const STATUS_FAIL = 9;
const TYPE_SITE_GROUP = 1;
const TYPE_ADS = 2;
const TYPE_AI_SITE_GROUP = 3;
const TYPE_SPIDER = 4;
/**
* 状态映射
* @return array
*/
public function statusMap()
{
return [
self::STATUS_INIT => '未处理',
self::STATUS_FINISH => '已处理',
self::STATUS_INVALID => '无效数据',
self::STATUS_FAIL => '失败数据',
];
}
/**
* 类型映射
* @return array
*/
public function typeMap()
{
return [
self::TYPE_SITE_GROUP => '站群询盘',
self::TYPE_ADS => 'ads采集站询盘',
self::TYPE_AI_SITE_GROUP => 'AI站群询盘',
self::TYPE_SPIDER => '蜘蛛询盘',
];
}
/**
* 创建新盘信息
* @param $name
* @param $phone
* @param $email
* @param $ip
* @param $country
* @param $message
* @param $message_cn
* @param $type
* @param $inquiry_date
* @param $url
* @param $url_title
* @param $url_keyword
* @param $message_sign
* @param $origin_key
* @param string $image
* @return bool
*/
public function createInquiry($name, $phone, $email, $ip, $country, $message, $message_cn, $type, $inquiry_date, $url, $url_title, $url_keyword, $message_sign, $origin_key, $image = '')
{
try {
$self = new self();
$self->name = $name;
$self->phone = $phone;
$self->email = $email;
$self->ip = $ip;
$self->country = $country;
$self->message = $message;
$self->message_cn = $message_cn;
$self->type = $type;
$self->inquiry_date = $inquiry_date;
$self->url = $url;
$self->url_title = $url_title;
$self->url_keyword = $url_keyword;
$self->image = $image;
$self->origin_key = $origin_key;
$self->message_sign = $message_sign;
$self->save();
return true;
} catch (\Exception $e) {
return false;
}
}
}
... ...
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2025/2/13
* Time: 11:01
*/
namespace App\Services;
use Illuminate\Support\Facades\Log;
/**
* Class InquiryRelayService
* @package App\Services
*/
class InquiryRelayService
{
/**
* 获取询盘
* @return mixed|string
*/
public function getInquiryRelay()
{
$date = date('Y-m-d');
$token = md5($date . 'qqs');
$url = 'https://form.globalso.com/api/external-interface/echo_inquriy/d1483a8e57cb485a?date=' . $date . '&token=' . $token . '&type=2';
$result = http_get($url);
return $result;
}
/**
* 获取询盘
* @param $id
* @return array|bool
*/
public function getInquirySzcm($id)
{
try {
// 获取数据
$url = "https://api.szcmapi.com/get_inquiry.aspx?id=".$id;
$json = $this->szcmCurl($url);
// 兼容过去到的数据, 比较乱
$json = trim(str_replace("\r\n", '\n', (string) $json));
$json = str_replace('3/4"','3/4',$json);
$json = str_replace('"Steklopribor"','Steklopribor',$json);
$json = str_replace('"Net30 days"','Net30 days',$json);
$json = trim($json);
$json = str_replace("\n",'',$json);
$array = json_decode($json,true);
if (empty($array))
return false;
// 整合最终数据
$title = base64_decode($array['title']);
$title = str_replace("'",'',$title);
$title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
$title = str_replace("'",'',$title);
$message = html_entity_decode(addslashes(base64_decode($array['Message'])), ENT_QUOTES, 'UTF-8');
$message = str_replace("'",'',$message);
$email = trim($array['Email']);
$name = html_entity_decode($array['Name'], ENT_QUOTES, 'UTF-8');
$name = str_replace("'",'',$name);
$result = [
'image' => $array['image'] ?: '',
'time' => $array['submit_time'] ?: date('Y-m-d H:i:s'),
'name' => $name,
'email' => $email,
'phone' => $array['Phone'] ?: '',
'refer' => $array['refer'] ?: '',
'message' => $message,
'ip' => $array['submit_ip'] ?: '',
'source_address' => 'api.szcmapi.com',
'title' => $title,
'submit_country' => $array['submit_country'] ?: '',
'origin_key' => $array['Id'],
];
return $result;
} catch (\Exception $e) {
Log::error('获取询盘: getInquirySzcm : ' . $id . ', error: ' . $e->getMessage());
return false;
}
}
/**
* @param $url
* @return bool|string
*/
public function szcmCurl($url)
{
$agent = 'ChuangMao_API_Bot';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSLVERSION, 'all');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
}
\ No newline at end of file
... ...