Skip to content

Commit e021b12

Browse files
committed
增加第三方平台JSSDK签名包
1 parent 77f5b41 commit e021b12

File tree

6 files changed

+64
-31
lines changed

6 files changed

+64
-31
lines changed

Wechat/Lib/Cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static public function del($name) {
6060
if (isset(Loader::$callback['CacheDel'])) {
6161
return call_user_func_array(Loader::$callback['CacheDel'], func_get_args());
6262
}
63-
return self::check() && unlink(self::$cachepath . $name);
63+
return self::check() && @unlink(self::$cachepath . $name);
6464
}
6565

6666
/**

Wechat/Lib/Common.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,16 @@ class Common {
2828
public $access_token;
2929
public $postxml;
3030
public $_msg;
31-
public $_funcflag = 1;
32-
public $debug = false;
33-
public $errCode = 40001;
31+
public $errCode = 0;
3432
public $errMsg = "no access";
3533
public $config = array();
3634
private $_retry = FALSE;
3735

3836
/**
3937
* 构造方法
40-
* @param type $options
38+
* @param array $options
4139
*/
42-
public function __construct($options) {
40+
public function __construct($options = array()) {
4341
$config = Loader::config($options);
4442
$this->token = isset($config['token']) ? $config['token'] : '';
4543
$this->appid = isset($config['appid']) ? $config['appid'] : '';

Wechat/Lib/Tools.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ static public function xml2arr($xml) {
102102

103103
/**
104104
* 生成安全JSON数据
105-
* @param $array
105+
* @param array $array
106106
* @return string
107107
*/
108108
static public function json_encode($array) {
@@ -136,7 +136,7 @@ static public function httpGet($url) {
136136
/**
137137
* 以post方式提交请求
138138
* @param string $url
139-
* @param array $postdata
139+
* @param array|string $postdata
140140
* @return bool|mixed
141141
*/
142142
static public function httpPost($url, $postdata) {

Wechat/WechatScript.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WechatScript extends Common {
1818
* JSAPI授权TICKET
1919
* @var string
2020
*/
21-
private $jsapi_ticket;
21+
public $jsapi_ticket;
2222

2323
/**
2424
* 删除JSAPI授权TICKET
@@ -36,11 +36,15 @@ public function resetJsTicket($appid = '') {
3636
* 获取JSAPI授权TICKET
3737
* @param string $appid 用于多个appid时使用,可空
3838
* @param string $jsapi_ticket 手动指定jsapi_ticket,非必要情况不建议用
39+
* @param string $access_token 获取 jsapi_ticket 指定 access_token
3940
* @return bool|string
4041
*/
41-
public function getJsTicket($appid = '', $jsapi_ticket = '') {
42-
if (!$this->access_token && !$this->getAccessToken()) {
43-
return false;
42+
public function getJsTicket($appid = '', $jsapi_ticket = '', $access_token = '') {
43+
if (empty($access_token)) {
44+
if (!$this->access_token && !$this->getAccessToken()) {
45+
return false;
46+
}
47+
$access_token = $this->access_token;
4448
}
4549
if (empty($appid)) {
4650
$appid = $this->appid;
@@ -61,7 +65,7 @@ public function getJsTicket($appid = '', $jsapi_ticket = '') {
6165
return $this->jsapi_ticket = call_user_func_array(Loader::$callback[__FUNCTION__], array(&$this, &$cache));
6266
}
6367
# 调接口获取
64-
$result = Tools::httpGet(self::API_URL_PREFIX . self::GET_TICKET_URL . "access_token={$this->access_token}" . '&type=jsapi');
68+
$result = Tools::httpGet(self::API_URL_PREFIX . self::GET_TICKET_URL . "access_token={$access_token}" . '&type=jsapi');
6569
if ($result) {
6670
$json = json_decode($result, true);
6771
if (!$json || !empty($json['errcode'])) {
@@ -82,10 +86,11 @@ public function getJsTicket($appid = '', $jsapi_ticket = '') {
8286
* @param int $timestamp 当前时间戳 (为空则自动生成)
8387
* @param string $noncestr 随机串 (为空则自动生成)
8488
* @param string $appid 用于多个appid时使用,可空
85-
* @return bool|array 返回签名字串
89+
* @param string $access_token 获取 jsapi_ticket 指定 access_token
90+
* @return array|bool 返回签名字串
8691
*/
87-
public function getJsSign($url, $timestamp = 0, $noncestr = '', $appid = '') {
88-
if (!$this->jsapi_ticket && !$this->getJsTicket($appid) || empty($url)) {
92+
public function getJsSign($url, $timestamp = 0, $noncestr = '', $appid = '', $access_token = '') {
93+
if (!$this->jsapi_ticket && !$this->getJsTicket($appid, '', $access_token) || empty($url)) {
8994
return false;
9095
}
9196
$data = array(
@@ -95,7 +100,8 @@ public function getJsSign($url, $timestamp = 0, $noncestr = '', $appid = '') {
95100
"url" => trim($url),
96101
);
97102
return array(
98-
# 'debug' => true,
103+
"url" => $url,
104+
'debug' => false,
99105
"appId" => empty($appid) ? $this->appid : $appid,
100106
"nonceStr" => $data['noncestr'],
101107
"timestamp" => $data['timestamp'],

Wechat/WechatService.php

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ class WechatService {
2525
const QUERY_AUTH_URL = '/api_query_auth';
2626
// 获取授权方的账户信息
2727
const GET_AUTHORIZER_INFO_URL = '/api_get_authorizer_info';
28-
// 刷新授权令牌
29-
const REFRESH_AUTHORIZER_TOKEN = './api_authorizer_token';
3028
// 获取授权方的选项设置信息
3129
const GET_AUTHORIZER_OPTION_URL = '/api_get_authorizer_option';
3230
// 设置授权方的选项信息
@@ -50,14 +48,8 @@ class WechatService {
5048
protected $component_access_token;
5149
// 刷新令牌
5250
protected $authorizer_appid;
53-
// 预授权码
54-
protected $authorizer_access_token;
55-
// Wechat对象缓存
56-
protected $authorizer_refresh_token;
5751
// JSON数据
5852
protected $pre_auth_code;
59-
// 错误代码
60-
protected $wechat = array();
6153
// 错误消息
6254
protected $data;
6355

@@ -76,7 +68,7 @@ public function __construct($options = array()) {
7668

7769
/**
7870
* 接收公众平台推送的 Ticket
79-
* @return bool
71+
* @return bool|array
8072
*/
8173
public function getComonentTicket() {
8274
$receive = new WechatReceive(array(
@@ -330,7 +322,7 @@ public function getPreauthCode() {
330322
*/
331323
public function getOauthRedirect($appid, $redirect_uri, $scope = 'snsapi_userinfo') {
332324
return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri=" . urlencode($redirect_uri)
333-
. "&response_type=code&scope={$scope}&state={$appid}&component_appid={$this->component_appid}#wechat_redirect";
325+
. "&response_type=code&scope={$scope}&state={$appid}&component_appid={$this->component_appid}#wechat_redirect";
334326
}
335327

336328
/**
@@ -348,10 +340,10 @@ public function getOauthAccessToken($appid) {
348340
return false;
349341
}
350342
$url = "https://api.weixin.qq.com/sns/oauth2/component/access_token?"
351-
. "appid={$appid}&code={$code}&"
352-
. "grant_type=authorization_code&"
353-
. "component_appid={$this->component_appid}&"
354-
. "component_access_token={$this->component_access_token}";
343+
. "appid={$appid}&code={$code}&"
344+
. "grant_type=authorization_code&"
345+
. "component_appid={$this->component_appid}&"
346+
. "component_access_token={$this->component_access_token}";
355347
$json = $this->parseJson(Tools::httpGet($url));
356348
if ($json !== false) {
357349
return $json;
@@ -386,4 +378,5 @@ public function getOauthUserInfo($openid, $oauthAccessToken) {
386378
return $this->parseJson(Tools::httpGet($url));
387379
}
388380

381+
389382
}

test.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,39 @@
1818
var_dump($userlist);
1919
var_dump($wechat->errMsg);
2020
var_dump($wechat->errCode);
21+
22+
exit;
23+
24+
// 第三方平台 JSSDK 签名包
25+
26+
$wechat = Db::table('wechat_config')->where('authorizer_appid', 'wx60a43dd8161666d4')->find();
27+
// 第三方授权获取到的 Access_token
28+
$access_token = $wechat['authorizer_access_token'];
29+
// 参与授权的公众号 APPID
30+
$authorizer_appid = $wechat['authorizer_appid'];
31+
// 当前微信页面URL地址(完整)
32+
$current_url = url('', '', true, true);
33+
// 实例SDK脚本
34+
$script = load_wechat('Script', $authorizer_appid);
35+
// 获取JS签名包
36+
$result = $script->getJsSign($current_url, 0, '', $authorizer_appid, $access_token);
37+
dump($result);
38+
dump([$script->errMsg, $script->errCode]);
39+
40+
$json = json_encode($result, JSON_PRETTY_PRINT);
41+
echo '<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>';
42+
echo "
43+
<script>
44+
// JSSDK 错误处理
45+
wx.error(function(){
46+
//alert(JSON.stringify(arguments));
47+
});
48+
// JSSDK 配置参数
49+
wx.config({$json});
50+
// JSSDK 初始化成功
51+
wx.ready(function(){
52+
alert('初始化成功!');
53+
wx.hideOptionMenu();
54+
});
55+
</script>
56+
";

0 commit comments

Comments
 (0)