青草久久影院-青草久久伊人-青草久久久-青草久久精品亚洲综合专区-SM双性精跪趴灌憋尿调教H-SM脚奴调教丨踩踏贱奴

17站長網

17站長網 首頁 CMS dedecms 查看內容

所有網站都能用到的微信自定義分享代碼

2023-6-17 17:09| 查看: 2734 |來源: 互聯網

實現方法: 在網站根目錄下新建js目錄,放入以下2個文件js/jssdk.phpjs/share.js jssdk.php 代碼如下?php$url = $_GET;//2017-06-27class JSSDK {private $appI ...

實現方法:

在網站根目錄下新建js目錄,放入以下2個文件

js/jssdk.php

js/share.js

jssdk.php 代碼如下

<?php

$url = $_GET[‘url‘];//2017-06-27

class JSSDK {

private $appId;

private $appSecret;

private $url;//2017-06-27

public function __construct($appId, $appSecret,$url) {

$this->appId = $appId;

$this->appSecret = $appSecret;

$this->url = $url;//2017-06-27

}

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]";//2017-06-27

$url =$this->url;//2017-06-27

$timestamp = time();

$nonceStr = $this->createNonceStr();

// 這里參數的順序要按照 key 值 ASCII 碼升序排序

$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr&timestamp=$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() {

// jsapi_ticket 應該全局存儲與更新,以下代碼以寫入到文件中做示例

$data = json_decode(file_get_contents("jsapi_ticket.json"));

if ($data->expire_time < time()) {

$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;

$fp = fopen("jsapi_ticket.json", "w");

fwrite($fp, json_encode($data));

fclose($fp);

}

} else {

$ticket = $data->jsapi_ticket;

}

return $ticket;

}

private function getAccessToken() {

// access_token 應該全局存儲與更新,以下代碼以寫入到文件中做示例

$data = json_decode(file_get_contents("access_token.json"));

if ($data->expire_time < time()) {

// 如果是企業號用以下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=$this->appId&secret=$this->appSecret";

$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("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, true);

curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true); //https

curl_setopt($curl, CURLOPT_URL, $url);

$res = curl_exec($curl);

curl_close($curl);

return $res;

}

}

$jssdk = new JSSDK("AAPID", "appSecret",$url);

$signPackage = $jssdk->GetSignPackage();

$tmp=json_encode(array (‘appId‘=>$signPackage["appId"],‘timestamp‘=>$signPackage["timestamp"],

‘nonceStr‘=>$signPackage["nonceStr"],‘signature‘=>$signPackage["signature"],‘url‘=>$signPackage["url"]));

$callback = $_GET[‘callback‘];

echo $callback.‘(‘.$tmp.‘)‘;

exit;

?>

share.js代碼如下

url = location.href; $.ajax({

type: "get",

url: "../js/jssdk.php?url=" + url,

dataType: "jsonp",

jsonp: "callback",

jsonpCallback: "success_jsonpCallback",

success: function(data) {

wx.config({

appId: data.appId,

timestamp: data.timestamp,

nonceStr: data.nonceStr,

signature: data.signature,

jsApiList: [‘chackJsApi‘, ‘onMenuShareTimeline‘, ‘onMenuShareAppMessage‘,

‘onMenuShareQQ‘, ‘onMenuShareWeibo‘, ‘onMenuShareQZone‘]

});

},

error: function(data) {

alert("連接失敗!");

} }); wx.ready(function() {

var shareData = {

title: title,

desc: desc,

link: url,

imgUrl: img,

success: function(res) {

//分享成功

},

cancel: function(res) {

alert(‘已取消分享‘);

}

};

wx.onMenuShareAppMessage(shareData);

wx.onMenuShareTimeline(shareData);

wx.onMenuShareQQ(shareData);

wx.onMenuShareWeibo(shareData);

wx.onMenuShareQZone(shareData); });

部署方式 頁面底部添加如下代碼

<script src="https://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script> <script src="https://你的域名/js/share.js"></script> <script> var url = ‘你的域名‘;//用戶要分享的網址 var title = ‘您的標題‘;//分享的標題 var img = ‘您的圖片地址‘;//分享的圖片 var desc = ‘您的描述信息‘;//分享的描述信息 </script>

這樣就可以了。

本文最后更新于 2023-6-17 17:09,某些文章具有時效性,若有錯誤或已失效,請在網站留言或聯系站長:17tui@17tui.com
·END·
站長網微信號:w17tui,關注站長、創業、關注互聯網人 - 互聯網創業者營銷服務中心

免責聲明:本站部分文章和圖片均來自用戶投稿和網絡收集,旨在傳播知識,文章和圖片版權歸原作者及原出處所有,僅供學習與參考,請勿用于商業用途,如果損害了您的權利,請聯系我們及時修正或刪除。謝謝!

17站長網微信二維碼

始終以前瞻性的眼光聚焦站長、創業、互聯網等領域,為您提供最新最全的互聯網資訊,幫助站長轉型升級,為互聯網創業者提供更加優質的創業信息和品牌營銷服務,與站長一起進步!讓互聯網創業者不再孤獨!

掃一掃,關注站長網微信

大家都在看

    熱門排行

      最近更新

        返回頂部
        主站蜘蛛池模板: 久久久午夜精品福利内容 | 甜宠溺H宝贝嗯撞PLAY啊 | 国产精品视频一区二区猎奇 | 国产精品久久久久AV麻豆 | 嗯 用力啊 嗯 c我 啊哈老师 | 麻豆一二三区果冻 | 久久精品亚洲 | 波多野结衣的AV一区二区三区 | 18禁黄无遮挡禁游戏在线下载 | 成 人 动漫3d 在线看 | 欧美高清xxx | GOGOGO高清免费播放 | 影音先锋av男人资源 | 午夜福到在线4国产 | 在线精彩视频在线观看免费 | 91九色网址 | 诱人的秘书BD在线观看 | 月夜直播免费观看全集 | 性色AV一区二区三区V视界影院 | 思思99热久久精品在线6 | 欧美男女爱爱 | 不知火舞vs精子 | 女人高潮久久久叫人喷水 | 伊人热人久久中文字幕 | 亚洲精品久久久一区 | 日本精品卡一卡2卡3卡四卡三卡 | 色婷婷激婷婷深爱五月小蛇 | 老熟风间由美AV在线一区二区 | 狠狠插狠狠干 | 2018高清国产一区二区三区 | 亚洲 欧美 另类 中文 在线 | 诱人的秘书BD在线观看 | 免费三级现频在线观看 | 麻豆蜜桃国语精品无码视频 | 色爱区综合小说 | 国产亚洲精品香蕉视频播放 | 五花大绑esebdsm国产 | 国产精品久久久久久久人热 | 免费看毛片网 | 国产精品欧美久久久久天天影视 | 无码爽死成人777在线观看网站 |