正在显示
1 个修改的文件
包含
969 行增加
和
0 行删除
| 1 | +<?php | ||
| 2 | +/** | ||
| 3 | + * Created by PhpStorm. | ||
| 4 | + * User: zhl | ||
| 5 | + * Date: 2024/09/30 | ||
| 6 | + * Time: 11:51 | ||
| 7 | + */ | ||
| 8 | +namespace App\Console\Commands\Inquiry; | ||
| 9 | + | ||
| 10 | +use App\Helper\Common; | ||
| 11 | +use App\Helper\Gpt; | ||
| 12 | +use App\Helper\Translate; | ||
| 13 | +use App\Helper\Validate; | ||
| 14 | +use App\Models\Ai\AiCommand; | ||
| 15 | +use App\Models\Inquiry\ReInquiryConfig; | ||
| 16 | +use App\Models\Inquiry\ReInquiryDetail; | ||
| 17 | +use App\Models\Inquiry\ReInquiryDetailLog; | ||
| 18 | +use App\Models\Inquiry\ReInquiryForm; | ||
| 19 | +use App\Models\Inquiry\ReInquiryTask; | ||
| 20 | +use App\Models\Inquiry\ReInquiryText; | ||
| 21 | +use App\Models\Project\InquiryFilterConfig; | ||
| 22 | +use App\Models\Project\Project; | ||
| 23 | +use App\Models\WebSetting\WebLanguage; | ||
| 24 | +use Illuminate\Console\Command; | ||
| 25 | +use Illuminate\Support\Arr; | ||
| 26 | +use Illuminate\Support\Facades\Cache; | ||
| 27 | +use Illuminate\Support\Facades\DB; | ||
| 28 | +use Illuminate\Support\Facades\Http; | ||
| 29 | +use Illuminate\Support\Facades\Log; | ||
| 30 | +use Illuminate\Support\Str; | ||
| 31 | + | ||
| 32 | +/** | ||
| 33 | + * Class RelayInquiry | ||
| 34 | + * @package App\Console\Commands\Inquiry | ||
| 35 | + */ | ||
| 36 | +class RelayInquiryTest extends Command | ||
| 37 | +{ | ||
| 38 | + /** | ||
| 39 | + * The name and signature of the console command. | ||
| 40 | + * | ||
| 41 | + * @var string | ||
| 42 | + */ | ||
| 43 | + protected $signature = 'relay_inquiry_test'; | ||
| 44 | + | ||
| 45 | + /** | ||
| 46 | + * The console command description. | ||
| 47 | + * | ||
| 48 | + * @var string | ||
| 49 | + */ | ||
| 50 | + protected $description = '重启失败的转发询盘'; | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * Create a new command instance. | ||
| 54 | + * | ||
| 55 | + * @return void | ||
| 56 | + */ | ||
| 57 | + public function __construct() | ||
| 58 | + { | ||
| 59 | + parent::__construct(); | ||
| 60 | + } | ||
| 61 | + | ||
| 62 | + /** | ||
| 63 | + * 模拟访问来源占比 | ||
| 64 | + * @var array | ||
| 65 | + */ | ||
| 66 | + protected $lyzb = [ | ||
| 67 | + 'https://www.google.com/' => 630, | ||
| 68 | + 'http://www.google.com/' => 30, | ||
| 69 | + 'http://www.bing.com/' => 20, | ||
| 70 | + 'https://www.bing.com/' => 5, | ||
| 71 | + 'https://www.youtube.com/' => 5, | ||
| 72 | + 'https://search.yahoo.com/' => 5, | ||
| 73 | + 'https://www.facebook.com/' => 5, | ||
| 74 | + ]; | ||
| 75 | + | ||
| 76 | + /** | ||
| 77 | + * 俄语站 模拟访问来源占比 | ||
| 78 | + * @var array | ||
| 79 | + */ | ||
| 80 | + protected $eylyzb = [ | ||
| 81 | + 'https://www.yandex.com/' => 630, | ||
| 82 | + 'https://www.google.com/' => 30, | ||
| 83 | + 'http://www.google.com/' => 30, | ||
| 84 | + 'http://www.bing.com/' => 20, | ||
| 85 | + 'https://www.bing.com/' => 5, | ||
| 86 | + 'https://www.youtube.com/' => 5, | ||
| 87 | + 'https://search.yahoo.com/' => 5, | ||
| 88 | + 'https://www.facebook.com/' => 5, | ||
| 89 | + ]; | ||
| 90 | + | ||
| 91 | + /** | ||
| 92 | + * PC端访问头信息 | ||
| 93 | + * @var array | ||
| 94 | + */ | ||
| 95 | + protected $pc_ua = [ | ||
| 96 | + 0 => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11', | ||
| 97 | + 1 => 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36', | ||
| 98 | + 2 => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1' | ||
| 99 | + ]; | ||
| 100 | + | ||
| 101 | + /** | ||
| 102 | + * 移动端访问头信息 | ||
| 103 | + * @var array | ||
| 104 | + */ | ||
| 105 | + protected $mobile_ua = [ | ||
| 106 | + 0 => 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko; googleweblight) Chrome/38.0.1025.166 Mobile Safari/535.19', | ||
| 107 | + ]; | ||
| 108 | + | ||
| 109 | + /** | ||
| 110 | + * google域名后缀 | ||
| 111 | + * @var string[] | ||
| 112 | + */ | ||
| 113 | + protected $suffix = [ | ||
| 114 | + 'co.jp' => '日本', | ||
| 115 | + 'com.tr' => '土耳其', | ||
| 116 | + 'nl' => '荷兰', | ||
| 117 | + 'ru' => '俄罗斯', | ||
| 118 | + 'fr' => '法国', | ||
| 119 | + 'co.kr' => '韩国', | ||
| 120 | + 'fi' => '芬兰', | ||
| 121 | + 'be' => '比利时', | ||
| 122 | + 'lt' => '立陶宛', | ||
| 123 | + 'es' => '西班牙', | ||
| 124 | + 'it' => '意大利', | ||
| 125 | + 'com.au' => '澳大利亚', | ||
| 126 | + 'no' => '挪威', | ||
| 127 | + 'al' => '阿尔巴尼亚', | ||
| 128 | + 'pt' => '葡萄牙', | ||
| 129 | + 'lv' => '拉脱维亚', | ||
| 130 | + 'hu' => '匈牙利', | ||
| 131 | + 'cz' => '捷克', | ||
| 132 | + 'de' => '德国', | ||
| 133 | + 'ca' => '加拿大', | ||
| 134 | + 'co.in' => '印度', | ||
| 135 | + 'co.uk' => '英国', | ||
| 136 | + 'com.vn' => '越南', | ||
| 137 | + 'com.br' => '巴西', | ||
| 138 | + 'co.il' => '以色列', | ||
| 139 | + 'pl' => '波兰', | ||
| 140 | + 'com.eg' => '埃及', | ||
| 141 | + 'co.th' => '泰国', | ||
| 142 | + 'sk' => '斯洛伐克', | ||
| 143 | + 'ro' => '罗马尼亚', | ||
| 144 | + 'com.mx' => '墨西哥', | ||
| 145 | + 'com.my' => '马来西亚', | ||
| 146 | + 'com.pk' => '巴基斯坦', | ||
| 147 | + 'co.nz' => '新西兰', | ||
| 148 | + 'co.za' => '南非', | ||
| 149 | + 'com.ar' => '阿根廷', | ||
| 150 | + 'com.kw' => '科威特', | ||
| 151 | + 'com.sg' => '新加坡', | ||
| 152 | + 'com.co' => '哥伦比亚', | ||
| 153 | + 'co.id' => '印度尼西亚', | ||
| 154 | + 'gr' => '希腊', | ||
| 155 | + 'bg' => '保加利亚', | ||
| 156 | + 'mn' => '蒙古', | ||
| 157 | + 'dk' => '丹麦', | ||
| 158 | + 'com.sa' => '沙特阿拉伯', | ||
| 159 | + 'com.pe' => '秘鲁', | ||
| 160 | + 'com.ph' => '菲律宾', | ||
| 161 | + 'com.ua' => '乌克兰', | ||
| 162 | + 'ge' => '格鲁吉亚', | ||
| 163 | + 'ae' => '阿拉伯联合酋长国', | ||
| 164 | + 'tn' => '突尼斯', | ||
| 165 | + ]; | ||
| 166 | + | ||
| 167 | + protected $otherzb = [700, 300]; //模拟访问来源占比 (非美国) google.com|google.其他后缀 | ||
| 168 | + | ||
| 169 | + /** | ||
| 170 | + * 手机号过滤 0去掉+ 1不转发手机 2不处理 | ||
| 171 | + * @var int[] | ||
| 172 | + */ | ||
| 173 | + protected $filter_phone = [30,12,58]; | ||
| 174 | + | ||
| 175 | + /** | ||
| 176 | + * 需要多个ip访问的国家 | ||
| 177 | + * @var | ||
| 178 | + */ | ||
| 179 | + protected $multiple_ip_visit_country = [ | ||
| 180 | + '阿尔及利亚', | ||
| 181 | + '埃及', | ||
| 182 | + '埃塞俄比亚', | ||
| 183 | + '安哥拉', | ||
| 184 | + '贝宁', | ||
| 185 | + '博茨瓦纳', | ||
| 186 | + '布基纳法索', | ||
| 187 | + '布隆迪', | ||
| 188 | + '赤道几内亚', | ||
| 189 | + '多哥', | ||
| 190 | + '厄立特里亚', | ||
| 191 | + '佛得角', | ||
| 192 | + '冈比亚', | ||
| 193 | + '刚果共和国', | ||
| 194 | + '刚果民主共和国', | ||
| 195 | + '吉布提', | ||
| 196 | + '几内亚', | ||
| 197 | + '几内亚比绍共和国', | ||
| 198 | + '加纳', | ||
| 199 | + '加蓬', | ||
| 200 | + '津巴布韦', | ||
| 201 | + '喀麦隆', | ||
| 202 | + '科摩罗', | ||
| 203 | + '科特迪瓦', | ||
| 204 | + '肯尼亚', | ||
| 205 | + '莱索托', | ||
| 206 | + '利比里亚', | ||
| 207 | + '利比亞', | ||
| 208 | + '留尼汪', | ||
| 209 | + '卢旺达', | ||
| 210 | + '马达加斯加', | ||
| 211 | + '马拉维', | ||
| 212 | + '马里', | ||
| 213 | + '马约特', | ||
| 214 | + '毛里求斯', | ||
| 215 | + '毛里塔尼亚', | ||
| 216 | + '摩洛哥', | ||
| 217 | + '莫桑比克', | ||
| 218 | + '纳米比亚', | ||
| 219 | + '南非', | ||
| 220 | + '南苏丹', | ||
| 221 | + '尼日尔', | ||
| 222 | + '尼日利亚', | ||
| 223 | + '塞拉利昂', | ||
| 224 | + '塞内加尔', | ||
| 225 | + '塞舌尔', | ||
| 226 | + '圣多美和普林西比', | ||
| 227 | + '圣赫勒拿', | ||
| 228 | + '斯威士兰', | ||
| 229 | + '索马里', | ||
| 230 | + '坦桑尼亚', | ||
| 231 | + '突尼斯', | ||
| 232 | + '乌干达', | ||
| 233 | + '西撒哈拉', | ||
| 234 | + '赞比亚', | ||
| 235 | + '乍得', | ||
| 236 | + '中非共和国', | ||
| 237 | + '阿富汗', | ||
| 238 | + '阿联酋', | ||
| 239 | + '阿曼', | ||
| 240 | + '阿塞拜疆', | ||
| 241 | + '澳门', | ||
| 242 | + '巴基斯坦', | ||
| 243 | + '巴勒斯坦', | ||
| 244 | + '巴林', | ||
| 245 | + '不丹', | ||
| 246 | + '东帝汶', | ||
| 247 | + '菲律宾', | ||
| 248 | + '格鲁吉亚', | ||
| 249 | + '哈萨克斯坦', | ||
| 250 | + '韩国', | ||
| 251 | + '吉尔吉斯斯坦', | ||
| 252 | + '柬埔寨', | ||
| 253 | + '卡塔尔', | ||
| 254 | + '科威特', | ||
| 255 | + '老挝', | ||
| 256 | + '黎巴嫩', | ||
| 257 | + '马尔代夫', | ||
| 258 | + '马来西亚', | ||
| 259 | + '蒙古', | ||
| 260 | + '孟加拉国', | ||
| 261 | + '缅甸', | ||
| 262 | + '尼泊尔', | ||
| 263 | + '日本', | ||
| 264 | + '塞浦路斯', | ||
| 265 | + '沙特阿拉伯', | ||
| 266 | + '斯里兰卡', | ||
| 267 | + '塔吉克斯坦', | ||
| 268 | + '泰国', | ||
| 269 | + '土耳其', | ||
| 270 | + '土库曼斯坦', | ||
| 271 | + '文莱', | ||
| 272 | + '乌兹别克斯坦', | ||
| 273 | + '新加坡', | ||
| 274 | + '亚美尼亚', | ||
| 275 | + '也门', | ||
| 276 | + '伊拉克', | ||
| 277 | + '以色列', | ||
| 278 | + '印度尼西亚', | ||
| 279 | + '约旦', | ||
| 280 | + '越南', | ||
| 281 | + ]; | ||
| 282 | + | ||
| 283 | + /** | ||
| 284 | + * @return bool | ||
| 285 | + */ | ||
| 286 | + public function handle() | ||
| 287 | + { | ||
| 288 | + while (true) { | ||
| 289 | + $inquiry = ReInquiryForm::where('created_at', '>', '2025-05-19 00:00:00') | ||
| 290 | + ->where('created_at', '<', '2025-05-22 18:00:00') | ||
| 291 | + ->where(['status' => ReInquiryForm::STATUS_FORGO]) | ||
| 292 | + ->orderBy('id', 'asc')->get(); | ||
| 293 | + | ||
| 294 | + if ($inquiry->isEmpty()){ | ||
| 295 | + $this->output('未发现待处理询盘!'); | ||
| 296 | + sleep(60); | ||
| 297 | + continue; | ||
| 298 | + } | ||
| 299 | + | ||
| 300 | + $this->output('开始处理本轮询盘!'); | ||
| 301 | + foreach ($inquiry as $key=>$val) { | ||
| 302 | + $this->output('询盘ID:' . $val->id); | ||
| 303 | + | ||
| 304 | + | ||
| 305 | + $detail_ids = ReInquiryDetail::where('form_id', $val->id)->pluck('id')->toArray(); | ||
| 306 | + | ||
| 307 | + $inquiry_log = ReInquiryDetailLog::whereIn('detail_id', $detail_ids)->where('type', ReInquiryDetailLog::TYPE_INQUIRY)->first(); | ||
| 308 | + //有询盘的了 就跳过 | ||
| 309 | + if($inquiry_log){ | ||
| 310 | + continue; | ||
| 311 | + } | ||
| 312 | + | ||
| 313 | + // 询盘对应广告 | ||
| 314 | + $ad_task = $this->getAdTask($val->ad_id); | ||
| 315 | + // 没有获取到转发任务 | ||
| 316 | + if (empty($ad_task)) { | ||
| 317 | + $val->status = ReInquiryForm::STATUS_FORGO; | ||
| 318 | + $val->remark = '未找到需要转发的广告任务!'; | ||
| 319 | + $val->save(); | ||
| 320 | + continue; | ||
| 321 | + } | ||
| 322 | + // 未设置对法对象 | ||
| 323 | + $target_num_arr = array_map(function ($item) { | ||
| 324 | + return count($item['target']) + count($item['shop_site']) + count($item['fob_pro']); | ||
| 325 | + }, $ad_task); | ||
| 326 | + if (!array_sum($target_num_arr)) { | ||
| 327 | + $val->status = ReInquiryForm::STATUS_FORGO; | ||
| 328 | + $val->remark = '广告任务转发对象为空!'; | ||
| 329 | + $val->save(); | ||
| 330 | + continue; | ||
| 331 | + } | ||
| 332 | + //是否要过滤 | ||
| 333 | + $filter_res = $this->filter($val); | ||
| 334 | + if($filter_res !== true){ | ||
| 335 | + $val->status = ReInquiryForm::STATUS_FORGO; | ||
| 336 | + $val->remark = $filter_res; | ||
| 337 | + $val->save(); | ||
| 338 | + continue; | ||
| 339 | + } | ||
| 340 | + //验证手机号 无效 号码不推送 | ||
| 341 | + if(!Validate::phone($val->phone)){ | ||
| 342 | + $val->phone = ''; | ||
| 343 | + } | ||
| 344 | + try { | ||
| 345 | + $res = false; | ||
| 346 | + foreach ($ad_task as $task){ | ||
| 347 | + $res += $this->relayDetail($task, $val); | ||
| 348 | + $res += $this->relayShopDetail($task, $val); | ||
| 349 | + $res += $this->relayFobDetail($task, $val); | ||
| 350 | + } | ||
| 351 | + $val->status = $res ? ReInquiryForm::STATUS_SUCCESS : ReInquiryForm::STATUS_FORGO; | ||
| 352 | + $val->save(); | ||
| 353 | + } catch (\Exception $e) { | ||
| 354 | + $this->logChannel()->info('执行询盘错误:',[$e->getMessage(), $e->getFile(), $e->getLine()]); | ||
| 355 | + $this->output('执行询盘错误:' . $e->getMessage()); | ||
| 356 | + } | ||
| 357 | + } | ||
| 358 | + $this->output('本轮询盘处理结束!'); | ||
| 359 | + } | ||
| 360 | + return true; | ||
| 361 | + } | ||
| 362 | + | ||
| 363 | + | ||
| 364 | + public function filter($data) | ||
| 365 | + { | ||
| 366 | + //通用过滤规则 | ||
| 367 | + $config = InquiryFilterConfig::getCacheInfoByProjectId(Project::DEMO_PROJECT_ID); | ||
| 368 | + //FB询盘的全局过滤规则 | ||
| 369 | + $fb_config = ReInquiryConfig::getDefaultConfigCache(ReInquiryConfig::TYPE_FILTER_WORDS); | ||
| 370 | + | ||
| 371 | + $fb_config['filter_contents'] = array_filter(explode("\r\n", $fb_config['filter_contents']??'')); | ||
| 372 | + $fb_config['filter_emails'] = array_filter(explode("\r\n", $fb_config['filter_emails']??'')); | ||
| 373 | + $fb_config['filter_mobiles'] = array_filter(explode("\r\n", $fb_config['filter_mobiles']??'')); | ||
| 374 | + $fb_config['filter_names'] = array_filter(explode("\r\n", $fb_config['filter_names']??'')); | ||
| 375 | + | ||
| 376 | + $config['filter_contents'] = array_unique(array_merge($fb_config['filter_contents'],$config['filter_contents'])); | ||
| 377 | + $config['filter_emails'] = array_unique(array_merge($fb_config['filter_emails'],$config['filter_emails'])); | ||
| 378 | + $config['filter_mobiles'] = array_unique(array_merge($fb_config['filter_mobiles'],$config['filter_mobiles'])); | ||
| 379 | + $config['filter_names'] = array_unique(array_merge($fb_config['filter_names'],$config['filter_names'])); | ||
| 380 | + | ||
| 381 | + //过滤内容 | ||
| 382 | + if(!empty($data['message']) && !empty($config['filter_contents'])) { | ||
| 383 | + foreach ($config['filter_contents'] as $filter_content) { | ||
| 384 | + if (Str::contains(strtolower($data['message']), strtolower($filter_content))) { | ||
| 385 | + return '过滤内容:' . $filter_content; | ||
| 386 | + } | ||
| 387 | + } | ||
| 388 | + } | ||
| 389 | + //过滤邮箱 | ||
| 390 | + if(!empty($data['email']) && !empty($config['filter_emails'])){ | ||
| 391 | + foreach ($config['filter_emails'] as $filter_email){ | ||
| 392 | + if(Str::contains(strtolower($data['email']), strtolower($filter_email))){ | ||
| 393 | + return '过滤邮箱:' . $filter_email; | ||
| 394 | + } | ||
| 395 | + } | ||
| 396 | + //邮箱有效性 | ||
| 397 | + if(!Validate::email($data['email'])){ | ||
| 398 | + return '邮箱无效'; | ||
| 399 | + } | ||
| 400 | + } | ||
| 401 | + //过滤电话 | ||
| 402 | + if(!empty($data['phone']) && !empty($config['filter_mobiles'])){ | ||
| 403 | + foreach ($config['filter_mobiles'] as $filter_mobile){ | ||
| 404 | + if(Str::contains(strtolower($data['phone']), strtolower($filter_mobile))){ | ||
| 405 | + return '过滤电话:' . $filter_mobile; | ||
| 406 | + } | ||
| 407 | + } | ||
| 408 | + } | ||
| 409 | + //过滤姓名 | ||
| 410 | + if(!empty($data['full_name'] && !empty($config['filter_names']))){ | ||
| 411 | + foreach ($config['filter_names'] as $filter_name){ | ||
| 412 | + if(Str::contains(strtolower($data['full_name']), strtolower($filter_name))){ | ||
| 413 | + return '过滤姓名:' . $filter_name; | ||
| 414 | + } | ||
| 415 | + } | ||
| 416 | + } | ||
| 417 | + return true; | ||
| 418 | + } | ||
| 419 | + | ||
| 420 | + /** | ||
| 421 | + * 创建转发详情 | ||
| 422 | + * TODO 通过任务生成转发对象, 更具转发对象获取对应数据, 写入着陆记录 | ||
| 423 | + * @param $task | ||
| 424 | + * @param $form | ||
| 425 | + * @return bool | ||
| 426 | + */ | ||
| 427 | + public function relayDetail($task, $form) | ||
| 428 | + { | ||
| 429 | + $this->output('获取转发对象'); | ||
| 430 | + if(empty($task['target'] )){ | ||
| 431 | + $this->output('没有独立站转发对象'); | ||
| 432 | + return 0; | ||
| 433 | + } | ||
| 434 | + //是否有必选的渠道 渠道有一个及以上必选 就在组内随机一个 | ||
| 435 | + $require_agent_group = []; | ||
| 436 | + foreach ($task['target'] as $item) { | ||
| 437 | + if (!empty($item['is_require'])) { | ||
| 438 | + $require_agent_group[] = $item['agent_group']; | ||
| 439 | + } | ||
| 440 | + } | ||
| 441 | + $require_agent_group = array_unique($require_agent_group); | ||
| 442 | + $require_agent_group_data = collect($task['target'])->whereIn('agent_group', $require_agent_group)->groupBy('agent_group'); | ||
| 443 | + $require_data = $require_agent_group_data->keys()->random(count($require_agent_group))->map(function ($group) use ($require_agent_group_data) { | ||
| 444 | + return $require_agent_group_data[$group]->random(); | ||
| 445 | + })->all(); | ||
| 446 | + //代理商组 一个组只发一个 | ||
| 447 | + $agent_group = collect($task['target'])->whereNotIn('agent_group', $require_agent_group)->groupBy('agent_group'); | ||
| 448 | + // 获取转发对象 重置num数量, array_rand数量不足会报错 | ||
| 449 | + $task['num'] = $task['num'] - count($require_agent_group); | ||
| 450 | + $num = $task['num'] > count($agent_group) ? count($agent_group) : $task['num']; | ||
| 451 | + $random_data = $agent_group->keys()->random($num)->map(function ($group) use ($agent_group) { | ||
| 452 | + return $agent_group[$group]->random(); | ||
| 453 | + })->all(); | ||
| 454 | + $random_data = array_merge($require_data, $random_data); | ||
| 455 | + | ||
| 456 | + if (empty($random_data)) { | ||
| 457 | + $this->logChannel()->info('当前任务未发现转发对象!', ['广告任务ID:' . $task['id'], '询盘ID:' . $form->id]); | ||
| 458 | + $form->remark = $form->remark . '当前任务未发现转发对象,广告ID: ' . $form->ad_id . '!'; | ||
| 459 | + return false; | ||
| 460 | + } | ||
| 461 | + $this->logChannel()->info('随机域名', array_column($random_data, 'url')); | ||
| 462 | + foreach ($random_data as $item) { | ||
| 463 | + $times = 1; | ||
| 464 | + $inquiry_time = 1; | ||
| 465 | + | ||
| 466 | + //需要多个ip访问的国家 随机2-5次访问,只有一次询盘 | ||
| 467 | + if (in_array($form->country_name, $this->multiple_ip_visit_country)) { | ||
| 468 | + $times = mt_rand(1, 2); //随机次数 | ||
| 469 | + $inquiry_time = mt_rand(1, $times); //第几次询盘 | ||
| 470 | + $this->output('多次访问模拟:' . $times .',第' . $inquiry_time . '次询盘'); | ||
| 471 | + } | ||
| 472 | + for ($i = 1; $i <= $times; $i++) { | ||
| 473 | + $is_inquiry = ($inquiry_time == $i); | ||
| 474 | + if($is_inquiry){ | ||
| 475 | + $this->output('第' . $i . '次询盘'); | ||
| 476 | + } | ||
| 477 | + //手机号过滤 | ||
| 478 | + $phone = $form->phone; | ||
| 479 | + $filter_phone = $this->get_rand($this->filter_phone); | ||
| 480 | + if($filter_phone == 0){ | ||
| 481 | + $phone = trim(str_replace("+", '', $phone)); | ||
| 482 | + }elseif($filter_phone == 1){ | ||
| 483 | + $phone = ''; | ||
| 484 | + } | ||
| 485 | + | ||
| 486 | + // 推送站点 | ||
| 487 | + $domain = $item['url']; | ||
| 488 | + $is_v6 = $item['is_v6']; | ||
| 489 | + $re_website = 'https://' . $domain . '/'; | ||
| 490 | + | ||
| 491 | + //urls | ||
| 492 | + list($urls, $lang, $inquiry_product_url) = $this->getUrls($is_v6, $domain, $re_website, $form, $task); | ||
| 493 | + if(!$urls){ | ||
| 494 | + continue; | ||
| 495 | + } | ||
| 496 | + | ||
| 497 | + //ip | ||
| 498 | + $ip_data = $this->getIpData($form->country_name); | ||
| 499 | + $ip = $ip_data->ip; | ||
| 500 | + $country_name = $ip_data->ip_area; | ||
| 501 | + | ||
| 502 | + //message | ||
| 503 | + $message = ''; | ||
| 504 | + $message_id = 0; | ||
| 505 | + $msg_lang = ''; | ||
| 506 | + if($is_inquiry) { | ||
| 507 | + list($message, $message_id, $msg_lang) = $this->getMessage($task, $form->message, $domain, $inquiry_product_url); | ||
| 508 | + } | ||
| 509 | + $lang = $lang ?: $msg_lang; | ||
| 510 | + | ||
| 511 | + $this->output('获取转发设备信息'); | ||
| 512 | + // 客户端 头信息 来源 | ||
| 513 | + $device_port = $form->email ? '1' : '2'; //1 pc 2移动端 | ||
| 514 | + $user_agent = $form->email ? Arr::random($this->pc_ua) : Arr::random($this->mobile_ua); | ||
| 515 | + $referrer = $this->getReferer($country_name, $lang); | ||
| 516 | + $this->output('写入数据'); | ||
| 517 | + | ||
| 518 | + $pre = 0; | ||
| 519 | + $start_time = strtotime($form->inquiry_date . ' + 4 days'); | ||
| 520 | + if($start_time < time()){ | ||
| 521 | + $start_time = time(); | ||
| 522 | + $seconds = rand(0, 72 * 3600); // 3天内发完 | ||
| 523 | + }else{ | ||
| 524 | + $seconds = rand(100, 2 * 3600); | ||
| 525 | + } | ||
| 526 | + $email = ''; | ||
| 527 | + if($is_inquiry) { | ||
| 528 | + $exists = ReInquiryDetail::where('re_website', $domain)->where('email', $form->email)->first(); | ||
| 529 | + if ($exists) { | ||
| 530 | + $this->output('转发站点邮件已存在'); | ||
| 531 | + // continue; | ||
| 532 | + } | ||
| 533 | + $email = $form->email; | ||
| 534 | + } | ||
| 535 | + dump(date('Y-m-d H:i:s', $start_time + $seconds)); | ||
| 536 | + // 写入推送详情 | ||
| 537 | + $re_detail = ReInquiryDetail::createInquiry($task['id'], $form->id, $domain, $country_name, $ip, $form->full_name, $email, $phone, $message, $message_id, $device_port, | ||
| 538 | + $user_agent, $referrer, $urls, $is_v6, date('Y-m-d H:i:s', $start_time + $seconds)); | ||
| 539 | + foreach ($urls as $k=>$v){ | ||
| 540 | + $pre++; | ||
| 541 | + $seconds += rand(5,60); | ||
| 542 | + ReInquiryDetailLog::createInquiryLog($re_detail->id, ReInquiryDetailLog::TYPE_VISIT, $pre, $v, date('Y-m-d H:i:s', $start_time + $seconds)); | ||
| 543 | + // 最后一次访问询盘 加上询盘 | ||
| 544 | + if($is_inquiry && ($k+1) == count($urls)){ | ||
| 545 | + $this->output('第' . ($k+1) . '个链接询盘'); | ||
| 546 | + $seconds += rand(30,120); | ||
| 547 | + $pre++; | ||
| 548 | + ReInquiryDetailLog::createInquiryLog($re_detail->id, ReInquiryDetailLog::TYPE_INQUIRY, $pre, $v, date('Y-m-d H:i:s', $start_time + $seconds)); | ||
| 549 | + } | ||
| 550 | + } | ||
| 551 | + } | ||
| 552 | + } | ||
| 553 | + return true; | ||
| 554 | + } | ||
| 555 | + | ||
| 556 | + public function relayShopDetail($task, $form) | ||
| 557 | + { | ||
| 558 | + $this->output('获取商城转发对象'); | ||
| 559 | + | ||
| 560 | + if(empty($task['shop_site'])){ | ||
| 561 | + $this->output('没有商城转发对象'); | ||
| 562 | + return 0; | ||
| 563 | + } | ||
| 564 | + | ||
| 565 | + $num = $task['num'] > count($task['shop_site']) ? count($task['shop_site']) : $task['num']; | ||
| 566 | + $shop_site = collect($task['shop_site'])->random($num)->all(); | ||
| 567 | + | ||
| 568 | + foreach ($shop_site as $item) { | ||
| 569 | + //手机号过滤 | ||
| 570 | + $phone = $form->phone; | ||
| 571 | + // 推送站点 | ||
| 572 | + $domain = $item; | ||
| 573 | + $re_website = 'https://' . $domain . '/'; | ||
| 574 | + $paths = ['', 'contact-us']; | ||
| 575 | + $url = $re_website . $paths[array_rand($paths)]; | ||
| 576 | + //ip | ||
| 577 | + $ip_data = $this->getIpData($form->country_name); | ||
| 578 | + $ip = $ip_data->ip; | ||
| 579 | + $country_name = $ip_data->ip_area; | ||
| 580 | + | ||
| 581 | + //message | ||
| 582 | + list($message, $message_id, $lang) = $this->getMessage($task, $form->message, $domain); | ||
| 583 | + | ||
| 584 | + $device_port = $form->email ? '1' : '2'; //1 pc 2移动端 | ||
| 585 | + $user_agent = $form->email ? Arr::random($this->pc_ua) : Arr::random($this->mobile_ua); | ||
| 586 | + $referrer = $this->getReferer($country_name, $lang); | ||
| 587 | + | ||
| 588 | + $start_time = strtotime($form->inquiry_date . ' + 13 days'); | ||
| 589 | + if($start_time < time()){ | ||
| 590 | + $start_time = time(); | ||
| 591 | + $seconds = rand(0, 48 * 3600); // 开始时间 从5-2小时后开始 | ||
| 592 | + }else{ | ||
| 593 | + $seconds = rand(100, 2 * 3600); | ||
| 594 | + } | ||
| 595 | + dump(date('Y-m-d H:i:s', $start_time + $seconds)); | ||
| 596 | + $exists = ReInquiryDetail::where('re_website', $domain)->where('email', $form->email)->first(); | ||
| 597 | + if($exists){ | ||
| 598 | + $this->output('转发站点邮件已存在'); | ||
| 599 | + continue; | ||
| 600 | + } | ||
| 601 | + $re_detail = ReInquiryDetail::createInquiry($task['id'], $form->id, $domain, $country_name, $ip, $form->full_name, $form->email, $phone, $message, $message_id, | ||
| 602 | + $device_port, $user_agent, $referrer, [$url], 0, date('Y-m-d H:i:s', $start_time + $seconds), ReInquiryDetail::STATUS_INIT, 2); | ||
| 603 | + | ||
| 604 | + ReInquiryDetailLog::createInquiryLog($re_detail->id, ReInquiryDetailLog::TYPE_INQUIRY, 1, $url, date('Y-m-d H:i:s', $start_time + $seconds)); | ||
| 605 | + } | ||
| 606 | + return true; | ||
| 607 | + } | ||
| 608 | + | ||
| 609 | + | ||
| 610 | + public function relayFobDetail($task, $form) | ||
| 611 | + { | ||
| 612 | + $this->output('获取FOB转发对象'); | ||
| 613 | + | ||
| 614 | + if(empty($task['fob_pro'])){ | ||
| 615 | + $this->output('没有FOB转发对象'); | ||
| 616 | + return 0; | ||
| 617 | + } | ||
| 618 | + | ||
| 619 | + foreach ($task['fob_pro'] as $item) { | ||
| 620 | + //手机号过滤 | ||
| 621 | + $phone = $form->phone; | ||
| 622 | + // 推送站点 | ||
| 623 | + $postid = $item; | ||
| 624 | + //message | ||
| 625 | + list($message, $message_id, $lang) = $this->getMessage($task, $form->message, $postid); | ||
| 626 | + | ||
| 627 | + $device_port = $form->email ? '1' : '2'; //1 pc 2移动端 | ||
| 628 | + $user_agent = $form->email ? Arr::random($this->pc_ua) : Arr::random($this->mobile_ua); | ||
| 629 | + | ||
| 630 | + $start_time = strtotime($form->inquiry_date . ' + 13 days'); | ||
| 631 | + if($start_time < time()){ | ||
| 632 | + $start_time = time(); | ||
| 633 | + $seconds = rand(0, 10 * 3600); // 开始时间 从5-2小时后开始 | ||
| 634 | + }else{ | ||
| 635 | + $seconds = rand(100, 2 * 3600); | ||
| 636 | + } | ||
| 637 | + dump(date('Y-m-d H:i:s', $start_time + $seconds)); | ||
| 638 | + $exists = ReInquiryDetail::where('re_website', $postid)->where('email', $form->email)->first(); | ||
| 639 | + if($exists){ | ||
| 640 | + $this->output('转发站点邮件已存在'); | ||
| 641 | + continue; | ||
| 642 | + } | ||
| 643 | + $re_detail = ReInquiryDetail::createInquiry($task['id'], $form->id, $postid, $country_name??'', $ip??'', $form->full_name, $form->email, $phone, | ||
| 644 | + $message, $message_id, $device_port, $user_agent, $referrer??'', [$postid], 0, date('Y-m-d H:i:s', $start_time + $seconds), ReInquiryDetail::STATUS_INIT, 3); | ||
| 645 | + | ||
| 646 | + ReInquiryDetailLog::createInquiryLog($re_detail->id, ReInquiryDetailLog::TYPE_INQUIRY, 1, $postid, date('Y-m-d H:i:s', $start_time + $seconds)); | ||
| 647 | + } | ||
| 648 | + return true; | ||
| 649 | + } | ||
| 650 | + | ||
| 651 | + public function getIpData($country_name){ | ||
| 652 | + $this->output('获取转发ip'); | ||
| 653 | + $country = $country_name; | ||
| 654 | + // 有国家 通过国家查询, 如果没有获取到就随机获取 | ||
| 655 | + $where = []; | ||
| 656 | + $country && $where['ip_area'] = $country; | ||
| 657 | + $ip_data = DB::table('gl_xunpan_ipdata')->where($where)->inRandomOrder()->first(); | ||
| 658 | + if (empty($ip_data)) { | ||
| 659 | + $ip_data = DB::table('gl_xunpan_ipdata')->inRandomOrder()->first(); | ||
| 660 | + } | ||
| 661 | + return $ip_data; | ||
| 662 | + } | ||
| 663 | + | ||
| 664 | + public function getMessage($task, $message, $domain, $inquiry_product_url = ''){ | ||
| 665 | + $this->output('转发内容'); | ||
| 666 | + $form_message = $message; | ||
| 667 | + $message_id = 0; | ||
| 668 | + | ||
| 669 | + // TODO 当原始询盘内容长度大于15个字符, 60%几率直接发送原始内容。 | ||
| 670 | + if (strlen($message) >= 15) { | ||
| 671 | + $not_use_probability = AiCommand::where('key', 'fb_inquiry_text')->value('not_use_probability'); | ||
| 672 | + $randomNumber = rand(0, 100); | ||
| 673 | + if($randomNumber < $not_use_probability){ | ||
| 674 | + //原内容非英语,转为对应语种 | ||
| 675 | + if (is_numeric($form_message)) { //数字会被识别为中文 | ||
| 676 | + $lang = 'en'; | ||
| 677 | + } else { | ||
| 678 | + $translateSl = Translate::translateSl($form_message); | ||
| 679 | + $lang = $translateSl['texts']['sl'] ?? 'en'; | ||
| 680 | + } | ||
| 681 | + return [$message, $message_id, $lang??'']; | ||
| 682 | + } | ||
| 683 | + } | ||
| 684 | + | ||
| 685 | + //开启文案替换 | ||
| 686 | + if ($task['is_replace_text'] == 2) { | ||
| 687 | + //AI生成 | ||
| 688 | + $error = 0; | ||
| 689 | + while ($error<3){ | ||
| 690 | + $message = $this->ai_send($task['ai_param'], $message, $inquiry_product_url); | ||
| 691 | + if(!$message){ | ||
| 692 | + $this->output('AI文案生成失败'); | ||
| 693 | + $error++; | ||
| 694 | + if($error==2){ | ||
| 695 | + $task['is_replace_text'] = 1; | ||
| 696 | + $this->output('AI文案生成失败,使用文案库'); | ||
| 697 | + } | ||
| 698 | + }else{ | ||
| 699 | + break; | ||
| 700 | + } | ||
| 701 | + } | ||
| 702 | + } | ||
| 703 | + if ($task['is_replace_text'] == 1 || strlen($message) <= 4) { | ||
| 704 | + //配置文案库替换或者字符少于4个,直接替换文案 | ||
| 705 | + $use_ids = ReInquiryDetail::where(['re_website' => $domain])->where('status', '<>', ReInquiryDetail::STATUS_FAIL)->pluck('text_id')->toArray(); | ||
| 706 | + $text = ReInquiryText::whereNotIn('id', $use_ids)->where('status', ReInquiryText::STATUS_USABLE)->inRandomOrder()->first(); | ||
| 707 | + $message = $text->content; | ||
| 708 | + $message_id = $text->id; | ||
| 709 | + // 获取后,使用次数+1 | ||
| 710 | + $text->use_time += 1; | ||
| 711 | + $text->save(); | ||
| 712 | + | ||
| 713 | + //原内容非英语,转为对应语种 | ||
| 714 | + if (is_numeric($form_message)) { //数字会被识别为中文 | ||
| 715 | + $lang = 'en'; | ||
| 716 | + } else { | ||
| 717 | + $translateSl = Translate::translateSl($form_message); | ||
| 718 | + $lang = $translateSl['texts']['sl'] ?? 'en'; | ||
| 719 | + } | ||
| 720 | + | ||
| 721 | + if ($lang != 'en' && !Str::contains($lang, 'zh')) { | ||
| 722 | + $message = Translate::tran($message, $lang); | ||
| 723 | + } | ||
| 724 | + } | ||
| 725 | + return [$message, $message_id, $lang??'']; | ||
| 726 | + } | ||
| 727 | + | ||
| 728 | + public function getUrls($is_v6, $domain, $re_website, $form, $task){ | ||
| 729 | + $this->output('转发对象:' . $domain); | ||
| 730 | + $this->output('获取转发链接'); | ||
| 731 | + // v6:有邮箱推送主站,没有邮箱推送AMP站;v5:仅推送有邮箱到主站 | ||
| 732 | + $lang = ''; | ||
| 733 | + if ($is_v6) { | ||
| 734 | + // 获取语种, 6.0是可以确定语种的 | ||
| 735 | + $project = Project::getProjectByDomain($domain); | ||
| 736 | + if (empty($project)) { | ||
| 737 | + $this->logChannel()->info('广告任务ID:' . $task['id'] . ', 转发对象:' . $re_website . '非v6链接,转发失败;', ['广告任务ID:' . $task['id'], '询盘ID:' . $form->id]); | ||
| 738 | + return [[], $lang]; | ||
| 739 | + } | ||
| 740 | + $lang = WebLanguage::getLangById($project->main_lang_id ?? 1)['short']; | ||
| 741 | + | ||
| 742 | + // 获取访问明细和着陆页 | ||
| 743 | + $product_url = $this->getLinksFromSitemap($re_website . 'product_sitemap.xml'); | ||
| 744 | + $product_cate_url = $this->getLinksFromSitemap($re_website . 'product_category_sitemap.xml'); | ||
| 745 | + $keywords_url = $this->getLinksFromSitemap($re_website . 'product_keywords_sitemap.xml'); | ||
| 746 | + $page_url = $this->getLinksFromSitemap($re_website . 'page_sitemap.xml'); | ||
| 747 | + } else { | ||
| 748 | + if($form->email){ | ||
| 749 | + //通过sitemap拿访问页面 | ||
| 750 | + $product_url = $this->getLinksFromSitemap($re_website . 'sitemap_post.xml'); | ||
| 751 | + $product_cate_url = $this->getLinksFromSitemap($re_website . 'sitemap_category.xml'); | ||
| 752 | + $keywords_url = $this->getLinksFromSitemap($re_website . 'sitemap_post_tag.xml'); | ||
| 753 | + $page_url = $this->getLinksFromSitemap($re_website . 'sitemap_page.xml'); | ||
| 754 | + }else{ | ||
| 755 | + //m站先就往contact-us着陆 | ||
| 756 | + $product_url = $product_cate_url = $keywords_url = []; | ||
| 757 | + $page_url = [$re_website . 'contact-us/']; | ||
| 758 | + } | ||
| 759 | + } | ||
| 760 | + | ||
| 761 | + // 所有可用url | ||
| 762 | + $urls = $inquiry_urls = []; | ||
| 763 | + //入口url 首页30%,单页10%,聚合页60% | ||
| 764 | + $type = getRandByRatio([30,10,60]); | ||
| 765 | + $inlet = $re_website; | ||
| 766 | + $type == 1 && $inlet = $page_url ? Arr::random($page_url) : $re_website; | ||
| 767 | + $type == 2 && $inlet = $keywords_url ? Arr::random($keywords_url) : $re_website; | ||
| 768 | + $urls[] = $inquiry_urls[] = $inlet; | ||
| 769 | + $all_urls = array_merge($urls, $product_url, $product_cate_url, $keywords_url, $page_url); | ||
| 770 | + $inquiry_urls = array_merge($urls, $product_cate_url, $keywords_url, $page_url); | ||
| 771 | + | ||
| 772 | + // 随机访问1-6个页面 | ||
| 773 | + $deep = rand(1,6); | ||
| 774 | + if($deep > 2) { | ||
| 775 | + $visit_urls = Arr::random($all_urls, rand(1, count($all_urls) > 4 ? 4 : count($all_urls))); | ||
| 776 | + $urls = array_merge($urls, $visit_urls); | ||
| 777 | + } | ||
| 778 | + if($deep > 1) { | ||
| 779 | + // 推送着落页只能是 首页、产品分类、单页面、聚合页 | ||
| 780 | + if (!in_array(end($urls), $inquiry_urls)) { | ||
| 781 | + $urls[] = Arr::random($inquiry_urls); | ||
| 782 | + } | ||
| 783 | + } | ||
| 784 | + //着陆页是否是产品页面或产品列表页 | ||
| 785 | + $inquiry_product_url = ''; | ||
| 786 | + if(in_array(Arr::last($urls), $product_url) || in_array(Arr::last($urls), $product_cate_url)){ | ||
| 787 | + $inquiry_product_url = Arr::last($urls); | ||
| 788 | + } | ||
| 789 | + | ||
| 790 | + return [$urls, $lang, $inquiry_product_url]; | ||
| 791 | + } | ||
| 792 | + /** | ||
| 793 | + * 获取待处理询盘表单 | ||
| 794 | + * @param int $num | ||
| 795 | + * @return mixed | ||
| 796 | + */ | ||
| 797 | + public function getInquiry($num = 10) | ||
| 798 | + { | ||
| 799 | + $result = ReInquiryForm::where(['status' => ReInquiryForm::STATUS_INIT])->orderBy('id', 'asc')->limit($num)->get(); | ||
| 800 | + return $result; | ||
| 801 | + } | ||
| 802 | + | ||
| 803 | + /** | ||
| 804 | + * 获取广告任务配置数据 | ||
| 805 | + * @param $ad_id | ||
| 806 | + * @return array | ||
| 807 | + */ | ||
| 808 | + public function getAdTask($ad_id) | ||
| 809 | + { | ||
| 810 | + $cache_key = 'inquiry_ads_tasks'; | ||
| 811 | + $ads = Cache::get($cache_key, function () use ($cache_key) { | ||
| 812 | + $ads = ReInquiryTask::where(['status' => ReInquiryTask::STATUS_OPEN])->get(['id', 'ad_id', 'num', 'target', 'is_replace_text', 'ai_param', 'shop_site', 'fob_pro']); | ||
| 813 | + $array = []; | ||
| 814 | + foreach ($ads as $key=>$val) { | ||
| 815 | + $ad_ids = explode(',', $val['ad_id']); | ||
| 816 | + foreach ($ad_ids as $ad_id){ | ||
| 817 | + $array[$ad_id][] = $val; | ||
| 818 | + } | ||
| 819 | + } | ||
| 820 | + if ($array) | ||
| 821 | + Cache::put($cache_key, $array, 60); | ||
| 822 | + return $array; | ||
| 823 | + }); | ||
| 824 | + return empty($ads[$ad_id]) ? [] : $ads[$ad_id]; | ||
| 825 | + } | ||
| 826 | + | ||
| 827 | + /** | ||
| 828 | + * 获取头信息 | ||
| 829 | + * @param $ip_area | ||
| 830 | + * @param $lang | ||
| 831 | + * @return int|string | ||
| 832 | + */ | ||
| 833 | + public function getReferer($ip_area, $lang) | ||
| 834 | + { | ||
| 835 | + if($lang == 'ru'){ | ||
| 836 | + return $this->get_rand($this->eylyzb); | ||
| 837 | + } | ||
| 838 | + if($ip_area == '美国'){ | ||
| 839 | + $referer = $this->get_rand($this->lyzb); | ||
| 840 | + }else{ | ||
| 841 | + $referer = 'https://www.google.com/'; | ||
| 842 | + $suffix = array_search($ip_area, $this->suffix); | ||
| 843 | + if($suffix){ | ||
| 844 | + $res_qtzb = $this->get_rand($this->otherzb); | ||
| 845 | + if($res_qtzb == 1){ | ||
| 846 | + $referer = 'https://www.google.'.$suffix.'/'; | ||
| 847 | + } | ||
| 848 | + } | ||
| 849 | + } | ||
| 850 | + return $referer; | ||
| 851 | + } | ||
| 852 | + | ||
| 853 | + /** | ||
| 854 | + * 概率算法 | ||
| 855 | + * @param $proArr | ||
| 856 | + * @return int|string | ||
| 857 | + */ | ||
| 858 | + protected function get_rand($proArr) | ||
| 859 | + { | ||
| 860 | + $result = ''; | ||
| 861 | + $proSum = array_sum($proArr); | ||
| 862 | + foreach ($proArr as $key => $proCur) { | ||
| 863 | + $randNum = mt_rand(1, $proSum); | ||
| 864 | + if ($randNum <= $proCur) { | ||
| 865 | + $result = $key; | ||
| 866 | + break; | ||
| 867 | + } else { | ||
| 868 | + $proSum -= $proCur; | ||
| 869 | + } | ||
| 870 | + } | ||
| 871 | + unset ($proArr); | ||
| 872 | + return $result; | ||
| 873 | + } | ||
| 874 | + | ||
| 875 | + /** | ||
| 876 | + * 获取sitemap内容 | ||
| 877 | + * @param $sitemapUrl | ||
| 878 | + * @return array|mixed | ||
| 879 | + */ | ||
| 880 | + function getLinksFromSitemap($sitemapUrl) { | ||
| 881 | + try { | ||
| 882 | + //忽略cert证书 先下载到临时文件 | ||
| 883 | + $result = Http::withoutVerifying()->get($sitemapUrl)->body(); | ||
| 884 | + $tempFilePath = tempnam(sys_get_temp_dir(), 'remote_file_'); | ||
| 885 | + file_put_contents($tempFilePath, $result); | ||
| 886 | + $xml = simplexml_load_file($tempFilePath); | ||
| 887 | + $links = []; | ||
| 888 | + foreach ($xml->url as $url) { | ||
| 889 | + $loc = (string) $url->loc; | ||
| 890 | + if(!Str::contains($loc, ['404', 'thanks', 'test'])){ | ||
| 891 | + $links[] = $loc; | ||
| 892 | + } | ||
| 893 | + } | ||
| 894 | + //随机取20个 | ||
| 895 | + $total = count($links); | ||
| 896 | + return Arr::random($links, $total > 20 ? 20 : $total); | ||
| 897 | + }catch (\Exception $e){ | ||
| 898 | + echo date('Y-m-d H:i:s') . 'sitemap获取失败:' . $e->getMessage() . PHP_EOL; | ||
| 899 | + return $links??[]; | ||
| 900 | + } | ||
| 901 | + } | ||
| 902 | + | ||
| 903 | + public function ai_send($ai_param, $incontent, $inquiry_product_url) | ||
| 904 | + { | ||
| 905 | + $ai_command = AiCommand::where('key', 'fb_inquiry_text')->value('ai'); | ||
| 906 | + if (!$ai_command) { | ||
| 907 | + return ''; | ||
| 908 | + } | ||
| 909 | + $translateSl = Translate::translateSl($incontent); | ||
| 910 | + $lang = $translateSl['texts']['sl'] ?? 'en'; | ||
| 911 | + if ($lang == 'en' || $lang == 'ja' || $lang == 'ko' || Str::contains($lang, 'zh')) { | ||
| 912 | + $language = '英文'; | ||
| 913 | + $lang = 'en'; | ||
| 914 | + }else{ | ||
| 915 | + $language = Translate::getTls($lang); | ||
| 916 | + } | ||
| 917 | + | ||
| 918 | + | ||
| 919 | + | ||
| 920 | + //着陆页是否是产品页面或产品列表页 | ||
| 921 | + if($inquiry_product_url){ | ||
| 922 | + $title = Common::getUrlTitle($inquiry_product_url); | ||
| 923 | + if($title){ | ||
| 924 | + $ai_command = str_replace('{mkeywords}', $title, $ai_command); | ||
| 925 | + } | ||
| 926 | + } | ||
| 927 | + $ai_command = str_replace('{mkeywords}', Arr::random(explode("\r\n", $ai_param['mkeywords'])), $ai_command); | ||
| 928 | + $ai_command = str_replace('{incontent}', Arr::random(explode("\r\n", $incontent)), $ai_command); | ||
| 929 | + $ai_command = str_replace('{characters}', Arr::random(explode("\r\n", $ai_param['characters'])), $ai_command); | ||
| 930 | +// $ai_command = str_replace('{language}', Arr::random(explode("\r\n", $language)), $ai_command); | ||
| 931 | + $ai_command = str_replace('{language}', '英语', $ai_command); //输出英文 后面再翻译 | ||
| 932 | + $ai_command = str_replace('{inkeywords}', Arr::random(explode("\r\n", $ai_param['inkeywords'])), $ai_command); | ||
| 933 | + $ai_command = str_replace('{suoxie}', Arr::random(explode("\r\n", $ai_param['suoxie'])), $ai_command); | ||
| 934 | + //中括号里的根据概率使用 | ||
| 935 | + preg_match_all("/\[([^\]]+)\]/", $ai_command, $matches); | ||
| 936 | + foreach ($matches[1] as $k => $match){ | ||
| 937 | + //按比例使用 | ||
| 938 | + $matche_arr = explode('|', $match); | ||
| 939 | + $percentage = intval(trim($matche_arr[0], "%")); | ||
| 940 | + if(rand(1,100) <= $percentage){ | ||
| 941 | + //使用 | ||
| 942 | + $ai_command = str_replace($matches[0][$k], $matche_arr[1], $ai_command); | ||
| 943 | + }else{ | ||
| 944 | + //删除中括号 | ||
| 945 | + $ai_command = str_replace($matches[0][$k], '', $ai_command); | ||
| 946 | + } | ||
| 947 | + } | ||
| 948 | + | ||
| 949 | + $text = Gpt::instance()->openai_chat_qqs($ai_command); | ||
| 950 | + if ($lang != 'en' && !Str::contains($lang, 'zh')) { | ||
| 951 | + $text = Translate::tran($text, $lang); | ||
| 952 | + } | ||
| 953 | + $this->logChannel()->info("AI询盘文案", [$ai_command, $text]); | ||
| 954 | + return Common::deal_str($text); | ||
| 955 | + } | ||
| 956 | + | ||
| 957 | + /** | ||
| 958 | + * @return \Psr\Log\LoggerInterface | ||
| 959 | + */ | ||
| 960 | + public function logChannel() | ||
| 961 | + { | ||
| 962 | + return Log::channel('inquiry_relay'); | ||
| 963 | + } | ||
| 964 | + | ||
| 965 | + public function output($message) | ||
| 966 | + { | ||
| 967 | + echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL; | ||
| 968 | + } | ||
| 969 | +} |
-
请 注册 或 登录 后发表评论