Completed
Push — master ( 02c99c...4eb343 )
by Carlos
02:15
created

Client::configSignature()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 10
nc 8
nop 3
dl 0
loc 14
ccs 0
cts 8
cp 0
crap 20
rs 9.2
c 0
b 0
f 0
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\BasicService\Jssdk;
13
14
use EasyWeChat\Kernel\BaseClient;
15
use EasyWeChat\Kernel\Support;
16
use EasyWeChat\Kernel\Traits\InteractsWithCache;
17
18
/**
19
 * Class Client.
20
 *
21
 * @author overtrue <[email protected]>
22
 */
23
class Client extends BaseClient
24
{
25
    use InteractsWithCache;
26
27
    /**
28
     * @var string
29
     */
30
    const API_GET_TICKET = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';
31
32
    /**
33
     * Current URI.
34
     *
35
     * @var string
36
     */
37
    protected $url;
38
39
    /**
40
     * Get config json for jsapi.
41
     *
42
     * @param array $jsApiList
43
     * @param bool  $debug
44
     * @param bool  $beta
45
     * @param bool  $json
46
     *
47
     * @return array|string
48
     */
49
    public function buildConfig(array $jsApiList, bool $debug = false, bool $beta = false, bool $json = true)
50
    {
51
        $config = array_merge(compact('debug', 'beta', 'jsApiList'), $this->configSignature());
52
53
        return $json ? json_encode($config) : $config;
54
    }
55
56
    /**
57
     * Return jsapi config as a PHP array.
58
     *
59
     * @param array $apis
60
     * @param bool  $debug
61
     * @param bool  $beta
62
     *
63
     * @return array
64
     */
65
    public function getConfigArray(array $apis, bool $debug = false, bool $beta = false)
66
    {
67
        return $this->buildConfig($apis, $debug, $beta, false);
68
    }
69
70
    /**
71
     * Get js ticket.
72
     *
73
     * @param bool   $refresh
74
     * @param string $type
75
     *
76
     * @return array
77
     */
78
    public function getTicket(bool $refresh = false, string $type = 'jsapi'): array
79
    {
80
        $cacheKey = sprintf('easywechat.basic_service.jssdk.ticket.%s.%s', $type, $this->getAppId());
81
82
        if (!$refresh && $this->getCache()->has($cacheKey)) {
83
            return $this->getCache()->get($cacheKey);
84
        }
85
86
        $result = $this->resolveResponse(
87
            $this->requestRaw(static::API_GET_TICKET, 'GET', ['query' => ['type' => $type]]),
88
            'array'
89
        );
90
91
        $this->getCache()->set($cacheKey, $result, $result['expires_in'] - 500);
92
93
        return $result;
94
    }
95
96
    /**
97
     * Build signature.
98
     *
99
     * @param string|null $url
100
     * @param string|null $nonce
101
     * @param int|null    $timestamp
102
     *
103
     * @return array
104
     */
105
    protected function configSignature(string $url = null, string $nonce = null, $timestamp = null): array
106
    {
107
        $url = $url ?: $this->getUrl();
108
        $nonce = $nonce ?: Support\Str::quickRandom(10);
109
        $timestamp = $timestamp ?: time();
110
111
        return [
112
            'appId' => $this->getAppId(),
113
            'nonceStr' => $nonce,
114
            'timestamp' => $timestamp,
115
            'url' => $url,
116
            'signature' => $this->getTicketSignature($this->getTicket()['ticket'], $nonce, $timestamp, $url),
117
        ];
118
    }
119
120
    /**
121
     * Sign the params.
122
     *
123
     * @param string $ticket
124
     * @param string $nonce
125
     * @param int    $timestamp
126
     * @param string $url
127
     *
128
     * @return string
129
     */
130
    public function getTicketSignature($ticket, $nonce, $timestamp, $url): string
131
    {
132
        return sha1("jsapi_ticket={$ticket}&noncestr={$nonce}&timestamp={$timestamp}&url={$url}");
133
    }
134
135
    /**
136
     * @return string
137
     */
138
    public function dictionaryOrderSignature()
139
    {
140
        $params = func_get_args();
141
142
        sort($params, SORT_STRING);
143
144
        return sha1(implode($params));
145
    }
146
147
    /**
148
     * Set current url.
149
     *
150
     * @param string $url
151
     *
152
     * @return $this
153
     */
154
    public function setUrl(string $url)
155
    {
156
        $this->url = $url;
157
158
        return $this;
159
    }
160
161
    /**
162
     * Get current url.
163
     *
164
     * @return string
165
     */
166
    public function getUrl(): string
167
    {
168
        if ($this->url) {
169
            return $this->url;
170
        }
171
172
        return Support\current_url();
173
    }
174
175
    /**
176
     * @return string
177
     */
178
    protected function getAppId()
179
    {
180
        return $this->app['config']->get('app_id');
181
    }
182
}
183