From 6851680b996e64c1d66c035245b2f0f6eb3425c6 Mon Sep 17 00:00:00 2001
From: YM <479443481@qq.com>
Date: 星期六, 07 九月 2024 15:33:42 +0800
Subject: [PATCH] 1

---
 packageDomain/pages/sampleBookList/applicationForm/index.js |  288 +++++++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 216 insertions(+), 72 deletions(-)

diff --git a/packageDomain/pages/sampleBookList/applicationForm/index.js b/packageDomain/pages/sampleBookList/applicationForm/index.js
index 19715ff..451f903 100644
--- a/packageDomain/pages/sampleBookList/applicationForm/index.js
+++ b/packageDomain/pages/sampleBookList/applicationForm/index.js
@@ -1,76 +1,123 @@
-import { worksDataBytool } from "../../../../../assets/js/toolClass.js";
+import {
+  worksDataBytool
+} from "../../../../assets/js/toolClass.js";
 const app = getApp();
 Page({
   data: {
     userInfo: null,
-    active: 0,
+    active: '0',
     description: "", //鐢宠瑙勫垯
     teacherInfo: {
       workInfo: null,
       schoolName: "",
       courseName: "",
+      address: ""
     },
     contactInfo: {
-      fullName: "",
+      user: "",
       phone: "",
-      detailedAddress: ""
+      address: ""
     },
+    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.user': JSON.parse(that.data.userInfo.data).fullName,
+          'contactInfo.phone': JSON.parse(that.data.userInfo.data).phone,
+          'contactInfo.address': 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) {
+    // 浣跨敤姝e垯琛ㄨ揪寮忓尮閰嶆棩鏈熼儴鍒�  
+    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,23 @@
     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 +175,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,7 +185,36 @@
         })
       })
   },
-
+  //濮撳悕
+  onFullNameInput(e) {
+    this.setData({
+      "contactInfo.user": 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(e) {
+    this.setData({
+      "contactInfo.address": e.detail.value,
+    });
+  },
   submit() {
     let that = this;
     if (that.data.bookList.length > 0) {
@@ -173,11 +248,11 @@
         }
         applyInfo = {
           ticketRefCodeOrGuid: 'electronicSampleBookapplyNum',
-          roleId: userStore.userInfo.roleId,
+          roleId: that.data.userInfo.roleId,
           refType: 'applyBook'
         }
       } else {
-        if (!(that.data.contactInfo.fullName && that.data.contactInfo.phone && that.data.contactInfo.detailedAddress)) {
+        if (!(that.data.contactInfo.user && that.data.contactInfo.phone && that.data.contactInfo.address)) {
           wx.showToast({
             title: "璇峰皢鏀惰揣浜轰俊鎭~鍐欏畬鏁达紒",
             icon: 'none',
@@ -187,10 +262,9 @@
         }
         const mailAddress = JSON.stringify(that.data.contactInfo)
         that.setData({
-          teacherInfo: {
-            detailedAddress: mailAddress,
-          }
+          'teacherInfo.address': mailAddress
         })
+
         data = {
           topicIdOrRefCode: 'applyEntityBook',
           name: that.data.userInfo.name || '',
@@ -214,25 +288,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 +329,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) => {
+          // 鍙娇鐢ㄧ獥鍙i珮搴�
+          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);
+  },
   /**
    * 椤甸潰鐩稿叧浜嬩欢澶勭悊鍑芥暟--鐩戝惉鐢ㄦ埛涓嬫媺鍔ㄤ綔
    */

--
Gitblit v1.9.1