InquiryForm.php
3.2 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
<?php
namespace App\Models\Inquiry;
use App\Models\Base;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;
/**
* Class InquiryForm
* @package App\Models\Inquiry
* @author zbj
* @date 2023/12/5
*/
class InquiryForm extends Base
{
use SoftDeletes;
//设置关联表名
/**
* @var mixed
*/
protected $connection = "custom_mysql";
protected $table = 'gl_inquiry_form';
/**
* 预设字段名称
* @author zbj
* @date 2023/12/5
*/
public static function fieldMap($field = ''){
$map = [
'name' => '姓名',
'email' => '邮箱',
'phone' => '电话',
'mobile' => '电话',
'message' => '询盘内容',
'company' => '公司名称',
'file' => '文件'
];
if($field){
return $map[Str::lower($field)] ?? Str::studly($field);
}
return $map;
}
/**
* @author zbj
* @date 2023/12/5
*/
public static function getField($form_id){
$cache_key = 'inquiry_form_field_' . $form_id;
$field = Cache::get($cache_key);
if(!$field){
$field = self::where('id', $form_id)->value('field');
$field && Cache::set($cache_key, $field, 3600);
}
return $field;
}
/**
* 根据提交数据的key获取表单id
* @param $data
* @return mixed
* @author zbj
* @date 2023/12/4
*/
public static function getFromId($data){
unset($data['globalso-domain_host_url']);
unset($data['globalso-domain']);
unset($data['globalso-edition']);
unset($data['globalso-date']);
foreach ($data as $k => $v){
if(in_array($k, ['name', 'email', 'message']) && empty($v)){
//特殊处理1986的表单
if($k == 'message' && $data['project_id'] == 1986){
continue;
}
unset($data[$k]);
}
}
ksort($data);
$field = array_keys($data);
$sign = md5(json_encode($field));
$model = self::where('sign', $sign)->first();
if(!$model){
$model = new self();
$model->sign = $sign;
$model->field = $field;
if(!empty($data['name']) && !empty($data['email']) && !empty($data['message'])){
$has_file = false;
foreach ($data as $v){
if (is_array($v)){
$has_file = true;
break;
}
}
!$has_file && $model->is_default = 1;
}
//移动端询盘表单
if($sign == md5(json_encode(["__amp_source_origin", "message", "name", "phone"]))){
$model->title = '移动端询盘';
}
$model->save();
}
return $model->id;
}
public function setFieldAttribute($value)
{
$this->attributes['field'] = json_encode($value);
}
public function getFieldAttribute($value)
{
return json_decode($value, true);
}
}