InquiryController.php 4.5 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhl
 * Date: 2023/10/25
 * Time: 14:38
 */

namespace App\Http\Controllers\Api;

use App\Console\Commands\CosService;
use App\Exceptions\TipException;
use App\Http\Controllers\Controller;
use App\Models\Project\Project;
use App\Models\SyncSubmitTask\SyncSubmitTask;
use App\Models\WebSetting\WebSettingFormBack;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

/**
 * Class InquiryController
 * @package App\Http\Controllers\Api
 */
class InquiryController extends Controller
{
    /**
     * C端表单提交 询盘信息
     * @param Request $request
     * @return mixed
     */
    public function inquiry(Request $request)
    {
        $data = $request->all();

        $black_ips = ['203.86.233.27', '37.19.221.202'];
        if(in_array(request()->getClientIp(), $black_ips)){
            $this->responseA();
        }


        @file_put_contents(storage_path('logs/form_submit_' . date('Y-m-d') . '.log'), var_export(date('Y-m-d H:i:s') . "-表单提交数据:" . json_encode($data), true) . PHP_EOL, FILE_APPEND);

        //同一ip 5秒内超过5次 限制1小时
        $ip = $request->ip();
        $lock_key = 'lock_ip_' . $ip;
        $rate_key = 'rate_limit:' . $ip;
        if(Cache::has($lock_key)){
            $this->responseA();
        }
        if (Cache::has($rate_key)) {
            $count = Cache::get($rate_key);
            if ($count >= 5) {
                Cache::put($lock_key, 1, 3600);
                $this->responseA();
            }
            Cache::increment($rate_key);
        } else {
            Cache::put($rate_key, 1, 5);
        }

        try {
            $files = $request->allFiles();
            foreach ($files as $key => $file) {
                $cos = new CosService();
                $fileinfo = $cos->checkFile($file);
                $fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();
                $path = $cos->uploadFile($file, '/inquiry/'. date('Ymd'), $fileName);
                $data[$key] = [
                    'path' => $path,
                    'original_name' => $fileinfo['name'],
                ];
            }
        }catch (TipException $e){
            $this->responseA([], 400, $e->getMessage());
        }catch (\Exception $e){
            $this->responseA([], 400, 'File upload failed');
        }

        //异步处理
        if(!SyncSubmitTask::addTask(SyncSubmitTask::TYPE_INQUIRY, $data)){
            $this->responseA([], 400, 'error');
        }

        $this->responseA($this->inquiryResult());
    }

    /**
     * 收集邮箱或者手机号等其他信息
     * @param Request $request
     * @return mixed
     */
    public function inquiryOtherInfo(Request $request)
    {
       return $this->inquiry($request);
    }

    /**
     * 起点表单询盘
     * @param Request $request
     * @return mixed
     * @author zbj
     * @date 2023/12/8
     */
    public function inquiryQd(Request $request){
        $data = $request->post();
        @file_put_contents(storage_path('logs/form_submit_' . date('Y-m-d') . '.log'), var_export(date('Y-m-d H:i:s') . "-起点表单提交数据:" . json_encode($data), true) . PHP_EOL, FILE_APPEND);

        //Name,Email,Phone,Message,submit_ip,submit_time,refer
        $submit_ip = $data['submit_ip'] ?? '';
        $submit_time = $data['submit_time'] ?? '';
        $refer = $data['refer'] ?? '';
        unset($data['submit_ip']);
        unset($data['submit_time']);
        unset($data['refer']);

        //异步处理
        if(!SyncSubmitTask::addTask(SyncSubmitTask::TYPE_INQUIRY, $data, $submit_ip, $refer, $submit_time)){
            $this->responseA([], 400, 'error');
        }
        $this->responseA($this->inquiryResult());
    }

    /**
     * @return mixed|string[]
     * @author zbj
     * @date 2023/12/4
     */
    protected function inquiryResult(){
        $domain = request()->getHost();
        $cache_key = 'inquiry_form_back_' . $domain;
        $result = Cache::get($cache_key);
        if(!$result){
            $result = [
                'message' => "",
                'url' => ""
            ];
            $projectDomain = Project::getProjectByDomain($domain);
            $webFormBack = WebSettingFormBack::where("project_id", $projectDomain['project_id']??0)->first();
            if (!empty($webFormBack)) {
                $result["message"] = $webFormBack->message ?? "";
                $result["url"] = $webFormBack->url ?? "";
                Cache::put($cache_key, $result, 3600);
            }
        }

        return $result;
    }
}