experience-webview.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. let currentRequest = null
  2. let currentEventChannel = null
  3. let pageResolved = false
  4. function appendQueryParam(url, key, value) {
  5. const separator = url.indexOf('?') >= 0 ? '&' : '?'
  6. return `${url}${separator}${key}=${encodeURIComponent(value)}`
  7. }
  8. function buildWebViewSrc(request) {
  9. let nextUrl = request.url
  10. nextUrl = appendQueryParam(nextUrl, 'cmrBridge', request.bridgeVersion)
  11. nextUrl = appendQueryParam(nextUrl, 'cmrKind', request.kind)
  12. return nextUrl
  13. }
  14. function emitFallbackAndClose() {
  15. if (!currentRequest || !currentEventChannel) {
  16. return
  17. }
  18. if (!pageResolved) {
  19. pageResolved = true
  20. currentEventChannel.emit('fallback', currentRequest.fallback)
  21. }
  22. wx.navigateBack({
  23. fail() {},
  24. })
  25. }
  26. function emitCloseAndBack(payload) {
  27. if (currentEventChannel && !pageResolved) {
  28. pageResolved = true
  29. currentEventChannel.emit('close', payload || {})
  30. }
  31. wx.navigateBack({
  32. fail() {},
  33. })
  34. }
  35. Page({
  36. data: {
  37. pageTitle: '内容体验',
  38. pageSubtitle: '',
  39. presentation: 'sheet',
  40. webViewSrc: '',
  41. webViewReady: false,
  42. loadErrorText: '',
  43. panelBodyHeightPx: 420,
  44. },
  45. onLoad() {
  46. const systemInfo = wx.getSystemInfoSync()
  47. const windowHeight = typeof systemInfo.windowHeight === 'number' ? systemInfo.windowHeight : 700
  48. pageResolved = false
  49. currentRequest = null
  50. currentEventChannel = null
  51. this.setData({
  52. pageTitle: '内容体验',
  53. pageSubtitle: '',
  54. presentation: 'sheet',
  55. webViewSrc: '',
  56. webViewReady: false,
  57. loadErrorText: '',
  58. panelBodyHeightPx: Math.max(420, Math.floor(windowHeight * 0.62)),
  59. })
  60. try {
  61. currentEventChannel = this.getOpenerEventChannel()
  62. } catch (error) {
  63. currentEventChannel = null
  64. }
  65. if (!currentEventChannel) {
  66. return
  67. }
  68. currentEventChannel.on('init', (request) => {
  69. currentRequest = request
  70. const presentation = request.presentation || 'sheet'
  71. const panelHeightPx = presentation === 'dialog'
  72. ? Math.max(420, Math.floor(windowHeight * 0.7))
  73. : presentation === 'fullscreen'
  74. ? Math.max(520, windowHeight - 24)
  75. : Math.max(420, Math.floor(windowHeight * 0.72))
  76. const headerHeightPx = presentation === 'fullscreen' ? 84 : 76
  77. this.setData({
  78. pageTitle: request.title || '内容体验',
  79. pageSubtitle: request.subtitle || '',
  80. presentation,
  81. webViewSrc: buildWebViewSrc(request),
  82. webViewReady: true,
  83. loadErrorText: '',
  84. panelBodyHeightPx: Math.max(240, panelHeightPx - headerHeightPx),
  85. })
  86. })
  87. },
  88. onUnload() {
  89. if (currentEventChannel && !pageResolved) {
  90. currentEventChannel.emit('close', {})
  91. }
  92. pageResolved = false
  93. currentRequest = null
  94. currentEventChannel = null
  95. },
  96. handleWebViewMessage(event) {
  97. const dataList = event.detail && Array.isArray(event.detail.data)
  98. ? event.detail.data
  99. : []
  100. const rawMessage = dataList.length ? dataList[dataList.length - 1] : null
  101. if (!rawMessage || typeof rawMessage !== 'object') {
  102. return
  103. }
  104. const action = rawMessage.action || rawMessage.type || ''
  105. if (!action) {
  106. return
  107. }
  108. if (action === 'close') {
  109. emitCloseAndBack(rawMessage.payload)
  110. return
  111. }
  112. if (action === 'submitResult') {
  113. if (currentEventChannel) {
  114. currentEventChannel.emit('submitResult', rawMessage.payload || {})
  115. }
  116. return
  117. }
  118. if (action === 'fallback') {
  119. emitFallbackAndClose()
  120. }
  121. },
  122. handleWebViewError() {
  123. this.setData({
  124. loadErrorText: '页面打开失败,已回退原生内容',
  125. })
  126. emitFallbackAndClose()
  127. },
  128. handleCloseTap() {
  129. emitCloseAndBack({})
  130. },
  131. })