api/controller/ApiWechat.php
public function getSignPackage($jsapiTicket,$redirectUrl,$appid,$jsApiList) {
$url = $redirectUrl;
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=".$jsapiTicket."&noncestr=".$nonceStr."×tamp=".$timestamp."&url=".$url."";
$signature = sha1($string);
$signPackage = array(
"debug" =>true,
"appId" => $appid,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"signature" => $signature,
"jsApiList" => $jsApiList,
"openTagList"=>["wx-open-subscribe"]
);
return $signPackage;
}
前端 notice.html
<wx-open-subscribe template="CH930CkG426xV2OZ2-4BL5SPD33Jon55GjgZp_GyrBc" id="subscribe-btn">
<script type="text/wxtag-template" slot="style">
<style>
.subscribe-btn {
color: #fff;
background-color: #07c160;
}
</style>
</script>
<script type="text/wxtag-template">
<button id="subscribe-btn">
一次性模版消息订阅
</button>
</script>
</wx-open-subscribe>
<script src="https://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
<script src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js" ></script>
<script>
$(document).ready(function(){
// 在这里写你的代码...
$.getJSON("https://zhyframe.fzh.fun/app/index.php?i=1023&from=wxapp&c=entry&a=wxapp&do=api_wechat|getJssdkConfig&url=https%3a%2f%2fzhyframe.fzh.fun%2fnotice.html", function(res){
var data = res.data;
wx.config(data);
});
});
var btn = document.getElementById('subscribe-btn');
btn.addEventListener('success', function (e) {
console.log('success', e.detail);
});
btn.addEventListener('error',function (e) {
console.log('fail', e.detail);
});
</script>