UserSize.php
4.5 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
namespace app\common\model;
use think\Model;
class UserSize extends Model
{
// 自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
protected $deleteTime = false;
// 追加属性
protected $append = [
'body_images_list',
'size_text'
];
/**
* 隐藏字段
* @var array
*/
protected $hidden = [
'createtime',
'updatetime'
];
/**
* 尺寸图片
*/
public function getBodyImagesListAttr($value, $data)
{
$list = !empty($data['body_images']) ? explode(',',$data['body_images']) : [];
foreach($list as &$v){
$v = cdnurl($v,true);
}
return $list;
}
/**
* 尺寸列表
*/
public static function sizeList($user_id)
{
return self::all(compact('user_id'));
}
/**
* 新增尺寸
*/
public function add($user, $data)
{
empty($data['isdefault']) && $data['isdefault'] = '0';
// 该尺寸设为默认,其他地址就设为非默认
if($data['isdefault'] == '1'){
$this->where('user_id',$user['id'])->update(['isdefault'=>'0']);
}
// 对象转json字符串
if(!empty($data['body_info']) && is_array($data['body_info'])){
$data['body_info'] = json_encode($data['body_info']);
}
$this->allowField(true)->save(array_merge([
'user_id' => $user['id'],
], $data));
// 没有默认收货地址,就把该收货地址设为默认
!$this->get(['user_id'=>$user['id'],'isdefault'=>'1']) && $this->update(['id'=>$this['id'],'isdefault'=>'1']);
return true;
}
/**
* 编辑尺寸
*/
public function edit($data)
{
empty($data['isdefault']) && $data['isdefault'] = '0';
return $data['isdefault'];
// 该地址设为默认,其他地址就设为非默认
if($data['isdefault'] == '1'){
$this->where('user_id',$this['user_id'])->update(['isdefault'=>'0']);
}
// 对象转json字符串
if(!empty($data['body_info']) && is_array($data['body_info'])){
$data['body_info'] = json_encode($data['body_info']);
}
return $this->allowField(true)->save($data);
}
/**
* 删除尺寸
*/
public function remove($user)
{
// 查询当前是否为默认地址
if($this['isdefault'] == '1'){
$this->delete();
$size = $this->get(['user_id'=>$user['id']]);
$size->isdefault = '1';
$size->save();
}else{
$this->delete();
}
return true;
}
/**
* 设置为默认
*/
public function setDefault()
{
$this->where('user_id',$this['user_id'])->update(['isdefault'=>'0']);
$this->update(['id'=>$this['id'],'isdefault'=>'1']);
return true;
}
/**
* 尺寸详情
*/
public static function detail($user_id, $id)
{
return self::get(compact('user_id', 'id'));
}
/**
* 尺寸详情
*/
public function getSizeTextAttr($value,$data){
$size_text = [];
if(!empty($data['body_info'])){
$size = json_decode(str_replace('\'','"',$data['body_info']),true);
foreach($size as $k => $v){
$style = Style::where('id',$k)->field('style_name,style_type')->find()->toArray();
$style['style_value'] = '';
switch ($style['style_type']) {
case '1':
$style['style_value'] = StyleValue::where('id',$v)->value('style_value_name');
break;
case '2':
$style['style_value'] = $v;
break;
case '3':
$style['style_value'] = cdnurl($v,true);
break;
}
$size_text[] = $style;
}
}
$size_text = array_merge([
['style_name'=>'身高','style_type'=>'1','style_value'=>$data['height'].'CM'],
['style_name'=>'体重','style_type'=>'1','style_value'=>$data['weight'].'KG'],
['style_name'=>'腰围','style_type'=>'1','style_value'=>!empty($data['waistline']) ? $data['waistline'] : '未填写'],
],$size_text);
return $size_text;
}
}