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

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

/**
 * 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) {
                $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();
                        }
                    }
                }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){
        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($detail['re_website'] . 'api/traffic_visit/', $data)->json();
            if (empty($res['status']) || $res['status'] != 200) {
                $log->status = ReInquiryDetailLog::STATUS_FAIL;
                $log->remark = $res['message'] ?? '';
                $log->save();

                Log::channel('inquiry_relay')->error('inquiry_relay visit error', [$res, $detail['re_website'] . 'api/traffic_visit/',$data]);
                return false;
            }
        }else{
            //v4 v5分离项目 往测试链接推
            $website = $detail['re_website'];
            $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');
            $key = array_search('https://' . $detail['re_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)->json();
            if($res != 200){
                $log->status = ReInquiryDetailLog::STATUS_FAIL;
                $log->remark = $res['message'] ?? '';
                $log->save();

                Log::channel('inquiry_relay')->error('inquiry_relay v4|v5 visit error', [$res, $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){
        // 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($detail['re_website'], '/');
            }

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

                Log::channel('inquiry_relay')->error('inquiry_relay v6 inquiry error', [$res, $detail['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,
            ];

            $res = Http::withoutVerifying()->post('https://form.globalso.com/api/external-interface/add/fa043f9cbec6b38f', $data)->json();
            if(empty($res['data'][0]['code']) || $res['data'][0]['code'] != 200){
                $log->status = ReInquiryDetailLog::STATUS_FAIL;
                $log->remark = $res['message'] ?? '';
                $log->save();

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


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