考生网_备考资料_考试真题_复习资料_备考资源免费下载
{php
// 1. 仅微信浏览器执行
$is_wechat = strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false;
if ($is_wechat) {
// 2. 公众号配置(写死)
$appid = 'wx79640aa54a902a07';
$appsecret = '6ee18463a4ee92d87b1997cef6ba701d';
// 3. 生成JSSDK签名(独立逻辑,避开Destoon冲突)
function getWxSign($url, $appid, $appsecret) {
// 3.1 获取access_token(缓存1小时)
$token_file = DT_ROOT.'/cache/wx_token.txt';
if (file_exists($token_file) && time() - filemtime($token_file) < 3600) {
$access_token = file_get_contents($token_file);
} else {
$token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=&secret=";
$token_res = file_get_contents($token_url);
$token_data = json_decode($token_res, true);
$access_token = $token_data['access_token'];
file_put_contents($token_file, $access_token);
}
// 3.2 获取jsapi_ticket(缓存1小时)
$ticket_file = DT_ROOT.'/cache/wx_ticket.txt';
if (file_exists($ticket_file) && time() - filemtime($ticket_file) < 3600) {
$jsapi_ticket = file_get_contents($ticket_file);
} else {
$ticket_url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=&type=jsapi";
$ticket_res = file_get_contents($ticket_url);
$ticket_data = json_decode($ticket_res, true);
$jsapi_ticket = $ticket_data['ticket'];
file_put_contents($ticket_file, $jsapi_ticket);
}
// 3.3 生成签名
$timestamp = time();
$noncestr = md5(rand(1000,9999).time());
$string = "jsapi_ticket=&noncestr=×tamp=&url=https://m.kaosheng.com/down/index-htm-page-246.html";
$signature = sha1($string);
return array(
'appId' => $appid,
'timestamp' => $timestamp,
'nonceStr' => $noncestr,
'signature' => $signature
);
}
// 4. 构造当前URL(去除锚点,确保签名正确)
$current_url = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$current_url = explode('#', $current_url)[0];
$signPackage = getWxSign($current_url, $appid, $appsecret);
// 5. 传递参数到前端
$share_title = isset($title) ? $title : '考生网专属内容';
$share_link = $current_url;
$share_icon = isset($thumb) ? $thumb : 'https://m.kaosheng.com/images/logo.png'; // 兜底logo
}
}