jump-mp.html 7.2 KB

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