Visit.php
2.9 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
<?php
namespace App\Models\Visit;
use App\Models\Base;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
/**
* Class Visit
* @package App\Models
* @author zbj
* @date 2023/5/22
*/
class Visit extends Base
{
//设置关联表名
protected $table = 'gl_customer_visit';
//连接数据库
protected $connection = 'custom_mysql';
protected $appends = ['device_text'];
protected $fillable = ['id','ip','device_port','country','city','url','referrer_url','depth','domain','is_inquiry','original_id','updated_date', 'created_at', 'user_agent'];
const DEVICE_PC = 1;
const DEVICE_MOBILE = 2;
public static function deviceMap(){
return [
self::DEVICE_PC => 'PC',
self::DEVICE_MOBILE => '移动端'
];
}
/**
* @return string
* @author zbj
* @date 2023/5/22
*/
public function getDeviceTextAttribute(){
return self::deviceMap()[$this->device_port] ?? '';
}
/**
* @author zbj
* @date 2023/9/11
*/
public function yesterdayIpCount(){
return self::deviceMap()[$this->device_port] ?? '';
}
/**
* 访问写入
*/
public static function saveData($data, $date)
{
if(!$date){
$date = date('Y-m-d');
}
//判断IP当天是否有一条数据
$visit = Visit::where("ip",$data['ip'])->where("created_at",">=",Carbon::make($date)->startOfDay())
->where("created_at","<=",Carbon::make($date)->endOfDay())
->first();
DB::connection('custom_mysql')->beginTransaction();
if (!empty($visit) && $visit->count() >= 1){
//当天已存在IP访问记录
try{
$data["customer_visit_id"] = $visit->id;
VisitItem::create($data);
Visit::where("id",$visit->id)->update(['depth' => $visit->depth + 1]);
DB::connection('custom_mysql')->commit();
}catch (\Exception $e){
DB::connection('custom_mysql')->rollBack();
throw new \Exception($e->getMessage());
}
}else{
//第一次访问
try{
$id = Visit::create($data)->id;
$data["customer_visit_id"] = $id;
VisitItem::create($data);
DB::connection('custom_mysql')->commit();
}catch (\Exception $e){
DB::connection('custom_mysql')->rollBack();
throw new \Exception($e->getMessage());
}
}
return true;
}
public static function isInquiry($ip){
$visit = Visit::where("ip",$ip)->where("created_at",">=",Carbon::now()->today()->startOfDay())
->where("created_at","<=",Carbon::now()->today()->endOfDay())
->first();
if($visit){
$visit->is_inquiry = 1;
$visit->save();
}
return true;
}
}