|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/wechat. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace EasyWeChat\OfficialAccount\Card; |
|
13
|
|
|
|
|
14
|
|
|
use EasyWeChat\BasicService\Jssdk\Client as Jssdk; |
|
15
|
|
|
use EasyWeChat\Kernel\Support\Arr; |
|
16
|
|
|
use function EasyWeChat\Kernel\Support\str_random; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class Jssdk. |
|
20
|
|
|
* |
|
21
|
|
|
* @author overtrue <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class JssdkClient extends Jssdk |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @param bool $refresh |
|
27
|
|
|
* @param string $type |
|
28
|
|
|
* |
|
29
|
|
|
* @return array |
|
30
|
|
|
* |
|
31
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
32
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function getTicket(bool $refresh = false, string $type = 'wx_card'): array |
|
35
|
|
|
{ |
|
36
|
1 |
|
return parent::getTicket($refresh, $type); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* 微信卡券:JSAPI 卡券发放. |
|
41
|
|
|
* |
|
42
|
|
|
* @param array $cards |
|
43
|
|
|
* |
|
44
|
|
|
* @return string |
|
45
|
|
|
*/ |
|
46
|
|
|
public function assign(array $cards) |
|
47
|
|
|
{ |
|
48
|
1 |
|
return json_encode(array_map(function ($card) { |
|
49
|
1 |
|
return $this->attachExtension($card['card_id'], $card); |
|
50
|
1 |
|
}, $cards)); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* 生成 js添加到卡包 需要的 card_list 项. |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $cardId |
|
57
|
|
|
* @param array $extension |
|
58
|
|
|
* |
|
59
|
|
|
* @return array |
|
60
|
|
|
* |
|
61
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException |
|
62
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
|
63
|
|
|
*/ |
|
64
|
1 |
|
public function attachExtension($cardId, array $extension = []) |
|
65
|
|
|
{ |
|
66
|
1 |
|
$timestamp = time(); |
|
67
|
1 |
|
$nonce = str_random(6); |
|
|
|
|
|
|
68
|
1 |
|
$ticket = $this->getTicket()['ticket']; |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
1 |
|
$ext = array_merge(['timestamp' => $timestamp, 'nonce_str' => $nonce], Arr::only( |
|
71
|
1 |
|
$extension, |
|
72
|
1 |
|
['code', 'openid', 'outer_id', 'balance', 'fixed_begintimestamp', 'outer_str'] |
|
73
|
|
|
)); |
|
74
|
|
|
|
|
75
|
1 |
|
$ext['signature'] = $this->dictionaryOrderSignature($ticket, $timestamp, $cardId, $ext['code'] ?? '', $ext['openid'] ?? '', $nonce); |
|
76
|
|
|
|
|
77
|
|
|
return [ |
|
78
|
1 |
|
'cardId' => $cardId, |
|
79
|
1 |
|
'cardExt' => json_encode($ext), |
|
80
|
|
|
]; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.