You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
3.3 KiB
84 lines
3.3 KiB
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>JS SDK</title>
|
|
<!-- 引入微信JS SDK -->
|
|
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
|
|
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
|
</head>
|
|
<body>
|
|
|
|
<?php
|
|
function get_jsapi_ticket() {
|
|
$access_token = get_access_token(); // 获取access_token
|
|
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=".$access_token;
|
|
$result = file_get_contents($url);
|
|
$obj = json_decode($result);
|
|
return $obj->ticket;
|
|
}
|
|
|
|
// 获取access_token
|
|
function get_access_token() {
|
|
$appid = 'wxebc3cb2a62b1eaf2';
|
|
$appsecret = 'd18b00b601c71a275a19e6a4e922a412';
|
|
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
|
|
$result = file_get_contents($url);
|
|
$obj = json_decode($result);
|
|
return $obj->access_token;
|
|
}
|
|
// 获取当前页面URL
|
|
function get_current_url() {
|
|
$protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ? 'https://' : 'http://';
|
|
$url = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
|
|
return $url;
|
|
}
|
|
|
|
// 生成签名
|
|
function get_signature($jsapi_ticket, $nonceStr, $timestamp, $url) {
|
|
$string = "jsapi_ticket=".$jsapi_ticket."&noncestr=".$nonceStr."×tamp=".$timestamp."&url=".$url;
|
|
return sha1($string);
|
|
}
|
|
// 设置JS-SDK配置参数
|
|
$jsapi_ticket = get_jsapi_ticket(); // 获取jsapi_ticket
|
|
$nonceStr = "sdafsrtyw8u"; // 生成随机字符串
|
|
$timestamp = time(); // 获取时间戳
|
|
$url = get_current_url(); // 获取当前页面URL
|
|
$signature = get_signature($jsapi_ticket, $nonceStr, $timestamp, $url); // 获取签名
|
|
$config = array(
|
|
'appId' => 'wxebc3cb2a62b1eaf2',
|
|
'timestamp' => $timestamp,
|
|
'nonceStr' => $nonceStr,
|
|
'signature' => $signature,
|
|
'jsApiList' => array('chooseWXPay', 'otherApiName'),
|
|
);
|
|
// 初始化JS-SDK
|
|
echo '<script type="text/javascript">';
|
|
echo 'wx.config('.json_encode($config).');';
|
|
echo '</script>';
|
|
|
|
?>
|
|
<script>
|
|
wx.login({
|
|
success: function(res) {
|
|
if (res.code) {
|
|
// 发送code到后端服务器获取openid
|
|
$.ajax({
|
|
url: 'test_get_openid.php',
|
|
data: {code: res.code},
|
|
success: function(data) {
|
|
// 获取用户openid并处理支付请求
|
|
var openid = data.openid;
|
|
console.log(openid);
|
|
// ...
|
|
},
|
|
error: function(jqXHR, textStatus, errorThrown) {
|
|
console.error('Error:', errorThrown);
|
|
// 处理错误
|
|
}
|
|
});
|
|
}
|
|
}
|
|
});
|
|
</script>
|
|
</body>
|
|
</html>
|