限制条件:
-
目前仅支持在微信内打开H5页面;
-
已认证的服务号,服务号绑定“JS接口安全域名”下的网页可使用此标签跳转任意合法合规的小程序;
-
已认证的非个人主体的小程序,使用小程序云开发的静态网页托管绑定的域名下的网页,可以使用此标签跳转任意合法合规的小程序。
01
绑定安全域名
登录微信公众平台进入【公众号设置】-->【功能设置】里设置好 “js接口安全域名”。
02
IP白名单设置
登录微信公众平台进入【开发】-->【基本配置】,这里填写你服务器的IP
03
完整代码
后面直接伸手就给,已经给大家准备了完整的DEMO,填一下自己的信息就可以了。公众号回复『h5跳转小程序』,即可获取完整代码。遇到其他问题可以留言,看到了第一时间会回复大家。
<!--=========================================================公众号:被动睡后收入==========================================================-->
<!--==========================================================PHP --start==========================================================-->
<?php
// 微信 JS 接口签名校验工具: https://mp.weixin.qq/debug/cgi-bin/sandbox?t=jsapisign
$appid = 'AppID'; //填入服务号AppID
$secret = 'AppSecret'; //填入服务号AppSecret
// 获取token
$token_data = file_get_contents('./wechat_token.txt');
if (!empty($token_data)) {
$token_data = json_decode($token_data, true);
}
$time = time() - $token_data['time'];
if ($time > 3600) {
$token_url = "https://api.weixin.qq/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
$token_res = https_request($token_url);
$token_res = json_decode($token_res, true);
$token = $token_res['access_token'];
$data = array(
'time' => time(),
'token' => $token
);
$res = file_put_contents('./wechat_token.txt', json_encode($data));
if ($res) {
echo '更新 token 成功';
}
} else {
$token = $token_data['token'];
}
// 获取ticket
$ticket_data = file_get_contents('./wechat_ticket.txt');
if (!empty($ticket_data)) {
$ticket_data = json_decode($ticket_data, true);
}
$time = time() - $ticket_data['time'];
if ($time > 3600) {
$ticket_url = "https://api.weixin.qq/cgi-bin/ticket/getticket?access_token={$token}&type=jsapi";
$ticket_res = https_request($ticket_url);
$ticket_res = json_decode($ticket_res, true);
$ticket = $ticket_res['ticket'];
$data = array(
'time' => time(),
'ticket' => $ticket
);
$res = file_put_contents('./wechat_ticket.txt', json_encode($data));
if ($res) {
echo '更新 ticket 成功';
}
} else {
$ticket = $ticket_data['ticket'];
}
// 进行sha1签名
$timestamp = time();
$nonceStr = createNonceStr();
// 注意 URL 建议动态获取(也可以写死).
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // 调用JSSDK的页面地址
// $url = $_SERVER['HTTP_REFERER']; // 前后端分离的, 获取请求地址(此值不准确时可以通过其他方式解决)
$str = "jsapi_ticket={$ticket}&noncestr={$nonceStr}×tamp={$timestamp}&url={$url}";
$sha_str = sha1($str);
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;
}
/**
* 模拟 http 请求
* @param String $url 请求网址
* @param Array $data 数据
*/
function https_request($url, $data = null)
{
// curl 初始化
$curl = curl_init();
// curl 设置
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
// 判断 $data get or post
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
// 执行
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
?>
<!--==========================================================PHP --end==========================================================-->
<!--==========================================================HTML --start==========================================================-->
<html>
<head>
<title>被动睡后收入-->微信公众号</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="width=device-width,initial-scale=0.5">
</head>
<body>
<div id="app">
<wx-open-launch-weapp
id="launch-btn"
username="gh_xxx" <!-- 需要跳转的小程序的原生id gh_开头 -->
path="pages/index/index.html?uid=16" <!-- 需要跳转的小程序的路径以及参数 -->
>
<template>
<style>
.btn {
padding: 12px;
width: 10rem;
height: 10rem;
}
</style>
<div style="text-align: center;align-items: center;margin: 8rem">
<button class="btn">打开小程序</button>
</div>
</template>
</wx-open-launch-weapp>
<script>
var btn = document.getElementById('launch-btn');
btn.addEventListener('launch', function (e) {
console.log('success');
});
btn.addEventListener('error', function (e) {
console.log('fail', e.detail);
});
</script>
</div>
<script type="text/javascript" src="https://res2.wx.qq/open/js/jweixin-1.6.0.js"></script>
<script type="text/javascript">
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: '<? echo $appid ?>', // 必填,公众号的唯一标识
timestamp: <? echo $timestamp ?>, // 必填,生成签名的时间戳
nonceStr: '<? echo $nonceStr ?>', // 必填,生成签名的随机串
signature: '<? echo $sha_str ?>',// 必填,签名
jsApiList: ['onMenuShareTimeline', 'scanQRCode'],// 必填,需要使用的JS接口列表,
openTagList: ['wx-open-launch-weapp']
});
wx.ready(function () {
console.log('接口配置成功');
});
</script>
</body>
</html>
<!--==========================================================HTML --end==========================================================-->
查看APPID、原始ID、页面路径请详见文末更多推荐
更多推荐
H5网页跳转打开微信小程序详解(含完整代码)
发布评论