|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the mingyoung/dingtalk. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 张铭阳 <[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 EasyDingTalk\H5app; |
|
13
|
|
|
|
|
14
|
|
|
use EasyDingTalk\Kernel\BaseClient; |
|
15
|
|
|
use EasyDingTalk\Kernel\Concerns\InteractsWithCache; |
|
16
|
|
|
use EasyDingTalk\Kernel\Exceptions\InvalidCredentialsException; |
|
17
|
|
|
|
|
18
|
|
|
class Client extends BaseClient |
|
19
|
|
|
{ |
|
20
|
|
|
use InteractsWithCache; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* 获取 jsapi_ticket |
|
24
|
|
|
* |
|
25
|
|
|
* @return mixed |
|
26
|
|
|
*/ |
|
27
|
|
|
public function get() |
|
28
|
|
|
{ |
|
29
|
|
|
if ($value = $this->getCache()->get($this->cacheFor())) { |
|
30
|
|
|
return $value; |
|
31
|
|
|
} |
|
32
|
|
|
$value = $this->client->get('get_jsapi_ticket'); |
|
33
|
|
|
if (0 !== $value['errcode']) { |
|
34
|
|
|
throw new InvalidCredentialsException(json_encode($value)); |
|
35
|
|
|
} |
|
36
|
|
|
$this->getCache()->set($this->cacheFor(), $value, $value['expires_in']); |
|
37
|
|
|
return $value; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* 获取 ticket |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getTicket() |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->get()['ticket']; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* 获取签名相关信息 |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $url |
|
54
|
|
|
* |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getSignature($url) |
|
58
|
|
|
{ |
|
59
|
|
|
$nonceStr = $this->getNonceStr(); |
|
60
|
|
|
$timeStamp = time(); |
|
61
|
|
|
$plain = 'jsapi_ticket=' . $this->getTicket() . '&noncestr=' . $nonceStr . '×tamp=' . $timeStamp . '&url=' . $url; |
|
62
|
|
|
$signature = sha1($plain); |
|
63
|
|
|
return [ |
|
64
|
|
|
'agentId' => $this->app['config']->get('agent_id'), |
|
65
|
|
|
'corpId' => $this->app['config']->get('corp_id'), |
|
66
|
|
|
'timeStamp' => $timeStamp, |
|
67
|
|
|
'nonceStr' => $nonceStr, |
|
68
|
|
|
'signature' => $signature, |
|
69
|
|
|
'url' => $url |
|
70
|
|
|
]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* 缓存 Key |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
|
|
protected function cacheFor() |
|
79
|
|
|
{ |
|
80
|
|
|
return sprintf('jsapi_ticket.%s', $this->app['config']->get('app_key')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* 生产 随机字符串 |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
protected function getNonceStr($length=16) |
|
89
|
|
|
{ |
|
90
|
|
|
$strs = "QWERTYUIOPASDFGHJKLZXCVBNM1234567890qwertyuiopasdfghjklzxcvbnm"; |
|
91
|
|
|
return substr(str_shuffle($strs), mt_rand(0, strlen($strs)-11), $length); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|