jump.html 7.5 KB

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