InquiryController.php
4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?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;
}
}