Authored by whb

修改微信

... ... @@ -192,7 +192,6 @@ EOT;
public function detailAction()
{
$id = intval($this->_request->id);
$link = SITE_MAIN.url('news/detail', array('id' => $id));
$news = array();
if (empty($id))
{
... ... @@ -215,7 +214,6 @@ EOT;
$news = array($temp['id'] => $temp);
}
$news = array_slice(array_filter($news + $temp2), 0, 3);
$this->_view['wechat'] = Lib_Wechat_JSSDK::getSignPackage($link);
}
$info['title'] = strip_tags($info['title']);
$info['pics'] = array_filter(explode('|', $info['pics']));
... ... @@ -265,4 +263,13 @@ EOT;
}
return $this->returnJson(true, 200, $list);
}
public function getwechatsignpackageAction()
{
$signPackage = Lib_Wechat_JSSDK::getSignPackage();
$callback = $this->_request->query('callback', '');
$json = json_encode($signPackage);
header('Content-Type: application/javascript;');
return $callback . '(' . $json . ');';
}
}
... ...
... ... @@ -8,9 +8,10 @@ class Lib_Wechat_JSSDK {
$jsapiTicket = self::getJsApiTicket();
// 注意 URL 一定要动态获取,不能 hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
// $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
//$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
// 确保你获取用来签名的url是动态获取的,动态页面可参见实例代码中php的实现方式。如果是html的静态页面在前端通过ajax将url传到后台签名,前端需要用js获取当前页面除去'#'hash部分的链接(可用location.href.split('#')[0]获取),因为页面一旦分享,微信客户端会在你的链接末尾加入其它参数,如果不是动态获取当前链接,将导致分享后的页面签名失败。
$url = isset($_REQUEST['pageurl'])? $_REQUEST['pageurl']:'';
$timestamp = time();
$nonceStr = self::createNonceStr();
... ...
... ... @@ -70,9 +70,7 @@
<div id="share" class="share" cover-url="<?php
$thumb = empty($this->view->info['client_thumb']) ? $this->view->info['thumb'] : $this->view->info['client_thumb'];
echo Lib_Images::getImageUrl($thumb, 'source','fragmentimg');?>"
data-link="<?php echo SITE_MAIN.'/news/detail/id/'.$this->view->info['id'];?>" wechatAppId="<?php echo $this->view->wechat['appId'];?>"
wechatTimestamp="<?php echo $this->view->wechat['timestamp'];?>" wechatNonceStr="<?php echo $this->view->wechat['nonceStr'];?>"
wechatSignature="<?php echo $this->view->wechat['signature'];?>">
data-link="<?php echo SITE_MAIN.'/news/detail/id/'.$this->view->info['id'];?>">
<ul class="clearfix">
<li class="yohoo-share-button-sina"><a href="javascript:;"></a></li>
<li class="yohoo-share-button-qq"><a href="javascript:;"></a></li>
... ... @@ -82,43 +80,85 @@
<?php $this->_endblock();?>
<?php $this->_block('js');?>
<script type="text/javascript">
var shareData = {
//初始化config信息
var _weChatInterface = '<?php echo SITE_MAIN;?>/news/getwechatsignpackage';//签名等相关配置,yoho公众号
$.getJSON(_weChatInterface + "?pageurl=" + encodeURIComponent(location.href.split('#')[0]) + "&callback=?", function (json) {
if (json !== undefined && json !== '') {
var _appId = json.appId.toString();
var _timestamp = json.timestamp;
var _nonceStr = json.nonceStr.toString();
var _signature = json.signature.toString();
wx.config({
debug: true,
appId: _appId,
timestamp: _timestamp,
nonceStr: _nonceStr,
signature: _signature,
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo',
'hideMenuItems',
'showMenuItems',
'hideAllNonBaseMenuItem',
'showAllNonBaseMenuItem',
'translateVoice',
'startRecord',
'stopRecord',
'onRecordEnd',
'playVoice',
'pauseVoice',
'stopVoice',
'uploadVoice',
'downloadVoice',
'chooseImage',
'previewImage',
'uploadImage',
'downloadImage',
'getNetworkType',
'hideOptionMenu',
'showOptionMenu',
'closeWindow',
'scanQRCode',
'chooseWXPay',
'openProductSpecificView',
'addCard',
'chooseCard',
'openCard'
]
});
}
else {
}
});
/*
* 微信分享
*/
wx.ready(function () {
//构造分享信息
var shareData = {
title: $('.detail-title').find('.title'),
desc: '',
imgUrl: $(".share").attr("cover-url"),
link: $(".share").attr("data-link")
};
var wechatAppId = $(".share").attr("wechatAppId"),
wechatTimestamp = $(".share").attr("wechatTimestamp"),
wechatNonceStr = $(".share").attr("wechatNonceStr"),
wechatSignature = $(".share").attr("wechatSignature");
wx.config({
debug: true,
appId: wechatAppId,
timestamp: wechatTimestamp,
nonceStr: wechatNonceStr,
signature: wechatSignature,
jsApiList: ['checkJsApi','onMenuShareTimeline','onMenuShareAppMessage','onMenuShareQQ',
'onMenuShareWeibo','hideMenuItems','showMenuItems','hideAllNonBaseMenuItem','showAllNonBaseMenuItem',
'translateVoice','startRecord','stopRecord','onRecordEnd','playVoice','pauseVoice','stopVoice',
'uploadVoice','downloadVoice','chooseImage','previewImage','uploadImage','downloadImage',
'getNetworkType','openLocation','getLocation','hideOptionMenu','showOptionMenu','closeWindow',
'scanQRCode','chooseWXPay','openProductSpecificView','addCard','chooseCard','openCard'
]
});
wx.ready(function () {
// 2.1 “分享给朋友”
link: $(".share").attr("data-link"),
};
// 2.1 “分享给朋友”
wx.onMenuShareAppMessage(shareData);
// 2.2 “分享到朋友圈”
wx.onMenuShareTimeline(shareData);
// 2.3 “分享到QQ”
wx.onMenuShareQQ(shareData);
// 2.4 “分享到微博”
wx.onMenuShareWeibo(shareData);
wx.onMenuShareWeibo(shareData);
});
wx.error(function (res) {
alert(res.errMsg);
});
alert(res.errMsg);
});
seajs.use(['jquery', 'mobile'],function($, mobile) {
mobile.detail();
});
... ...