riku
2026-03-05 9465dc404f7e7cd56100e4859ee0946a3fef7b09
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// component/filegrid/index.js
Component({
  options: {
    addGlobalClass: true,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    files: {
      type: Array,
      value: [],
      observer(newVal) {
        this.formatFiles(newVal);
      },
    },
    gridHeight: {
      type: String,
      value: '29vw',
    },
    gridWidth: {
      type: String,
      value: '29vw',
    },
  },
 
  /**
   * 组件的初始数据
   */
  data: {
    currentFiles: [],
  },
 
  /**
   * 组件的方法列表
   */
  methods: {
    formatFiles(value) {
      const currentFiles = value.map(f => {
        // 根据文件类型选择不同样式
        let extensionClass = '';
        switch (f.ext) {
          case 'xls':
          case 'xlsx':
          case 'csv':
            extensionClass = 'file_xlsx';
            break;
          case 'doc':
          case 'docx':
            extensionClass = 'file_word';
            break;
          case 'pdf':
            extensionClass = 'file_pdf';
            break;
          case 'ppt':
          case 'pptx':
            extensionClass = 'file_ppt';
            break;
          default:
            break;
        }
        return { ...f, styleClass: extensionClass };
      });
      this.setData({ currentFiles });
    },
    //图片放大预览
    previewImage(e) {
      const { index } = e.currentTarget.dataset;
      const f = this.data.currentFiles[index];
      const previewImageUrls = this.data.currentFiles
        .filter(f => f.type == 'image')
        .map(f => f.url);
      const i = previewImageUrls.indexOf(f.url);
      this.setData({
        previewImageUrls,
        previewCurrent: i,
        showPreview: true,
      });
    },
    // 查看文件
    previewFile(e) {
      const { index } = e.currentTarget.dataset;
      const file = this.data.currentFiles[index];
      if (file.url.indexOf('http') != -1) {
        wx.showLoading({
          title: '下载文件中',
          mask: true,
        });
        wx.downloadFile({
          url: file.url,
          success: res => {
            wx.hideLoading();
            if (res.statusCode === 200) {
              const filePath = res.tempFilePath;
              wx.openDocument({ filePath: filePath });
            } else {
              wx.showToast({
                title: '文件下载失败',
                icon: 'error',
              });
            }
          },
          fail(err) {
            wx.showToast({
              title: '文件下载失败',
              icon: 'error',
            });
          },
        });
      } else {
        wx.openDocument({ filePath: file.url });
      }
    },
  },
});