惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
Blog — PlanetScale
Blog — PlanetScale
F
Fortinet All Blogs
腾讯CDC
大猫的无限游戏
大猫的无限游戏
Jina AI
Jina AI
WordPress大学
WordPress大学
雷峰网
雷峰网
小众软件
小众软件
D
DataBreaches.Net
V
Visual Studio Blog
博客园 - Franky
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
博客园 - 聂微东
T
Tailwind CSS Blog
有赞技术团队
有赞技术团队
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
G
Google Developers Blog
云风的 BLOG
云风的 BLOG

博客园 - 新悟空

VB6 Excel VBA 如何复制文件到剪贴板 的一个Bug修复 (用Windows API 来将文件/文件夹复制到剪贴板的源代码 ) VS2022 Blazor WebAssembly 发布前对自己的dll进行混淆 微信小程序 wx.getUserInfo 解密 C# 代码 简单的文件同步备份程序,仅支持单向同步_程序及源代码 Xilium.CefGlue CEF Chrome 自动上传文件不弹出对话框 CefDialogHandler 通过反射绑定事件_Office Visio 从红米手机经常发生UIM没有服务的一些猜想 为什么丰田的软件存在缺陷?(转) C# WebBrowser 网页缩放的方法 《ASP.NET的一些坑》的人造坑,真不是微软问题,是你的理解问题 批《对.net系统架构改造的经验教训》所犯的N个错误 乔布斯:遗失的访谈_文字版 WebApplication 完全编译即aspx不可修改,不包括设计内容的方法 .net 使用管道方式访问mySQL的连接字符串 从智能手机App来看C/S和B/S的区别在哪?(Web的技术本质) 会员充值消费管理系统 mysql 绿色版 安装的一些脚本 分库分表的设计思路让大系统变简单 下载 针式个人知识库管理软件 V9.08 版
通过微信公众号打开小程序 传参数给小程序 开放标签 的html ...
新悟空 · 2024-03-01 · via 博客园 - 新悟空

下述源代码修改后,上传到 小程序的云开始的"更多--静态网站"后,打开这个网址即可打开小程序

包含了打开小程序后,自动关闭微信网页

<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>

      window.onerror = e => {

        console.error(e)

        alert('发生错误' + e)

      }

    </script>

    <!--小程序要先通过认证 {"noPermissionJsApi": ["chooselmage"]"errMsg"."c onfig:ok"} -->

    <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 isPCWixin = ua.match(/WindowsWechat/i) == 'windowswechat'

        var isWeixin = !isWXWork&& !isPCWixin && 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.info('开放标签 ready')

          })

          //alert("13");

          launchBtn.addEventListener('launch', function (e) {

            //console.info('开放标签 success')

           // alert("2");

            setTimeout(()=>{

                            //不需要jssdk 两种方法都行

                            document.addEventListener("WeixinJSBridgeReady", function() {

                                WeixinJSBridge.call("closeWindow");

                            }, false);

                            //这个可以关闭ios系统的手机

                            WeixinJSBridge.call("closeWindow");

                            //需要jssdk

                            wx.closeWindow() 

                          },1000)  

          })

          launchBtn.addEventListener('error', function (e) {

            console.error('开放标签 fail', e.detail)

          })

          wx.config({

           // debug: true, // 调试时可开启

            appId: '你的小程序appid', // <!-- replace -->

            timestamp: 30, // 必填,填任意数字即可

            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 小程序 AppID-->

            // 资源方环境 ID

            resourceEnv: '你的id', // <!--云开发环境 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')

            })

           // alert("OK1")

          } catch (e) {

            buttonEl.classList.remove('weui-btn_loading')

            buttonLoadingEl.classList.add('hidden')

           // alert("Eerror1")

            throw e

          }

        }

      })

      async function openWeapp(onBeforeJump) {

        var c = window.c

        alert("openWeapp1")

        const res = await c.callFunction({

          name: 'public',

          data: {

            action: 'getUrlScheme',

            parameter: 'cdp=1121_212_321',

          },

        })

        //alert("openWeapp2"+res)

        console.warn(res)

        if (onBeforeJump) {

          onBeforeJump()

        }

        //alert("href"+ res.result.openlink)

        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" appid="你的小程序appid'" path="pages/WebView/WebView"+  env-version="trial"> <!-- 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; #07c160; color:#fff;">开始学习</button>

          </template>

        </wx-open-launch-weapp>

      </div>

      <div id="desktop-web-container" class="hidden">

        <p class="">请在手机打开网页链接</p>

      </div>

    </div>

    <script>

      function getQueryString(name) {

        const url_string = window.location.href;

        const url = new URL(url_string);

        return url.searchParams.get(name);

      }

      var cdp=(getQueryString('cdp')) // mick

      var launchBtn =document.getElementById('launch-btn')

      //alert(launchBtn)

      var mypath="pages/WebView/WebView?cdp="+cdp

      //alert(mypath)

      launchBtn.setAttribute('path',mypath)

    </script>

  </body>

</html>