JSSDK 用来生成 JSAPI 配置文件,详情 http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
定义 Jssdk 需要注入 AccessToken 对象
use Thenbsp\Wechat\Jssdk;
$jssdk = new Jssdk($accessToken);
定义接口
$jssdk->addApi('onMenuShareAppMessage');
$jssdk->addApi('onMenuShareTimeline');
$jssdk->addApi('chooseWXPay');
使用数组式
$apis = array('onMenuShareAppMessage', 'onMenuShareTimeline', 'chooseWXPay')
$jssdk->addApi($apis);
链式访问
$jssdk
->addApi('onMenuShareAppMessage')
->addApi('onMenuShareTimeline')
->addApi('chooseWXPay');
开启调试模式
$jssdk->enableDebug();
获取配置,默认返回 JSON
$config = $jssdk->getConfig();
var_dump($config);
结果
{
"appId": "wx345f3830c2897po4",
"nonceStr": "5610f512b0aew4",
"timestamp": "1443951658",
"signature": "d417c5489b69fba8c118157e87da80818b974a46",
"jsApiList":[
"onMenuShareTimeline",
"onMenuShareAppMessage"
],
"debug": true
}
也可以生成数组
$configArray = $jssdk->getConfig(true);
将配置文件注入到接口
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
wx.config(<?php echo $config; ?>);
</script>
如果是数组,可以单独指定参数
<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
wx.config({
"appId": "<?php echo $configArray['appId']; ?>",
"nonceStr": "<?php echo $configArray['nonceStr']; ?>",
"timestamp": "<?php echo $configArray['timestamp']; ?>",
"signature": "<?php echo $configArray['signature']; ?>",
"jsApiList": ["onMenuShareTimeline", "onMenuShareAppMessage" ],
"debug": true
});
</script>