Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Card often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Card, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 30 | class Card extends AbstractAPI |
||
| 31 | { |
||
| 32 | /** |
||
| 33 | * Cache. |
||
| 34 | * |
||
| 35 | * @var Cache |
||
| 36 | */ |
||
| 37 | protected $cache; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Ticket cache key. |
||
| 41 | * |
||
| 42 | * @var string |
||
| 43 | */ |
||
| 44 | protected $ticketCacheKey; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Ticket cache prefix. |
||
| 48 | * |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $ticketCachePrefix = 'overtrue.wechat.card_api_ticket.'; |
||
| 52 | |||
| 53 | const API_GET_COLORS = 'https://api.weixin.qq.com/card/getcolors'; |
||
| 54 | const API_CREATE_CARD = 'https://api.weixin.qq.com/card/create'; |
||
| 55 | const API_CREATE_QRCODE = 'https://api.weixin.qq.com/card/qrcode/create'; |
||
| 56 | const API_SHOW_QRCODE = 'https://mp.weixin.qq.com/cgi-bin/showqrcode'; |
||
| 57 | const API_GET_CARD_TICKET = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket'; |
||
| 58 | const API_CREATE_LANDING_PAGE = 'https://api.weixin.qq.com/card/landingpage/create'; |
||
| 59 | const API_DEPOSIT_CODE = 'https://api.weixin.qq.com/card/code/deposit'; |
||
| 60 | const API_GET_DEPOSIT_COUNT = 'https://api.weixin.qq.com/card/code/getdepositcount'; |
||
| 61 | const API_CHECK_CODE = 'https://api.weixin.qq.com/card/code/checkcode'; |
||
| 62 | const API_GET_HTML = 'https://api.weixin.qq.com/card/mpnews/gethtml'; |
||
| 63 | const API_SET_TEST_WHITE_LIST = 'https://api.weixin.qq.com/card/testwhitelist/set'; |
||
| 64 | const API_GET_CODE = 'https://api.weixin.qq.com/card/code/get'; |
||
| 65 | const API_CONSUME_CARD = 'https://api.weixin.qq.com/card/code/consume'; |
||
| 66 | const API_DECRYPT_CODE = 'https://api.weixin.qq.com/card/code/decrypt'; |
||
| 67 | const API_GET_CARD_LIST = 'https://api.weixin.qq.com/card/user/getcardlist'; |
||
| 68 | const API_GET_CARD = 'https://api.weixin.qq.com/card/get'; |
||
| 69 | const API_LIST_CARD = 'https://api.weixin.qq.com/card/batchget'; |
||
| 70 | const API_UPDATE_CARD = 'https://api.weixin.qq.com/card/update'; |
||
| 71 | const API_SET_PAY_CELL = 'https://api.weixin.qq.com/card/paycell/set'; |
||
| 72 | const API_MODIFY_STOCK = 'https://api.weixin.qq.com/card/modifystock'; |
||
| 73 | const API_UPDATE_CODE = 'https://api.weixin.qq.com/card/code/update'; |
||
| 74 | const API_DELETE_CARD = 'https://api.weixin.qq.com/card/delete'; |
||
| 75 | const API_DISABLE_CARD = 'https://api.weixin.qq.com/card/code/unavailable'; |
||
| 76 | const API_ACTIVATE_MEMBER_CARD = 'https://api.weixin.qq.com/card/membercard/activate'; |
||
| 77 | const API_ACTIVATE_MEMBER_USER_FORM = 'https://api.weixin.qq.com/card/membercard/activateuserform/set'; |
||
| 78 | const API_GET_MEMBER_USER_INFO = 'https://api.weixin.qq.com/card/membercard/userinfo/get'; |
||
| 79 | const API_UPDATE_MEMBER_CARD_USER = 'https://api.weixin.qq.com/card/membercard/updateuser'; |
||
| 80 | const API_CREATE_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/submit'; |
||
| 81 | const API_UPDATE_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/update'; |
||
| 82 | const API_GET_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/get'; |
||
| 83 | const API_LIST_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/batchget'; |
||
| 84 | const API_GET_CATEGORIES = 'https://api.weixin.qq.com/card/getapplyprotocol'; |
||
| 85 | const API_ACTIVATE_GENERAL_CARD = 'https://api.weixin.qq.com/card/generalcard/activate'; |
||
| 86 | const API_UPDATE_GENERAL_CARD_USER = 'https://api.weixin.qq.com/card/generalcard/updateuser'; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * 获取卡券颜色. |
||
| 90 | * |
||
| 91 | * @return \EasyWeChat\Support\Collection |
||
| 92 | */ |
||
| 93 | 1 | public function getColors() |
|
| 94 | { |
||
| 95 | 1 | return $this->parseJSON('get', [self::API_GET_COLORS]); |
|
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * 创建卡券. |
||
| 100 | * |
||
| 101 | * @param string $cardType |
||
| 102 | * @param array $baseInfo |
||
| 103 | * @param array $especial |
||
| 104 | * @param array $advancedInfo |
||
| 105 | * |
||
| 106 | * @return \EasyWeChat\Support\Collection |
||
| 107 | */ |
||
| 108 | 1 | public function create($cardType = 'member_card', array $baseInfo = [], array $especial = [], array $advancedInfo = []) |
|
| 109 | { |
||
| 110 | $params = [ |
||
| 111 | 'card' => [ |
||
| 112 | 1 | 'card_type' => strtoupper($cardType), |
|
| 113 | 1 | strtolower($cardType) => array_merge(['base_info' => $baseInfo], $especial, $advancedInfo), |
|
| 114 | 1 | ], |
|
| 115 | 1 | ]; |
|
| 116 | |||
| 117 | 1 | return $this->parseJSON('json', [self::API_CREATE_CARD, $params]); |
|
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * 创建二维码. |
||
| 122 | * |
||
| 123 | * @param array $cards |
||
| 124 | * |
||
| 125 | * @return \EasyWeChat\Support\Collection |
||
| 126 | */ |
||
| 127 | 1 | public function QRCode(array $cards = []) |
|
| 128 | { |
||
| 129 | 1 | return $this->parseJSON('json', [self::API_CREATE_QRCODE, $cards]); |
|
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * ticket 换取二维码图片. |
||
| 134 | * |
||
| 135 | * @param string $ticket |
||
| 136 | * |
||
| 137 | * @return array |
||
| 138 | */ |
||
| 139 | public function showQRCode($ticket = null) |
||
| 140 | { |
||
| 141 | $params = [ |
||
| 142 | 'ticket' => $ticket, |
||
| 143 | ]; |
||
| 144 | |||
| 145 | $http = $this->getHttp(); |
||
| 146 | |||
| 147 | /** @var ResponseInterface $response */ |
||
| 148 | $response = $http->get(self::API_SHOW_QRCODE, $params); |
||
| 149 | |||
| 150 | return [ |
||
| 151 | 'status' => $response->getStatusCode(), |
||
| 152 | 'reason' => $response->getReasonPhrase(), |
||
| 153 | 'headers' => $response->getHeaders(), |
||
| 154 | 'body' => strval($response->getBody()), |
||
| 155 | 'url' => self::API_SHOW_QRCODE.'?'.http_build_query($params), |
||
| 156 | ]; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * 通过ticket换取二维码 链接. |
||
| 161 | * |
||
| 162 | * @param string $ticket |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | 1 | public function getQRCodeUrl($ticket) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * 获取 卡券 Api_ticket. |
||
| 173 | * |
||
| 174 | * @param bool $refresh 是否强制刷新 |
||
| 175 | * |
||
| 176 | * @return string $apiTicket |
||
| 177 | */ |
||
| 178 | 2 | View Code Duplication | public function getAPITicket($refresh = false) |
| 179 | { |
||
| 180 | 2 | $key = $this->getTicketCacheKey(); |
|
| 181 | |||
| 182 | 2 | $ticket = $this->getCache()->fetch($key); |
|
| 183 | |||
| 184 | 2 | if (!$ticket || $refresh) { |
|
| 185 | 1 | $result = $this->parseJSON('get', [self::API_GET_CARD_TICKET, ['type' => 'wx_card']]); |
|
| 186 | |||
| 187 | 1 | $this->getCache()->save($key, $result['ticket'], $result['expires_in'] - 500); |
|
| 188 | |||
| 189 | 1 | return $result['ticket']; |
|
| 190 | } |
||
| 191 | |||
| 192 | 1 | return $ticket; |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * 微信卡券:JSAPI 卡券发放. |
||
| 197 | * |
||
| 198 | * @param array $cards |
||
| 199 | * |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function jsConfigForAssign(array $cards) |
||
| 203 | { |
||
| 204 | 1 | return json_encode(array_map(function ($card) { |
|
| 205 | 1 | return $this->attachExtension($card['card_id'], $card); |
|
| 206 | 1 | }, $cards)); |
|
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * 生成 js添加到卡包 需要的 card_list 项. |
||
| 211 | * |
||
| 212 | * @param string $cardId |
||
| 213 | * @param array $extension |
||
| 214 | * |
||
| 215 | * @return string |
||
| 216 | */ |
||
| 217 | 1 | public function attachExtension($cardId, array $extension = []) |
|
| 218 | { |
||
| 219 | 1 | $timestamp = time(); |
|
| 220 | $ext = [ |
||
| 221 | 1 | 'code' => Arr::get($extension, 'code'), |
|
| 222 | 1 | 'openid' => Arr::get($extension, 'openid', Arr::get($extension, 'open_id')), |
|
| 223 | 1 | 'timestamp' => $timestamp, |
|
| 224 | 1 | 'outer_id' => Arr::get($extension, 'outer_id'), |
|
| 225 | 1 | 'balance' => Arr::get($extension, 'balance'), |
|
| 226 | 1 | 'fixed_begintimestamp' => Arr::get($extension, 'fixed_begintimestamp'), |
|
| 227 | 1 | 'outer_str' => Arr::get($extension, 'outer_str'), |
|
| 228 | 1 | ]; |
|
| 229 | 1 | $ext['signature'] = $this->getSignature( |
|
| 230 | 1 | $this->getAPITicket(), |
|
| 231 | 1 | $timestamp, |
|
| 232 | 1 | $cardId, |
|
| 233 | 1 | $ext['code'], |
|
| 234 | 1 | $ext['openid'], |
|
| 235 | 1 | $ext['balance'] |
|
| 236 | 1 | ); |
|
| 237 | |||
| 238 | return [ |
||
| 239 | 1 | 'cardId' => $cardId, |
|
| 240 | 1 | 'cardExt' => json_encode($ext), |
|
| 241 | 1 | ]; |
|
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * 生成签名. |
||
| 246 | * |
||
| 247 | * @return string |
||
| 248 | */ |
||
| 249 | 1 | public function getSignature() |
|
| 250 | { |
||
| 251 | 1 | $params = func_get_args(); |
|
| 252 | 1 | sort($params, SORT_STRING); |
|
| 253 | |||
| 254 | 1 | return sha1(implode($params)); |
|
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * 创建货架接口. |
||
| 259 | * |
||
| 260 | * @param string $banner |
||
| 261 | * @param string $pageTitle |
||
| 262 | * @param bool $canShare |
||
| 263 | * @param string $scene [SCENE_NEAR_BY 附近,SCENE_MENU 自定义菜单,SCENE_QRCODE 二维码,SCENE_ARTICLE 公众号文章, |
||
| 264 | * SCENE_H5 h5页面,SCENE_IVR 自动回复,SCENE_CARD_CUSTOM_CELL 卡券自定义cell] |
||
| 265 | * @param array $cardList |
||
| 266 | * |
||
| 267 | * @return \EasyWeChat\Support\Collection |
||
| 268 | */ |
||
| 269 | 1 | public function createLandingPage($banner, $pageTitle, $canShare, $scene, $cardList) |
|
| 270 | { |
||
| 271 | $params = [ |
||
| 272 | 1 | 'banner' => $banner, |
|
| 273 | 1 | 'page_title' => $pageTitle, |
|
| 274 | 1 | 'can_share' => $canShare, |
|
| 275 | 1 | 'scene' => $scene, |
|
| 276 | 1 | 'card_list' => $cardList, |
|
| 277 | 1 | ]; |
|
| 278 | |||
| 279 | 1 | return $this->parseJSON('json', [self::API_CREATE_LANDING_PAGE, $params]); |
|
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * 导入code接口. |
||
| 284 | * |
||
| 285 | * @param string $cardId |
||
| 286 | * @param array $code |
||
| 287 | * |
||
| 288 | * @return \EasyWeChat\Support\Collection |
||
| 289 | */ |
||
| 290 | 1 | View Code Duplication | public function deposit($cardId, $code) |
| 291 | { |
||
| 292 | $params = [ |
||
| 293 | 1 | 'card_id' => $cardId, |
|
| 294 | 1 | 'code' => $code, |
|
| 295 | 1 | ]; |
|
| 296 | |||
| 297 | 1 | return $this->parseJSON('json', [self::API_DEPOSIT_CODE, $params]); |
|
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * 查询导入code数目. |
||
| 302 | * |
||
| 303 | * @param string $cardId |
||
| 304 | * |
||
| 305 | * @return \EasyWeChat\Support\Collection |
||
| 306 | */ |
||
| 307 | 1 | View Code Duplication | public function getDepositedCount($cardId) |
| 308 | { |
||
| 309 | $params = [ |
||
| 310 | 1 | 'card_id' => $cardId, |
|
| 311 | 1 | ]; |
|
| 312 | |||
| 313 | 1 | return $this->parseJSON('json', [self::API_GET_DEPOSIT_COUNT, $params]); |
|
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * 核查code接口. |
||
| 318 | * |
||
| 319 | * @param string $cardId |
||
| 320 | * @param array $code |
||
| 321 | * |
||
| 322 | * @return \EasyWeChat\Support\Collection |
||
| 323 | */ |
||
| 324 | 1 | View Code Duplication | public function checkCode($cardId, $code) |
| 325 | { |
||
| 326 | $params = [ |
||
| 327 | 1 | 'card_id' => $cardId, |
|
| 328 | 1 | 'code' => $code, |
|
| 329 | 1 | ]; |
|
| 330 | |||
| 331 | 1 | return $this->parseJSON('json', [self::API_CHECK_CODE, $params]); |
|
| 332 | } |
||
| 333 | |||
| 334 | /** |
||
| 335 | * 查询Code接口. |
||
| 336 | * |
||
| 337 | * @param string $code |
||
| 338 | * @param bool $checkConsume |
||
| 339 | * @param string $cardId |
||
| 340 | * |
||
| 341 | * @return \EasyWeChat\Support\Collection |
||
| 342 | */ |
||
| 343 | 1 | View Code Duplication | public function getCode($code, $checkConsume, $cardId) |
| 344 | { |
||
| 345 | $params = [ |
||
| 346 | 1 | 'code' => $code, |
|
| 347 | 1 | 'check_consume' => $checkConsume, |
|
| 348 | 1 | 'card_id' => $cardId, |
|
| 349 | 1 | ]; |
|
| 350 | |||
| 351 | 1 | return $this->parseJSON('json', [self::API_GET_CODE, $params]); |
|
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * 核销Code接口. |
||
| 356 | * |
||
| 357 | * @param string $code |
||
| 358 | * @param string $cardId |
||
| 359 | * |
||
| 360 | * @return \EasyWeChat\Support\Collection |
||
| 361 | */ |
||
| 362 | 1 | public function consume($code, $cardId = null) |
|
| 363 | { |
||
| 364 | 1 | if (strlen($code) === 28 && $cardId && strlen($cardId) !== 28) { |
|
|
|
|||
| 365 | 1 | list($code, $cardId) = [$cardId, $code]; |
|
| 366 | 1 | } |
|
| 367 | |||
| 368 | $params = [ |
||
| 369 | 1 | 'code' => $code, |
|
| 370 | 1 | ]; |
|
| 371 | |||
| 372 | 1 | if ($cardId) { |
|
| 373 | 1 | $params['card_id'] = $cardId; |
|
| 374 | 1 | } |
|
| 375 | |||
| 376 | 1 | return $this->parseJSON('json', [self::API_CONSUME_CARD, $params]); |
|
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Code解码接口. |
||
| 381 | * |
||
| 382 | * @param string $encryptedCode |
||
| 383 | * |
||
| 384 | * @return \EasyWeChat\Support\Collection |
||
| 385 | */ |
||
| 386 | 1 | public function decryptCode($encryptedCode) |
|
| 387 | { |
||
| 388 | $params = [ |
||
| 389 | 1 | 'encrypt_code' => $encryptedCode, |
|
| 390 | 1 | ]; |
|
| 391 | |||
| 392 | 1 | return $this->parseJSON('json', [self::API_DECRYPT_CODE, $params]); |
|
| 393 | } |
||
| 394 | |||
| 395 | /** |
||
| 396 | * 图文消息群发卡券. |
||
| 397 | * |
||
| 398 | * @param string $cardId |
||
| 399 | * |
||
| 400 | * @return \EasyWeChat\Support\Collection |
||
| 401 | */ |
||
| 402 | 1 | View Code Duplication | public function getHtml($cardId) |
| 403 | { |
||
| 404 | $params = [ |
||
| 405 | 1 | 'card_id' => $cardId, |
|
| 406 | 1 | ]; |
|
| 407 | |||
| 408 | 1 | return $this->parseJSON('json', [self::API_GET_HTML, $params]); |
|
| 409 | } |
||
| 410 | |||
| 411 | /** |
||
| 412 | * 设置测试白名单. |
||
| 413 | * |
||
| 414 | * @param array $openids |
||
| 415 | * |
||
| 416 | * @return \EasyWeChat\Support\Collection |
||
| 417 | */ |
||
| 418 | 1 | public function setTestWhitelist($openids) |
|
| 419 | { |
||
| 420 | $params = [ |
||
| 421 | 1 | 'openid' => $openids, |
|
| 422 | 1 | ]; |
|
| 423 | |||
| 424 | 1 | return $this->parseJSON('json', [self::API_SET_TEST_WHITE_LIST, $params]); |
|
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * 设置测试白名单(by username). |
||
| 429 | * |
||
| 430 | * @param array $usernames |
||
| 431 | * |
||
| 432 | * @return \EasyWeChat\Support\Collection |
||
| 433 | */ |
||
| 434 | 1 | public function setTestWhitelistByUsername($usernames) |
|
| 435 | { |
||
| 436 | $params = [ |
||
| 437 | 1 | 'username' => $usernames, |
|
| 438 | 1 | ]; |
|
| 439 | |||
| 440 | 1 | return $this->parseJSON('json', [self::API_SET_TEST_WHITE_LIST, $params]); |
|
| 441 | } |
||
| 442 | |||
| 443 | /** |
||
| 444 | * 获取用户已领取卡券接口. |
||
| 445 | * |
||
| 446 | * @param string $openid |
||
| 447 | * @param string $cardId |
||
| 448 | * |
||
| 449 | * @return \EasyWeChat\Support\Collection |
||
| 450 | */ |
||
| 451 | 1 | View Code Duplication | public function getUserCards($openid, $cardId = '') |
| 452 | { |
||
| 453 | $params = [ |
||
| 454 | 1 | 'openid' => $openid, |
|
| 455 | 1 | 'card_id' => $cardId, |
|
| 456 | 1 | ]; |
|
| 457 | |||
| 458 | 1 | return $this->parseJSON('json', [self::API_GET_CARD_LIST, $params]); |
|
| 459 | } |
||
| 460 | |||
| 461 | /** |
||
| 462 | * 查看卡券详情. |
||
| 463 | * |
||
| 464 | * @param string $cardId |
||
| 465 | * |
||
| 466 | * @return \EasyWeChat\Support\Collection |
||
| 467 | */ |
||
| 468 | 1 | View Code Duplication | public function getCard($cardId) |
| 469 | { |
||
| 470 | $params = [ |
||
| 471 | 1 | 'card_id' => $cardId, |
|
| 472 | 1 | ]; |
|
| 473 | |||
| 474 | 1 | return $this->parseJSON('json', [self::API_GET_CARD, $params]); |
|
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * 批量查询卡列表. |
||
| 479 | * |
||
| 480 | * @param int $offset |
||
| 481 | * @param int $count |
||
| 482 | * @param string $statusList |
||
| 483 | * |
||
| 484 | * @return \EasyWeChat\Support\Collection |
||
| 485 | */ |
||
| 486 | 1 | View Code Duplication | public function lists($offset = 0, $count = 10, $statusList = 'CARD_STATUS_VERIFY_OK') |
| 487 | { |
||
| 488 | $params = [ |
||
| 489 | 1 | 'offset' => $offset, |
|
| 490 | 1 | 'count' => $count, |
|
| 491 | 1 | 'status_list' => $statusList, |
|
| 492 | 1 | ]; |
|
| 493 | |||
| 494 | 1 | return $this->parseJSON('json', [self::API_LIST_CARD, $params]); |
|
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * 更改卡券信息接口 and 设置跟随推荐接口. |
||
| 499 | * |
||
| 500 | * @param string $cardId |
||
| 501 | * @param string $type |
||
| 502 | * @param array $baseInfo |
||
| 503 | * @param array $especial |
||
| 504 | * |
||
| 505 | * @return \EasyWeChat\Support\Collection |
||
| 506 | */ |
||
| 507 | 1 | public function update($cardId, $type, $baseInfo = [], $especial = []) |
|
| 508 | { |
||
| 509 | 1 | $card = []; |
|
| 510 | 1 | $card['card_id'] = $cardId; |
|
| 511 | 1 | $card[$type] = []; |
|
| 512 | |||
| 513 | 1 | $cardInfo = []; |
|
| 514 | 1 | if ($baseInfo) { |
|
| 515 | 1 | $cardInfo['base_info'] = $baseInfo; |
|
| 516 | 1 | } |
|
| 517 | |||
| 518 | 1 | $card[$type] = array_merge($cardInfo, $especial); |
|
| 519 | |||
| 520 | 1 | return $this->parseJSON('json', [self::API_UPDATE_CARD, $card]); |
|
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * 设置微信买单接口. |
||
| 525 | * 设置买单的 card_id 必须已经配置了门店,否则会报错. |
||
| 526 | * |
||
| 527 | * @param string $cardId |
||
| 528 | * @param bool $isOpen |
||
| 529 | * |
||
| 530 | * @return \EasyWeChat\Support\Collection |
||
| 531 | */ |
||
| 532 | 1 | View Code Duplication | public function setPayCell($cardId, $isOpen = true) |
| 533 | { |
||
| 534 | $params = [ |
||
| 535 | 1 | 'card_id' => $cardId, |
|
| 536 | 1 | 'is_open' => $isOpen, |
|
| 537 | 1 | ]; |
|
| 538 | |||
| 539 | 1 | return $this->parseJSON('json', [self::API_SET_PAY_CELL, $params]); |
|
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * 增加库存. |
||
| 544 | * |
||
| 545 | * @param string $cardId |
||
| 546 | * @param int $amount |
||
| 547 | * |
||
| 548 | * @return \EasyWeChat\Support\Collection |
||
| 549 | */ |
||
| 550 | 1 | public function increaseStock($cardId, $amount) |
|
| 554 | |||
| 555 | /** |
||
| 556 | * 减少库存. |
||
| 557 | * |
||
| 558 | * @param string $cardId |
||
| 559 | * @param int $amount |
||
| 560 | * |
||
| 561 | * @return \EasyWeChat\Support\Collection |
||
| 562 | */ |
||
| 563 | 1 | public function reduceStock($cardId, $amount) |
|
| 567 | |||
| 568 | /** |
||
| 569 | * 修改库存接口. |
||
| 570 | * |
||
| 571 | * @param string $cardId |
||
| 572 | * @param int $amount |
||
| 573 | * @param string $action |
||
| 574 | * |
||
| 575 | * @return \EasyWeChat\Support\Collection |
||
| 576 | */ |
||
| 577 | 1 | protected function updateStock($cardId, $amount, $action = 'increase') |
|
| 578 | { |
||
| 579 | 1 | $key = $action === 'increase' ? 'increase_stock_value' : 'reduce_stock_value'; |
|
| 580 | $params = [ |
||
| 581 | 1 | 'card_id' => $cardId, |
|
| 582 | 1 | $key => abs($amount), |
|
| 583 | 1 | ]; |
|
| 584 | |||
| 585 | 1 | return $this->parseJSON('json', [self::API_MODIFY_STOCK, $params]); |
|
| 586 | } |
||
| 587 | |||
| 588 | /** |
||
| 589 | * 更改Code接口. |
||
| 590 | * |
||
| 591 | * @param string $code |
||
| 592 | * @param string $newCode |
||
| 593 | * @param array $cardId |
||
| 594 | * |
||
| 595 | * @return \EasyWeChat\Support\Collection |
||
| 596 | */ |
||
| 597 | 1 | View Code Duplication | public function updateCode($code, $newCode, $cardId = []) |
| 598 | { |
||
| 599 | $params = [ |
||
| 600 | 1 | 'code' => $code, |
|
| 601 | 1 | 'new_code' => $newCode, |
|
| 602 | 1 | 'card_id' => $cardId, |
|
| 603 | 1 | ]; |
|
| 604 | |||
| 605 | 1 | return $this->parseJSON('json', [self::API_UPDATE_CODE, $params]); |
|
| 606 | } |
||
| 607 | |||
| 608 | /** |
||
| 609 | * 删除卡券接口. |
||
| 610 | * |
||
| 611 | * @param string $cardId |
||
| 612 | * |
||
| 613 | * @return \EasyWeChat\Support\Collection |
||
| 614 | */ |
||
| 615 | 1 | View Code Duplication | public function delete($cardId) |
| 616 | { |
||
| 617 | $params = [ |
||
| 618 | 1 | 'card_id' => $cardId, |
|
| 619 | 1 | ]; |
|
| 620 | |||
| 621 | 1 | return $this->parseJSON('json', [self::API_DELETE_CARD, $params]); |
|
| 622 | } |
||
| 623 | |||
| 624 | /** |
||
| 625 | * 设置卡券失效. |
||
| 626 | * |
||
| 627 | * @param string $code |
||
| 628 | * @param string $cardId |
||
| 629 | * |
||
| 630 | * @return \EasyWeChat\Support\Collection |
||
| 631 | */ |
||
| 632 | 1 | View Code Duplication | public function disable($code, $cardId = '') |
| 633 | { |
||
| 634 | $params = [ |
||
| 635 | 1 | 'code' => $code, |
|
| 636 | 1 | 'card_id' => $cardId, |
|
| 637 | 1 | ]; |
|
| 638 | |||
| 639 | 1 | return $this->parseJSON('json', [self::API_DISABLE_CARD, $params]); |
|
| 640 | } |
||
| 641 | |||
| 642 | /** |
||
| 643 | * 会员卡接口激活. |
||
| 644 | * |
||
| 645 | * @param array $info |
||
| 646 | * |
||
| 647 | * @return \EasyWeChat\Support\Collection |
||
| 648 | */ |
||
| 649 | 1 | public function activate($info = [], $cardType = 'member_card') |
|
| 650 | { |
||
| 651 | 1 | if ($cardType === 'general_card') { |
|
| 652 | return $this->parseJSON('json', [self::API_ACTIVATE_GENERAL_CARD, $info]); |
||
| 653 | } |
||
| 654 | |||
| 655 | 1 | return $this->parseJSON('json', [self::API_ACTIVATE_MEMBER_CARD, $info]); |
|
| 656 | } |
||
| 657 | |||
| 658 | /** |
||
| 659 | * 设置开卡字段接口. |
||
| 660 | * |
||
| 661 | * @param string $cardId |
||
| 662 | * @param array $requiredForm |
||
| 663 | * @param array $optionalForm |
||
| 664 | * |
||
| 665 | * @return \EasyWeChat\Support\Collection |
||
| 666 | */ |
||
| 667 | 1 | public function activateUserForm($cardId, array $requiredForm = [], array $optionalForm = []) |
|
| 668 | { |
||
| 669 | 1 | $params = array_merge(['card_id' => $cardId], $requiredForm, $optionalForm); |
|
| 670 | |||
| 671 | 1 | return $this->parseJSON('json', [self::API_ACTIVATE_MEMBER_USER_FORM, $params]); |
|
| 672 | } |
||
| 673 | |||
| 674 | /** |
||
| 675 | * 拉取会员信息接口. |
||
| 676 | * |
||
| 677 | * @param string $cardId |
||
| 678 | * @param string $code |
||
| 679 | * |
||
| 680 | * @return \EasyWeChat\Support\Collection |
||
| 681 | */ |
||
| 682 | 1 | View Code Duplication | public function getMemberCardUser($cardId, $code) |
| 683 | { |
||
| 684 | $params = [ |
||
| 685 | 1 | 'card_id' => $cardId, |
|
| 686 | 1 | 'code' => $code, |
|
| 687 | 1 | ]; |
|
| 688 | |||
| 689 | 1 | return $this->parseJSON('json', [self::API_GET_MEMBER_USER_INFO, $params]); |
|
| 690 | } |
||
| 691 | |||
| 692 | /** |
||
| 693 | * 更新会员信息. |
||
| 694 | * |
||
| 695 | * @param array $params |
||
| 696 | * |
||
| 697 | * @return \EasyWeChat\Support\Collection |
||
| 698 | */ |
||
| 699 | 1 | public function updateMemberCardUser(array $params = []) |
|
| 700 | { |
||
| 701 | 1 | return $this->parseJSON('json', [self::API_UPDATE_MEMBER_CARD_USER, $params]); |
|
| 702 | } |
||
| 703 | |||
| 704 | /** |
||
| 705 | * 更新通用员信息. |
||
| 706 | * |
||
| 707 | * @param array $params |
||
| 708 | * |
||
| 709 | * @return \EasyWeChat\Support\Collection |
||
| 710 | */ |
||
| 711 | public function updateGeneralCardUser(array $params = []) |
||
| 712 | { |
||
| 713 | return $this->parseJSON('json', [self::API_UPDATE_GENERAL_CARD_USER, $params]); |
||
| 714 | } |
||
| 715 | |||
| 716 | /** |
||
| 717 | * 添加子商户. |
||
| 718 | * |
||
| 719 | * @param array $info |
||
| 720 | * |
||
| 721 | * @return \EasyWeChat\Support\Collection |
||
| 722 | */ |
||
| 723 | 1 | View Code Duplication | public function createSubMerchant(array $info = []) |
| 741 | |||
| 742 | /** |
||
| 743 | * 更新子商户. |
||
| 744 | * |
||
| 745 | * @param int $merchantId |
||
| 746 | * @param array $info |
||
| 747 | * |
||
| 748 | * @return \EasyWeChat\Support\Collection |
||
| 749 | */ |
||
| 750 | 1 | View Code Duplication | public function updateSubMerchant($merchantId, array $info = []) |
| 751 | { |
||
| 752 | $params = [ |
||
| 753 | 1 | 'info' => array_merge(['merchant_id' => $merchantId], |
|
| 754 | 1 | Arr::only($info, [ |
|
| 755 | 1 | 'brand_name', |
|
| 756 | 1 | 'logo_url', |
|
| 757 | 1 | 'protocol', |
|
| 758 | 1 | 'end_time', |
|
| 759 | 1 | 'primary_category_id', |
|
| 760 | 1 | 'secondary_category_id', |
|
| 761 | 1 | 'agreement_media_id', |
|
| 762 | 1 | 'operator_media_id', |
|
| 763 | 1 | 'app_id', |
|
| 764 | 1 | ])), |
|
| 765 | 1 | ]; |
|
| 766 | |||
| 767 | 1 | return $this->parseJSON('json', [self::API_UPDATE_SUB_MERCHANT, $params]); |
|
| 768 | } |
||
| 769 | |||
| 770 | /** |
||
| 771 | * 获取子商户信息. |
||
| 772 | * |
||
| 773 | * @param int $merchantId |
||
| 774 | * |
||
| 775 | * @return \EasyWeChat\Support\Collection |
||
| 776 | */ |
||
| 777 | public function getSubMerchant($merchantId) |
||
| 781 | |||
| 782 | /** |
||
| 783 | * 批量获取子商户信息. |
||
| 784 | * |
||
| 785 | * @param int $beginId |
||
| 786 | * @param int $limit |
||
| 787 | * @param string $status |
||
| 788 | * |
||
| 789 | * @return \EasyWeChat\Support\Collection |
||
| 790 | */ |
||
| 791 | public function listSubMerchants($beginId = 0, $limit = 50, $status = 'CHECKING') |
||
| 801 | |||
| 802 | /** |
||
| 803 | * 卡券开放类目查询接口. |
||
| 804 | * |
||
| 805 | * @return \EasyWeChat\Support\Collection |
||
| 806 | */ |
||
| 807 | 1 | public function getCategories() |
|
| 811 | |||
| 812 | /** |
||
| 813 | * Set cache manager. |
||
| 814 | * |
||
| 815 | * @param \Doctrine\Common\Cache\Cache $cache |
||
| 816 | * |
||
| 817 | * @return $this |
||
| 818 | */ |
||
| 819 | 2 | public function setCache(Cache $cache) |
|
| 825 | |||
| 826 | /** |
||
| 827 | * Return cache manager. |
||
| 828 | * |
||
| 829 | * @return \Doctrine\Common\Cache\Cache |
||
| 830 | */ |
||
| 831 | 2 | public function getCache() |
|
| 835 | |||
| 836 | /** |
||
| 837 | * Set Api_ticket cache prifix. |
||
| 838 | * |
||
| 839 | * @param string $prefix |
||
| 840 | * |
||
| 841 | * @return $this |
||
| 842 | */ |
||
| 843 | public function setTicketCachePrefix($prefix) |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Set Api_ticket cache key. |
||
| 852 | * |
||
| 853 | * @param string $cacheKey |
||
| 854 | * |
||
| 855 | * @return $this |
||
| 856 | */ |
||
| 857 | public function setTicketCacheKey($cacheKey) |
||
| 863 | |||
| 864 | /** |
||
| 865 | * Get ApiTicket token cache key. |
||
| 866 | * |
||
| 867 | * @return string |
||
| 868 | */ |
||
| 869 | 2 | public function getTicketCacheKey() |
|
| 877 | |||
| 878 | /** |
||
| 879 | * Set current url. |
||
| 880 | * |
||
| 881 | * @param string $url |
||
| 882 | * |
||
| 883 | * @return Card |
||
| 884 | */ |
||
| 885 | 1 | public function setUrl($url) |
|
| 891 | } |
||
| 892 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: