1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Stock.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\Stock as StockInterface; |
20
|
|
|
use Overtrue\Wechat\Shop\Foundation\ShopsException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class Stock |
24
|
|
|
* @package Shop |
25
|
|
|
*/ |
26
|
|
|
class Stock extends Base implements StockInterface |
27
|
|
|
{ |
28
|
|
|
|
29
|
|
|
const API_ADD = 'https://api.weixin.qq.com/merchant/stock/add'; |
30
|
|
|
const API_REDUCE = 'https://api.weixin.qq.com/merchant/stock/reduce'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* 增加库存 |
34
|
|
|
* |
35
|
|
|
* @param $productId |
36
|
|
|
* @param $quantity |
37
|
|
|
* @param string|array $skuInfo |
38
|
|
|
* @return bool |
39
|
|
|
* @throws ShopsException |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function add($productId, $quantity, $skuInfo = null) |
42
|
|
|
{ |
43
|
|
|
$this->response = $this->http->jsonPost(self::API_ADD,array( |
44
|
|
|
'product_id' => $productId, |
45
|
|
|
'sku_info' => is_null($skuInfo) ? '' : $this->getSkuInfo($skuInfo), |
|
|
|
|
46
|
|
|
'quantity' => $quantity |
47
|
|
|
)); |
48
|
|
|
|
49
|
|
|
return $this->getResponse(); |
|
|
|
|
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* 减少库存 |
55
|
|
|
* |
56
|
|
|
* @param array $productId |
57
|
|
|
* @param string|array $skuInfo |
58
|
|
|
* @param int $quantity |
59
|
|
|
* @return bool |
60
|
|
|
* @throws ShopsException |
61
|
|
|
*/ |
62
|
|
View Code Duplication |
public function reduce($productId, $quantity, $skuInfo = null) |
63
|
|
|
{ |
64
|
|
|
$this->response = $this->http->jsonPost(self::API_REDUCE,array( |
65
|
|
|
'product_id' => $productId, |
66
|
|
|
'sku_info' => is_null($skuInfo) ? '' : $this->getSkuInfo($skuInfo), |
|
|
|
|
67
|
|
|
'quantity' => $quantity |
68
|
|
|
)); |
69
|
|
|
|
70
|
|
|
return $this->getResponse(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* 拼装 SkuInfo Str |
75
|
|
|
* |
76
|
|
|
* @param array $skuInfo |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
|
|
public static function getSkuInfo($skuInfo) |
80
|
|
|
{ |
81
|
|
|
if (is_string($skuInfo)) return $skuInfo; |
82
|
|
|
|
83
|
|
|
$str = ''; |
84
|
|
|
// array( |
85
|
|
|
// array('id','vid'), |
86
|
|
|
// array('id1','vid1'), |
87
|
|
|
// //..... |
88
|
|
|
// ); |
89
|
|
|
$i = 0; |
90
|
|
|
foreach ($skuInfo as $v) { |
91
|
|
|
$i++; |
92
|
|
|
if (count($skuInfo) > $i) { |
93
|
|
|
$str.= $v[0].':'.$v[1].';'; |
94
|
|
|
} else { |
95
|
|
|
$str.= $v[0].':'.$v[1]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $str; |
101
|
|
|
} |
102
|
|
|
} |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.