Litestoreadress.php
2.6 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
<?php
namespace addons\litestore\model;
use think\Model;
class Litestoreadress extends Model
{
// 表名
protected $name = 'litestore_adress';
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 追加属性
protected $append = ['Area'];
public function getList($user_id)
{
return self::all(compact('user_id'));
}
public function getAreaAttr($value, $data)
{
return [
'province' => Area::getNameById($data['province_id']),
'city' => Area::getNameById($data['city_id']),
'region' => Area::getNameById($data['region_id']),
];
}
public function add($use_id, $data)
{
$listdata = $this->all(['user_id'=>$use_id]);
foreach ($listdata as $key => $value) {
$value['isdefault'] = '0';
$value->save();
}
// 添加收货地址
$region = explode(',', $data['region']);
$province_id = Area::getIdByName($region[0], 1);
$city_id = Area::getIdByName($region[1], 2, $province_id);
$region_id = Area::getIdByName($region[2], 3, $city_id);
$this->allowField(true)->save(array_merge([
'user_id' => $use_id,
'province_id' => $province_id,
'city_id' => $city_id,
'region_id' => $region_id,
'isdefault' => '1'
], $data));
return true;
}
public function setdefault($use_id,$id){
$listdata = $this->all(['user_id'=>$use_id]);
foreach ($listdata as $key => $value) {
$value['isdefault'] = '0';
$value->save();
}
return ($this->get($id))->save(['isdefault' => '1']);
}
public function del($id){
return ($this->get($id))->delete();
}
public function detail($user_id, $address_id)
{
return self::get(compact('user_id', 'address_id'));
}
public function edit($data){
$region = explode(',', $data['region']);
$province_id = Area::getIdByName($region[0], 1);
$city_id = Area::getIdByName($region[1], 2, $province_id);
$region_id = Area::getIdByName($region[2], 3, $city_id);
return $this->allowField(true)
->save(array_merge(compact('province_id', 'city_id', 'region_id'), $data));
}
public static function getdefault($use_id){
$filter = [];
$filter['isdefault'] = '1';
$filter['user_id'] = $use_id;
return self::get($filter);
}
}