网站建设 类,企业网站的制作方式,天津市城乡建设局网站,学院网站建设流程参考链接
需求#xff1a;从外部浏览器#xff0c;点击H5链接跳转打开微信小程序#xff1b;以及在微信内直接点击H5链接打开微信小程序#xff1b;
步骤1#xff1a; 小程序开发需要使用云开发创建项目#xff0c;使用云开发生成的项目会自带云函数文件夹#xff1b;…参考链接
需求从外部浏览器点击H5链接跳转打开微信小程序以及在微信内直接点击H5链接打开微信小程序
步骤1 小程序开发需要使用云开发创建项目使用云开发生成的项目会自带云函数文件夹 步骤2 项目开启云开发 步骤3 下载官方的H5静态html和public文件进行配置下载地址点击下载HTML 即可下载全部文件下载后文件夹有两个位置需要修改
下载的文件夹
步骤3.1更改静态的html替换6项云开发字段直接搜索replace 将对象位置的信息改成你的云开发配置 步骤3.2更改public文件跳转打开路径
上面静态html和public的index修改文件我放在了末尾可以详细看下
步骤4更改好上述两个文件后回到微信开发者工具将刚才修改好的public文件添加到cloudfunctions文件夹下只需要public下的文件静态文件html不用放进来 步骤5public文件夹右键上传并部署如报云端安装依赖和没有上传并部署问题看下面截图 问题5.1如云函数右键没有上传可能是没有连接上云函数环境。可以修改project.config.json文件修改对应的目录cloudfunctionRoot为云开发的目录
miniprogramRoot: miniprogram/,
cloudfunctionRoot: cloudfunctions/,**问题5.2**上传并部署-报问题缺少依赖安装依赖即可
npm install --production步骤6开启两个权限 开启6.1开启 未登录用户访问云资源权限设置 开启6.2云函数的权限设置开启 允许所有用户访问 步骤7上传之前修改好的云环境参数的静态H5文件jump-mp.html 上传成功后点击详情复制链接在微信和浏览器地址栏都可以打开小程序了 8.此时操作完按理来说就可以通过链接打开微信小程序了但实际操作中发现在微信端可以打开但是外部浏览器输入链接后没有跳转微信打开没反应了直接如下图
**问题8.1**查看jump-mp.html代码将原方法注释使用try catch方法 alertres看看到底什么原因导致再具体修改即可。 代码 async function openWeapp(onBeforeJump) {var c window.cconst res await c.callFunction({name: public,data: {action: getUrlScheme,},})console.warn(res)if (onBeforeJump) {onBeforeJump()}location.href res.result.openlink}/* async function openWeapp(onBeforeJump) {alert(静态)var c window.ctry {const res await c.callFunction({name: public,data: {action: getUrlScheme,},})alert(res)alert(res.result.openlink)console.warn(res)//跳转地址alert(res.result.openlink)if (onBeforeJump) {onBeforeJump()}alert(res.result.openlink)location.href res.result.openlink} catch (error) {alert(error)}} */我当时问题报的Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID , cloud function service error code -501023, error message Unauthenticated access is denied (callid 1626072949050-0.6283798683396115); at cloud.callFunction api;
步骤9打开链接成功跳转到小程序 微信端打开链接
外部浏览器打开链接
步骤10.部分代码
10.1生成静态H5连接的文件jump-mp.html
htmlheadtitle打开小程序/titlemeta http-equivContent-Type contenttext/html; charsetutf-8meta nameviewport contentwidthdevice-width, user-scalableno, initial-scale1, maximum-scale1scriptwindow.onerror e {console.error(e)alert(发生错误 e)}/script!-- weui 样式 --link relstylesheet hrefhttps://res.wx.qq.com/open/libs/weui/2.4.1/weui.min.css/link!-- 调试用的移动端 console --!-- script srchttps://cdn.jsdelivr.net/npm/eruda/script --!-- scripteruda.init();/script --!-- 公众号 JSSDK --script srchttps://res.wx.qq.com/open/js/jweixin-1.6.0.js/script!-- 云开发 Web SDK --script srchttps://res.wx.qq.com/open/js/cloudbase/1.1.0/cloud.js/scriptscriptfunction docReady(fn) {if (document.readyState complete || document.readyState interactive) {fn()} else {document.addEventListener(DOMContentLoaded, fn);}}docReady(async function () {var ua navigator.userAgent.toLowerCase()var isWXWork ua.match(/wxwork/i) wxworkvar isWeixin !isWXWork ua.match(/micromessenger/i) micromessengervar isMobile falsevar isDesktop falseif (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|IEMobile)/i)) {isMobile true} else {isDesktop true}if (isWeixin) {var containerEl document.getElementById(wechat-web-container)containerEl.classList.remove(hidden)containerEl.classList.add(full, wechat-web-container)var launchBtn document.getElementById(launch-btn)launchBtn.addEventListener(ready, function (e) {console.log(开放标签 ready)})launchBtn.addEventListener(launch, function (e) {console.log(开放标签 success)})launchBtn.addEventListener(error, function (e) {console.log(开放标签 fail, e.detail)})wx.config({// debug: true, // 调试时可开启appId: wxf6be280b0bb, // !-- replace --timestamp: 0, // 必填填任意数字即可nonceStr: nonceStr, // 必填填任意非空字符串即可signature: signature, // 必填填任意非空字符串即可jsApiList: [chooseImage], // 必填随意一个接口即可 openTagList: [wx-open-launch-weapp], // 填入打开小程序的开放标签名})} else if (isDesktop) {// 在 pc 上则给提示引导到手机端打开var containerEl document.getElementById(desktop-web-container)containerEl.classList.remove(hidden)containerEl.classList.add(full, desktop-web-container)} else {var containerEl document.getElementById(public-web-container)containerEl.classList.remove(hidden)containerEl.classList.add(full, public-web-container)var c new cloud.Cloud({// 必填表示是未登录模式identityless: true,// 资源方 AppIDresourceAppid: wxf6be280b0bb, // !-- replace --// 资源方环境 IDresourceEnv: zuihuibaomarket-1gerisnfx, // !-- replace --})await c.init()window.c cvar buttonEl document.getElementById(public-web-jump-button)var buttonLoadingEl document.getElementById(public-web-jump-button-loading)try {await openWeapp(() {buttonEl.classList.remove(weui-btn_loading)buttonLoadingEl.classList.add(hidden)})} catch (e) {buttonEl.classList.remove(weui-btn_loading)buttonLoadingEl.classList.add(hidden)throw e}}})// 如果在外部浏览器链接中打开不跳转微信小程序可能是返回的res有问题可以把这个方法注释掉 把下方的注释方法打开根据具体的报错进行修改async function openWeapp(onBeforeJump) {var c window.cconst res await c.callFunction({name: public,data: {action: getUrlScheme,},})console.warn(res)if (onBeforeJump) {onBeforeJump()}location.href res.result.openlink}/* async function openWeapp(onBeforeJump) {alert(静态)var c window.ctry {const res await c.callFunction({name: public,data: {action: getUrlScheme,},})alert(res)alert(res.result.openlink)console.warn(res)//跳转地址alert(res.result.openlink)if (onBeforeJump) {onBeforeJump()}alert(res.result.openlink)location.href res.result.openlink} catch (error) {alert(error)}} *//scriptstyle.hidden {display: none;}.full {position: absolute;top: 0;bottom: 0;left: 0;right: 0;}.public-web-container {display: flex;flex-direction: column;align-items: center;}.public-web-container p {position: absolute;top: 40%;}.public-web-container a {position: absolute;bottom: 40%;}.wechat-web-container {display: flex;flex-direction: column;align-items: center;}.wechat-web-container p {position: absolute;top: 40%;}.wechat-web-container wx-open-launch-weapp {position: absolute;bottom: 40%;left: 0;right: 0;display: flex;flex-direction: column;align-items: center;}.desktop-web-container {display: flex;flex-direction: column;align-items: center;}.desktop-web-container p {position: absolute;top: 40%;}/style
/headbodydiv classpage fulldiv idpublic-web-container classhiddenp class正在打开 “惠多保”.../p !-- replace --a idpublic-web-jump-button hrefjavascript: classweui-btn weui-btn_primary weui-btn_loadingonclickopenWeapp()span idpublic-web-jump-button-loading classweui-primary-loading weui-primary-loading_transparenticlassweui-primary-loading__dot/i/span打开小程序/a/divdiv idwechat-web-container classhiddenp class点击以下按钮打开 “惠多保”/p !-- replace --!-- 跳转小程序的开放标签。文档 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html --wx-open-launch-weapp idlaunch-btn usernamegh_bafb9bdb0517 pathpages/index/index!-- replace --templatebuttonstylewidth: 200px; height: 45px; text-align: center; font-size: 17px; display: block; margin: 0 auto; padding: 8px 24px; border: none; border-radius: 4px; background-color: #07c160; color:#fff;打开小程序/button/template/wx-open-launch-weapp/divdiv iddesktop-web-container classhiddenp class请在手机打开网页链接/p/div/div
/body/html10.2:public文件夹下的index.js云函数
// 云函数入口文件
const cloud require(wx-server-sdk)cloud.init()// 云函数入口函数
exports.main async (event, context) {const wxContext cloud.getWXContext()switch (event.action) {case getUrlScheme: {return getUrlScheme()}}return action not found
}async function getUrlScheme() {return cloud.openapi.urlscheme.generate({jumpWxa: {// path: /page/component/index, // !-- replace --// 外部浏览器打开的小程序页面地址path: /pages/index/index, // !-- replace --query: ,},// 如果想不过期则置为 false并可以存到数据库isExpire: false,// 一分钟有效期expireTime: parseInt(Date.now() / 1000 60),})
}
10.3public下的另外两个文件config.json和package.json无需修改