InquiryController.php
3.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
<?php
namespace App\Http\Controllers\Bside;
use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Http\Logic\Bside\InquiryLogic;
use App\Http\Requests\Bside\InquiryRequest;
use App\Rules\Ids;
use App\Services\BatchExportService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
/**
* 精准询盘
* Class InquiryController
* @package App\Http\Controllers\Bside
* @author zbj
* @date 2023/5/4
*/
class InquiryController extends BaseController
{
public function index(InquiryLogic $logic)
{
$map = [];
if(!empty($this->param['search'])){
$map[] = ['name|email|content', 'like', "%{$this->param['search']}%"];
}
$sort = ['id' => 'desc'];
$data = $logic->getList($map, $sort, ['id', 'name', 'email', 'phone', 'url', 'ip', 'ip_country', 'status', 'created_at']);
return $this->success($data);
}
public function info(Request $request, InquiryLogic $logic){
$request->validate([
'id'=>'required'
],[
'id.required' => 'ID不能为空'
]);
$data = $logic->getInfo($this->param['id']);
return $this->success(Arr::twoKeepKeys($data, ['id', 'name', 'email', 'phone', 'url', 'ip', 'ip_country', 'status', 'content', 'trans_content', 'created_at']));
}
public function delete(Request $request, InquiryLogic $logic)
{
$request->validate([
'ids'=>['required', new Ids()]
],[
'ids.required' => 'ID不能为空'
]);
$data = $logic->delete($this->param['ids']);
return $this->success($data);
}
/**
* 导出
* @param InquiryLogic $logic
* @return \Illuminate\Http\JsonResponse
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
* @author zbj
* @date 2023/5/8
*/
public function export(InquiryLogic $logic)
{
$sort = ['id' => 'desc'];
//最多到1w条
$data = $logic->getList([], $sort, ['name', 'email', 'phone', 'url', 'ip', 'ip_country', 'content', 'created_at'], 10000);
$data = $data['list'] ?? [];
foreach ($data as &$item){
$item['ip_address'] = "{$item['ip_country']}({$item['ip']})";
}
$map = [
'created_at' => '询盘发送时间',
'name' => '姓名',
'email' => '邮箱',
'phone' => '电话',
'ip_address' => '访问国家/地区(IP)',
'url' => '发送页面',
'content' => '询盘内容',
];
//生成文件,发送到客户端
$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); //直接下载
return $this->success(['url' => $fileurl]);
}
}