UserAddress.php
3.3 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
<?php
namespace addons\shopro\model;
use think\Model;
use addons\shopro\model\Area;
use traits\model\SoftDelete;
/**
* 用户地址模型
*/
class UserAddress extends Model
{
use SoftDelete;
protected $name = 'shopro_user_address';
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = 'deletetime';
protected $hidden = ['createtime', 'updatetime', 'deletetime'];
// 追加属性
protected $append = [
];
public static function getUserAddress()
{
$user = User::info();
return self::all([
'user_id' => $user->id,
'deletetime' => null
]);
}
public static function getUserDefaultAddress()
{
$user = User::info();
return self::get([
'user_id' => $user->id,
'deletetime' => null,
'is_default' => '1'
]);
}
public static function edit($params)
{
$user = User::info();
extract($params);
$area = Area::get($area_id);
$city = Area::get($area->pid);
$province = Area::get($city->pid);
$is_default = (isset($is_default) && $is_default) ? '1' : '0';
if ($is_default) {
self::where(['user_id' => $user->id, 'is_default' => '1']) ->update(['is_default' => '0']);
}
if (!isset($id) || $id == 0) {
$edit = self::create([
'consignee' => $consignee,
'phone' => $phone,
'province_id' => $province->id,
'province_name' => $province->name,
'city_id' => $city->id,
'city_name' => $city->name,
'area_id' => $area->id,
'area_name' => $area->name,
'is_default' => $is_default,
'latitude' => (isset($latitude) && $latitude) ? $latitude : null,
'longitude' => (isset($longitude) && $longitude) ? $longitude : null,
'user_id' => $user->id,
'address' => $address
]);
}else{
$edit = self::update([
'consignee' => $consignee,
'phone' => $phone,
'province_id' => $province->id,
'province_name' => $province->name,
'city_id' => $city->id,
'city_name' => $city->name,
'area_id' => $area->id,
'area_name' => $area->name,
'is_default' => $is_default,
'latitude' => (isset($latitude) && $latitude) ? $latitude : null,
'longitude' => (isset($longitude) && $longitude) ? $longitude : null,
'user_id' => $user->id,
'address' => $address,
'id' => $id
]);
}
return $edit;
}
public static function info($params)
{
$user = User::info();
extract($params);
return self::get(['id' => $id, 'user_id' => $user->id]);
}
public static function del($params)
{
$user = User::info();
extract($params);
return self::get(['id' => $id, 'user_id' => $user->id])->delete();
}
}