1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
Page({
  data: {
    webviewUrl: '',
    title: ''
  },
 
  onLoad(options) {
    const { url, title } = options
    
    if (!url) {
      wx.showToast({
        title: '缺少URL参数',
        icon: 'none'
      })
      wx.navigateBack()
      return
    }
 
    const decodedUrl = decodeURIComponent(url)
    const decodedTitle = decodeURIComponent(title || '文档预览')
 
    this.setData({
      webviewUrl: decodedUrl,
      title: decodedTitle
    })
 
    // 设置导航栏标题
    wx.setNavigationBarTitle({
      title: decodedTitle
    })
 
    console.log('WebView加载URL:', decodedUrl)
  },
 
  onLoad() {
    console.log('WebView页面加载完成')
  },
 
  onError(e) {
    console.error('WebView加载错误:', e.detail)
    wx.showModal({
      title: '加载失败',
      content: '文档预览失败,请检查网络连接或尝试下载文件',
      showCancel: false,
      confirmText: '确定',
      success: () => {
        wx.navigateBack()
      }
    })
  },
 
  onMessage(e) {
    console.log('WebView消息:', e.detail)
  },
 
  onShareAppMessage() {
    return {
      title: this.data.title,
      path: `/pages/webview/webview?url=${encodeURIComponent(this.data.webviewUrl)}&title=${encodeURIComponent(this.data.title)}`
    }
  }
})