作者 刘锟

合并分支 'akun' 到 'master'

Akun



查看合并请求 !1108
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: zhl
  5 + * Date: 2025/2/13
  6 + * Time: 16:45
  7 + */
  8 +
  9 +namespace App\Console\Commands\Inquiry;
  10 +
  11 +use App\Helper\Translate;
  12 +use App\Models\Inquiry\InquiryInfo;
  13 +use App\Services\InquiryRelayService;
  14 +use Illuminate\Console\Command;
  15 +use Illuminate\Support\Facades\DB;
  16 +
  17 +class SyncInquiryRelay extends Command
  18 +{
  19 + /**
  20 + * The name and signature of the console command.
  21 + *
  22 + * @var string
  23 + */
  24 + protected $signature = 'sync_inquiry_relay';
  25 +
  26 + /**
  27 + * The console command description.
  28 + *
  29 + * @var string
  30 + */
  31 + protected $description = '同步询盘信息:站群询盘,';
  32 +
  33 + /**
  34 + * Create a new command instance.
  35 + *
  36 + * @return void
  37 + */
  38 + public function __construct()
  39 + {
  40 + parent::__construct();
  41 + }
  42 +
  43 +
  44 + public function handle()
  45 + {
  46 + $this->getInquiryForm();
  47 + $this->getInquirySzcm();
  48 + }
  49 +
  50 + /**
  51 + * 获取表单系统中询盘信息
  52 + * message_sign唯一值:md5(name+email+phone+ip+time)
  53 + */
  54 + public function getInquiryForm()
  55 + {
  56 + $service = new InquiryRelayService();
  57 + $result = $service->getInquiryRelay();
  58 + if (isset($result['status']) && $result['status'] == 200) {
  59 + $data = $result['data'] ?? [];
  60 + foreach ($data as $item) {
  61 + $this->saveDate($item, $item['source_type']);
  62 + }
  63 + }
  64 + }
  65 +
  66 + /**
  67 + * 获取秋哥asp采集询盘信息
  68 + * type固定4
  69 + * message_sign唯一值:md5(name+email+phone+ip+time)
  70 + * 先查数据库最大origin_key,再获取数据
  71 + */
  72 + public function getInquirySzcm()
  73 + {
  74 + $max_origin_key = InquiryInfo::where('type', 4)->orderBy('origin_key', 'desc')->value('origin_key');
  75 + if ($max_origin_key) {
  76 + $start_id = $max_origin_key + 1;
  77 + } else {
  78 + $start_id = 65029;
  79 + }
  80 +
  81 + $service = new InquiryRelayService();
  82 + while ($start_id > 0) {
  83 + $result = $service->getInquirySzcm($start_id);
  84 + if ($result) {
  85 + $this->saveDate($result, 4);
  86 + $start_id += 1;
  87 + } else {
  88 + $start_id = 0;
  89 + }
  90 + }
  91 + }
  92 +
  93 + public function saveDate($data, $type)
  94 + {
  95 + $model = new InquiryInfo();
  96 + $message_sign = md5($data['name'] . $data['email'] . $data['phone'] . $data['ip'] . $data['time']);
  97 + $inquiry_info = $model->where('message_sign', $message_sign)->first();
  98 + if (!$inquiry_info) {
  99 + //没有IP,根据submit_country获取
  100 + if (empty($data['ip']) && isset($data['submit_country']) && $data['submit_country']) {
  101 + $chinese_name = DB::table('gl_world_country_city')->where('pid', 0)->where('iso2', $data['submit_country'])->value('chinese_name');
  102 + $data['ip'] = $this->getIpData($chinese_name);
  103 + }
  104 +
  105 + //获取country
  106 + $country = '';
  107 + if ($data['ip']) {
  108 + $country = file_get_contents("http://ip.globalso.com?ip=" . $data['ip']);
  109 + }
  110 +
  111 + //翻译message
  112 + $message_cn = '';
  113 + $re_message_trans = Translate::translate($data['message'], 'zh');
  114 + if (isset($re_message_trans[0]['code']) && $re_message_trans[0]['code'] == 200) {
  115 + $message_cn = $re_message_trans[0]['texts'];
  116 + }
  117 +
  118 + //获取页面上title和keywords
  119 + $html = curl_c($data['refer'], false);
  120 +
  121 + if (empty($data['title'])) {
  122 + preg_match_all('/<title>([\w\W]*?)<\/title>/', $html, $matches);
  123 + if (!empty($matches[1])) {
  124 + $data['title'] = substr($matches[1][0], 0, 255);
  125 + }
  126 + }
  127 +
  128 + $keywords = '';
  129 + preg_match_all('/<meta\s+[^>]*?name=[\'|\"]keywords[\'|\"]\s+[^>]*?content=[\'|\"]([\w\W]*?)[\'|\"]/', $html, $matches);
  130 + if (!empty($matches[1])) {
  131 + $keywords = substr($matches[1][0], 0, 255);
  132 + }
  133 +
  134 + if (empty($data['origin_key'])) {
  135 + $data['origin_key'] = 0;
  136 + }
  137 +
  138 + if (empty($data['image'])) {
  139 + $data['image'] = '';
  140 + }
  141 +
  142 + $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']);
  143 + }
  144 + }
  145 +
  146 + /**
  147 + * 获取国家随机ip
  148 + * @param $country_name
  149 + * @return mixed|string
  150 + * @author Akun
  151 + * @date 2025/02/14 14:19
  152 + */
  153 + public function getIpData($country_name)
  154 + {
  155 + // 有国家 通过国家查询, 如果没有获取到就随机获取
  156 + $where = [];
  157 + $country_name && $where['ip_area'] = $country_name;
  158 + $ip = DB::table('gl_xunpan_ipdata')->where($where)->inRandomOrder()->value('ip');
  159 + if (empty($ip_data)) {
  160 + $ip = DB::table('gl_xunpan_ipdata')->inRandomOrder()->value('ip');
  161 + }
  162 + return $ip;
  163 + }
  164 +}
