|
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; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
use Overtrue\Wechat\Shop\Foundation\Base; |
|
19
|
|
|
use Overtrue\Wechat\Shop\Foundation\Product as ProductInterface; |
|
20
|
|
|
use Overtrue\Wechat\Shop\Foundation\ShopsException; |
|
21
|
|
|
use Overtrue\Wechat\Shop\Data\Product as ProductData; |
|
22
|
|
|
|
|
23
|
|
|
class Product extends Base implements ProductInterface |
|
24
|
|
|
{ |
|
25
|
|
|
|
|
26
|
|
|
const API_CREATE = 'https://api.weixin.qq.com/merchant/create'; |
|
27
|
|
|
const API_DELETE = 'https://api.weixin.qq.com/merchant/del'; |
|
28
|
|
|
const API_UPDATE = 'https://api.weixin.qq.com/merchant/update'; |
|
29
|
|
|
const API_GET = 'https://api.weixin.qq.com/merchant/get'; |
|
30
|
|
|
const API_GET_BY_STATUS = 'https://api.weixin.qq.com/merchant/getbystatus'; |
|
31
|
|
|
const API_UPDATE_STATUS = 'https://api.weixin.qq.com/merchant/modproductstatus'; |
|
32
|
|
|
const API_SUB = 'https://api.weixin.qq.com/merchant/category/getsub'; |
|
33
|
|
|
const API_SKU = 'https://api.weixin.qq.com/merchant/category/getsku'; |
|
34
|
|
|
const API_Property = 'https://api.weixin.qq.com/merchant/category/getproperty'; |
|
|
|
|
|
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* 新建商品 |
|
38
|
|
|
* |
|
39
|
|
|
* @param array|callable $data |
|
40
|
|
|
* @return array|bool |
|
41
|
|
|
* @throws ShopsException |
|
42
|
|
|
* @throws |
|
43
|
|
|
*/ |
|
44
|
|
|
public function create($data) |
|
45
|
|
|
{ |
|
46
|
|
|
if (is_callable($data)) { |
|
47
|
|
|
$product = call_user_func($data,new ProductData()); |
|
48
|
|
|
|
|
49
|
|
|
if (!($product instanceof ProductData)) throw new ShopsException('请返回 Shop\Data\Product Class'); |
|
50
|
|
|
|
|
51
|
|
|
$data = $product->toArray(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if (!is_array($data)) throw new ShopsException('$product 必须是数组'); |
|
55
|
|
|
|
|
56
|
|
|
$this->response = $this->http->jsonPost(self::API_CREATE,$data); |
|
57
|
|
|
|
|
58
|
|
|
return $this->getResponse(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* 删除商品 |
|
63
|
|
|
* |
|
64
|
|
|
* @param $productId |
|
65
|
|
|
* @return bool |
|
66
|
|
|
* @throws ShopsException |
|
67
|
|
|
*/ |
|
68
|
|
|
public function delete($productId) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->response = $this->http->jsonPost(self::API_DELETE, array('product_id'=>$productId)); |
|
71
|
|
|
|
|
72
|
|
|
return $this->getResponse(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* 修改商品 |
|
77
|
|
|
* |
|
78
|
|
|
* @param $productId |
|
79
|
|
|
* @param $data |
|
80
|
|
|
* @param bool|false $shelf |
|
81
|
|
|
* @return array|bool |
|
82
|
|
|
* @throws ShopsException |
|
83
|
|
|
*/ |
|
84
|
|
|
public function update($data,$productId = null,$shelf = false) |
|
85
|
|
|
{ |
|
86
|
|
|
if (is_callable($data)) { |
|
87
|
|
|
$product = call_user_func($data,new ProductData($shelf)); |
|
88
|
|
|
|
|
89
|
|
|
if (!($product instanceof ProductData)) throw new ShopsException('请返回 Shop\Data\Product Class'); |
|
90
|
|
|
|
|
91
|
|
|
$data = $product->toArray(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
if (!is_array($data)) throw new ShopsException('$product 必须是数组'); |
|
95
|
|
|
|
|
96
|
|
|
if ($shelf) { |
|
97
|
|
|
if (isset($data['product_base']['name'])) throw new ShopsException('请下架之后修改name'); |
|
98
|
|
|
if (isset($data['product_base']['category'])) throw new ShopsException('请下架之后修改category'); |
|
99
|
|
|
if (isset($data['product_base']['Property'])) throw new ShopsException('请下架之后修改Property'); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
if (!isset($data['product_id']) && empty($productId)) throw new ShopsException('$productId 不允许为空'); |
|
103
|
|
|
|
|
104
|
|
|
$data['product_id'] = isset($data['product_id']) ? $data['product_id'] : $productId; |
|
105
|
|
|
|
|
106
|
|
|
$this->response = $this->http->jsonPost(self::API_CREATE,$data); |
|
107
|
|
|
|
|
108
|
|
|
return $this->getResponse(); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* 查询商品 |
|
113
|
|
|
* |
|
114
|
|
|
* @param $productId |
|
115
|
|
|
* @return bool |
|
116
|
|
|
* @throws ShopsException |
|
117
|
|
|
*/ |
|
118
|
|
|
public function get($productId) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->response = $this->http->jsonPost(self::API_GET, array('product_id'=>$productId)); |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
return $this->getResponse(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* 从状态获取商品 |
|
128
|
|
|
* |
|
129
|
|
|
* @param int $status |
|
130
|
|
|
* @return mixed |
|
131
|
|
|
* @throws ShopsException |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getByStatus($status = 0) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->response = $this->http->jsonPost(self::API_GET_BY_STATUS, array('status'=>$status)); |
|
136
|
|
|
|
|
137
|
|
|
return $this->getResponse(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* 商品上下架 |
|
142
|
|
|
* |
|
143
|
|
|
* @param $productId |
|
144
|
|
|
* @param int $status |
|
145
|
|
|
* @return bool |
|
146
|
|
|
* @throws ShopsException |
|
147
|
|
|
*/ |
|
148
|
|
|
public function updateStatus($productId, $status = 0) |
|
149
|
|
|
{ |
|
150
|
|
|
$this->response = $this->http->jsonPost(self::API_UPDATE_STATUS, array( |
|
151
|
|
|
'product_id'=>$productId, |
|
152
|
|
|
'status'=>$status |
|
153
|
|
|
)); |
|
154
|
|
|
|
|
155
|
|
|
return $this->getResponse(); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* 获取指定分类的所有子分类 |
|
160
|
|
|
* |
|
161
|
|
|
* @param $cateId |
|
162
|
|
|
* @return mixed |
|
163
|
|
|
* @throws ShopsException |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getSub($cateId = 1) |
|
166
|
|
|
{ |
|
167
|
|
|
$this->response = $this->http->jsonPost(self::API_SUB, array('cate_id'=> $cateId)); |
|
168
|
|
|
|
|
169
|
|
|
return $this->getResponse(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* 获取指定子分类的所有SKU |
|
174
|
|
|
* |
|
175
|
|
|
* @param $cateId |
|
176
|
|
|
* @return mixed |
|
177
|
|
|
* @throws ShopsException |
|
178
|
|
|
*/ |
|
179
|
|
|
public function getSku($cateId) |
|
180
|
|
|
{ |
|
181
|
|
|
$this->response = $this->http->jsonPost(self::API_SKU, array('cate_id'=> $cateId)); |
|
182
|
|
|
|
|
183
|
|
|
return $this->getResponse(); |
|
184
|
|
|
|
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* 获取指定分类的所有属性 |
|
189
|
|
|
* |
|
190
|
|
|
* @param $cateId |
|
191
|
|
|
* @return mixed |
|
192
|
|
|
* @throws ShopsException |
|
193
|
|
|
*/ |
|
194
|
|
|
public function getProperty($cateId) |
|
195
|
|
|
{ |
|
196
|
|
|
$this->response = $this->http->jsonPost(self::API_Property, array('cate_id'=> $cateId)); |
|
197
|
|
|
|
|
198
|
|
|
return $this->getResponse(); |
|
199
|
|
|
|
|
200
|
|
|
|
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
|
|
204
|
|
|
} |