InquiryFormData.php
3.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
<?php
namespace App\Models\Inquiry;
use App\Helper\FormGlobalsoApi;
use App\Models\Base;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;
/**
* Class InquiryFormData
* @package App\Models\Inquiry
* @author zbj
* @date 2023/12/4
*/
class InquiryFormData extends Base
{
use SoftDeletes;
//设置关联表名
/**
* @var mixed
*/
protected $connection = "custom_mysql";
protected $table = 'gl_inquiry_form_data';
/**
* 根据提交数据的key获取表单id
* @param $form_id
* @param $domain
* @param $ip
* @param $country
* @param $referer
* @param $user_agent
* @param $submit_at
* @param $data
* @return mixed
* @author zbj
* @date 2023/12/4
*/
public static function saveData($form_id, $domain, $ip, $country, $referer, $user_agent, $submit_at, $data){
//数据标识
ksort($data);
$sign = md5(json_encode($data));
//5分钟内是否有重复数据
$is_exist = self::where('sign', $sign)->where('created_at', '>', date('Y-m-d H:i:s', strtotime('-5 minute')))->first();
if($is_exist){
return true;
}
$model = new self();
$model->form_id = $form_id;
$model->domain = $domain;
$model->ip = $ip;
$model->country = $country;
$model->referer = $referer;
$model->user_agent = $user_agent;
$model->submit_at = $submit_at;
$model->data = $data;
$model->sign = $sign;
$model->save();
if(!empty($data['name']) && !empty($data['email']) && !empty($data['message'])){
unset($data['globalso-domain_host_url']);
unset($data['globalso-domain']);
unset($data['globalso-edition']);
unset($data['globalso-date']);
//推送邮件发送
$has_file = false;
foreach ($data as $k => $v){
if(is_array($v)){
$has_file = true;
break;
}
//其他字段补充到message里
if(!in_array($k, ['name', 'email', 'message', 'phone', 'ip', 'date', 'cname', 'domain', 'edition', 'domain_host_url'])){
$data['message'].= "<br/>" . $k .': ' . $v;
}
}
!$has_file && (new FormGlobalsoApi())->submitInquiry($ip, $referer, $submit_at, $data);
}
return true;
}
public function setDataAttribute($value)
{
$this->attributes['data'] = json_encode($value);
}
public function getDataAttribute($value)
{
return json_decode($value, true);
}
/**
* 非默认表单的数量统计
* @author zbj
* @date 2023/12/12
*/
public static function getCount($submit_at = []){
return self::leftjoin('gl_inquiry_form', 'gl_inquiry_form.id', '=', 'gl_inquiry_form_data.form_id')
->where('gl_inquiry_form.is_default', 0)
->when($submit_at, function ($query, $submit_at) {
$query->whereBetween('submit_at',[$submit_at[0], $submit_at[1]]);
})
->count();
}
/**
* 非默认表单的国家统计
* @author zbj
* @date 2023/12/12
*/
public static function getCountryCount($submit_at = []){
return self::leftjoin('gl_inquiry_form', 'gl_inquiry_form.id', '=', 'gl_inquiry_form_data.form_id')
->where('gl_inquiry_form.is_default', 0)
->when($submit_at, function ($query, $submit_at) {
$query->whereBetween('submit_at',[$submit_at[0], $submit_at[1]]);
})
->select("country",DB::raw('COUNT(*) as count'))->groupBy('country')->get()->toArray();
}
}