@@ -12,6 +12,12 @@ use App\Models\Base; @@ -12,6 +12,12 @@ use App\Models\Base;
12 */ 12 */
13 class InquiryInfo extends Base 13 class InquiryInfo extends Base
14 { 14 {
  15 + /**
  16 + * @var string 数据表
  17 + */
  18 + protected $table = 'gl_inquiry_info';
  19 +
  20 + // TODO 之前的状态, 留下可以找到之前的程序
15 const STATUS_ZERO = 0;//未转发 21 const STATUS_ZERO = 0;//未转发
16 const STATUS_ONE = 1;//无效 22 const STATUS_ONE = 1;//无效
17 23
@@ -21,5 +27,87 @@ class InquiryInfo extends Base @@ -21,5 +27,87 @@ class InquiryInfo extends Base
21 27
22 const STATUS_FOUR = 4;//延时转发中 28 const STATUS_FOUR = 4;//延时转发中
23 29
24 - protected $table = 'gl_inquiry_info'; 30 + const STATUS_INIT = 0;
  31 + const STATUS_FINISH = 1;
  32 + const STATUS_INVALID = 2;
  33 + const STATUS_FAIL = 9;
  34 +
  35 +
  36 + const TYPE_SITE_GROUP = 1;
  37 + const TYPE_ADS = 2;
  38 + const TYPE_AI_SITE_GROUP = 3;
  39 + const TYPE_SPIDER = 4;
  40 +
  41 + /**
  42 + * 状态映射
  43 + * @return array
  44 + */
  45 + public function statusMap()
  46 + {
  47 + return [
  48 + self::STATUS_INIT => '未处理',
  49 + self::STATUS_FINISH => '已处理',
  50 + self::STATUS_INVALID => '无效数据',
  51 + self::STATUS_FAIL => '失败数据',
  52 + ];
  53 + }
  54 +
  55 + /**
  56 + * 类型映射
  57 + * @return array
  58 + */
  59 + public function typeMap()
  60 + {
  61 + return [
  62 + self::TYPE_SITE_GROUP => '站群询盘',
  63 + self::TYPE_ADS => 'ads采集站询盘',
  64 + self::TYPE_AI_SITE_GROUP => 'AI站群询盘',
  65 + self::TYPE_SPIDER => '蜘蛛询盘',
  66 + ];
  67 + }
  68 +
  69 + /**
  70 + * 创建新盘信息
  71 + * @param $name
  72 + * @param $phone
  73 + * @param $email
  74 + * @param $ip
  75 + * @param $country
  76 + * @param $message
  77 + * @param $message_cn
  78 + * @param $type
  79 + * @param $inquiry_date
  80 + * @param $url
  81 + * @param $url_title
  82 + * @param $url_keyword
  83 + * @param $message_sign
  84 + * @param $origin_key
  85 + * @param string $image
  86 + * @return bool
  87 + */
  88 + public function createInquiry($name, $phone, $email, $ip, $country, $message, $message_cn, $type, $inquiry_date, $url, $url_title, $url_keyword, $message_sign, $origin_key, $image = '')
  89 + {
  90 + try {
  91 + $self = new self();
  92 + $self->name = $name;
  93 + $self->phone = $phone;
  94 + $self->email = $email;
  95 + $self->ip = $ip;
  96 + $self->country = $country;
  97 + $self->message = $message;
  98 + $self->message_cn = $message_cn;
  99 + $self->type = $type;
  100 + $self->inquiry_date = $inquiry_date;
  101 + $self->url = $url;
  102 + $self->url_title = $url_title;
  103 + $self->url_keyword = $url_keyword;
  104 + $self->image = $image;
  105 + $self->origin_key = $origin_key;
  106 + $self->message_sign = $message_sign;
  107 + $self->save();
  108 + return true;
  109 + } catch (\Exception $e) {
  110 + return false;
  111 + }
  112 + }
25 } 113 }
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: zhl
  5 + * Date: 2025/2/13
  6 + * Time: 11:01
  7 + */
  8 +namespace App\Services;
  9 +
  10 +use Illuminate\Support\Facades\Log;
  11 +
  12 +/**
  13 + * Class InquiryRelayService
  14 + * @package App\Services
  15 + */
  16 +class InquiryRelayService
  17 +{
  18 + /**
  19 + * 获取询盘
  20 + * @return mixed|string
  21 + */
  22 + public function getInquiryRelay()
  23 + {
  24 + $date = date('Y-m-d');
  25 + $token = md5($date . 'qqs');
  26 + $url = 'https://form.globalso.com/api/external-interface/echo_inquriy/d1483a8e57cb485a?date=' . $date . '&token=' . $token . '&type=2';
  27 + $result = http_get($url);
  28 + return $result;
  29 + }
  30 +
  31 + /**
  32 + * 获取询盘
  33 + * @param $id
  34 + * @return array|bool
  35 + */
  36 + public function getInquirySzcm($id)
  37 + {
  38 + try {
  39 + // 获取数据
  40 + $url = "https://api.szcmapi.com/get_inquiry.aspx?id=".$id;
  41 + $json = $this->szcmCurl($url);
  42 + // 兼容过去到的数据, 比较乱
  43 + $json = trim(str_replace("\r\n", '\n', (string) $json));
  44 + $json = str_replace('3/4"','3/4',$json);
  45 + $json = str_replace('"Steklopribor"','Steklopribor',$json);
  46 + $json = str_replace('"Net30 days"','Net30 days',$json);
  47 + $json = trim($json);
  48 + $json = str_replace("\n",'',$json);
  49 + $array = json_decode($json,true);
  50 +
  51 + if (empty($array))
  52 + return false;
  53 +
  54 + // 整合最终数据
  55 + $title = base64_decode($array['title']);
  56 + $title = str_replace("'",'',$title);
  57 + $title = html_entity_decode($title, ENT_QUOTES, 'UTF-8');
  58 + $title = str_replace("'",'',$title);
  59 + $message = html_entity_decode(addslashes(base64_decode($array['Message'])), ENT_QUOTES, 'UTF-8');
  60 + $message = str_replace("'",'',$message);
  61 + $email = trim($array['Email']);
  62 + $name = html_entity_decode($array['Name'], ENT_QUOTES, 'UTF-8');
  63 + $name = str_replace("'",'',$name);
  64 +
  65 + $result = [
  66 + 'image' => $array['image'] ?: '',
  67 + 'time' => $array['submit_time'] ?: date('Y-m-d H:i:s'),
  68 + 'name' => $name,
  69 + 'email' => $email,
  70 + 'phone' => $array['Phone'] ?: '',
  71 + 'refer' => $array['refer'] ?: '',
  72 + 'message' => $message,
  73 + 'ip' => $array['submit_ip'] ?: '',
  74 + 'source_address' => 'api.szcmapi.com',
  75 + 'title' => $title,
  76 + 'submit_country' => $array['submit_country'] ?: '',
  77 + 'origin_key' => $array['Id'],
  78 + ];
  79 + return $result;
  80 + } catch (\Exception $e) {
  81 + Log::error('获取询盘: getInquirySzcm : ' . $id . ', error: ' . $e->getMessage());
  82 + return false;
  83 + }
  84 + }
  85 +
  86 + /**
  87 + * @param $url
  88 + * @return bool|string
  89 + */
  90 + public function szcmCurl($url)
  91 + {
  92 + $agent = 'ChuangMao_API_Bot';
  93 + $ch = curl_init($url);
  94 + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  95 + curl_setopt($ch, CURLOPT_HEADER, false);
  96 + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
  97 + curl_setopt($ch, CURLOPT_USERAGENT, $agent);
  98 + curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  99 + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
  100 + curl_setopt($ch, CURLOPT_TIMEOUT, 120);
  101 + curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
  102 + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  103 + curl_setopt($ch, CURLOPT_SSLVERSION, 'all');
  104 + $result = curl_exec($ch);
  105 + curl_close($ch);
  106 + return $result;
  107 + }
  108 +}