InquiryController.php
6.7 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?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->response($this->inquiryResult());
}
/**
* 新增表单提交返回跳转链接
*/
protected function inquiryResult(){
$domain = request()->getHost();
$cache_key = 'inquiry_form_back_' . $domain;
$result = Cache::get($cache_key);
if(!$result){
$result = [
'message' => "",
'url' => "",
'other' => ""
];
$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 ?? "";
$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?:[]);
}
}