Goods.php
4.0 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
<?php
namespace app\common\model;
use think\Model;
class Goods extends Model
{
/**
* 隐藏字段
* @var array
*/
protected $hidden = [
'createtime',
'updatetime'
];
protected $append = [
'goods_image',
'goods_price'
];
/**
* 关联商品规格表
*/
public function spec()
{
return $this->hasMany('GoodsSpec');
}
/**
* 关联商品规格关系表
*/
public function specRel()
{
return $this->belongsToMany('SpecValue', 'GoodsSpecRel');
}
/**
* 商品单图
*/
public function getGoodsImageAttr($value, $data)
{
$goods_images = $this->getAttr('goods_images');
return !empty($goods_images) ? $goods_images[0] : '';
}
/**
* 商品图片
*/
public function getGoodsImagesAttr($value, $data)
{
$list = !empty($value) ? explode(',',$value) : [];
foreach($list as &$v){
$v = cdnurl($v,true);
}
return $list;
}
/**
* 商品价格
*/
public function getGoodsPriceAttr($value, $data)
{
return $this['spec'][0]['goods_price'];
}
/**
* 买家秀
*/
public function getBuyerImagesAttr($value, $data)
{
$list = !empty($value) ? explode(',',$value) : [];
foreach($list as &$v){
$v = cdnurl($v,true);
}
return $list;
}
/**
* 商品列表
*/
public static function getList($where,$page,$rows)
{
$list = self::where('issale','1')
->where($where)
->order(['updatetime'=>'desc'])
->paginate($rows,false,['page'=>$page])
->each(function($v){
$v->visible(['id','goods_image','goods_name','goods_price','ismake']);
})->toArray();
return ['total'=>$list['total'],'list'=>$list['data']];
}
/**
* 获取规格信息
*/
public function getManySpecData($spec_rel, $skuData)
{
// spec_attr
$specAttrData = [];
foreach ($spec_rel as $item) {
if (!isset($specAttrData[$item['spec_id']])) {
$specAttrData[$item['spec_id']] = [
'spec_id' => $item['spec']['id'],
'spec_name' => $item['spec']['spec_name'],
'spec_value_list' => [],
];
}
$specAttrData[$item['spec_id']]['spec_value_list'][] = [
'spec_value_id' => $item['id'],
'spec_value_name' => $item['spec_value_name'],
];
}
// spec_list
$goods_spec_list = [];
foreach ($skuData as $item) {
$goods_spec_list[] = [
'goods_spec_id' => $item['id'],
'spec_sku_id' => $item['spec_sku_id'],
'rows' => [],
'form' => [
'goods_price' => $item['goods_price'],
'goods_weight' => $item['goods_weight'],
'stock_num' => $item['stock_num'],
],
];
}
return ['spec_attr' => array_values($specAttrData), 'goods_spec_list' => $goods_spec_list];
}
/**
* 商品多规格信息
*/
public function getGoodsSku($goods_sku_id)
{
$goodsSkuData = array_column(collection($this['spec'])->toArray(), null, 'spec_sku_id');
if (!isset($goodsSkuData[$goods_sku_id])) {
return false;
}
$goods_sku = $goodsSkuData[$goods_sku_id];
// 多规格文字内容
$goods_sku['goods_attr'] = '';
if ($this['spec_type'] == '2') {
$attrs = explode('_', $goods_sku['spec_sku_id']);
$spec_rel = array_column(collection($this['spec_rel'])->toArray(), null, 'id');
foreach ($attrs as $specValueId) {
$goods_sku['goods_attr'] .= $spec_rel[$specValueId]['spec']['spec_name'] . ':'
. $spec_rel[$specValueId]['spec_value_name'] . '; ';
}
}
return $goods_sku;
}
}