jump.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <html>
  2. <head>
  3. <title>打开小程序</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1">
  6. <script>
  7. window.onerror = e => {
  8. console.error(e)
  9. alert('发生错误' + e)
  10. }
  11. </script>
  12. <!-- weui 样式 -->
  13. <link rel="stylesheet" href="https://res.wx.qq.com/open/libs/weui/2.4.1/weui.min.css"></link>
  14. <!-- 调试用的移动端 console -->
  15. <!-- <script src="https://cdn.jsdelivr.net/npm/eruda"></script> -->
  16. <!-- <script>eruda.init();</script> -->
  17. <!-- 公众号 JSSDK -->
  18. <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  19. <!-- 云开发 Web SDK -->
  20. <script src="https://res.wx.qq.com/open/js/cloudbase/1.1.0/cloud.js"></script>
  21. <script>
  22. function docReady(fn) {
  23. if (document.readyState === 'complete' || document.readyState === 'interactive') {
  24. fn()
  25. } else {
  26. document.addEventListener('DOMContentLoaded', fn);
  27. }
  28. }
  29. //获取当前URL
  30. var url = document.location.href;
  31. //声明一个对象
  32. var getRequest = new Object();
  33. //获取?的位置
  34. var index = url.indexOf("?")
  35. if (index != -1) {
  36. //截取出?后面的字符串
  37. var str = url.substr(index + 1);
  38. //将截取出来的字符串按照&变成数组
  39. strs = str.split("&");
  40. //将get传参存入对象中
  41. for (var i = 0; i < strs.length; i++) {
  42. getRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
  43. }
  44. }
  45. docReady(async function() {
  46. var ua = navigator.userAgent.toLowerCase()
  47. var isWXWork = ua.match(/wxwork/i) == 'wxwork'
  48. var isWeixin = !isWXWork && ua.match(/micromessenger/i) == 'micromessenger'
  49. var isMobile = false
  50. var isDesktop = false
  51. if (navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|IEMobile)/i)) {
  52. isMobile = true
  53. } else {
  54. isDesktop = true
  55. }
  56. if (isWeixin) {
  57. var containerEl = document.getElementById('wechat-web-container')
  58. containerEl.classList.remove('hidden')
  59. containerEl.classList.add('full', 'wechat-web-container')
  60. var launchBtn = document.getElementById('launch-btn')
  61. launchBtn.addEventListener('ready', function (e) {
  62. console.log('开放标签 ready')
  63. })
  64. launchBtn.addEventListener('launch', function (e) {
  65. console.log('开放标签 success')
  66. })
  67. launchBtn.addEventListener('error', function (e) {
  68. console.log('开放标签 fail', e.detail)
  69. })
  70. launchBtn.setAttribute('path', `pages/lucky/index.html?id=${ getRequest.id }`);
  71. wx.config({
  72. //debug: true, // 调试时可开启
  73. appId: 'wx8533800e393dbd6b', // <!-- replace -->
  74. timestamp: 0, // 必填,填任意数字即可
  75. nonceStr: 'nonceStr', // 必填,填任意非空字符串即可
  76. signature: 'signature', // 必填,填任意非空字符串即可
  77. jsApiList: [], // 必填,随意一个接口即可
  78. openTagList:['wx-open-launch-weapp'], // 填入打开小程序的开放标签名
  79. })
  80. } else if (isDesktop) {
  81. // 在 pc 上则给提示引导到手机端打开
  82. var containerEl = document.getElementById('desktop-web-container')
  83. containerEl.classList.remove('hidden')
  84. containerEl.classList.add('full', 'desktop-web-container')
  85. } else {
  86. var containerEl = document.getElementById('public-web-container')
  87. containerEl.classList.remove('hidden')
  88. containerEl.classList.add('full', 'public-web-container')
  89. var c = new cloud.Cloud({
  90. // 必填,表示是未登录模式
  91. identityless: true,
  92. // 资源方 AppID
  93. resourceAppid: 'wx8533800e393dbd6b', // <!-- replace -->
  94. // 资源方环境 ID
  95. resourceEnv: 'mangpiao-prod-0gc3lhscbc41fb6e', // <!-- replace -->
  96. })
  97. await c.init()
  98. window.c = c
  99. var buttonEl = document.getElementById('public-web-jump-button')
  100. var buttonLoadingEl = document.getElementById('public-web-jump-button-loading')
  101. try {
  102. await openWeapp(() => {
  103. buttonEl.classList.remove('weui-btn_loading')
  104. buttonLoadingEl.classList.add('hidden')
  105. })
  106. } catch (e) {
  107. buttonEl.classList.remove('weui-btn_loading')
  108. buttonLoadingEl.classList.add('hidden')
  109. throw e
  110. }
  111. }
  112. })
  113. async function openWeapp(onBeforeJump) {
  114. var c = window.c
  115. const res = await c.callFunction({
  116. name: 'public',
  117. data: {
  118. action: 'getUrlScheme',
  119. options: `id=${ getRequest.id }`,
  120. },
  121. })
  122. console.warn(res)
  123. if (onBeforeJump) {
  124. onBeforeJump()
  125. }
  126. location.href = res.result.openlink
  127. }
  128. </script>
  129. <style>
  130. .hidden {
  131. display: none;
  132. }
  133. .full {
  134. position: absolute;
  135. top: 0;
  136. bottom: 0;
  137. left: 0;
  138. right: 0;
  139. }
  140. .public-web-container {
  141. display: flex;
  142. flex-direction: column;
  143. align-items: center;
  144. }
  145. .public-web-container p {
  146. position: absolute;
  147. top: 40%;
  148. }
  149. .public-web-container a {
  150. position: absolute;
  151. bottom: 40%;
  152. }
  153. .wechat-web-container {
  154. display: flex;
  155. flex-direction: column;
  156. align-items: center;
  157. }
  158. .wechat-web-container p {
  159. position: absolute;
  160. top: 40%;
  161. }
  162. .wechat-web-container wx-open-launch-weapp {
  163. position: absolute;
  164. bottom: 40%;
  165. left: 0;
  166. right: 0;
  167. display: flex;
  168. flex-direction: column;
  169. align-items: center;
  170. }
  171. .desktop-web-container {
  172. display: flex;
  173. flex-direction: column;
  174. align-items: center;
  175. }
  176. .desktop-web-container p {
  177. position: absolute;
  178. top: 40%;
  179. }
  180. </style>
  181. </head>
  182. <body>
  183. <div class="page full">
  184. <div id="public-web-container" class="hidden">
  185. <p class="">正在打开 “盲票小程序”...</p> <!-- replace -->
  186. <a id="public-web-jump-button" href="javascript:" class="weui-btn weui-btn_primary weui-btn_loading" onclick="openWeapp()">
  187. <span id="public-web-jump-button-loading" class="weui-primary-loading weui-primary-loading_transparent"><i class="weui-primary-loading__dot"></i></span>
  188. 打开小程序
  189. </a>
  190. </div>
  191. <div id="wechat-web-container" class="hidden">
  192. <p class="">点击以下按钮打开 “盲票小程序”</p> <!-- replace -->
  193. <!-- 跳转小程序的开放标签。文档 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html -->
  194. <wx-open-launch-weapp id="launch-btn" username="gh_cf2872611f66" path="" env-version="release"> <!-- replace -->
  195. <template>
  196. <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>
  197. </template>
  198. </wx-open-launch-weapp>
  199. </div>
  200. <div id="desktop-web-container" class="hidden">
  201. <p class="">请在手机打开网页链接</p>
  202. </div>
  203. </div>
  204. </body>
  205. </html>