1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Product.php |
4
|
|
|
* |
5
|
|
|
* Part of Overtrue\Wechat. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @author a939638621 <[email protected]> |
11
|
|
|
* @copyright 2015 a939638621 <[email protected]> |
12
|
|
|
* @link https://github.com/a939638621 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Overtrue\Wechat\Shop\Data; |
16
|
|
|
|
17
|
|
|
|
18
|
|
|
use Overtrue\Wechat\Utils\MagicAttributes; |
19
|
|
|
use Overtrue\Wechat\Shop\Foundation\ShopsException; |
20
|
|
|
use Overtrue\Wechat\Shop\Stock; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* 商品属性 |
24
|
|
|
* |
25
|
|
|
* Class ProductData |
26
|
|
|
* @package Shop |
27
|
|
|
* @property array $express |
28
|
|
|
* @property array $attrext |
29
|
|
|
*/ |
30
|
|
|
class Product extends MagicAttributes |
31
|
|
|
{ |
32
|
|
|
|
33
|
|
|
private $shelf; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* 是否在货架上 |
37
|
|
|
* |
38
|
|
|
* @param bool|false $shelf |
39
|
|
|
*/ |
40
|
|
|
public function __construct($shelf = false) |
41
|
|
|
{ |
42
|
|
|
$this->shelf = $shelf; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* |
47
|
|
|
* 设置基本属性 |
48
|
|
|
* |
49
|
|
|
* @param $main_img |
50
|
|
|
* @param array $img |
51
|
|
|
* @param null $buyLimit |
52
|
|
|
* @param null $name |
53
|
|
|
* @param null $categoryId |
54
|
|
|
* @return $this |
55
|
|
|
* @throws ShopsException |
56
|
|
|
*/ |
57
|
|
|
public function setBaseAttr($main_img, array $img, $buyLimit = null,$name = null, $categoryId = null) |
58
|
|
|
{ |
59
|
|
|
|
60
|
|
|
$this->attributes['product_base'] = array( |
61
|
|
|
'main_img' => $main_img, |
62
|
|
|
'img' => $img |
63
|
|
|
); |
64
|
|
|
|
65
|
|
|
if (!$this->shelf) { |
66
|
|
|
$this->attributes['product_base']['name'] = $name; |
67
|
|
|
$this->attributes['product_base']['category_id'][] = $categoryId; |
68
|
|
|
} else { |
69
|
|
|
if (!empty($name)) throw new ShopsException('请下架之后在设置name'); |
70
|
|
|
if (!empty($category)) throw new ShopsException('请下架之后在设置 category'); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (!empty($buyLimit)) { |
74
|
|
|
$this->attributes['product_base']['buy_limit'] = $buyLimit; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* 商品详情列表 |
82
|
|
|
* |
83
|
|
|
* @param $name |
84
|
|
|
* @param $value |
85
|
|
|
* @return $this |
86
|
|
|
* @throws ShopsException |
87
|
|
|
*/ |
88
|
|
|
public function setDetail($name,$value) |
89
|
|
|
{ |
90
|
|
|
|
91
|
|
|
if ($name != 'text' && $name != 'img') throw new ShopsException('name 只能为 text 或者 img '); |
92
|
|
|
|
93
|
|
|
$this->attributes['product_base']['detail'][][$name] = $value; |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* 商品属性列表 |
100
|
|
|
* |
101
|
|
|
* @param $id |
102
|
|
|
* @param $vid |
103
|
|
|
* @return $this |
104
|
|
|
*/ |
105
|
|
|
public function setProperty($id,$vid) |
106
|
|
|
{ |
107
|
|
|
$this->attributes['product_base']['property'][] = array( |
108
|
|
|
'id' => $id, |
109
|
|
|
'vid' => $vid |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* 设置sku |
117
|
|
|
* |
118
|
|
|
* @param $id |
119
|
|
|
* @param array $vid |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
|
|
public function setSkuInfo($id, array $vid) |
123
|
|
|
{ |
124
|
|
|
// '$属性' '$值' |
125
|
|
|
// 123123 '$值' |
126
|
|
|
$this->attributes['product_base']['sku_info'][] = array( |
127
|
|
|
'id' => $id, |
128
|
|
|
'vid' => $vid |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* sku信息列表 |
136
|
|
|
* |
137
|
|
|
* @param $oriPrice |
138
|
|
|
* @param $price |
139
|
|
|
* @param $iconUrl |
140
|
|
|
* @param $quantity |
141
|
|
|
* @param null $skuId |
142
|
|
|
* @return $this |
143
|
|
|
*/ |
144
|
|
|
public function setSkuList($oriPrice, $price, $iconUrl, $quantity, $skuId = null) |
145
|
|
|
{ |
146
|
|
|
|
147
|
|
|
$data = array( |
148
|
|
|
'ori_price' => $oriPrice, |
149
|
|
|
'price' => $price, |
150
|
|
|
'icon_url' => $iconUrl, |
151
|
|
|
'quantity' => $quantity |
152
|
|
|
); |
153
|
|
|
|
154
|
|
|
if (is_array($skuId)) { |
155
|
|
|
$skuId = Stock::getSkuInfo($skuId); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (!empty($skuId)) $data['sku_id'] = $skuId; |
159
|
|
|
|
160
|
|
|
$this->attributes['sku_list'][] = $data; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* 商品其他属性 |
167
|
|
|
* |
168
|
|
|
* @param $isPostFree |
169
|
|
|
* @param $isHasReceipt |
170
|
|
|
* @param $isUnderGuaranty |
171
|
|
|
* @param $isSupportReplace |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function setAttrext($isPostFree, $isHasReceipt, $isUnderGuaranty, $isSupportReplace) |
175
|
|
|
{ |
176
|
|
|
$this->attrext = array( |
177
|
|
|
'isPostFree' => $isPostFree, |
178
|
|
|
'isHasReceipt' => $isHasReceipt, |
179
|
|
|
'isUnderGuaranty' => $isUnderGuaranty, |
180
|
|
|
'isSupportReplace' => $isSupportReplace |
181
|
|
|
); |
182
|
|
|
|
183
|
|
|
return $this; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
public function setLocation($province, $city, $address, $country = '中国') |
187
|
|
|
{ |
188
|
|
|
$this->attributes['attrext']['location'] = array( |
189
|
|
|
'country' => $country, |
190
|
|
|
'province' => $province, |
191
|
|
|
'city' => $city, |
192
|
|
|
'address' => $address |
193
|
|
|
); |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* 运费信息 |
200
|
|
|
* |
201
|
|
|
* @param $deliveryType |
202
|
|
|
* @param null $template |
203
|
|
|
* @return $this |
204
|
|
|
* @throws ShopsException |
205
|
|
|
*/ |
206
|
|
|
public function setDeliveryInfo($deliveryType, $template = null) |
207
|
|
|
{ |
208
|
|
|
$data['delivery_type'] = $deliveryType; |
|
|
|
|
209
|
|
|
|
210
|
|
|
if (!empty($template) ) { |
211
|
|
|
if (!is_string($template)) throw new ShopsException('错误数据类型'); |
212
|
|
|
$data['template_id'] = $template; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
$this->attributes['delivery_info'] = $data; |
216
|
|
|
|
217
|
|
|
return $this; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* 默认模板 |
222
|
|
|
* |
223
|
|
|
* @param $id |
224
|
|
|
* @param $price |
225
|
|
|
* @return $this |
226
|
|
|
*/ |
227
|
|
|
public function setExpress($id, $price) |
228
|
|
|
{ |
229
|
|
|
$this->attributes['delivery_info']['express'][] = array( |
230
|
|
|
'id' => $id, |
231
|
|
|
'price' => $price |
232
|
|
|
); |
233
|
|
|
|
234
|
|
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* 拼接最后的data |
239
|
|
|
* |
240
|
|
|
* @return array |
241
|
|
|
* @throws ShopsException |
242
|
|
|
*/ |
243
|
|
|
public function toArray() |
244
|
|
|
{ |
245
|
|
|
if ($this->shelf) { |
246
|
|
|
|
247
|
|
|
if (isset($this->attributes['product_base']['Property'])) throw new ShopsException('不能设置 Property 字段,请下架商品再修改'); |
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
if (empty($this->attributes['product_base'])) throw new ShopsException('baseAttr 不允许为空'); |
251
|
|
|
if (empty($this->attributes['sku_list'])) throw new ShopsException('skuList 不允许为空'); |
252
|
|
|
if (empty($this->attrext)) throw new ShopsException('attrext 不允许为空'); |
253
|
|
|
|
254
|
|
|
if ($this->attributes['attrext']['isPostFree'] == 0) { |
255
|
|
|
|
256
|
|
|
if (empty($this->attributes['delivery_info'])) throw new ShopsException('deliveryInfo 不允许为空'); |
257
|
|
|
|
258
|
|
|
if ($this->attributes['delivery_info']['delivery_type'] == 0) { |
259
|
|
|
|
260
|
|
|
if (empty($this->attributes['delivery_info']['express'])) throw new ShopsException('express 不允许为空'); |
261
|
|
|
$this->attributes['delivery_info']['express'] = $this->express; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $this->attributes; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
public function __isset($name) |
270
|
|
|
{ |
271
|
|
|
return isset($this->attributes[$name]); |
272
|
|
|
} |
273
|
|
|
} |
This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.
The variable may have been renamed without also renaming all references.