JavaScript
<script type="text/javascript"src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript">
window.onload = function(){
var url = location.href.split('#')[0]
console.log(location.href.split('#')[0])
? ? ? ? $.get("https://"+window.location.host+"/index/index/shareparams?url="+encodeURIComponent(url), {}, function(res){
console.log(res)
? ? ? ? ? wx.config({
? ? ? ? ? ? ? ? debug: false,
? ? ? ? ? ? ? ? appId: res.appId,
? ? ? ? ? ? ? ? timestamp: parseInt(res.timestamp),
? ? ? ? ? ? ? ? nonceStr: res.nonceStr,
? ? ? ? ? ? ? ? signature: res.signature,
? ? ? ? ? ? ? ? jsApiList: [
? ? ? ? ? ? ? ? ? ? 'onMenuShareTimeline',
? ? ? ? ? ? ? ? ? ? 'onMenuShareAppMessage'
? ? ? ? ? ? ? ? ]
? ? ? ? ? ? });
? ? ? }, 'json');?
$.get("https://"+window.location.host+"/index/index/index2?id="+document.getElementById('id').value, {}, function(wxData){
console.log(wxData)
? ? ? ? ? wx.ready(function () {
? wx.onMenuShareTimeline({
? title: ""+wxData.name+"在法信平臺被懸賞"+wxData.reward_clue+"元",
// ? link: wxData.link,
? imgUrl: wxData.picture,
? success: function () {
//分享成功后的回調(diào)
? }
? });
? wx.onMenuShareAppMessage({
? title: ""+wxData.name+"在法信平臺被懸賞"+wxData.reward_clue+"元",
? desc: ""+wxData.name+"在法信懸賞執(zhí)行平臺被懸賞"+wxData.reward_clue+"元,億元賞金等您來拿赁温,點(diǎn)擊了解更多",
// ? link: wxData.link,
? imgUrl: wxData.picture,
? type: 'link',
? dataUrl: '',
? success: function () {
//分享成功后的回調(diào)
? }
? });
? wx.error(function(res){
console.log(res)
// config信息驗證失敗會執(zhí)行error函數(shù),如簽名過期導(dǎo)致驗證失敗,具體錯誤信息可以打開config的debug模式查看丽猬,也可以在返回的res參數(shù)中查看杆煞,對于SPA可以在這里更新簽名寂玲。
? });
? ? ? });
? ? ? }, 'json');?
? ? }
</script>
PHP
public function shareparams(){
? ? ? ? $url = input('param.url');
? ? ? ? $jsapiTicket = $this->getJsApiTicket();
? ? ? ? // 注意 URL 一定要動態(tài)獲取,不能 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();
? ? ? ? // 這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序
? ? ? ? $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";
? ? ? ? $signature = sha1($string);
? ? ? ? $signPackage = array(
? ? ? ? ? ? "appId"? ? => 'wxe0808637343365ec',
? ? ? ? ? ? "nonceStr"? => $nonceStr,
? ? ? ? ? ? "timestamp" => $timestamp,
? ? ? ? ? ? "url"? ? ? => $url,
? ? ? ? ? ? "signature" => $signature,
? ? ? ? ? ? "rawString" => $string
? ? ? ? );
//? ? ? ? $this->assign('data', $signPackage);
? ? ? ? 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() {
? ? ? ? // jsapi_ticket 應(yīng)該全局存儲與更新染苛,以下代碼以寫入到文件中做示例
//? ? ? ? mkdir("jsapi_ticket.json", 0777);
//? ? ? ? chmod($_SERVER['DOCUMENT_ROOT']."/jsapi_ticket.json", 0755);
? ? ? ? $data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT'].'/jsapi_ticket.json'));
//? ? ? ? $data = json_decode(file_get_contents("jsapi_ticket.json"));
? ? ? ? if ($data->expire_time < time()) {
? ? ? ? ? ? $accessToken = $this->getAccessToken();
? ? ? ? ? ? // 如果是企業(yè)號用以下 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;
? ? ? ? ? ? ? ? $fp = fopen($_SERVER['DOCUMENT_ROOT']."/jsapi_ticket.json", "w");
? ? ? ? ? ? ? ? fwrite($fp, json_encode($data));
? ? ? ? ? ? ? ? fclose($fp);
? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? $ticket = $data->jsapi_ticket;
? ? ? ? }
? ? ? ? return $ticket;
? ? }
? ? private function getAccessToken() {
//? ? ? ? chmod($_SERVER['DOCUMENT_ROOT']."/access_token.json", 0755);
// access_token 應(yīng)該全局存儲與更新,以下代碼以寫入到文件中做示例
? ? ? ? $data = json_decode(file_get_contents($_SERVER['DOCUMENT_ROOT']."/access_token.json"));
? ? ? ? if ($data->expire_time < time()) {
? ? ? ? ? ? // 如果是企業(yè)號用以下URL獲取access_token
// $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
? ? ? ? ? ? $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxe0808637343365ec&secret=abab6d35a824e14ab9f8b38bd11c09f3";
? ? ? ? ? ? $res = json_decode($this->httpGet($url));
? ? ? ? ? ? $access_token = $res->access_token;
? ? ? ? ? ? if ($access_token) {
? ? ? ? ? ? ? ? $data->expire_time = time() + 7000;
? ? ? ? ? ? ? ? $data->access_token = $access_token;
? ? ? ? ? ? ? ? $fp = fopen($_SERVER['DOCUMENT_ROOT']."/access_token.json", "w");
? ? ? ? ? ? ? ? fwrite($fp, json_encode($data));
? ? ? ? ? ? ? ? fclose($fp);
? ? ? ? ? ? }
? ? ? ? } else {
? ? ? ? ? ? $access_token = $data->access_token;
? ? ? ? }
? ? ? ? return $access_token;
? ? }
? ? private function httpGet($url) {
? ? ? ? $curl = curl_init();
? ? ? ? curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
? ? ? ? curl_setopt($curl, CURLOPT_TIMEOUT, 500);
? ? ? ? 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;
? ? }