InquiryController.php 6.7 KB
<?php

namespace App\Http\Controllers\Api;

use App\Enums\Common\Code;
use App\Exceptions\InquiryFilterException;
use App\Models\Domain\DomainInfo;
use App\Models\Inquiry\ReInquiryCount;
use App\Models\Product\Category;
use App\Models\Product\Product;
use App\Models\Project\Project;
use App\Models\SyncSubmitTask\SyncSubmitTask;
use App\Models\Visit\Visit;
use App\Models\WebSetting\WebLanguage;
use App\Models\WebSetting\WebSettingFormBack;
use App\Services\CosService;
use App\Services\ProjectServer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

/**
 * Class InquiryController
 * @package App\Http\Controllers\Api
 * @author zbj
 * @date 2024/2/2
 */
class InquiryController extends BaseController
{
    /**
     * 提交询盘
     * C端部署自己服务器的
     * @param Request $request
     * @return false|string
     * @author zbj
     * @date 2024/2/2
     */
    public function submit(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);

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

        //异步处理
        if(!SyncSubmitTask::addTask(SyncSubmitTask::TYPE_INQUIRY, $data)){
            return $this->error();
        }
        //return $this->success();
        //新增表单提,返回跳转链接
        return $this->responseA($this->inquiryResult());
    }

    /**
     * 新增表单提交返回跳转链接
     */
    protected function inquiryResult(){
        $domain = request()->getHost();
        $cache_key = 'inquiry_form_back_' . $domain;
        $result = Cache::get($cache_key);
        if(!$result){
            $result = [
                'message' => "1",
                'url' => "1",
                'other' => "1"
            ];
            $projectDomain = Project::getProjectByDomain($domain);
            $webFormBack = WebSettingFormBack::where("project_id", $projectDomain['id']??0)->first();
            if (!empty($webFormBack)) {
                $result["message"] = $webFormBack->message ?? "";
                $result["url"] = $webFormBack->url ?? "";
                $result["other"] = $webFormBack->other ?? "";
                Cache::put($cache_key, $result, 3600);
            }
        }
        return $result;
    }

    /**
     * @remark :修改路由状态
     * @name   :editInquiryStatus
     * @author :lyh
     * @method :post
     * @time   :2024/3/22 15:41
     */
    public function editInquiryStatus(){
        $this->request->validate([
            'domain'=>'required',
            'ip'=>'required',
            'updated_date'=>'required',
        ],[
            'domain.required' => 'domain不能为空',
            'ip.required' => 'ip不能为空',
            'updated_date.required' => '日期不能为空',
        ]);
        $project = Project::getProjectByDomain($this->param['domain']);
        if(!$project){
            $this->response('域名不存在',Code::SYSTEM_ERROR);
        }
        ProjectServer::useProject($project->id);
        $customerVisitModel = new Visit();
        $info = $customerVisitModel->read([
            'ip'=>$this->param['ip'],
            'updated_date'=>$this->param['updated_date']
        ]);
        if($info === false){
            $this->response('当前记录不存在',Code::SYSTEM_ERROR);
        }
        try {
            $customerVisitModel->edit(['is_inquiry'=>1],['id'=>$info['id']]);
        }catch (\Exception $e){
            $this->response('操作失败',Code::SYSTEM_ERROR);
        }
        DB::disconnect('custom_mysql');
        $this->response('success');
    }

    public function getRandomIp(Request $request){
        $country = $request->input('country', '');
        $where = [];
        $country && $where['ip_area'] = $country;
        $ipdata = DB::table('gl_xunpan_ipdata')->where($where)->inRandomOrder()->first();
        return $this->success($ipdata);
    }

    public function getVisitUrl(Request $request){
        $domain = $request->input('domain', '');
        if(!$domain){
            return $this->error('域名必填');
        }
        $project = Project::getProjectByDomain($domain);
        if(!$project){
            return $this->error('项目不存在');
        }
        $project_id = $project->id;

        ProjectServer::useProject($project_id);
        //已发布产品分类页面
        $data['urls_cats'] = DB::connection('custom_mysql')->table('gl_product_category')
            ->where('project_id', $project_id)->where('status',  Category::STATUS_ACTIVE)
            ->whereNull('deleted_at')
            ->pluck('route','id')->toArray();

        //已发布单页面
        $data['urls_page'] = [];
        //已发布产品详情页
        $data['urls_details'] =  DB::connection('custom_mysql')->table('gl_product')
            ->where('project_id', $project_id)->where('status', Product::STATUS_ON)
            ->whereNull('deleted_at')
            ->pluck('route', 'id')->toArray();

        $data['urls_cats'] = array_merge($data['urls_cats'], $data['urls_page']);
        if(empty($data['urls_cats'])){
            $data['urls_cats'] = $data['urls_details'];
        }
        $data['lang'] = WebLanguage::getLangById($project['main_lang_id']??1)['short'];
        DB::disconnect('custom_mysql');
        return $this->success($data);
    }

    public function getFbInquiry(Request $request){
        $domain = $request->input('domain', '');
        if(!$domain){
            return $this->error('域名必填');
        }
        $result = ReInquiryCount::where('domain', 'like', '%'.$domain.'%')->first();
        if($result){

            $return = 0;  //所有都关闭或删除就不返回
            foreach ($result->tasks as $task){
                $return += (!$task['is_del'] && $task['status']);
            }
            if(!$return){
                return $this->success([]);
            }
        }
        return $this->success($result?:[]);
    }
}