InquiryController.php
6.0 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
<?php
namespace App\Http\Controllers\Bside\Inquiry;
use App\Enums\Common\Code;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Logic\Bside\Inquiry\InquiryLogic;
use App\Models\Inquiry\InquiryForm;
use App\Models\Inquiry\PhoneData;
use App\Rules\Ids;
use App\Services\BatchExportService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
/**
* 精准询盘
* Class InquiryInfoController
* @package App\Http\Controllers\Bside
* @author zbj
* @date 2023/5/4
*/
class InquiryController extends BaseController
{
public function form_list(){
$list = InquiryForm::where('is_default', 0)->get();
$data = $list->toArray();
foreach ($data as &$item){
$field_text = [];
foreach ($item['field'] as $v){
$field_text[$v] = InquiryForm::fieldMap($v);
}
$item['field_text'] = $field_text;
}
$this->response('success',Code::SUCCESS,$data);
}
public function index(InquiryLogic $logic)
{
if(!empty($this->param['form_id'])){
$data = $logic->getFormDataList();
}elseif(($this->param['type']??'') == 'other'){
$data = $logic->getOtherList();
}else{
$data = $logic->getApiList();
}
if(!empty($data) && !empty($data['list'])){
foreach ($data['list'] as $k => &$v){
if(isset($v['phone']) && !empty($v['phone'])){
$phoneInfo = (new PhoneData())->read(['phone'=>$v['phone']]);
if($phoneInfo === false){
$v['phone_data'] = [];
}else{
$v['phone_data'] = json_decode($phoneInfo['data']);
}
}
}
}
$this->response('success',Code::SUCCESS,$data);
}
/**
* @remark :发送请求(获取手机号码对应信息)
* @name :sendMobileVerifyData
* @author :lyh
* @method :post
* @time :2024/9/5 17:44
*/
public function sendMobileVerifyData(InquiryLogic $logic){
$this->request->validate([
'phone' => 'required',
],[
'phone.required' => 'phone不能为空'
]);
$data = $logic->sendMobileVerifyData($this->param['phone']);
$this->response('success',Code::SUCCESS,$data);
}
public function info(Request $request, InquiryLogic $logic){
$request->validate([
'id' => 'required',
],[
'id.required' => 'ID不能为空'
]);
if(!empty($this->param['form_id'])){
$data = $logic->getFormDataInfo($this->param['id'], $this->param['form_id']);
}elseif(($this->param['type']??'') == 'other'){
$data = $logic->getOtherInfo($this->param['id']);
}else{
$data = $logic->getInfo($this->param['id']);
}
$this->response('success',Code::SUCCESS,$data);
}
public function delete(Request $request, InquiryLogic $logic)
{
$request->validate([
'ids'=>['required', new Ids()]
],[
'ids.required' => 'ID不能为空'
]);
if(!empty($this->param['form_id'])){
$logic->deleteFormData($this->param['ids'], ['form_id' => $this->param['form_id']]);
}elseif(($this->param['type']??'') == 'other'){
$logic->deleteOther($this->param['ids']);
}else{
$logic->delete($this->param['ids']);
}
$this->response('success');
}
/**
* 导出
* @param InquiryLogic $logic
* @return \Illuminate\Http\JsonResponse
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
* @author zbj
* @date 2023/5/8
*/
public function export(InquiryLogic $logic)
{
if(!empty($this->param['form_id'])){
$data = $logic->getFormDataList(true);
$field = InquiryForm::getField($this->param['form_id']);
$map = [
'submit_time' => '询盘发送时间',
];
foreach ($field as $v) {
$map[$v] = InquiryForm::fieldMap($v);
}
$map['ip_address'] = '访问国家/地区(IP)';
$map['refer'] = '发送页面';
}elseif(($this->param['type']??'') == 'other'){
$data = $logic->getOtherList(true);
$map = [
'submit_time' => '询盘发送时间',
'email' => '邮箱',
'ip_address' => '访问国家/地区(IP)',
'refer' => '发送页面',
];
}else{
$data = $logic->getApiList(true);
$map = [
'submit_time' => '询盘发送时间',
'name' => '姓名',
'email' => '邮箱',
'phone' => '电话',
'ip_address' => '访问国家/地区(IP)',
'refer' => '发送页面',
'message' => '询盘内容',
];
}
$data = $data['list'] ?? [];
foreach ($data as &$item){
$item['ip_address'] = "{$item['country']}({$item['ip']})";
if(!empty($this->param['form_id'])){
$item = array_merge($item, $item['data']);
}
foreach ($map as $field => $name) {
if(is_array($item[$field])){
$item[$field] = implode(',',$item[$field]);
}
if (Str::startsWith($item[$field], '=')) {
$item[$field] = "'" . $item[$field];
}
}
}
//生成文件,发送到客户端
$table = new BatchExportService("询盘数据导出");
$file = $table->head($map)->data($data)->save();
if (!$file) {
throw new \Exception('文件生成失败,请重试');
}
$fileurl = Storage::disk('runtime')->url($file);
// return Storage::disk('runtime')->download($file); //直接下载
$this->response('success',Code::SUCCESS,['url' => $fileurl]);
}
}