h5⽹页⾃定义分享(标题、描述、图标)⼀、绑定域名
先登录公众平台进⼊“设置”的“功能设置”⾥填写“JS接⼝安全域名”。
注:登录后可在“开发者中⼼”查看对应的接⼝权限。
⼆、引⼊JS⽂件
三、引⼊封装好的⽅法
require_once "jssdk.php";
$jssdk = new JSSDK("appid", "app密钥");
$signPackage = $jssdk->GetSignPackage();
四、jssdk.php
<?php
class JSSDK {
private $appId;
private $appSecret;
public function __construct($appId, $appSecret) {
$this->appId = $appId;
$this->appSecret = $appSecret;
}
public function getSignPackage() {
$jsapiTicket = $this->getJsApiTicket();
// 注意 URL ⼀定要动态获取,不能 hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "" : "";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();
// 这⾥参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$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($this->get_php_file("jsapi_ticket.php"));
if ($data->expire_time < time()) {
$accessToken = $this->getAccessToken();
// 如果是企业号⽤以下 URL 获取 ticket
// $url = "qyapi.weixin.qq/cgi-bin/get_jsapi_ticket?access_token=$accessToken";
$url = "api.weixin.qq/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;
$this->set_php_file("jsapi_ticket.php", json_encode($data));
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}
private function getAccessToken() {
/
/ access_token 应该全局存储与更新,以下代码以写⼊到⽂件中做⽰例
$data = json_decode($this->get_php_file("access_token.php"));
if ($data->expire_time < time()) {
// 如果是企业号⽤以下URL获取access_token
// $url = "qyapi.weixin.qq/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
$url = "api.weixin.qq/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;
$this->set_php_file("access_token.php", json_encode($data));
}
} 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);
// 为保证第三⽅服务器与服务器之间数据传输的安全性,所有接⼝采⽤https⽅式调⽤,必须使⽤下⾯2⾏代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 curl.haxx.se/ca/cacert.pem 下载新的证书判别⽂件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
private function get_php_file($filename) {
return trim(substr(file_get_contents($filename), 15));
}
private function set_php_file($filename, $content) {
$fp = fopen($filename, "w");
fwrite($fp, "<?php exit();?>" . $content);
fclose($fp);
}
}
新建⼀个 jsapi_ticket.php 存储ticket数据
<?php exit();?>
{"jsapi_ticket":"","expire_time":0}
新建⼀个 access_tocken.php 存储access_tocken数据
<?php exit();?>
{"access_token":"","expire_time":0}
五、通过config接⼝注⼊权限验证配置
debug: false,
appId: '<?php echo $signPackage["appId"];?>',
timestamp: <?php echo $signPackage["timestamp"];?>,
nonceStr: '<?php echo $signPackage["nonceStr"];?>',
signature: '<?php echo $signPackage["signature"];?>',
jsApiList: [
// 所有要调⽤的 API 都要加到这个列表中
'updateAppMessageShareData',
'updateTimelineShareData'
]
});
六、config接⼝验证成功后执⾏ready
// config信息验证后会执⾏ready⽅法,所有接⼝调⽤都必须在config接⼝获得结果之后,config是⼀个客户端的异步操作,所以如果需要在页⾯加载时就调⽤相关接⼝,则须把相关接⼝放在ready函数中调⽤来确保正确执⾏。对于⽤户触发时才调⽤的});
以下对于⽤户触发时才调⽤的接⼝,则可以直接调⽤,不需要放在ready函数中。(⼀切按需求写就好)
// 在这⾥调⽤ API
let url = window.location.search;
//分享给朋友、分享到QQ
wx.updateAppMessageShareData({
title: '分享给朋友、分享到QQ分享的标题', // 分享标题
desc: '各种分享的描述各种分享的描述各种分享的描述各种分享的描述各种分享的描述', // 分享描述
link: 'service.solomore/quest/Assessment/pay_page/zhi_see.php'+url, // 分享链接,该链接域名或路径必须与当前页⾯对应的JS安全域名⼀致
imgUrl: 'service.solomore/quest/Assessment/img/touxiang.png', // 分享图标
success: function () {
/
/ 这⾥只是调⽤分享成功,但⽤户并没有进⾏分享
}
});
//分享到朋友圈、分享到QQ空间
wx.updateTimelineShareData({
title: '分享到朋友圈、分享到QQ空间分享的标题', // 分享标题
link: 'service.solomore/quest/Assessment/pay_page/zhi_see.php'+url, // 分享链接,该链接域名或路径必须与当前页⾯对应的JS安全域名⼀致
imgUrl: 'service.solomore/quest/Assessment/img/touxiang.png', // 分享图标
success: function () {
// 设置成功
}
});
});
七、config接⼝验证失败后执⾏error
<(function(res){
// config信息验证失败会执⾏error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这⾥更新签名。
});
⼋、更多接⼝调⽤