1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Shelf.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
|
|
|
* |
13
|
|
|
* @link https://github.com/a939638621 |
14
|
|
|
*/ |
15
|
|
|
namespace Overtrue\Wechat\Shop; |
16
|
|
|
|
17
|
|
|
use Overtrue\Wechat\Shop\Foundation\Base; |
18
|
|
|
use Overtrue\Wechat\Shop\Foundation\Shelf as ShelfInterface; |
19
|
|
|
use Overtrue\Wechat\Shop\Data\Shelf as ShelfData; |
20
|
|
|
use Overtrue\Wechat\Shop\Foundation\ShopsException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* 货架系统 |
24
|
|
|
* |
25
|
|
|
* Class Shelf |
26
|
|
|
*/ |
27
|
|
|
class Shelf extends Base implements ShelfInterface |
28
|
|
|
{ |
29
|
|
|
const API_ADD = 'https://api.weixin.qq.com/merchant/shelf/add'; |
30
|
|
|
const API_DELETE = 'https://api.weixin.qq.com/merchant/shelf/del'; |
31
|
|
|
const API_UPDATE = 'https://api.weixin.qq.com/merchant/shelf/mod'; |
32
|
|
|
const API_LISTS = 'https://api.weixin.qq.com/merchant/shelf/getall'; |
33
|
|
|
const API_GET_BY_ID = 'https://api.weixin.qq.com/merchant/shelf/getbyid'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* 添加货架 |
37
|
|
|
* |
38
|
|
|
* @param $shelfData |
39
|
|
|
* @param $shelfBanner |
40
|
|
|
* @param $shelfName |
41
|
|
|
* |
42
|
|
|
* @return int |
43
|
|
|
* |
44
|
|
|
* @throws ShopsException |
45
|
|
|
*/ |
46
|
|
View Code Duplication |
public function add($shelfData, $shelfBanner, $shelfName) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
if (is_callable($shelfData)) { |
49
|
|
|
$shelf = call_user_func($shelfData, new ShelfData()); |
50
|
|
|
if (!($shelf instanceof ShelfData)) { |
51
|
|
|
throw new ShopsException('必须返回 Shop\Data\Shelf class'); |
52
|
|
|
} |
53
|
|
|
$shelfData = $shelf->toArray(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
//todo 判断出bug |
57
|
|
|
//if (!is_array($shelfData)) throw new ShopsException('$shelfData 必须是数组'); |
58
|
|
|
|
59
|
|
|
$this->response = $this->http->jsonPost(self::API_ADD, array( |
60
|
|
|
'shelf_data' => array( |
61
|
|
|
'module_infos' => $shelfData, |
62
|
|
|
), |
63
|
|
|
'shelf_banner' => $shelfBanner, |
64
|
|
|
'shelf_name' => $shelfName, |
65
|
|
|
)); |
66
|
|
|
|
67
|
|
|
return $this->getResponse(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* 删除货架 |
72
|
|
|
* |
73
|
|
|
* @param int $shelfId |
74
|
|
|
* |
75
|
|
|
* @return bool |
76
|
|
|
* |
77
|
|
|
* @throws ShopsException |
78
|
|
|
*/ |
79
|
|
|
public function delete($shelfId) |
80
|
|
|
{ |
81
|
|
|
$this->response = $this->http->jsonPost(self::API_DELETE, array('shelf_id' => $shelfId)); |
82
|
|
|
|
83
|
|
|
return $this->getResponse(); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* 修改货架 |
88
|
|
|
* |
89
|
|
|
* @param array|callable $shelfData |
90
|
|
|
* @param $shelfId |
91
|
|
|
* @param $shelfBanner |
92
|
|
|
* @param $shelfName |
93
|
|
|
* |
94
|
|
|
* @return bool |
95
|
|
|
* |
96
|
|
|
* @throws ShopsException |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function update($shelfData, $shelfId, $shelfBanner, $shelfName) |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
if (is_callable($shelfData)) { |
101
|
|
|
$shelf = call_user_func($shelfData, new ShelfData()); |
102
|
|
|
if (!($shelf instanceof ShelfData)) { |
103
|
|
|
throw new ShopsException('必须返回 Shop\Data\Shelf class'); |
104
|
|
|
} |
105
|
|
|
$shelfData = $shelf->toArray(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
//todo 判断出bug |
109
|
|
|
//if (!is_array($shelfData)) throw new ShopsException('$shelfData 必须是数组'); |
110
|
|
|
|
111
|
|
|
$this->response = $this->http->jsonPost(self::API_UPDATE, array( |
112
|
|
|
'shelf_id' => $shelfId, |
113
|
|
|
'shelf_data' => array( |
114
|
|
|
'module_infos' => $shelfData, |
115
|
|
|
), |
116
|
|
|
'shelf_banner' => $shelfBanner, |
117
|
|
|
'shelf_name' => $shelfName, |
118
|
|
|
)); |
119
|
|
|
|
120
|
|
|
return $this->getResponse(); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* 获取所有货架 |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
* |
128
|
|
|
* @throws ShopsException |
129
|
|
|
*/ |
130
|
|
|
public function lists() |
131
|
|
|
{ |
132
|
|
|
$this->response = $this->http->get(self::API_LISTS); |
133
|
|
|
|
134
|
|
|
return $this->getResponse(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* 根据货架ID获取货架信息 |
139
|
|
|
* |
140
|
|
|
* @param $shelfId |
141
|
|
|
* |
142
|
|
|
* @return array |
143
|
|
|
* |
144
|
|
|
* @throws ShopsException |
145
|
|
|
*/ |
146
|
|
|
public function getById($shelfId) |
147
|
|
|
{ |
148
|
|
|
$this->response = $this->http->jsonPost(self::API_GET_BY_ID, array('shelf_id' => $shelfId)); |
149
|
|
|
|
150
|
|
|
return $this->getResponse(); |
|
|
|
|
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.