美文网首页
h5跳转小程序之静态网页

h5跳转小程序之静态网页

作者: 焚心123 | 来源:发表于2022-03-29 10:10 被阅读0次
  • 首先h5跳转小程序的方式有很多,这里说得不是小程序中嵌套h5页面,而是单纯的一个(外部的)h5页面跳转小程序,我的另一篇是通过生成URL Scheme进行跳转,这种是直接使用的网站生成的,参数是写死的,不能进行动态的传递参数,也有通过接口生成这种的,不过有时效性,就没有采用这种的方式
在这里先说下我的需求
  • 首先是通过点击链接跳转到小程序,还要是动态的传递参数,百度了下,没有很好地方法,这里就自己通过网页跳转网页,在通过网页跳转小程序的形式,不过这个是可行的,经过实验
首先使用微信官方提供的静态网站 H5 跳小程序
<html>
  <head>
    <title>打开小程序</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
    <script>
      // 获取传递的参数
      function getInfo(info){
        var regn = new RegExp("(^|&)" + info + "=([^&]*)(&|$)");
                var ri = window.location.search.substr(1).match(regn);
                if (ri != null) {
                    return decodeURI(ri[2])
                }
      }
      
      // window.onerror = e => {
      //   console.error(e)
      //   alert('发生错误' + e)
      // }
    </script>
    <!-- weui 样式 -->
    <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/2.4.1/weui.min.css"></link>
    <!-- 调试用的移动端 console -->
    <!-- <script src="https://cdn.jsdelivr.net/npm/eruda"></script> -->
    <!-- <script>eruda.init();</script> -->
    <!-- 公众号 JSSDK -->
    <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
    <!-- 云开发 Web SDK -->
    <script src="https://res.wx.qq.com/open/js/cloudbase/1.1.0/cloud.js"></script>
    <script>
      function 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) == 'wxwork'
        var isWeixin = !isWXWork && ua.match(/micromessenger/i) == 'micromessenger'
        var isMobile = false
        var isDesktop = false
        if (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',e)
          const card = getInfo('card');
          const ele = document.getElementById('launch-btn');
          ele.setAttribute('path','小程序的页面路径?card='+card)
          console.log('shuxing ',ele);
          })
          launchBtn.addEventListener('launch', function (e) {
            console.log('开放标签 success')
          })
          launchBtn.addEventListener('error', function (e) {
            console.log('开放标签 fail', e.detail)
          })

          wx.config({
            // debug: true, // 调试时可开启
            appId: '小程序的APPID', // <!-- 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,
            // 资源方 AppID
            resourceAppid: '小程序的APPID', // <!-- replace -->
            // 资源方环境 ID
            resourceEnv: '云开发的id', // <!-- replace -->
          })
          await c.init()
          window.c = c

          var 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
          }
        }
      })
   
      async function openWeapp(onBeforeJump) {
        var c = window.c
        const res = await c.callFunction({
          name: 'public',
          data: {
            action: 'getUrlScheme',
          },
        })
        console.warn(res)
        if (onBeforeJump) {
          onBeforeJump()
        }
         
     
        location.href = res.result.openlink
      }
    </script>
    <style>
      .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>
  </head>
  <body>

    <div class="page full">
      
      <div id="public-web-container" class="hidden">
       
        <p class="">正在打开 “和卡生活”...</p> <!-- replace -->
        <a id="public-web-jump-button" href="javascript:" class="weui-btn weui-btn_primary weui-btn_loading" onclick="openWeapp()">
          <span id="public-web-jump-button-loading" class="weui-primary-loading weui-primary-loading_transparent"><i class="weui-primary-loading__dot"></i></span>
          打开小程序
        </a>
      </div>
      <div id="wechat-web-container" class="hidden">
        <p class="">点击以下按钮打开 “和卡生活”</p> <!-- replace -->
        <!-- 跳转小程序的开放标签。文档 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html -->
        <wx-open-launch-weapp id="launch-btn" username="微信公众平台的设置中最底部就可以看到" path=""> <!-- replace -->
          <template>
            <button style="width: 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>
      </div>
      <div id="desktop-web-container" class="hidden">
        <p class="">请在手机打开网页链接</p>
      </div>
    </div>
  </body>
