闫增涛
2024-03-22 986d4798a9dad75b6ae617e52ea2918dfacd3708
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
 
import Message from 'tdesign-miniprogram/message/message';
const util = require('./components/util') // 引入封装过的加载提示
const app = getApp()
Component({
  // checked: false,
 
  properties: {
    treeList: {
      type: Array,
      value: []
    },
    bookInfo: {
      type: Object,
      value: {}
    },
    buyIds: {
      type: Array,
      value: []
    },
    tab: {
      type: String,
      value: ''
    },
    applyState: {
      type: String,
      value: ''
    },
    openTeachids: {
      type: Array,
      value: ''
    },
    openLearnids: {
      type: Array,
      value: ""
    }
  },
  data: {
    activeValues: [0],
    webpageSrc: '',
    cloudShoppingCart: []
  },
  onShow() {
 
  },
 
 
  methods: {
    // 节点展开
    handleChange(e) {
      // console.log(this.properties.treeList, 'this.properties.treeList');
      this.setData({
        activeValues: e.detail.value,
      });
      // console.log('传递', this.properties.buyIds);
    },
    handleCheck(data) {
      for (let index = 0; index < data.length; index++) {
        const element = data[index];
        element.checked = true
        if (element.childrenCount && element.type == "productFolder") {
          handleCheck(element.children)
        }
      }
    },
    downloadTeach(e) {
      const value = e.currentTarget.dataset.value
      var myEventDetail = {
        value,
      } // detail对象,提供给事件监听函数
      var myEventOption = {
        bubbles: true,
        composed: true
      } // 触发事件的选项
      this.triggerEvent('downloadTeach', myEventDetail, myEventOption)
 
    },
    // 判断资源是否购买
    resourceIsBuy(data) {
      if (data.saleMethod && data.saleMethod.length) {
        const isSHow = buyIdList.value.some((item) => item == data.saleMethod[0].Id)
        return !isSHow
      } else {
        return false
      }
    },
    // 跳转音视频播放器
    goPlayer(e) {
      console.log(e);
      // debugger
      const item = e.currentTarget.dataset.item
      const parent = e.currentTarget.dataset.parent
      let url
      if (item.selectType == 'video') {
        url = '/packageDomain/pages/resourceDetails/myVideo/index'
      } else if (item.selectType == 'audio') {
        url = '/packageDomain/pages/resourceDetails/myAudio/index'
      } else if (item.selectType == 'picture' || item.selectType == 'zip') {
        url = '/packageDomain/pages/resourceDetails/document/index'
      }
 
      // else {
      //   
      // }
      wx.navigateTo({
        url: `${url}?productLinkPath=${item.productLinkPath}&parentProductLinkPath=${parent.productLinkPath}&parentName=${parent.name}&activeId=${item.id}&bookId=${this.properties.bookInfo.id}&bookName=${this.properties.bookInfo.name}&cmsId=${this.properties.bookInfo.rootCmsItemId}`,
      })
 
 
      if (item.selectType == 'document' || item.selectType == 'pdf') {
        const fileLink = app.config.requestCtx + '/file/api/ApiDownload?md5=' + item.file
        console.log(fileLink, 'fileLink');
        //提示加载中
        util.showLoading()
        // 单次下载允许的最大文件为 200MB
        wx.downloadFile({
          url: fileLink, // 地址已打码,自己换个其他的地址("https://www.xxxxx.com/file/测试通知.pdf")
          success: function (res) {
            console.log(res, "wx.downloadFile success res")
            if (res.statusCode != 200) {
              util.hideLoadingWithErrorTips()
              return false
            }
            var Path = res.tempFilePath //返回的文件临时地址,用于后面打开本地预览所用
            wx.openDocument({
              filePath: Path,
              showMenu: true,
              success: function (res) {
                console.log('打开成功');
                util.hideLoading()
              }
            })
          },
          fail: function (err) {
            console.log(err, "wx.downloadFile fail err");
            util.hideLoadingWithErrorTips()
          }
        })
 
 
      }
 
      if (item.selectType == 'webpage') {
        console.log(11);
        this.setData({
          webpageSrc: item.jsek_link
        })
        console.log(this.data.webpageSrc, 'webpageSrc');
      }
 
 
 
    },
    // 拿到所有项
    getAllChildren(id) {
      let result = [];
      function findChildren(item) {
        if (item.id === id) {
          if (item.children && item.children.length > 0) {
            item.children.forEach(child => {
              findChildren(child);
            });
          }
        } else {
          if (item.children && item.children.length > 0) {
            item.children.forEach(child => {
              findChildren(child);
            });
          }
        }
        if (item.children && item.children.length > 0) {
          result.push(...item.children);
        }
      }
      console.log(this.properties.treeList, 'this.properties.treeList');
      this.properties.treeList.forEach(item => {
        findChildren(item);
      });
      return result;
    },
    // 拿到当前项子项
    flattenTree(tree) {
 
      let result = [];
 
      function flatten(node) {
 
        result.push(node);
 
        if (node.children && node.children.length > 0) {
 
          node.children.forEach(child => {
            flatten(child);
          });
        }
      }
      tree.forEach(node => {
 
        flatten(node);
      });
      // console.log(result, 'result');
      return result
    },
 
 
 
    findAndUpdateItemById(tree, id) {
      function findAndUpdate(node) {
        if (node.id === id) {
          node.check = true; // 将目标项的 check 属性设置为 true
          return true; // 返回 true 表示找到了目标项
        }
        if (node.children && node.children.length > 0) {
          for (let child of node.children) {
            if (findAndUpdate(child)) {
              return true; // 如果在子节点中找到了目标项,则直接返回 true
            }
          }
        }
        return false; // 表示未找到目标项
      }
 
      for (let node of tree) {
        if (findAndUpdate(node)) {
          break; // 如果在顶层节点中找到了目标项,直接退出循环
        }
      }
    },
    // 变为true
    findAndUpdateItemsByIds(tree, ids) {
 
      function findAndUpdate(node) {
 
        if (ids.includes(node.id)) {
          // console.log(node, 'node');
          node.checked = !node.checked  // 将目标项的 check 属性设置为 true
        }
        if (node.children && node.children.length > 0) {
          for (let child of node.children) {
            findAndUpdate(child); // 递归处理子节点
          }
        }
      }
 
      for (let node of tree) {
 
        findAndUpdate(node); // 对每个顶层节点执行查找和更新操作
      }
      // console.log(tree, 'tree345');
      return tree; // 返回修改后的完整数组
    },
 
    // 章节勾选
    checkResourceTitle(e) {
      // 这个时候checked的值是fals
      const item = e.currentTarget.dataset.item
 
      let list = this.flattenTree([item])
 
 
      let ids = []
      list.forEach(item => {
        ids.push(item.id)
      })
 
      const updatedTreeList = this.findAndUpdateItemsByIds(this.properties.treeList, ids);
      // console.log(updatedTreeList, 'updatedTreeList');   //这个时候checked的值就变成了true
 
      this.setData({
        treeList: updatedTreeList  // 更新 treeList 数据
      });
 
      console.log(list, 'list');
      list.forEach(items => {
        console.log(items.checked, 'itesasd');
        if (!items.checked) {
          this.setData({
            cloudShoppingCart: items
          })
        }
      })
 
      this.onCloudShoppingCart()
    },
    // 子项勾选
    checkResource(e) {
      const item = e.currentTarget.dataset.item
 
 
      // let list = this.flattenTree([item])
      // console.log(list, 'list121');
      // let ids = []
      // list.forEach(items => {
      //   console.log(items.id, 'itemsId');
      //   ids.push(items.id)
      // })
    },
    // 加入购物车
    async onCloudShoppingCart() {
 
 
 
      let requests = []
      const item = this.data.cloudShoppingCart
      console.log(item, 'item');
      item.forEach((items) => {
        if (items.saleMethod.length) {
          requests.push({
            saleMethodId: items.saleMethod[0].Id,
            count: 1
          })
        }
      })
      let query = {
        remarks: '云学习',
        requests
      }
      // 查询订单
      let initOrderRes = await app.MG.store.initOrder(query)
      let parameter = {
        orderNum: initOrderRes.orderNumber
      }
      const confirmOrderRes = await app.MG.store.confirmOrder(parameter)
      // if (confirmOrderRes.orderNumber) {
      //   wx.navigateTo({
      //     url: `/pages/cart/paymentPage/index?orderNumber=${confirmOrderRes.orderNumber}`,
      //   })
      //   requests = []
      // }
    },
  },
})