jump.html 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 GetRequest(name) {
  129. var url = window.parent.location.search; //获取url中"?"符后的字串
  130. // var theRequest = new Object();
  131. if (url.indexOf("?") != -1) {
  132. var str = url.substr(1);
  133. if (str.indexOf("#" != -1)) {
  134. str = str.substr(0);
  135. }
  136. strs = str.split("&");
  137. for (var i = 0; i < strs.length; i++) {
  138. if (strs[i].indexOf(name) != -1) {
  139. return strs[i].split("=")[1];
  140. }
  141. }
  142. }
  143. return null;
  144. }
  145. function openWeapp() {
  146. ajax({
  147. method: 'POST',
  148. url: 'https://test-mp.quanshu123.com/api/v1/mp/wx/urlschema/generate',
  149. data: {
  150. path: '/pages/index/index',
  151. query: 'id=3847838'
  152. }
  153. }).then(res => {
  154. console.log('res', res)
  155. })
  156. }
  157. </script>
  158. <style>
  159. .hidden {
  160. display: none;
  161. }
  162. .full {
  163. position: absolute;
  164. top: 0;
  165. bottom: 0;
  166. left: 0;
  167. right: 0;
  168. }
  169. .public-web-container {
  170. display: flex;
  171. flex-direction: column;
  172. align-items: center;
  173. }
  174. .public-web-container p {
  175. position: absolute;
  176. top: 40%;
  177. }
  178. .public-web-container a {
  179. position: absolute;
  180. bottom: 40%;
  181. }
  182. .wechat-web-container {
  183. display: flex;
  184. flex-direction: column;
  185. align-items: center;
  186. }
  187. .wechat-web-container p {
  188. position: absolute;
  189. top: 40%;
  190. }
  191. .wechat-web-container wx-open-launch-weapp {
  192. position: absolute;
  193. bottom: 40%;
  194. left: 0;
  195. right: 0;
  196. display: flex;
  197. flex-direction: column;
  198. align-items: center;
  199. }
  200. .desktop-web-container {
  201. display: flex;
  202. flex-direction: column;
  203. align-items: center;
  204. }
  205. .desktop-web-container p {
  206. position: absolute;
  207. top: 40%;
  208. }
  209. </style>
  210. </head>
  211. <body>
  212. <div className="page full">
  213. <div id="public-web-container" className="hidden">
  214. <p className="">正在打开 “盲票小程序”...</p> <!-- replace -->
  215. <a id="public-web-jump-button" href="javascript:" className="weui-btn weui-btn_primary weui-btn_loading"
  216. onClick="openWeapp()">
  217. <span id="public-web-jump-button-loading" className="weui-primary-loading weui-primary-loading_transparent"><i
  218. className="weui-primary-loading__dot"></i></span>
  219. 打开小程序
  220. </a>
  221. </div>
  222. <div id="wechat-web-container" className="hidden">
  223. <p className="">点击以下按钮打开 “盲票小程序”</p> <!-- replace -->
  224. <!-- 跳转小程序的开放标签。文档 https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_Open_Tag.html -->
  225. <wx-open-launch-weapp id="launch-btn" username="gh_cf2872611f66" path="pages/index/index?id=123456"
  226. env-version="trial">
  227. <!-- replace -->
  228. <template>
  229. <button
  230. 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;">
  231. 打开小程序
  232. </button>
  233. </template>
  234. </wx-open-launch-weapp>
  235. </div>
  236. <div id="desktop-web-container" className="hidden">
  237. <p className="">请在手机打开网页链接</p>
  238. </div>
  239. </div>
  240. </body>
  241. </html>