</html>
小程序的APPID就不说了,大家都可以找到,这里说下云开发的环境id怎么获取
  • 首先,在开发者工具上找到云开发(不用创建项目的时候就需要云开发)点击


    image.png
  • 看图中圈起来的就是环境ID,直接复制上就可


    image.png
  • 需要打开未登录的用户访问权限


    image.png
  • 在概览这个静态网站进行开通


    image.png
  • 这个静态网站是有免费的额度,超过后是需要收费的
  • 完成上面的这些,将代码复制替换后,将你们的html页面,直接拖拽到这里进行上传或者是点击上传文件夹都可,注意:下次要进行替换的时候,一定要进行勾选,否则不生效的
    image.png
    image.png
自己新建一个HTML进行跳转到h5部署的静态网站的这个页面进行传递参数
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title></title>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      img {
        height: 100%;
        width: 100%;
        margin: 0 auto;
      }
    </style>
  </head>
  <body>
    <script>
      function getInfo(info) {
        var regn = new RegExp("(^|&)" + info + "=([^&]*)(&|$)");
        var ri = window.location.search.substr(1).match(regn);
        if (ri != null) {
          return decodeURI(ri[2]);
        }
      }
   
      let card = getInfo("card");
      if (card) {
        location.href =
          "固定的路径&card=" +
          card;
      }
    </script>
  </body>
</html>

  • 路径获取,就是我们上传到云开发环境的文件下载地址


    image.png
    image.png
  • 图片中的下载地址就是我们需要的固定路径
  • 将我们写的这个HTML上传到自己的服务器上(通过点击练级及后面添加参数就可以了)
  • 这是我自己的https://www.baidu.com/bindCardH5.html?card=这是我传递的参数
经过上面的就大功告成了,不过静态网站跳转的是线上小程序,不利于调试

####### 如果对你有帮助,请双击点赞哟~

相关文章

  • h5跳转小程序之静态网页

    首先h5跳转小程序的方式有很多,这里说得不是小程序中嵌套h5页面,而是单纯的一个(外部的)h5页面跳转小程序,我的...

  • 静态网页跳转小程序

    前置条件:非个人已认证小程序 1.在微信开发者工具中点击 云开发->更多->静态网站,开通静态网站2.创建云函数 ...

  • 小程序和h5跳转

    公司要做h5跳转小程序,就试着调试了一下。目前只支持小程序内嵌H5,并且只有内嵌的H5才能跳回小程序 小程序跳H5...

  • 短信链接跳转小程序之静态网页

    可以参考https://www.cnblogs.com/hemei1212/p/14821249.html[htt...

  • 小程序与H5如何互相跳转

    由于小程序官方没有提供外部H5网页直接跳转到小程序的api,所以目前只支持小程序内嵌H5,并且只有内嵌的H5才能跳...

  • 小程序与H5如何互相跳转

    由于小程序官方没有提供外部H5网页直接跳转到小程序的api,所以目前只支持小程序内嵌H5,并且只有内嵌的H5才能跳...

  • 小程序与H5如何互相跳转

    由于小程序官方没有提供外部H5网页直接跳转到小程序的api,所以目前只支持小程序内嵌H5,并且只有内嵌的H5才能跳...

  • 静态h5跳转小程序

    1、先阅读微信官方文档https://developers.weixin.qq.com/miniprogram/d...

  • H5跳小程序

    H5跳转小程序分两种情况,一种是小程序内部打开的H5跳转小程序,一种是在外部的H5跳转到小程序 小程序内部打开的H...

  • uni-app 开发H5静态网页跳转小程序

    注意要点: 1.H5调用云函数引入cloud.js文件,在工程main.html的header标签中引入 然后在m...

网友评论

      本文标题:h5跳转小程序之静态网页

      本文链接:https://www.haomeiwen.com/subject/cddvjrtx.html