test.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
  6. <title>H5通信测试</title>
  7. <!-- 微信 JS-SDK -->
  8. <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
  9. <!-- Vue 3 CDN -->
  10. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  11. <!-- H5工具类 -->
  12. <script src="../js/utils/h5-bridge.js"></script>
  13. </head>
  14. <body>
  15. <div id="app">
  16. <div class="container">
  17. <h1>🎛️ H5通信功能测试</h1>
  18. <p>测试H5页面与小程序的各种通信功能</p>
  19. <div class="button-group">
  20. <button class="btn primary" @click="goToTest1">
  21. ➡️ 进入Test1
  22. </button>
  23. <button class="btn secondary" @click="handleScanCode">
  24. 📷 扫码
  25. </button>
  26. <button class="btn success" @click="handleChooseImage">
  27. 🖼️ 选择图片
  28. </button>
  29. <button class="btn warning" @click="handleH5UploadImage">
  30. 📤 H5图片上传
  31. </button>
  32. <button class="btn purple" @click="handleH5UploadFile">
  33. 📎 H5附件上传
  34. </button>
  35. <button class="btn info" @click="handleSetStorage">
  36. 💾 设置存储
  37. </button>
  38. <button class="btn info" @click="handleGetStorage">
  39. 📥 获取存储
  40. </button>
  41. <button class="btn danger" @click="handleGoBack">
  42. 🔙 返回小程序
  43. </button>
  44. </div>
  45. </div>
  46. </div>
  47. <script>
  48. const { createApp } = Vue
  49. createApp({
  50. data() {
  51. return {}
  52. },
  53. mounted() {
  54. // 初始化页面,使用自定义结果处理
  55. initPage('test', this.handleResult)
  56. },
  57. methods: {
  58. // 处理操作结果
  59. handleResult(data) {
  60. const { action, success, result, error, message, key, value, found, phoneNumber, count, paths, files } = data
  61. if (success) {
  62. switch (action) {
  63. case 'scanCode':
  64. console.log(`📷 扫码成功: ${result}`)
  65. alert(`扫码结果: ${result}`)
  66. break
  67. case 'chooseImage':
  68. console.log(`🖼️ 选择图片成功: ${count}张`)
  69. if (paths && paths.length > 0) {
  70. console.log(`📁 图片路径: ${paths[0]}`)
  71. }
  72. alert(`选择了 ${count} 张图片`)
  73. break
  74. case 'getStorage':
  75. if (found) {
  76. console.log(`📥 获取存储成功: ${key} = ${JSON.stringify(value)}`)
  77. alert(`存储内容: ${JSON.stringify(value)}`)
  78. } else {
  79. console.log(`📥 存储中没有找到: ${key}`)
  80. alert(`存储中没有找到: ${key}`)
  81. }
  82. break
  83. case 'setStorage':
  84. console.log(`💾 存储成功: ${key} = ${JSON.stringify(value)}`)
  85. alert('存储设置成功!')
  86. break
  87. case 'makePhoneCall':
  88. console.log(`📞 拨打电话成功: ${phoneNumber}`)
  89. break
  90. default:
  91. console.log(`✅ ${action} 操作成功`)
  92. }
  93. } else {
  94. console.error(`❌ ${action} 操作失败: ${error}`)
  95. alert(`${action} 操作失败: ${error}`)
  96. }
  97. },
  98. // 扫码
  99. handleScanCode() {
  100. scanCode('扫描二维码')
  101. },
  102. // 选择图片
  103. handleChooseImage() {
  104. chooseImage('选择图片')
  105. },
  106. // H5图片上传
  107. handleH5UploadImage() {
  108. // 创建文件输入元素
  109. const input = document.createElement('input')
  110. input.type = 'file'
  111. input.accept = 'image/*'
  112. input.multiple = false
  113. input.onchange = (event) => {
  114. const file = event.target.files[0]
  115. if (file) {
  116. this.uploadFileToServer(file, 'image')
  117. }
  118. }
  119. input.click()
  120. },
  121. // H5附件上传
  122. handleH5UploadFile() {
  123. // 创建文件输入元素
  124. const input = document.createElement('input')
  125. input.type = 'file'
  126. input.multiple = true
  127. input.accept = '*/*'
  128. input.onchange = (event) => {
  129. const files = Array.from(event.target.files)
  130. if (files.length > 0) {
  131. files.forEach(file => {
  132. this.uploadFileToServer(file, 'file')
  133. })
  134. }
  135. }
  136. input.click()
  137. },
  138. // 上传文件到服务器
  139. async uploadFileToServer(file, type) {
  140. try {
  141. console.log(`📤 开始上传${type}:`, file.name)
  142. const formData = new FormData()
  143. formData.append('file', file)
  144. formData.append('type', type)
  145. const response = await fetch('https://httpbin.org/post', {
  146. method: 'POST',
  147. body: formData
  148. })
  149. const result = await response.json()
  150. console.log('✅ 上传成功:', result)
  151. alert(`${type === 'image' ? '图片' : '文件'}上传成功!\n文件名: ${file.name}\n大小: ${(file.size / 1024).toFixed(2)}KB`)
  152. } catch (error) {
  153. console.error('❌ 上传失败:', error)
  154. alert(`${type === 'image' ? '图片' : '文件'}上传失败: ${error.message}`)
  155. }
  156. },
  157. // 拨打电话
  158. handleMakePhoneCall() {
  159. makePhoneCall('10086', '拨打电话')
  160. },
  161. // 获取存储
  162. handleGetStorage() {
  163. getStorage('test_data', '获取存储数据')
  164. },
  165. // 设置存储
  166. handleSetStorage() {
  167. setStorage('test_data', {
  168. message: '来自H5的测试数据',
  169. timestamp: Date.now(),
  170. user: 'H5用户'
  171. }, '设置存储数据')
  172. },
  173. // 跳转到Test1页面
  174. goToTest1() {
  175. navigateTo('test1', { from: 'home' })
  176. },
  177. // 返回小程序
  178. handleGoBack() {
  179. goBack('从Home页面返回')
  180. }
  181. }
  182. }).mount('#app')
  183. </script>
  184. <style>
  185. * {
  186. margin: 0;
  187. padding: 0;
  188. box-sizing: border-box;
  189. }
  190. body {
  191. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  192. background: #f5f5f5;
  193. padding: 20px;
  194. }
  195. .container {
  196. max-width: 600px;
  197. margin: 0 auto;
  198. background: white;
  199. border-radius: 12px;
  200. padding: 24px;
  201. box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  202. }
  203. h1 {
  204. text-align: center;
  205. color: #333;
  206. margin-bottom: 8px;
  207. }
  208. p {
  209. text-align: center;
  210. color: #666;
  211. margin-bottom: 32px;
  212. }
  213. .button-group {
  214. display: grid;
  215. grid-template-columns: 1fr 1fr;
  216. gap: 12px;
  217. margin-bottom: 32px;
  218. }
  219. .btn {
  220. padding: 12px 16px;
  221. border: none;
  222. border-radius: 8px;
  223. font-size: 14px;
  224. font-weight: 500;
  225. cursor: pointer;
  226. transition: all 0.2s;
  227. }
  228. .btn.primary { background: #007AFF; color: white; }
  229. .btn.secondary { background: #6c757d; color: white; }
  230. .btn.success { background: #28a745; color: white; }
  231. .btn.warning { background: #ffc107; color: #212529; }
  232. .btn.info { background: #17a2b8; color: white; }
  233. .btn.danger { background: #dc3545; color: white; }
  234. .btn.purple { background: #6f42c1; color: white; }
  235. .btn:hover {
  236. transform: translateY(-1px);
  237. box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  238. }
  239. @media (max-width: 480px) {
  240. .button-group {
  241. grid-template-columns: 1fr;
  242. }
  243. }
  244. </style>
  245. </body>
  246. </html>