postInquiry.php 8.7 KB
<?php
namespace App\Console\Commands\Inquiry;

use App\Models\Inquiry\ReInquiryCount;
use App\Models\Inquiry\ReInquiryDetail;
use App\Models\Inquiry\ReInquiryDetailLog;
use App\Models\Inquiry\ReInquiryForm;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;

/**
 * Class postInquiry
 * @package App\Console\Commands\Inquiry
 */
class postInquiry extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'post_inquiry';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '执行询盘请求';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }


    public function handle()
    {
        while (true) {
            $list = ReInquiryDetailLog::where('status', ReInquiryDetailLog::STATUS_INIT)->where('start_at', '<=', date('Y-m-d H:i:s'))->limit(100)->get();
            if (!$list->count()){
                //5分钟同步一次
                sleep(1);
            }
            // 询盘数据入库
            foreach ($list as $key => $val) {
                //加个锁 避免重复执行
                $lockKey = 're_inquiry_detail_log_lock_' . $val->id;
                if(!Redis::setnx($lockKey, 1)){
                    continue;
                }
                Redis::expire($lockKey, 60);

                $log = ReInquiryDetailLog::find($val->id);
                if($log->status != ReInquiryDetailLog::STATUS_INIT){
                    continue;
                }

                $this->output('开始执行' . $val->id);
                try {
                    $detail = ReInquiryDetail::find($val['detail_id']);
                    if($val['type'] == 1){
                        $this->visit($detail, $val);
                    }else{
                        $res = $this->inquiry($detail, $val);

                        //转发详情
                        $detail->status = $res ? ReInquiryDetail::STATUS_SUCCESS : ReInquiryDetail::STATUS_FAIL;
                        $detail->result = $val['url'];
                        $detail->save();
                        //转发表单
                        if($res){
                            $form = ReInquiryForm::find($detail['form_id']);
                            $form->success_num = $form->success_num + 1;
                            $form->save();
                            Log::channel('inquiry_relay')->info('询盘成功:',[$detail['form_id'], $val->id, getmypid()]);
                        }
                    }
                }catch (\Exception $e){
                    Log::channel('inquiry_relay')->error('inquiry_relay handle error', [$e->getMessage(), $e->getFile(), $e->getLine()]);

                    $val->status = ReInquiryDetailLog::STATUS_FAIL;
                    $val->remark = mb_substr($e->getMessage(), 0, 200);
                    $val->save();
                }
            }
        }
    }

    public function visit(ReInquiryDetail $detail, ReInquiryDetailLog $log){
        $website = 'https://' . $detail['re_website'] . '/';
        if($detail['is_v6']) {
            $data = [
                'ip' => $detail['ip'],
                'url' => $log['url'],
                'device_port' => $detail['device_port'],
                'referrer_url' => $detail['referrer'],
                'user_agent' => $detail['user_agent'],
            ];
            $res = Http::withoutVerifying()->post($website . 'api/traffic_visit/', $data)->json();
            if (empty($res['status']) || $res['status'] != 200) {
                $log->status = ReInquiryDetailLog::STATUS_FAIL;
                $log->remark = mb_substr($res['message'] ?? '', 0, 200);
                $log->save();

                Log::channel('inquiry_relay')->error('inquiry_relay visit error', [$res, $website . 'api/traffic_visit/',$data]);
                return false;
            }
        }else{
            //v4 v5分离项目 往测试链接推
            $split_api_cache_key = 'extend_api_saas_split';
            $mail_urls = Cache::get($split_api_cache_key);
            if(!$mail_urls){
                $client = new \GuzzleHttp\Client();
                $site_array = $client->request('GET', "https://www.quanqiusou.cn/extend_api/saas/split.php", [
                    'proxy' => env('CURL_PROXY'), // 代理服务器地址和端口号
                ])->getBody()->getContents();
                $site_array = json_decode($site_array, true);
                $mail_urls = array_column($site_array, 'main_url');
                Cache::put($split_api_cache_key, $mail_urls, 7200);
            }
            $key = array_search($website, $mail_urls);
            if ($key !== false) {
                // 分离项目 推送到测试链接
                $website = $site_array[$key]['test_url'];
            }

            $data = [
                'action' => 'stats_init',
                'assort' => 0,
                'referrer' => $detail['referrer'],
                'currweb' => $log['url'],
                'user_agent' => $detail['user_agent'],
                "ip" => $detail['ip'],
            ];
            $res = Http::get($website . 'wp-admin/admin-ajax.php', $data);
            $status = $res->status();
            if($status != 200){
                $log->status = ReInquiryDetailLog::STATUS_FAIL;
                $log->remark = mb_substr($res->body() ?? '', 0 ,200);
                $log->save();

                Log::channel('inquiry_relay')->error('inquiry_relay v4|v5 visit error', [$res->body(), $website . 'wp-admin/admin-ajax.php', $data]);
                return false;
            }
        }
        $log->status = ReInquiryDetailLog::STATUS_SUCCESS;
        $log->save();
        return true;
    }

    public function inquiry(ReInquiryDetail $detail, ReInquiryDetailLog $log){
        $website = 'https://' . $detail['re_website'] . '/';
        // v6
        if($detail['is_v6']) {
            $data = [
                'name' => $detail['name'],
                'phone' => $detail['phone'],
                'message' => $detail['message'],
                'submit_ip' => $detail['ip'],
                'refer' =>  $log->url,
            ];
            if($detail->email){
                $data['email'] = $detail->email;
            }else{
                $data['__amp_source_origin'] = trim($website, '/');
            }

            $res = Http::withoutVerifying()->withHeaders(['User-Agent' => $detail['user_agent']])->post($website . 'api/inquiryQd/', $data)->json();
            if(empty($res['code']) || $res['code'] != 200){
                $log->status = ReInquiryDetailLog::STATUS_FAIL;
                $log->remark = mb_substr($res['message'] ?? '', 0, 200);
                $log->save();

                Log::channel('inquiry_relay')->error('inquiry_relay v6 inquiry error', [$res, $website . 'api/inquiryQd/', $data]);
                return false;
            }
        }else{
            $data = [
                'name' => $detail['name'],
                'phone' => $detail['phone'],
                'message' => $detail['message'],
                'email' => $detail['email'],
                'ip' => $detail['ip'],
                'token' => md5($log->url . $detail['name'] . $detail['ip'] . date("Y-m-d")),
                'refer' => $log->url,
                'submit_time' => date('Y-m-d H:i:s'),
                'source' => 5,
            ];

            $result = Http::withoutVerifying()->post('https://form.globalso.com/api/external-interface/add/fa043f9cbec6b38f', $data);
            $res = $result->json();
            //兼容接口返回格式
            if(!empty($res['data'][0]['status'])){
                $res['data'][0]['code'] = $res['data'][0]['status'] == 'success' ? 200 : 400;
                $res['message'] = $res['data'][0]['msg'];
            }
            if(empty($res['data'][0]['code']) || !in_array($res['data'][0]['code'], [200,300])){
                $log->status = ReInquiryDetailLog::STATUS_FAIL;
                $log->remark = mb_substr($res['message'] ?? '', 0, 200);
                $log->save();

                Log::channel('inquiry_relay')->error('inquiry_relay v4|v5 inquiry error', [$result->body(), 'https://form.globalso.com/api/external-interface/add/fa043f9cbec6b38f', $data]);
                return false;
            }
        }
        $log->status = ReInquiryDetailLog::STATUS_SUCCESS;
        $log->save();

        //统计
        ReInquiryCount::addInquiryNum($detail['id'], $detail['re_website']);

        return true;
    }


    public function output($message)
    {
        echo date('Y-m-d H:i:s') . ' | ' . $message . PHP_EOL;
    }
}