forked from thenbsp/wechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsapi.php
More file actions
126 lines (104 loc) · 3.18 KB
/
Jsapi.php
File metadata and controls
126 lines (104 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
namespace Thenbsp\Wechat\Wechat;
use Thenbsp\Wechat\Bridge\Util;
use Thenbsp\Wechat\Bridge\CacheTrait;
use Thenbsp\Wechat\Bridge\Serializer;
use Thenbsp\Wechat\Wechat\Jsapi\Ticket;
use Thenbsp\Wechat\Wechat\AccessToken;
class Jsapi
{
/**
* Cache Trait
*/
use CacheTrait;
/**
* Thenbsp\Wechat\Wechat\AccessToken
*/
protected $accessToken;
/**
* 是否开起调试
*/
protected $debug = false;
/**
* 接口列表
*/
protected $api = array();
/**
* 全部接口
*/
protected $apiValids = array(
'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ', 'onMenuShareWeibo',
'onMenuShareQZone', 'startRecord', 'stopRecord', 'onVoiceRecordEnd', 'playVoice',
'pauseVoice', 'stopVoice', 'onVoicePlayEnd', 'uploadVoice', 'downloadVoice', 'chooseImage',
'previewImage', 'uploadImage', 'downloadImage', 'translateVoice', 'getNetworkType',
'openLocation', 'getLocation', 'hideOptionMenu', 'showOptionMenu', 'hideMenuItems',
'showMenuItems', 'hideAllNonBaseMenuItem', 'showAllNonBaseMenuItem', 'closeWindow',
'scanQRCode', 'chooseWXPay', 'openProductSpecificView', 'addCard', 'chooseCard', 'openCard'
);
/**
* 构造方法
*/
public function __construct(AccessToken $accessToken)
{
$this->accessToken = $accessToken;
}
/**
* 注入接口
*/
public function addApi($apis)
{
if( is_array($apis) ) {
foreach( $apis AS $api ) {
$this->addApi($api);
}
}
$apiName = (string) $apis;
if( !in_array($apiName, $this->apiValids, true) ) {
throw new \InvalidArgumentException(sprintf('Invalid Api: %s', $apiName));
}
array_push($this->api, $apiName);
return $this;
}
/**
* 启用调试模式
*/
public function enableDebug()
{
$this->debug = true;
return $this;
}
/**
* 获取配置文件
*/
public function getConfig($asArray = false)
{
$ticket = new Ticket($this->accessToken);
if( $this->cache ) {
$ticket->setCache($this->cache);
}
$options = array(
'jsapi_ticket' => $ticket->getTicketString(),
'timestamp' => Util::getTimestamp(),
'url' => Util::getCurrentUrl(),
'noncestr' => Util::getRandomString(),
);
ksort($options);
$signature = sha1(urldecode(http_build_query($options)));
$configure = array(
'appId' => $this->accessToken['appid'],
'nonceStr' => $options['noncestr'],
'timestamp' => $options['timestamp'],
'signature' => $signature,
'jsApiList' => $this->api,
'debug' => (bool) $this->debug
);
return $asArray ? $configure : Serializer::jsonEncode($configure);
}
/**
* 输出对象
*/
public function __toString()
{
return $this->getConfig();
}
}