jump.html 7.2 KB

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