yiming
2024-04-02 312fd09aaf90c0e52ab61bde22d0edbf105baa5e
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// pages/resourceDetails/document/index.js
const app = getApp()
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    webpageSrc: '',
    navBarHeight: '',
    barHeight: '',
    activeId: '',
    bookId: '',
    bookName: '',
    cmsId: '',
    parentName: '',
    parentProductLinkPath: '',
    productLinkPath: '',
    showData: '',
    titleName: '',
    selectType: '',
    zipData: '',
    naturalResources: [],
    titleName: '',
    pdfDatA: [],
    startTime: "", //进入页面当前时间
    pauseTime: 0 //暂停时间
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    const systInfo = wx.getSystemInfoSync();
    const menu = wx.getMenuButtonBoundingClientRect(); // 胶囊信息
    const navBarHeight = (menu.top - systInfo.statusBarHeight) * 2 + menu.height; // 导航栏高度
 
    this.setData({
      navBarHeight: navBarHeight,
      barHeight: systInfo.statusBarHeight,
      activeId: options.activeId,
      bookId: options.bookId,
      bookName: options.bookName,
      cmsId: options.cmsId,
      parentName: options.parentName,
      parentProductLinkPath: options.parentProductLinkPath,
      productLinkPath: options.productLinkPath
    })
    this.resourceDetailsData()
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
 
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      startTime: Date.now()
    })
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    this.setData({
      pauseTime: Date.now()
    })
    if (wx.getStorageSync(app.config.tokenKey) && epubObj.bookBuy) {
      let duration = this.data.pauseTime - this.data.startTime
      this.count(duration)
    }
  },
 
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.setData({
      pauseTime: Date.now()
    })
    if (wx.getStorageSync(app.config.tokenKey) && epubObj.bookBuy) {
      let duration = this.data.pauseTime - this.data.startTime
      this.count(duration)
    }
  },
 
  count(timeStr) {
    const data = {
      appRefCode: app.config.appRefCode,
      type: 'LearningTime', //统计类型--阅读时长
      data: timeStr + '', //统计内容--时长毫秒
      event: 'LearningTime',
      sysType: 'App'
    }
    //阅读商品的id
    if (this.data.bookId) {
      data.productId = this.data.bookId
    }
    //阅读资源的id
    if (this.data.cmsId) {
      data.cmsItemId = this.data.cmsId
    }
    app.MG.job.newJobWithApiNewEvent(data).then((res) => {})
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
 
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
 
  },
 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
 
  },
  goBack() {
    wx.navigateBack()
  },
 
  resourceDetailsData() {
    let query = {
      path: '*',
      queryType: '*',
      productId: this.data.bookId,
      cmsPath: this.data.parentProductLinkPath,
      itemFields: {
        SysType: 'CmsFolder',
        // 资源类型,试读文件,是否允许下载等参数
        selectType: [],
        freeFile: [],
        file: [],
        protectedFile: [],
        resourcesClassification: [],
        isDownload: [],
        jsek_resourceBrief: [],
        jsek_link: [],
        jsek_questionBank: [],
        learnSelectType: []
      },
      pading: {
        start: 0,
        size: 999
      }
    }
    app.MG.store.getProductDetail(query).then((res) => {
      console.log(res);
      res.datas.cmsDatas[0].datas.forEach((item) => {
 
 
 
 
        //刚刚进来的时候
        if (this.data.productLinkPath == item.productLinkPath) {
          this.handleTeachData(item)
          this.setData({
            titleName: item.name,
            selectType: item.selectType
          })
        }
 
 
      })
    })
  },
  //zpi文件下载
  onDownloadButton() {
    const item = this.data.zipData;
    if (!item || !item.file) {
      wx.showToast({
        title: '文件信息缺失',
        icon: 'none'
      });
      return;
    }
 
    const downloadUrl = app.config.requestCtx + '/file/api/ApiDownload?md5=' + item.file;
    wx.showLoading({
      title: '正在下载...',
    });
 
    wx.downloadFile({
      url: downloadUrl,
      success(res) {
        if (res.statusCode === 200) {
          // 下载成功,可以在这里进行一些操作,例如预览、保存等
          // 这里以保存文件到系统为例
          wx.saveFile({
            tempFilePath: res.tempFilePath,
            success(saveRes) {
              wx.hideLoading();
              wx.showToast({
                title: '保存成功',
              });
              // 获取保存后的文件路径
              const savedFilePath = saveRes.savedFilePath;
              // 可以在这里进行后续操作,例如打开文件
              wx.openDocument({
                filePath: savedFilePath,
                success: function () {
                  console.log('打开文档成功')
                },
                fail: function (error) {
                  console.error('打开文档失败', error);
                }
              });
            },
            fail() {
              wx.hideLoading();
              wx.showToast({
                title: '保存失败',
                icon: 'none'
              });
            }
          });
        } else {
          wx.hideLoading();
          wx.showToast({
            title: '下载失败',
            icon: 'none'
          });
        }
      },
      fail() {
        wx.hideLoading();
        wx.showToast({
          title: '下载失败',
          icon: 'none'
        });
      }
    });
  },
  //刚进来的时候调用
  handleTeachData(item) {
    //图片
    if (item.selectType == 'picture') {
      this.setData({
        showData: app.config.requestCtx + '/file/api/ApiDownload?md5=' + item.file,
 
      })
    }
    // 下载文件zip
    if (item.selectType == 'zip') {
      this.setData({
        zipData: item
      })
    }
    //网页
    if (item.selectType == 'webpage') {
      this.setData({
        webpageSrc: item.jsek_link
      })
    }
 
    //文档等
    if (item.selectType == 'pdf' || item.selectType == 'document') {
      app.MG.file.getPdfInfo({
        md5: item.file
      }).then((res) => {
        if (res && res.totalPages) {
          for (let i = 0; i < res.totalPages; i++) {
            const src = app.config.requestCtx + '/file/GetPdfPageImage?md5=' + item.file + '&index=' + (i + 1) + '&dpi=300'
            this.data.naturalResources.push(src)
            // console.log(this.data.naturalResources, ' this.data.naturalResources');
          }
        }
      })
 
 
 
 
    }
  },
 
 
 
  handleTap: function () {
    const naturalResources = this.data.naturalResources;
    naturalResources.forEach(function (item) {
      // console.log(item);
    });
  }
 
 
 
 
 
})