闫增涛
2024-05-06 e8ec903206cb82e4dbb4687291d5f7788e7ff046
packageDomain/pages/sampleBookList/applicationForm/index.js
@@ -1,4 +1,6 @@
import { worksDataBytool } from "../../../../../assets/js/toolClass.js";
import {
  worksDataBytool
} from "../../../../assets/js/toolClass.js";
const app = getApp();
Page({
  data: {
@@ -9,68 +11,113 @@
      workInfo: null,
      schoolName: "",
      courseName: "",
      detailedAddress: ""
    },
    contactInfo: {
      fullName: "",
      phone: "",
      detailedAddress: ""
    },
    phoneError: false,
    paperBookList: [], //样书单纸质书
    electronicBookList: [], //样书单电子书
    ebookCount: 0,
    paperBookCount: 0,
    bookList: [],
    isIos: wx.getSystemInfoSync().platform === 'ios',
    keyboardHeight: 0
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() { },
  onLoad() {},
  /**
 * 生命周期函数--监听页面显示
 */
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let that = this;
    that.setData({
      bookList: []
    })
    if (wx.getStorageSync(app.config.userInfoKey)) {
      this.setData({
      that.setData({
        userInfo: JSON.parse(wx.getStorageSync(app.config.userInfoKey))
      })
      if (this.data.userInfo.role == 'Teacher') {
        this.setData({
          teacherInfo: {
            schoolName: JSON.parse(this.data.userInfo.data).schoolName,
            courseName: JSON.parse(this.data.userInfo.data).courseName,
          },
          contactInfo: {
            fullName: JSON.parse(this.data.userInfo.data).fullName,
            phone: JSON.parse(this.data.userInfo.data).phone,
            detailedAddress: JSON.parse(this.data.userInfo.data).detailedAddress
      if (that.data.userInfo.role == 'Teacher') {
        that.setData({
          'teacherInfo.schoolName': JSON.parse(that.data.userInfo.data).schoolName,
          'teacherInfo.courseName': JSON.parse(that.data.userInfo.data).courseName,
          'contactInfo.fullName': JSON.parse(that.data.userInfo.data).fullName,
          'contactInfo.phone': JSON.parse(that.data.userInfo.data).phone,
          'contactInfo.detailedAddress': JSON.parse(that.data.userInfo.data).detailedAddress,
        })
      } else {
        wx.showModal({
          title: '尊敬的用户,您好!', //提示的标题
          content: '请先进行教师认证?', //提示的内容
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: "/packageDomain/pages/teacherCertification/index",
              });
            } else {
              wx.navigateBack();
            }
          }
        })
      }
      if (wx.getStorageSync("paperBookList")) {
        this.setData({
          paperBookList: JSON.parse(wx.getStorageSync("paperBookList")),
          bookList: JSON.parse(wx.getStorageSync("paperBookList"))
        const paperBookList = JSON.parse(wx.getStorageSync("paperBookList"))
        for (let index = 0; index < paperBookList.length; index++) {
          const item = paperBookList[index];
          if (item.publicationDate)
            item.publicationDate = this.formatDate(item.publicationDate)
        }
        that.setData({
          paperBookList: paperBookList,
          bookList: paperBookList
        })
      }
      if (wx.getStorageSync("electronicBookList")) {
        this.setData({
          electronicBookList: JSON.parse(wx.getStorageSync("electronicBookList"))
        const electronicBookList = JSON.parse(wx.getStorageSync("electronicBookList"))
        for (let index = 0; index < electronicBookList.length; index++) {
          const item = electronicBookList[index];
          if (item.publicationDate)
            item.publicationDate = this.formatDate(item.publicationDate)
        }
        that.setData({
          electronicBookList: electronicBookList
        })
      }
      this.getSelectBookCount();
      this.getSelectPaperBookCount();
      this.getPaperType();
    } else {
      wx.navigateTo({
        url: "/pages/testLogin/index",
      });
      that.getSelectBookCount();
      that.getSelectPaperBookCount();
      that.getPaperType();
    }
  },
  formatDate(inputDate) {
    // 使用正则表达式匹配日期部分
    const dateMatch = inputDate.match(/^(\d{4})\/(\d{1,2})\/(\d{1,2})/);
    if (dateMatch) {
      // 提取年、月、日
      const year = dateMatch[1];
      const month = dateMatch[2].padStart(2, '0'); // 确保月份为两位数
      const day = dateMatch[3].padStart(2, '0'); // 确保日期为两位数
      // 拼接并返回格式化后的日期
      return `${year}-${month}-${day}`;
    } else {
      // 如果没有匹配到日期部分,则返回原始输入或抛出错误
      return inputDate; // 或者 throw new Error('Invalid date format');
    }
  },
  tabClick(item) {
    let that = this;
    that.setData({
      bookList: []
    })
    if (item.detail.value == '1') {
      that.setData({
        active: item.detail.value,
@@ -87,12 +134,12 @@
  },
  getElectronType() {
    app.MG.resource.getCmsTypeByRefCode({ refCodes: ['jsek_electronicSampleBook'] }).then((res) => {
    app.MG.resource.getCmsTypeByRefCode({
      refCodes: ['jsek_electronicSampleBook']
    }).then((res) => {
      this.setData({
        description: res[0].description,
        teacherInfo: {
          workInfo: res[0].cmsTypeLinks[0].children
        },
        'teacherInfo.workInfo': res[0].cmsTypeLinks[0].children,
      })
    })
  },
@@ -100,24 +147,22 @@
    app.MG.resource.getCmsTypeByRefCode({
      refCodes: ['jsek_paperVersionSampleBook']
    }).
      then((res) => {
        if (res && res.length > 0) {
          this.setData({
            description: res[0].description,
            teacherInfo: {
              workInfo: res[0].cmsTypeLinks[0].children
            },
          })
        }
      })
    then((res) => {
      if (res && res.length > 0) {
        this.setData({
          description: res[0].description,
          'teacherInfo.workInfo': res[0].cmsTypeLinks[0].children,
        })
      }
    })
  },
  //获取当前已申请次数(电子)
  getSelectBookCount() {
    let that = this;
    app.MG.app.getTicketResult({
      ticketRefCodeOrGuid: 'electronicSampleBookapplyNum',
      roleId: that.data.userInfo.roleId
    })
        ticketRefCodeOrGuid: 'electronicSampleBookapplyNum',
        roleId: that.data.userInfo.roleId
      })
      .then((res) => {
        let num = res.totalCount - res.usedCount;
        that.setData({
@@ -129,9 +174,9 @@
  getSelectPaperBookCount() {
    let that = this;
    app.MG.app.getTicketResult({
      ticketRefCodeOrGuid: 'paperSampleBookapplyNum',
      roleId: that.data.userInfo.roleId
    })
        ticketRefCodeOrGuid: 'paperSampleBookapplyNum',
        roleId: that.data.userInfo.roleId
      })
      .then((res) => {
        let count = res.totalCount - res.usedCount;
        that.setData({
@@ -139,6 +184,37 @@
        })
      })
  },
  //姓名
  onFullNameInput(e) {
    this.setData({
      "contactInfo.fullName": e.detail.value,
    });
  },
  //输入手机号
  onPhoneInput(e) {
    const {
      phoneError
    } = this.data;
    if (e.detail.value) {
      const isPhoneNumber = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(e.detail.value);
      if (phoneError === isPhoneNumber) {
        this.setData({
          phoneError: !isPhoneNumber,
          "contactInfo.phone": e.detail.value,
        });
      }
    } else {
      this.setData({
        phoneError: false,
      });
    }
  },
  onAddressInput() {
    this.setData({
      "contactInfo.detailedAddress": e.detail.value,
    });
  },
  submit() {
    let that = this;
@@ -173,7 +249,7 @@
        }
        applyInfo = {
          ticketRefCodeOrGuid: 'electronicSampleBookapplyNum',
          roleId: userStore.userInfo.roleId,
          roleId: that.data.userInfo.roleId,
          refType: 'applyBook'
        }
      } else {
@@ -187,10 +263,9 @@
        }
        const mailAddress = JSON.stringify(that.data.contactInfo)
        that.setData({
          teacherInfo: {
            detailedAddress: mailAddress,
          }
          'teacherInfo.detailedAddress': mailAddress
        })
        data = {
          topicIdOrRefCode: 'applyEntityBook',
          name: that.data.userInfo.name || '',
@@ -214,25 +289,23 @@
                electronicBookList: [],
                bookList: [],
              })
              wx.setStorageSync("electronicBookList", that.data.electronicBookList);
              wx.removeStorageSync("electronicBookList")
            } else {
              that.setData({
                paperBookList: [],
                bookList: [],
              })
              wx.setStorageSync("paperBookList", that.data.paperBookList);
              wx.removeStorageSync("paperBookList")
            }
            wx.showModal({
              title: '提示!', //提示的标题
              content: '您的样书申请已提交,管理员审核中!申请状态在【个人中心—样书申请】中查询。', //提示的内容
              success: function (res) {
                if (res.confirm) {
                  wx.navigateTo({
                    url: "/packagePersonal/pages/sampleBooks/index",
                    url: "/packagePersonal/pages/sampleBooks/index?stateTab=1&tabActive=" + that.data.active,
                  });
                } else if (res.cancel) {
                }
                } else if (res.cancel) {}
              }
            })
          })
@@ -257,32 +330,104 @@
      success: function (res) {
        if (res.confirm) {
          if (that.data.active == '1') {
            that.data.electronicBookList.forEach((obj, index) => {
            let eList = that.data.electronicBookList;
            eList.forEach((obj, index) => {
              if (obj.id == item.id) {
                that.data.electronicBookList.splice(index, 1);
                eList.splice(index, 1);
              }
            })
            that.setData({
              bookList: that.data.electronicBookList
              bookList: eList
            })
            wx.setStorageSync("electronicBookList", that.data.electronicBookList);
            wx.setStorageSync("electronicBookList", JSON.stringify(eList));
          } else {
            that.data.paperBookList.forEach((obj, index) => {
            let pList = that.data.paperBookList;
            pList.forEach((obj, index) => {
              if (obj.id == item.id) {
                that.data.paperBookList.splice(index, 1);
                pList.splice(index, 1);
              }
            })
            that.setData({
              bookList: this.data.paperBookList
              bookList: pList
            })
            wx.setStorageSync("paperBookList", that.data.paperBookList);
            wx.setStorageSync("paperBookList", JSON.stringify(pList));
          }
        } else if (res.cancel) {
        }
        } else if (res.cancel) {}
      }
    })
  },
  // 监听页面软键盘弹起手动推动页面
  bindkeyboardheightchange(e) {
    // 只对ios 处理
    if (!this.data.isIos === 'ios') {
      return
    }
    // 键盘高度
    const height = e.detail.height;
    const className = e.target.dataset.class;
    console.log(height, className)
    if (height === 0) {
      this.scrollToInput(0);
      return;
    }
    try {
      this.createSelectorQuery()
        .select(`.${className}`)
        .boundingClientRect((res) => {
          // 可使用窗口高度
          const windowHeight = wx.getSystemInfoSync().windowHeight;
          // 除去键盘的剩余高度
          let restHeight = windowHeight - height;
          // 元素左下角坐标
          let bottom = res.bottom;
          // 只有当元素被软键盘覆盖的时候才上推页面
          if (bottom <= restHeight) return;
          // 现阶段需要滚动的大小
          let scrollTop = bottom - restHeight;
          console.log(scrollTop, 1)
          this.scrollToInput(height, scrollTop);
        })
        .exec();
    } catch (error) {}
  },
  // 获取页面滚动条位置
  getScrollOffset() {
    return new Promise((resolve) => {
      try {
        wx.createSelectorQuery()
          .selectViewport()
          .scrollOffset((res) => {
            resolve(res.scrollTop);
          })
          .exec();
      } catch (error) {
        resolve(0);
      }
    });
  },
  // 监听页面软键盘弹起手动推动页面
  scrollToInput(keyboardHeight, scrollTop) {
    this.setData({
      keyboardHeight,
    });
    if (scrollTop) {
      try {
        this.getScrollOffset().then((lastScrollTop) => {
          wx.pageScrollTo({
            // 如果已经存在滚动,在此基础上继续滚
            scrollTop: lastScrollTop ? lastScrollTop + scrollTop : scrollTop,
            duration: 300,
          });
        });
      } catch (error) {}
    }
  },
  changeParam(e) {
    this.scrollToInput(0);
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */