jump.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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(
  38. /(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. function ajax(options) {
  100. return new Promise((resolve, reject) => {
  101. if (!options.url) {
  102. alert("确认你的url");
  103. return;
  104. }
  105. let method = options.method || 'GET';
  106. let async = options.async || true;
  107. let xhr = new XMLHttpRequest();
  108. if (method === 'GET') {
  109. // url后面添加随机数防止请求缓存
  110. xhr.open(method, options.url + "?" + Math.random(), async);
  111. xhr.send(null);
  112. } else if (method === 'POST') {
  113. xhr.open(method, options.url, async);
  114. xhr.setRequestHeader('Content-type', 'application/json;charset=utf-8');
  115. xhr.send(JSON.stringify(options.data));
  116. }
  117. xhr.onreadystatechange = () => {
  118. if (xhr.responseText) {
  119. resolve(xhr.responseText);
  120. }
  121. };
  122. xhr.onerror = (e) => {
  123. reject(e);
  124. }
  125. }).catch(e => {
  126. });
  127. }
  128. function openWeapp() {
  129. ajax({
  130. method: 'POST',
  131. url: 'https://test-mp.quanshu123.com/api/v1/mp/wx/urlschema/generate',
  132. data: {
  133. path: '/pages/index/index',
  134. query: 'id=3847838'
  135. }
  136. })
  137. }
  138. </script>
  139. <style>
  140. .hidden {
  141. display: none;
  142. }
  143. .full {
  144. position: absolute;
  145. top: 0;
  146. bottom: 0;
  147. left: 0;
  148. right: 0;
  149. }
  150. .public-web-container {
  151. display: flex;
  152. flex-direction: column;
  153. align-items: center;
  154. }
  155. .public-web-container p {
  156. position: absolute;
  157. top: 40%;
  158. }
  159. .public-web-container a {
  160. position: absolute;
  161. bottom: 40%;
  162. }
  163. .wechat-web-container {
  164. display: flex;
  165. flex-direction: column;
  166. align-items: center;
  167. }
  168. .wechat-web-container p {
  169. position: absolute;
  170. top: 40%;
  171. }
  172. .wechat-web-container wx-open-launch-weapp {
  173. position: absolute;
  174. bottom: 40%;
  175. left: 0;
  176. right: 0;
  177. display: flex;
  178. flex-direction: column;
  179. align-items: center;
  180. }
  181. .desktop-web-container {
  182. display: flex;
  183. flex-direction: column;
  184. align-items: center;
  185. }
  186. .desktop-web-container p {
  187. position: absolute;
  188. top: 40%;
  189. }
  190. </style>
  191. </head>
  192. <body>
  193. <div className="page full">
  194. <div id="public-web-container" className="hidden">
  195. <p className="">正在打开 “盲票小程序”...</p> <!-- replace -->
  196. <a id="public-web-jump-button" href="javascript:" className="weui-btn weui-btn_primary weui-btn_loading"
  197. onClick="openWeapp()">
  198. <span id="public-web-jump-button-loading" className="weui-primary-loading weui-primary-loading_transparent"><i
  199. className="weui-primary-loading__dot"></i></span>
  200. 打开小程序
  201. </a>
  202. </div>
  203. <div id="wechat-web-container" className="hidden">
  204. <p className="">点击以下按钮打开 “盲票小程序”</p> <!-- replace -->
  205. <!-- 跳转小程序的开放标签。文档 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html -->
  206. <wx-open-launch-weapp id="launch-btn" username="gh_cf2872611f66" path="pages/index/index?id=123456"
  207. env-version="trial">
  208. <!-- replace -->
  209. <template>
  210. <button
  211. 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;">
  212. 打开小程序
  213. </button>
  214. </template>
  215. </wx-open-launch-weapp>
  216. </div>
  217. <div id="desktop-web-container" className="hidden">
  218. <p className="">请在手机打开网页链接</p>
  219. </div>
  220. </div>
  221. </body>
  222. </html>