闫增涛
2024-03-28 0b523220931d70aa752c3beaca5ed32ca85511c5
packageDomain/pages/sampleBookList/index.js
@@ -1,3 +1,6 @@
import {
  loginInfo
} from '../../../assets/js/login';
const app = getApp();
Page({
@@ -113,27 +116,41 @@
          Path: options.secondCode,
          Type: '*',
          StoreRefCode: app.config.goodsStore,
        }
        ],
        }],
      })
    }
    this.getIconTypeList();
    // 获取重点项目
    this.getProjectList('bookClassification');
    //新形态教材
    this.getProjectList('newFormsTextbooks');
    if (wx.getStorageSync(app.config.tokenKey)) {
      this.getAlreadyPBookList()
      this.getAlreadyEBookList()
    }
    if (wx.getStorageSync(app.config.userInfoKey)) {
    // 检查登录状态
    const token = wx.getStorageSync(app.config.tokenKey)
    if (!token) {
      loginInfo(app, (data) => {
        if (data) {
      this.setData({
        userInfo: JSON.parse(wx.getStorageSync(app.config.userInfoKey))
      })
    }
    this.getBookList(false);
          this.getAlreadyPBookList()
          this.getAlreadyEBookList()
          // 获取申请次数
          this.getSelectPaperBookCount()
          this.getSelectBookCount()
        }
      })
    } else {
      this.setData({
        userInfo: JSON.parse(wx.getStorageSync(app.config.userInfoKey))
      })
      this.getAlreadyPBookList()
      this.getAlreadyEBookList()
      // 获取申请次数
      this.getSelectPaperBookCount()
      this.getSelectBookCount()
    }
  },
  /**
@@ -160,27 +177,55 @@
    this.setData({
      num: this.data.paperBookList.length + this.data.electronicBookList.length
    })
    this.getBookList(false);
  },
  goBack() {
    wx.navigateBack();
  },
  // 获取纸质书剩余次数
  getSelectPaperBookCount(item) {
    app.MG.app
      .getTicketResult({
        ticketRefCodeOrGuid: 'paperSampleBookapplyNum',
        roleId: this.data.userInfo.roleId
      })
      .then((res) => {
        this.setData({
          paperBookCount: res.totalCount - res.usedCount
        })
      })
  },
  // 获取电子书剩余次数
  getSelectBookCount(item) {
    app.MG.app
      .getTicketResult({
        ticketRefCodeOrGuid: 'electronicSampleBookapplyNum',
        roleId: this.data.userInfo.roleId
      })
      .then((res) => {
        this.setData({
          ebookCount: res.totalCount - res.usedCount
        })
      })
  },
  //获取高级分类
  getIconTypeList() {
    let query = {
      path: '*',
      filterList: [
        {
      filterList: [{
          value: 'Normal',
          field: 'state'
        }
      ],
      }],
      queryType: '\\',
      searchList: [],
      size: '5',
      start: '0',
      storeRefCode: app.config.goodsStore,
      sort: { type: 'Asc', field: 'LinkOrder' }
      sort: {
        type: 'Asc',
        field: 'LinkOrder'
      }
    }
    app.MG.store.getStoreChannelList(query).then((res) => {
      let list = [];
@@ -213,7 +258,10 @@
      size: '30',
      start: '0',
      storeRefCode: app.config.goodsStore,
      sort: { type: 'Asc', field: 'LinkOrder' },
      sort: {
        type: 'Asc',
        field: 'LinkOrder'
      },
    };
    app.MG.store.getStoreChannelList(query).then(async (res) => {
      let options = [];
@@ -243,7 +291,10 @@
      size: '30',
      start: '0',
      storeRefCode: app.config.goodsStore,
      sort: { type: 'Asc', field: 'LinkOrder' },
      sort: {
        type: 'Asc',
        field: 'LinkOrder'
      },
    };
    app.MG.store.getStoreChannelList(query).then((res) => {
      if (res.datas.length > 0) {
@@ -346,12 +397,10 @@
    }
    const query = {
      ...pathObj,
      filterList: [
        {
      filterList: [{
          value: 'Normal',
          field: 'state',
        },
      ],
      }, ],
      sort,
      paging: {
        start: that.data.page * that.data.limit - that.data.limit,
@@ -458,9 +507,9 @@
  },
  // 一级分类切换
  onChangeStair(e) {
    const path = e.detail.value.length
      ? `${this.data.assortCheck.code}\\${e.detail.value}`
      : this.data.assortCheck.code;
    const path = e.detail.value.length ?
      `${this.data.assortCheck.code}\\${e.detail.value}` :
      this.data.assortCheck.code;
    this.setData({
      pathList: [],
      stairCode: e.detail.value,
@@ -556,7 +605,9 @@
  },
  // 跳转图书详情
  goDetail(e) {
    const { book } = e.currentTarget.dataset;
    const {
      book
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/packageBookService/pages/bookServices/detail/index?id=${book.id}&name=${book.name}`,
    });
@@ -680,17 +731,34 @@
  //纸质样书
  onChangePaper(e) {
    const item = e.currentTarget.dataset.book;
    const index = this.data.bookList.findIndex(citem => citem.id == item.id)
    let role = this.data.userInfo != null ? this.data.userInfo.role : null
    if (role) {
      if (item.paperDisabled) {
    if (role && role == 'Teacher') {
      // 是教师
      if (e.detail.checked) {
        // 点击选中
        if (this.data.paperBookCount == 0) {
        wx.showToast({
          title: "该样书已申请",
          icon: 'none',
            title: "您的纸质样书申请次数已用尽,若要继续,请联系管理员!",
            icon: "none",
          duration: 1000,
        })
        return
          return false;
      }
      if (item.paperChecked) {
        if (this.data.paperBookList.length >= 2) {
          wx.showToast({
            title: "每次最多只可申请2本样书",
            icon: "none",
            duration: 1000,
          })
          return false;
        }
        this.addBookFun(item, "pBook")
        this.setData({
          ["bookList[" + index + "].paperChecked"]: e.detail.checked
        })
      } else {
        // 取消选中
        let pList = this.data.paperBookList;
        if (pList.length > 0) {
          pList.forEach((obj, index) => {
@@ -699,30 +767,19 @@
            }
          })
        }
        item.paperChecked = false;
        if (this.data.bookList.length > 0) {
          this.data.bookList.forEach(i => {
            if (i.id == item.id) {
              i.paperChecked = false;
            }
        this.setData({
          paperBookList: pList,
          num: this.data.num - 1,
          ["bookList[" + index + "].paperChecked"]: e.detail.checked
          })
        }
        wx.setStorageSync("paperBookList", JSON.stringify(pList));
        wx.showToast({
          title: "已从样书申请单移除",
          icon: 'none',
          duration: 1000,
        })
        this.setData({
          paperBookList: pList,
          num: pList.length + this.data.electronicBookList.length
        })
        wx.setStorageSync("paperBookList", JSON.stringify(pList));
        return
      }
      if (role == 'Teacher') {
        this.getSelectPaperBookCount(item)
      } else {
        item.paperChecked = false
        wx.showModal({
          title: '尊敬的用户,您好!', //提示的标题
          content: '请先进行教师认证?', //提示的内容
@@ -731,45 +788,43 @@
              wx.navigateTo({
                url: "/packageDomain/pages/teacherCertification/index",
              });
            } else if (res.cancel) {
              item.paperChecked = false
            }
          }
        })
      }
    } else {
      item.paperChecked = false
    }
  },
  getSelectPaperBookCount(item) {
    app.MG.app
      .getTicketResult({
        ticketRefCodeOrGuid: 'paperSampleBookapplyNum',
        roleId: this.data.userInfo.roleId
      })
      .then((res) => {
        this.setData({
          paperBookCount: res.totalCount - res.usedCount
        })
        if (!item.paperDisabled) {
          this.getAlreadyBookList(item, 'pBook')
        }
      })
  },
  //选择电子样书
  onChangeElectron(e) {
    const item = e.currentTarget.dataset.book;
    let role = this.data.userInfo != null ? this.data.userInfo.role : null;
    if (role) {
      if (item.electronDisabled) {
    const index = this.data.bookList.findIndex(citem => citem.id == item.id)
    let role = this.data.userInfo != null ? this.data.userInfo.role : null
    if (role && role == 'Teacher') {
      // 是教师
      if (e.detail.checked) {
        // 点击选中
        if (this.data.ebookCount == 0) {
        wx.showToast({
          title: "该样书已申请",
          icon: 'none',
            title: "您的电子样书申请次数已用尽,若要继续,请联系管理员!",
            icon: "none",
          duration: 1000,
        })
        return
          return false;
      }
      if (item.electronChecked) {
        if (this.data.electronicBookList.length >= 2) {
          wx.showToast({
            title: "每次最多只可申请2本样书",
            icon: "none",
            duration: 1000,
          })
          return false;
        }
        this.addBookFun(item, "eBook")
        this.setData({
          ["bookList[" + index + "].electronChecked"]: e.detail.checked
        })
      } else {
        // 取消选中
        let eList = this.data.electronicBookList;
        if (eList.length > 0) {
          eList.forEach((obj, index) => {
@@ -778,62 +833,19 @@
            }
          })
        }
        if (this.data.bookList.length > 0) {
          this.data.bookList.forEach(i => {
            if (i.id == item.id) {
              i.electronChecked = false;
            }
        this.setData({
          electronicBookList: eList,
          num: this.data.num - 1,
          ["bookList[" + index + "].electronChecked"]: e.detail.checked
          })
        }
        item.electronChecked = false;
        wx.setStorageSync("electronicBookList", JSON.stringify(eList));
        wx.showToast({
          title: "已从样书申请单移除",
          icon: 'none',
          duration: 1000,
        })
        this.setData({
          electronicBookList: eList,
          num: eList.length + this.data.paperBookList.length
        })
        wx.setStorageSync("electronicBookList", eList);
        return
      }
      if (role == 'Teacher') {
        //有无子商品,子商品有无电子书文件
        const obj = {
          path: '*',
          queryType: 'SubProduct',
          sort: {
            type: 'Desc',
            field: 'CreateDate'
          },
          paging: {
            start: 0,
            size: 6
          },
          mainProductId: item.id,
          fields: {
            bookshelf_pdf: [],
            bookshelf_protectedEpub: [],
          }
        }
        app.MG.store.getProductList(obj).then((res) => {
          if (res.datas.length > 0) {
            if (res.datas[0].bookshelf_pdf || res.datas[0].bookshelf_protectedEpub) {
              this.getSelectBookCount(item)
            } else {
              wx.showToast({
                title: "暂无电子书文件,请联系管理员!",
                icon: 'none',
                duration: 1000,
              })
            }
          } else {
            this.getSelectBookCount(item)
          }
        })
      } else {
        item.electronChecked = false
        wx.showModal({
          title: '尊敬的用户,您好!', //提示的标题
          content: '请先进行教师认证?', //提示的内容
@@ -842,35 +854,17 @@
              wx.navigateTo({
                url: "/packageDomain/pages/teacherCertification/index",
              });
            } else if (res.cancel) {
              item.electronChecked = false
            }
          }
        })
      }
    } else {
      item.electronChecked = false
    }
  },
  getSelectBookCount(item) {
    app.MG.app
      .getTicketResult({
        ticketRefCodeOrGuid: 'electronicSampleBookapplyNum',
        roleId: this.data.userInfo.roleId
      })
      .then((res) => {
        this.setData({
          ebookCount: res.totalCount - res.usedCount
        })
        if (!item.electronDisabled) {
          this.getAlreadyBookList(item, 'eBook')
        }
      })
  },
  getAlreadyBookList(item, type) {
  //  添加样书
  addBookFun(item, type) {
    let itemAttr = null
    let isHas = false
    const eList = this.data.electronicBookList;
    const pList = this.data.paperBookList;
    itemAttr = {
      id: item.id,
      title: item.name || '-',
@@ -883,154 +877,27 @@
      isbn: item.isbn || '-',
      publicationDate: item.publicationDate
    }
    if (type == 'eBook') {
      if (this.data.ebookCount > 0) {
        const eBookData = this.data.electronicBookList
        if (eBookData.length == 2) {
          item.electronChecked = false;
          wx.showToast({
            title: "每次最多只可申请2本样书",
            icon: 'none',
            duration: 1000,
      eList.push(itemAttr)
      this.setData({
        electronicBookList: eList,
          })
          return false
      wx.setStorageSync("electronicBookList", eList);
    } else {
      pList.push(itemAttr)
      this.setData({
        paperBookList: pList,
      })
      wx.setStorageSync("paperBookList", pList);
        }
        if (eBookData.length > 0) {
          for (let i = 0; i < eBookData.length; i++) {
            if (eBookData[i].id == item.id) {
              isHas = true
            }
          }
          if (!isHas) {
    this.setData({
      num: this.data.num + 1
    })
            wx.showToast({
              title: "添加成功,在样书申请单等您哦~",
              icon: "none",
              duration: 1000,
            })
            this.setData({
              electronicBookList: [...this.data.electronicBookList, ...[itemAttr]],
            })
            wx.setStorageSync("electronicBookList", this.data.electronicBookList);
            this.setData({
              num: this.data.electronicBookList.length + this.data.paperBookList.length
            })
            this.getBookList(false);
          } else {
            let list = this.data.electronicBookList;
            list.forEach((obj, index) => {
              if (obj.id == item.id) {
                list.splice(index, 1);
              }
            })
            item.electronChecked = false
            wx.showToast({
              title: "已从样书申请单移除",
              icon: "none",
              duration: 1000,
            })
            this.setData({
              electronicBookList: list,
              num: list.length + this.data.paperBookList.length
            })
            wx.setStorageSync("electronicBookList", list);
            this.getBookList(false);
          }
        } else {
          wx.showToast({
            title: "添加成功,在样书申请单等您哦~",
            icon: "none",
            duration: 1000,
          })
          this.setData({
            electronicBookList: [itemAttr],
            num: 1 + this.data.paperBookList.length
          })
          wx.setStorageSync("electronicBookList", [itemAttr]);
        }
      } else {
        item.electronChecked = false
        wx.showToast({
          title: "您的电子样书申请次数已用尽,若要继续,请联系管理员!",
          icon: "none",
          duration: 1000,
        })
      }
    } else {
      if (this.data.paperBookCount > 0) {
        const pBookData = this.data.paperBookList
        if (pBookData.length == 2) {
          item.paperChecked = false
          wx.showToast({
            title: "每次最多只可申请2本样书",
            icon: 'none',
            duration: 1000,
          })
          return false
        }
        if (pBookData.length > 0) {
          for (let i = 0; i < pBookData.length; i++) {
            if (pBookData[i].id == item.id) {
              isHas = true
            }
          }
          if (!isHas) {
            item.paperChecked = true
            wx.showToast({
              title: "添加成功,在样书申请单等您哦~",
              icon: 'none',
              duration: 1000,
            })
            this.setData({
              paperBookList: [...this.data.paperBookList, ...[itemAttr]],
            })
            wx.setStorageSync("paperBookList", this.data.paperBookList);
            this.setData({
              num: this.data.paperBookList.length + this.data.electronicBookList.length
            })
          } else {
            let list = this.data.paperBookList;
            list.forEach((obj, index) => {
              if (obj.id == item.id) {
                list.splice(index, 1);
              }
            })
            item.paperChecked = false
            wx.showToast({
              title: "已从样书申请单移除",
              icon: "none",
              duration: 1000,
            })
            this.setData({
              paperChecked: list,
              num: list.length + this.data.electronicBookList.length
            })
            wx.setStorageSync("paperBookList", list);
          }
        } else {
          item.paperChecked = true;
          wx.showToast({
            title: "添加成功,在样书申请单等您哦~",
            icon: 'none',
            duration: 1000,
          })
          this.setData({
            paperBookList: [itemAttr],
            num: 1 + this.data.electronicBookList.length
          })
          wx.setStorageSync("paperBookList", [itemAttr]);
        }
      } else {
        item.paperChecked = false
        wx.showToast({
          title: "您的纸质样书申请次数已用尽,若要继续,请联系管理员!",
          icon: 'none',
          duration: 1000,
        })
      }
    }
  },
  //去样书申请单
  goApply() {