当前位置:网站首页>H5实现生成urlscheme并从H5跳转到小程序
H5实现生成urlscheme并从H5跳转到小程序
2022-07-15 11:45:00 【huaweichenai】
在2022年之前,想要在H5打开小程序只需要在平台上设置urlscheme即可,但是自 2022 年 4 月 11 日起,URL Scheme有效期最长 30 天,不再支持永久有效的URL Scheme、不再区分短期有效URL Scheme与长期有效URL Scheme。若在微信外打开,用户可以在浏览器页面点击进入小程序。每个独立的URL Scheme被用户访问后,仅此用户可以再次访问并打开对应小程序,其他用户无法再次通过相同URL Scheme打开该小程序。 在本次规则调整生效前已经生成的URL Scheme,如果有效期超过30天或长期会被降级为30天有效,只能被1个用户访问,开始时间从调整日期开始计算。【官方文档】
但是官方提供了动态获取urlscheme方法
步骤如下:
1:获取access_token
2:根据access_token获取urlscheme
3:根据urlscheme实现H5跳转小程序
具体实现
1. 通用的调用接口方法
/**
* curl
*/
public function httpRequest($url, $format = 'get', $data = null, $headerArray = []){
//设置头信息
$curl=curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if ($format == 'post') {
//post传值设置post传参
curl_setopt($curl, CURLOPT_POST, 1);
if ($data) {
$data = json_encode($data);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if ($headerArray) {
curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
}
$data=json_decode(curl_exec($curl), true);
curl_close($curl);
//返回接口返回数据
return $data;
}2. 获取access_token文档:【获取access_token】
$appId = 'XXX';
$appsecret = 'XXX';
$data = $this->httpRequest(
'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appId.'&secret='.$appsecret,
'get',
null,
array("Content-type:application/json;","Accept:application/json")
);
$accessToken = isset($data['access_token']) ? $data['access_token'] : '';3:根据access_token获取urlscheme[获取urlscheme]
$data = $this->httpRequest(
'https://api.weixin.qq.com/wxa/generatescheme?access_token='.$accessToken,
'post',
[
'jump_wxa' => [
'path' => "/pages/index/index",//跳转小程序地址
'query' => ""//跳转小程序额外参数
],
'expire_type' => 0
]
);
$openlink = isset($data['openlink']) ? $data['openlink'] : '';4:根据urlscheme跳转小程序
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
<script>
window.location.href = openlink;
</script>边栏推荐
- 2022-04-20 Unity入门7——物理系统之碰撞检测
- One of MySQL artifact locks
- 2022-04-21 unity entry 8 - sound system
- Valuing space over safety, Highlander and link 09, who do you choose
- Bc20 at command test
- Cache avalanche, cache breakdown, cache penetration
- 相关小知识点
- 吴恩达撰文:发展 AI 职业生涯的重要步骤——项目实践
- Ignore, on duplicate key update, delayed in MySQL
- CCF 202012-1 期末预测之安全指数
猜你喜欢

谷歌提出可靠性机器学习模型Plex:让大模型对分布外数据提出合适的预测结果

Google proposes the reliability machine learning model plex: let the large model put forward appropriate prediction results for out of distribution data

送你的代码上太空,与华为云一起开发'最伟大的作品'

1、OLED简单驱动

2022-04-18 Unity入门1——窗口布局

The No.1 port and shipping AI enterprise in the world integrates Feitong, port and shipping AI products are mature, standardized and applied on a large scale, and advanced core technologies greatly im

活动预告|Apache Doris x Apache SeaTunnel 联合 Meetup 开启报名!

What is kotlin sealed? Why does Google use it all

08 use timer to control LED flashing

2022-04-20 Unity入门6——光源组件
随机推荐
Use iceberg in CDP to pressurize the data Lake warehouse
Fragment(四)常见问题
ESP8266 lua入门(二)
07 printf重定向
To realize the remote direct electromagnetic communication of consciousness, the Dongda team, together with the National University of Singapore and others, is expected to build an electromagnetic bra
10分钟带你进入Swagger的世界,快来看一看吧
Google recommends using sealed and remotemediator in projects
Getting started with esp8266 Lua
二分查找(下)
Landing DDD (7) - some misunderstandings in tactical design
2022-04-18 Unity入门3——脚本基础
Efficient development of harmonyos course applications based on ETS
Figure 1: four directions and three strategies for digital transformation of state-owned enterprises
558. Intersection of quadtrees: simple recursive application problem
Difference between idea smart checkout and force checkout
2022-04-20 unity introduction 5 - Camera
pygame游戏——飞机大战(待完善)
No.1 port and shipping AI enterprises in the world have integrated fly pupil, and the mature and standardized AI products of port and shipping have been applied on a large scale, which has greatly imp
The No.1 port and shipping AI enterprise in the world integrates Feitong, port and shipping AI products are mature, standardized and applied on a large scale, and advanced core technologies greatly im
Sequence table