作者 赵彬吉

update

  1 +<?php
  2 +
  3 +namespace App\Http\Controllers\Api;
  4 +
  5 +use App\Exceptions\InquiryFilterException;
  6 +use App\Models\SyncSubmitTask\SyncSubmitTask;
  7 +use App\Services\CosService;
  8 +use Illuminate\Http\Request;
  9 +
  10 +/**
  11 + * Class InquiryController
  12 + * @package App\Http\Controllers\Api
  13 + * @author zbj
  14 + * @date 2024/2/2
  15 + */
  16 +class InquiryController extends BaseController
  17 +{
  18 + /**
  19 + * 提交询盘
  20 + * C端部署自己服务器的
  21 + * @param Request $request
  22 + * @return false|string
  23 + * @author zbj
  24 + * @date 2024/2/2
  25 + */
  26 + public function submit(Request $request){
  27 + $data = $request->post();
  28 + @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);
  29 +
  30 + try {
  31 + $files = $request->allFiles()['data'];
  32 + foreach ($files as $key => $file) {
  33 + $cos = new CosService();
  34 + $fileinfo = $cos->checkInquiryFile($file);
  35 + $fileName = uniqid().rand(10000,99999).'.'.$file->getClientOriginalExtension();
  36 + $path = $cos->uploadFile($file, '/inquiry/'. date('Ymd'), $fileName);
  37 + $data['data'][$key] = [
  38 + 'path' => $path,
  39 + 'original_name' => $fileinfo['name'],
  40 + ];
  41 + }
  42 + }catch (InquiryFilterException $e){
  43 + return $this->error($e->getMessage());
  44 + }catch (\Exception $e){
  45 + return $this->error('File upload fail');
  46 + }
  47 +
  48 + //异步处理
  49 + if(!SyncSubmitTask::addTask(SyncSubmitTask::TYPE_INQUIRY, $data)){
  50 + return $this->error();
  51 + }
  52 + return $this->success();
  53 + }
  54 +}
@@ -28,4 +28,42 @@ class SyncSubmitTask extends Model @@ -28,4 +28,42 @@ class SyncSubmitTask extends Model
28 protected $casts = [ 28 protected $casts = [
29 'data' => 'array', 29 'data' => 'array',
30 ]; 30 ];
  31 +
  32 + /**
  33 + * @param $type
  34 + * @param $data
  35 + * @return bool
  36 + * @author zbj
  37 + * @date 2023/11/28
  38 + */
  39 + public static function addTask($type, $data): bool
  40 + {
  41 + if (empty($data)) {
  42 + return false;
  43 + }
  44 + try {
  45 + $data = [
  46 + 'data' => $data['data'],
  47 + 'domain' => !empty($data['domain']) ? $data['domain'] : request()->getHost(),
  48 + 'ip' => !empty($data['ip']) ? $data['ip'] : request()->getClientIp(),
  49 + 'referer' => !empty($data['referer']) ? $data['referer'] : request()->header('Referer'),
  50 + 'user_agent' => !empty($data['user_agent']) ? $data['user_agent'] : request()->header('user_agent'),
  51 + ];
  52 +
  53 + if(empty($data['referer']) || empty($data['user_agent']) || empty($data['data'])){
  54 + return false;
  55 + }
  56 +
  57 + $model = new self();
  58 + $model->type = $type;
  59 + $model->data = $data;
  60 + !empty($data['submit_time']) && $model->created_at = $data['submit_time'];
  61 + $model->save();
  62 +
  63 + } catch (\Exception $e) {
  64 + Log::error('SyncSubmitTask addTask error', ['msg' => $e->getMessage(), 'data' => $data]);
  65 + return false;
  66 + }
  67 + return true;
  68 + }
31 } 69 }
@@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
2 2
3 namespace App\Services; 3 namespace App\Services;
4 4
  5 +use App\Exceptions\InquiryFilterException;
5 use App\Utils\LogUtils; 6 use App\Utils\LogUtils;
6 use Qcloud\Cos\Client; 7 use Qcloud\Cos\Client;
7 /** 8 /**
@@ -116,4 +117,28 @@ class CosService @@ -116,4 +117,28 @@ class CosService
116 } 117 }
117 } 118 }
118 119
  120 + /**
  121 + * @param $file
  122 + * @return array
  123 + * @throws \Exception
  124 + * @author zbj
  125 + * @date 2023/12/12
  126 + */
  127 + public function checkInquiryFile($file){
  128 + $size = $file->getSize();
  129 + if($size/1024/1024 > 20){
  130 + throw new InquiryFilterException('Your file size exceeds the limit. Please upload a file no larger than 20MB.');
  131 + }
  132 + $extension = $file->getClientOriginalExtension();
  133 +// JPEG (JPG) PDF DWG STEP(STP)IGS word xlsx
  134 + if(!in_array(strtolower($extension), ['png','jpg','jpeg', 'pdf', 'dwg', 'step', 'stp', 'igs','doc','docx','xls','xlsx'])){
  135 + throw new InquiryFilterException('Please upload file in png, jpg, jpeg, pdf, dwg, step, stp, igs, doc, docx, xls or xlsx format.');
  136 + }
  137 + return [
  138 + 'size' => $size,
  139 + 'extension' => $extension,
  140 + 'name' => $file->getClientOriginalName(),
  141 + 'mime' => $file->getMimeType(),
  142 + ];
  143 + }
119 } 144 }
@@ -22,4 +22,5 @@ Route::any('traffic_visit', [\App\Http\Controllers\Api\NoticeController::class, @@ -22,4 +22,5 @@ Route::any('traffic_visit', [\App\Http\Controllers\Api\NoticeController::class,
22 Route::get('optimize_project_list', [\App\Http\Controllers\Api\PrivateController::class, 'optimizeProjectList'])->name('api.optimize_project_list'); 22 Route::get('optimize_project_list', [\App\Http\Controllers\Api\PrivateController::class, 'optimizeProjectList'])->name('api.optimize_project_list');
23 Route::get('get_project_route', [\App\Http\Controllers\Api\PrivateController::class, 'getProjectRoute'])->name('api.get_project_route'); 23 Route::get('get_project_route', [\App\Http\Controllers\Api\PrivateController::class, 'getProjectRoute'])->name('api.get_project_route');
24 Route::any('get_product_images', [\App\Http\Controllers\Api\ProductController::class, 'getImages'])->name('api.get_product_images'); 24 Route::any('get_product_images', [\App\Http\Controllers\Api\ProductController::class, 'getImages'])->name('api.get_product_images');
  25 +Route::post('inquiry_submit', [\App\Http\Controllers\Api\InquiryController::class, 'submit'])->name('api.inquiry_submit');
25 26