Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
ruer_cms
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
蔡闯
ruer_cms
Commits
bf40fb7b
Commit
bf40fb7b
authored
Jul 02, 2021
by
王峰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
公众号分享获取签名信息
parent
908e59b2
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
166 additions
and
0 deletions
+166
-0
app/wechatapi/controller/Company.php
app/wechatapi/controller/Company.php
+14
-0
extend/wxjs_sdk/access_token.php
extend/wxjs_sdk/access_token.php
+2
-0
extend/wxjs_sdk/jsapi_ticket.php
extend/wxjs_sdk/jsapi_ticket.php
+2
-0
extend/wxjs_sdk/jssdk.php
extend/wxjs_sdk/jssdk.php
+106
-0
extend/wxjs_sdk/sample.php
extend/wxjs_sdk/sample.php
+42
-0
No files found.
app/wechatapi/controller/Company.php
View file @
bf40fb7b
...
...
@@ -47,6 +47,20 @@ class Company extends BaseController
}
/**
* 公众号分享获取签名信息
* Author wangfeng
* DateTime 2021/07/02 16:00
* @return \think\Response
*/
public
function
wxPackage
()
{
include_once
dirname
(
dirname
(
dirname
(
__DIR__
)))
.
'/extend/wxjs_sdk/jssdk.php'
;
$wx_sdk
=
new
\JSSDK
(
'wx46df32c2ccfc177d'
,
'cde9bf412f5a0e524c143241daf9a383'
);
$result
=
$wx_sdk
->
getSignPackage
();
return
$this
->
returnJson
(
$result
);
}
}
extend/wxjs_sdk/access_token.php
0 → 100644
View file @
bf40fb7b
<?php
exit
();
?>
{"access_token":"","expire_time":0}
extend/wxjs_sdk/jsapi_ticket.php
0 → 100644
View file @
bf40fb7b
<?php
exit
();
?>
{"jsapi_ticket":"","expire_time":0}
extend/wxjs_sdk/jssdk.php
0 → 100644
View file @
bf40fb7b
<?php
class
JSSDK
{
private
$appId
;
private
$appSecret
;
public
function
__construct
(
$appId
,
$appSecret
)
{
$this
->
appId
=
$appId
;
$this
->
appSecret
=
$appSecret
;
}
public
function
getSignPackage
()
{
$jsapiTicket
=
$this
->
getJsApiTicket
();
// 注意 URL 一定要动态获取,不能 hardcode.
$protocol
=
(
!
empty
(
$_SERVER
[
'HTTPS'
])
&&
$_SERVER
[
'HTTPS'
]
!==
'off'
||
$_SERVER
[
'SERVER_PORT'
]
==
443
)
?
"https://"
:
"http://"
;
$url
=
"
$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]
"
;
$timestamp
=
time
();
$nonceStr
=
$this
->
createNonceStr
();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string
=
"jsapi_ticket=
$jsapiTicket
&noncestr=
$nonceStr
×tamp=
$timestamp
&url=
$url
"
;
$signature
=
sha1
(
$string
);
$signPackage
=
array
(
"appId"
=>
$this
->
appId
,
"nonceStr"
=>
$nonceStr
,
"timestamp"
=>
$timestamp
,
"url"
=>
$url
,
"signature"
=>
$signature
,
"rawString"
=>
$string
);
return
$signPackage
;
}
private
function
createNonceStr
(
$length
=
16
)
{
$chars
=
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
;
$str
=
""
;
for
(
$i
=
0
;
$i
<
$length
;
$i
++
)
{
$str
.=
substr
(
$chars
,
mt_rand
(
0
,
strlen
(
$chars
)
-
1
),
1
);
}
return
$str
;
}
private
function
getJsApiTicket
()
{
$accessToken
=
$this
->
getAccessToken
();
// 如果是企业号用以下 URL 获取 ticket
// $url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
$url
=
"https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=
$accessToken
"
;
$res
=
json_decode
(
$this
->
httpGet
(
$url
));
$ticket
=
$res
->
ticket
;
/*if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$this->set_php_file("jsapi_ticket.php", json_encode($data));
}*/
return
$ticket
;
}
private
function
getAccessToken
()
{
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$url
=
"https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=
$this->appId
&secret=
$this->appSecret
"
;
$res
=
json_decode
(
$this
->
httpGet
(
$url
));
$access_token
=
$res
->
access_token
;
return
$access_token
;
}
private
function
httpGet
(
$url
)
{
$curl
=
curl_init
();
curl_setopt
(
$curl
,
CURLOPT_RETURNTRANSFER
,
true
);
curl_setopt
(
$curl
,
CURLOPT_TIMEOUT
,
500
);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt
(
$curl
,
CURLOPT_SSL_VERIFYPEER
,
false
);
curl_setopt
(
$curl
,
CURLOPT_SSL_VERIFYHOST
,
false
);
curl_setopt
(
$curl
,
CURLOPT_URL
,
$url
);
$res
=
curl_exec
(
$curl
);
curl_close
(
$curl
);
return
$res
;
}
private
function
get_php_file
(
$filename
)
{
return
trim
(
substr
(
file_get_contents
(
$filename
),
15
));
}
private
function
set_php_file
(
$filename
,
$content
)
{
$fp
=
fopen
(
$filename
,
"w"
);
fwrite
(
$fp
,
"<?php exit();?>"
.
$content
);
fclose
(
$fp
);
}
}
extend/wxjs_sdk/sample.php
0 → 100644
View file @
bf40fb7b
<?php
require_once
"jssdk.php"
;
$jssdk
=
new
JSSDK
(
"yourAppID"
,
"yourAppSecret"
);
$signPackage
=
$jssdk
->
GetSignPackage
();
?>
<!DOCTYPE html>
<html
lang=
"en"
>
<head>
<meta
charset=
"UTF-8"
>
<title></title>
</head>
<body>
</body>
<script
src=
"http://res.wx.qq.com/open/js/jweixin-1.0.0.js"
></script>
<script>
/*
* 注意:
* 1. 所有的JS接口只能在公众号绑定的域名下调用,公众号开发者需要先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
* 2. 如果发现在 Android 不能分享自定义内容,请到官网下载最新的包覆盖安装,Android 自定义分享接口需升级至 6.0.2.58 版本及以上。
* 3. 常见问题及完整 JS-SDK 文档地址:http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
*
* 开发中遇到问题详见文档“附录5-常见错误及解决办法”解决,如仍未能解决可通过以下渠道反馈:
* 邮箱地址:weixin-open@qq.com
* 邮件主题:【微信JS-SDK反馈】具体问题
* 邮件内容说明:用简明的语言描述问题所在,并交代清楚遇到该问题的场景,可附上截屏图片,微信团队会尽快处理你的反馈。
*/
wx
.
config
({
debug
:
true
,
appId
:
'
<?php
echo
$signPackage
[
"appId"
];
?>
'
,
timestamp
:
<?php
echo
$signPackage
[
"timestamp"
];
?>
,
nonceStr
:
'
<?php
echo
$signPackage
[
"nonceStr"
];
?>
'
,
signature
:
'
<?php
echo
$signPackage
[
"signature"
];
?>
'
,
jsApiList
:
[
// 所有要调用的 API 都要加到这个列表中
]
});
wx
.
ready
(
function
()
{
// 在这里调用 API
});
</script>
</html>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment