litian
2024-07-09 86cbb2a45a153a704bcc0d63697f83d2832caa71
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
const util = require("./components/util"); // 引入封装过的加载提示
import {
  loginInfo
} from "../../../../../../assets/js/login";
const app = getApp();
Component({
  properties: {
    treeList: {
      type: Array,
      value: [],
    },
    learnList: {
      type: Array,
      value: [],
    },
    bookInfo: {
      type: Object,
      value: {},
    },
    buyIds: {
      type: Array,
      value: [],
    },
    tab: {
      type: String,
      value: "",
    },
    applyState: {
      type: String,
      value: "",
    },
    openIds: {
      type: Array,
      value: [],
    },
    isShowCheck: {
      type: Boolean,
      value: false,
    },
    paymentPage: {
      type: Boolean,
      value: false,
    },
    superior: {
      type: Boolean,
      value: false,
    },
    checkAllState: {
      type: Boolean,
    },
    deadline: {
      type: String,
    },
  },
  data: {
    sonPurchaseSaleMethodId: "",
    superiorPurchaseSaleMethodId: [],
    activeValues: [1, 2],
    webpageSrc: "",
    cloudShoppingCart: [],
    selectAll: [],
    shoppingCart: true,
    stop: false,
    cart: true,
    canExecute: true
  },
  ready() {},
 
  onLoad() {
    this.setData({
      sonPurchaseSaleMethodId: [],
    });
  },
  observers: {
    treeList: function (newValue) {
      const list = [];
      let checked = "";
      this.properties.treeList.forEach((item) => {
        if (item.children && item.children.length > 0) {
          list.push(...this.flattenTree([item]));
        }
      });
      const flag = list.findIndex((item) => item.checked == true);
    },
  },
 
  methods: {
    handleList(tree) {
      for (let index = 0; index < tree.length; index++) {
        const item = tree[index];
        if (item.sysType == "CmsItem" && !item.checked) {}
        if (item.children && item.children.length > 0) {
          this.handleList(item.children);
        }
      }
    },
    // 节点展开
    handleChange(e) {
      this.setData({
        openIds: e.detail.value,
      });
    },
    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) {
        if (data.saleMethod[0].State == "Disable") return false;
        const isSHow = this.properties.buyIds.some(
          (item) => item == data.saleMethod[0].Id
        );
        return !isSHow;
      } else {
        return false;
      }
    },
    // 跳转音视频播放器
    goPlayer(e) {
      // 检查登录状态
      const token = wx.getStorageSync(app.config.tokenKey);
      if (token) {
        const item = e.currentTarget.dataset.item;
        const parent = e.currentTarget.dataset.parent;
        console.log('item', item);
        const parentProductLinkPath = parent ?
          parent.productLinkPath :
          wx.getStorageSync("teachResourcesPath");
        const parentName = parent ? parent.name : "教学资源";
        let url;
        if (item.selectType == 'zip') {
          if (this.properties.applyState == 'Normal') {
            return wx.showToast({
              icon: 'error',
              title: '请前往PC端下载',
            })
          } else {
            return wx.showToast({
              icon: 'error',
              title: '请先申请下载',
            })
          }
        }
        if (this.properties.isShowCheck && this.resourceIsBuy(item)) {
          return false;
        }
        if (item.selectType == "video" || item.learnSelectType == "video") {
          url = "/packageDomain/pages/resourceDetails/myVideo/index";
        } else if (
          item.selectType == "audio" ||
          item.learnSelectType == "audio"
        ) {
          url = "/packageDomain/pages/resourceDetails/myAudio/index";
        } else if (item.selectType == "picture" || item.selectType == "zip") {
          url = "/packageDomain/pages/resourceDetails/document/index";
        }
        if (
          this.properties.tab == "jsek_cloudLearning" &&
          this.resourceIsBuy(item)
        ) {
          return wx.showToast({
            icon: "error",
            title: item.saleMethod[0].Price > 0 ?
              "请先购买该资源" : "请先点击领取查看按钮",
          });
        }
        if (
          item.saleMethod.length == 0 &&
          item.sysType == "CmsItem" &&
          this.properties.tab == "jsek_cloudLearning"
        ) {
          return wx.showToast({
            icon: "error",
            title: "暂未开始销售,请联系管理员",
          });
        }
        if (this.properties.tab == "jsek_teachingResources" && (item.file || item.freeFile)) {
          let file = item.fileMap[item.file] ? item.fileMap[item.file] : item.fileMap[item.freeFile]
          if (file && file.protectType !== "Public") {
            return wx.showToast({
              icon: "error",
              title: "资源不可用,请联系管理员",
            });
          }
        } else if ((this.properties.tab == "jsek_teachingResources" || this.properties.tab == "jsek_cloudLearning") && (!item.file && !item.freeFile && !item.protectedFile)) {
          return wx.showToast({
            icon: 'error',
            title: '暂无资源',
          })
        }
        wx.navigateTo({
          url: `${url}?productLinkPath=${item.productLinkPath}&parentProductLinkPath=${parentProductLinkPath}&parentName=${parentName}&activeId=${item.id}&bookId=${this.properties.bookInfo.id}&bookName=${this.properties.bookInfo.name}&cmsId=${this.properties.bookInfo.rootCmsItemId}&formPath=${this.properties.tab}&applyState=${this.properties.applyState}&deadline=${this.properties.deadline}`,
        });
 
        // debugger
        if (item.selectType == "pdf" || item.selectType == "document") {
          const fileLink = item.file ? item.file : item.freeFile
          console.log(fileLink);
          wx.navigateTo({
            url: "/packageBookService/pages/components/webView/index?md5=" +
              fileLink +
              "&fileName=" +
              item.name +
              "&fileType=pdf" + "&bookBuy=true"
          });
        }
        // if (item.selectType == "document") {
        //   // freeFile
        //   // if (item.selectType == "document") {
        //   //   item.selectType = "pptx";
        //   // }
        //   const fileLink = item.file ?
        //     app.config.requestCtx + "/file/api/ApiDownload?md5=" + item.file :
        //     app.config.requestCtx +
        //     "/file/api/ApiDownload?md5=" +
        //     item.freeFile;
        //   console.log(fileLink, "fileLink");
        //   //提示加载中
        //   // 单次下载允许的最大文件为 200MB
        //   wx.downloadFile({
        //     url: fileLink,
        //     // filePath: wx.env.USER_DATA_PATH + `/${item.name}.${item.selectType}`,
        //     filePath: wx.env.USER_DATA_PATH + `/${item.name}.${item.selectType}`,
        //     success: function (res) {
        //       console.log(res, "wx.downloadFile success res");
        //       if (res.statusCode != 200) {
        //         util.hideLoadingWithErrorTips();
        //         return false;
        //       }
        //       var Path = res.tempFilePath; //返回的文件临时地址,用于后面打开本地预览所用
        //       let data = res.filePath;
        //       wx.openDocument({
        //         filePath: data,
        //         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") {
          this.setData({
            webpageSrc: item.jsek_link,
          });
        }
      }
    },
 
    // 拿到所有项
    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;
    },
 
    // 变为true
    findAndUpdateItemsByIds(tree, ids, state) {
      function findAndUpdate(node) {
        if (ids.includes(node.id)) {
          // console.log(node, 'node');
          node.checked = state; // 将目标项的 check 属性设置为 true
        }
        if (node.children && node.children.length > 0) {
          for (let child of node.children) {
            findAndUpdate(child); // 递归处理子节点
          }
        }
      }
      for (let node of tree) {
        findAndUpdate(node); // 对每个顶层节点执行查找和更新操作
      }
      return tree; // 返回修改后的完整数组
    },
 
    checkResoucrceInfo(tree, id) {
      function findAndUpdate(node) {
        if (node.sysType == "CmsFolder" && node.children.length > 0) {
          for (let index = 0; index < node.children.length; index++) {
            const element = node.children[index];
            findAndUpdate(element);
          }
        } else if (id == node.id && node.sysType == "CmsItem") {
          node.checked = !node.checked; // 将目标项的 check 属性设置为 true
        }
      }
      for (let node of tree) {
        findAndUpdate(node); // 对每个顶层节点执行查找和更新操作
      }
      return tree; // 返回修改后的完整数组
    },
    // 全选
    checkAll() {
      const ids = [];
      const list = [];
      let checked = "";
      this.properties.treeList.forEach((item) => {
        if (item.children && item.children.length > 0) {
          list.push(...this.flattenTree([item]));
        }
      });
      list.forEach((item) => {
        ids.push(item.id);
        checked = item.checked;
      });
      // const flag =
      //   list.findIndex((item) => item.checked == false) > -1 ? true : false;
      const flag = this.properties.checkAllState;
      // const updataList = this.findAndUpdateItemsByIds(
      //   this.properties.treeList,
      //   ids,
      //   flag
      // );
      // this.setData({
      //   treeList: updataList,
      // });
 
      var myEventDetail = {
        ids,
        flag,
      }; // detail对象,提供给事件监听函数
      var myEventOption = {
        bubbles: true,
        composed: true,
      }; // 触发事件的选项
      this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
 
      if (!checked) {
        this.setData({
          selectAll: list,
        });
      }
      var myEventDetail = {
        value: !flag,
      }; // detail对象,提供给事件监听函数
      var myEventOption = {
        bubbles: true,
        composed: true,
      }; // 触发事件的选项
      this.triggerEvent("changeCheckAll", myEventDetail, myEventOption);
    },
 
    // 章节勾选
    checkResourceTitle(e) {
      const flag = e.detail.checked;
      // 这个时候checked的值是fals
      let checkResourceTitleList = [];
      const item = e.currentTarget.dataset.item;
      const parent = this.findParentById(this.properties.learnList, item.id);
      let childrenList;
      let flagNum;
      if (parent)
        childrenList = parent.children.filter((citem) => citem.id !== item.id);
      if (childrenList)
        flagNum = childrenList.findIndex((item) => item.checked == false);
      let list = this.flattenTree([item]);
      let ids = [];
      list.forEach((item) => {
        if (
          item.sysType == "CmsFolder" ||
          (item.sysType == "CmsItem" && item.saleMethod.length)
        )
          ids.push(item.id);
      });
      var myEventDetail = {
        ids,
        flag,
      }; // detail对象,提供给事件监听函数
      var myEventOption = {
        bubbles: true,
        composed: true,
      }; // 触发事件的选项
      this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
      if (flagNum == -1) {
        let checkedState;
        var myEventDetail = {
          ids: [parent.id],
          flag,
        }; // detail对象,提供给事件监听函数
        var myEventOption = {
          bubbles: true,
          composed: true,
        }; // 触发事件的选项
        this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
      }
      if (!list.checked) {
        this.setData({
          superiorPurchaseSaleMethodId: list,
        });
      }
      list.forEach((items) => {
        console.log(items.checked, "itesasd");
        if (!items.checked) {
          this.setData({
            cloudShoppingCart: items,
          });
        }
      });
      this.setData({
        openIds: this.data.openIds,
      });
    },
    // 子项勾选
    checkResource(e) {
      const citem = e.currentTarget.dataset.item;
      const parent = e.currentTarget.dataset.parent;
      let outermost;
      if (parent)
        outermost = this.findParentById(this.properties.learnList, parent.id);
      const childrenList = parent.children.filter(
        (item) => item.id !== citem.id && item.saleMethod.length
      );
      const flag = childrenList.findIndex((item) => item.checked == false);
      // 改变该项checked
      var myEventDetail = {
        id: citem.id,
      };
      var myEventOption = {
        bubbles: true,
        composed: true,
      };
      this.triggerEvent("changeResourceChecked", myEventDetail, myEventOption);
      // 通过判断,联通父级checked
      if (flag == -1) {
        let checkedState;
        citem.checked == false ? (checkedState = true) : (checkedState = false);
        var myEventDetail = {
          ids: [parent.id],
          flag: checkedState,
        };
        var myEventOption = {
          bubbles: true,
          composed: true,
        };
        this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
      }
      // 联通最外层级checked ,如果有一个取消勾选,且当前最外层勾选,则取消最外层的勾选
      if (citem.checked && outermost && outermost.checked) {
        var myEventDetail = {
          ids: [outermost.id],
          flag: false,
        };
        var myEventOption = {
          bubbles: true,
          composed: true,
        };
        this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
      }
      // 联通最外层级checked, 如果所有子项勾选, 且当前最外层未勾选, 则加上最外层的勾选
      if (!citem.checked && outermost) {
        let allchildren = this.flattenTree([outermost]);
        allchildren = allchildren.filter(
          (item) =>
          item.sysType == "CmsItem" &&
          item.id !== citem.id &&
          item.saleMethod.length
        );
        const satate = allchildren.findIndex((item) => item.checked == false);
        if (satate == -1) {
          var myEventDetail = {
            ids: [outermost.id],
            flag: true,
          };
          var myEventOption = {
            bubbles: true,
            composed: true,
          };
          this.triggerEvent("changeListChecked", myEventDetail, myEventOption);
        }
      }
      if (!citem.checked) {
        //选中的时候添加id
        let sonPurchaseSaleMethodId = this.data.sonPurchaseSaleMethodId || [];
        if (this.properties.paymentPage) {
          sonPurchaseSaleMethodId = [];
          this.properties.paymentPage = false;
        }
        if (!this.properties.paymentPage) {
          sonPurchaseSaleMethodId.push(citem.saleMethod[0].Id);
        }
        this.setData({
          sonPurchaseSaleMethodId: sonPurchaseSaleMethodId,
        });
      } else {
        // 取消选中的时候删除id
      }
    },
    // 加入购物车
    async onCloudShoppingCart(e) {
      let shoppingCartGetId = [];
 
      const item = e.currentTarget.dataset.item;
      console.log(item.isShopCar);
      const saleMethodId = e.currentTarget.dataset.item.saleMethod[0].Id;
      console.log(saleMethodId);
      let query = {
        start: 0,
        size: 999,
        filterList: [],
        searchList: [],
      };
      const res = await app.MG.store.getShoppingCartProductList(query);
      res.datas.forEach((item) => {
        shoppingCartGetId.push(item.saleMethod.id);
      });
      if (shoppingCartGetId.includes(saleMethodId)) {
        // this.data.updateShoppingCartHidden();
        // this.data.updateCloudLearning();
        wx.showToast({
          icon: "error",
          title: "已添加",
        });
        this.setData({
          shoppingCart: false,
        });
      } else {
        let query = {
          requests: [{
            saleMethodId: saleMethodId,
            storeEventId: null,
            // agentCode: '电子书'
          }, ],
        };
        const addRes = await app.MG.store.addShoppingCart(query);
        console.log(addRes, "addRes");
        if (addRes) {
          wx.showToast({
            icon: "success",
            title: "添加成功",
          });
          item.isShopCar = false;
          this.triggerEvent("updateCloudLearning");
        }
      }
    },
    sadd() {
      this.triggerEvent("updateShoppingCartHidden");
      this.triggerEvent("updateCloudLearning");
    },
    findParentById(arr, id) {
      for (let i = 0; i < arr.length; i++) {
        const item = arr[i];
        // 检查当前项是否匹配(即它是不是我们要找的项)
        if (item.id === id) {
          // 如果当前项就是我们要找的项,则它没有父级,返回 null
          return null;
        }
        // 递归查找当前项的子项
        const child = this.findChildById(item.children, id);
        if (child) {
          // 如果在子项中找到匹配的项,返回当前项作为父级
          return item;
        }
      }
      // 如果没有找到匹配的父项,返回 null
      return null;
    },
    // 辅助函数,用于递归查找子项
    findChildById(children, id) {
      if (!children) {
        return null;
      }
      for (let i = 0; i < children.length; i++) {
        const child = children[i];
        if (child.id === id) {
          // 如果找到匹配的项,返回它
          return child;
        }
        // 递归检查子项的子项
        const subChild = this.findChildById(child.children, id);
        if (subChild) {
          return subChild;
        }
      }
      // 如果没有找到匹配的项,返回 null
      return null;
    },
  },
});