Browse Source

pref: await/async handle request

wangyuan 2 years ago
parent
commit
299f4615db
3 changed files with 106 additions and 84 deletions
  1. 30 27
      pages/index/index.js
  2. 43 33
      pages/my/my.js
  3. 33 24
      pages/report/report.js

+ 30 - 27
pages/index/index.js

@@ -208,38 +208,41 @@ Page({
       })
       })
     })
     })
   },
   },
-  async initIndexData (pullDownRefresh = false) {
+  async initIndexData () {
+    await this.awaitHomePage()
+    await this.awaitAppointmentList()
+    await this.awaitAppGetConfigImg()
+  },
+  // 获取首页档案信息 剩余次数
+  awaitHomePage () {
     var that = this
     var that = this
     wx.showLoading({
     wx.showLoading({
       title: '加载中...',
       title: '加载中...',
       mask: true
       mask: true
     })
     })
-    // 获取首页档案信息 剩余次数
-    homePage({}).then(hoemRes => {
-      wx.hideLoading()
-      if (pullDownRefresh) {
-        wx.stopPullDownRefresh()
-      }
-      var response = hoemRes.data.documentVos || []
-      response.map(item => {
-        item.birthDay = item.birthday.split(' ')[0]
-      })
-      that.setData({
-        useNumber: hoemRes.data.useNumber,
-        documentVos: response,
-        isRefreshPage: true
-      })
-      app.globalData.useNumber = hoemRes.data.useNumber
-    }).catch(e => {
-      wx.hideLoading()
-      wx.showModal({
-        content: e,
-        confirmColor: '#333',
-        showCancel: false
+    return new Promise((reslove,reject) => {
+      homePage({}).then(hoemRes => {
+        wx.hideLoading()
+        var response = hoemRes.data.documentVos || []
+        response.map(item => {
+          item.birthDay = item.birthday.split(' ')[0]
+        })
+        that.setData({
+          useNumber: hoemRes.data.useNumber,
+          documentVos: response,
+          isRefreshPage: true
+        })
+        app.globalData.useNumber = hoemRes.data.useNumber
+        reslove(hoemRes)
+      }).catch(e => {
+        wx.hideLoading()
+        wx.showModal({
+          content: e,
+          confirmColor: '#333',
+          showCancel: false
+        })
       })
       })
     })
     })
-    await that.awaitAppointmentList()
-    await that.awaitAppGetConfigImg()
   },
   },
   // 获取待预约次数
   // 获取待预约次数
   awaitAppointmentList () {
   awaitAppointmentList () {
@@ -250,7 +253,6 @@ Page({
         status: 1,
         status: 1,
         currentPage: 1
         currentPage: 1
       }).then(res => {
       }).then(res => {
-        reslove(res)
         wx.hideNavigationBarLoading()
         wx.hideNavigationBarLoading()
         if (res.data.vos.length !== 0) {
         if (res.data.vos.length !== 0) {
           const response = res.data.vos || []
           const response = res.data.vos || []
@@ -268,6 +270,7 @@ Page({
             topBarObj: null
             topBarObj: null
           })
           })
         }
         }
+        reslove(res)
       }).catch(e => {
       }).catch(e => {
         reject(e)
         reject(e)
         wx.hideNavigationBarLoading()
         wx.hideNavigationBarLoading()
@@ -282,11 +285,11 @@ Page({
       appGetConfigImg({
       appGetConfigImg({
         'imgType': '5'
         'imgType': '5'
       }).then(configRes => {
       }).then(configRes => {
-        reslove(configRes)
         wx.hideNavigationBarLoading()
         wx.hideNavigationBarLoading()
         that.setData({
         that.setData({
           homepageImg: configRes.data[0].imgUrl
           homepageImg: configRes.data[0].imgUrl
         })
         })
+        reslove(configRes)
       }).catch(e => {
       }).catch(e => {
         reject(e)
         reject(e)
         wx.hideNavigationBarLoading()
         wx.hideNavigationBarLoading()

+ 43 - 33
pages/my/my.js

@@ -75,9 +75,7 @@ Page({
 
 
   onShow () {
   onShow () {
     if (!this.data.isFirstGetCount) {
     if (!this.data.isFirstGetCount) {
-      this.getAccountNmberFn()
-      this.appointmentListFn()
-      this.getAppGetConfigImg()
+      this.initMyPage()
     }
     }
   },
   },
 
 
@@ -94,22 +92,28 @@ Page({
       appHeadImg: headImg,
       appHeadImg: headImg,
       appUserName: userName
       appUserName: userName
     })
     })
-    this.getAccountNmberFn()
-    this.appointmentListFn()
-    this.getAppGetConfigImg()
+    this.initMyPage()
+  },
+
+  async initMyPage () {
+    await this.getAccountNmberFn()
+    await this.appointmentListFn()
+    await this.getAppGetConfigImg()
   },
   },
 
 
   getAppGetConfigImg() {
   getAppGetConfigImg() {
     const that = this
     const that = this
     wx.showNavigationBarLoading()
     wx.showNavigationBarLoading()
     // 类型  【1.关于我的 2.用户协议 3.我的 4.实体卡兑换说明 】
     // 类型  【1.关于我的 2.用户协议 3.我的 4.实体卡兑换说明 】
-    appGetConfigImg({
-      'imgType': '3'
-    }).then(configRes => {
-      wx.hideNavigationBarLoading()
-      console.log(configRes, 'configRes my')
-      that.setData({
-        imageList: configRes.data || []
+    return new Promise((reslove,rej) => {
+      appGetConfigImg({
+        'imgType': '3'
+      }).then(configRes => {
+        wx.hideNavigationBarLoading()
+        that.setData({
+          imageList: configRes.data || []
+        })
+        reslove(configRes)
       })
       })
     })
     })
   },
   },
@@ -155,18 +159,21 @@ Page({
   // 获取用户预约信息
   // 获取用户预约信息
   appointmentListFn () {
   appointmentListFn () {
     wx.showNavigationBarLoading()
     wx.showNavigationBarLoading()
-    appointmentList({
-      status: 1,
-      currentPage: 1
-    }).then(res => {
-      wx.hideNavigationBarLoading()
-      if (res.data.vos.length !== 0) {
-        app.globalData.hasAppointment = true
-      } else {
-        app.globalData.hasAppointment = false
-      }
-    }).catch(e => {
-      wx.hideNavigationBarLoading()
+    return new Promise((resolve, reject) => {
+      appointmentList({
+        status: 1,
+        currentPage: 1
+      }).then(res => {
+        wx.hideNavigationBarLoading()
+        if (res.data.vos.length !== 0) {
+          app.globalData.hasAppointment = true
+        } else {
+          app.globalData.hasAppointment = false
+        }
+        resolve(res)
+      }).catch(e => {
+        wx.hideNavigationBarLoading()
+      })
     })
     })
   },
   },
 
 
@@ -174,15 +181,18 @@ Page({
   getAccountNmberFn () {
   getAccountNmberFn () {
     var that = this
     var that = this
     wx.showNavigationBarLoading()
     wx.showNavigationBarLoading()
-    getAccountNmber({}).then(res => {
-      wx.hideNavigationBarLoading()
-      that.setData({
-        count: res.data,
-        isFirstGetCount: false
+    return new Promise((resolve, reject) => {
+      getAccountNmber({}).then(res => {
+        wx.hideNavigationBarLoading()
+        that.setData({
+          count: res.data,
+          isFirstGetCount: false
+        })
+        app.globalData.useNumber = res.data
+        resolve(res)
+      }).catch(e => {
+        wx.hideNavigationBarLoading()
       })
       })
-      app.globalData.useNumber = res.data
-    }).catch(e => {
-      wx.hideNavigationBarLoading()
     })
     })
   },
   },
 
 

+ 33 - 24
pages/report/report.js

@@ -19,21 +19,27 @@ Page({
   onShow() {
   onShow() {
     app.globalData.selectedInex = 1
     app.globalData.selectedInex = 1
     wx.setStorageSync('selectedInex', 1)
     wx.setStorageSync('selectedInex', 1)
-    console.log(app.globalData.hasAppointment);
-    this.getMyReportList()
-    this.appointmentListFn()
+    this.initReportPage()
+  },
+
+  async initReportPage () {
+    await this.getMyReportList()
+    await this.appointmentListFn()
   },
   },
 
 
   appointmentListFn () {
   appointmentListFn () {
-    appointmentList({
-      status: 1,
-      currentPage: 1
-    }).then(res => {
-      if (res.data.vos.length !== 0) {
-        app.globalData.hasAppointment = true
-      } else {
-        app.globalData.hasAppointment = false
-      }
+    return new Promise((resolve, reject) => {
+      appointmentList({
+        status: 1,
+        currentPage: 1
+      }).then(res => {
+        if (res.data.vos.length !== 0) {
+          app.globalData.hasAppointment = true
+        } else {
+          app.globalData.hasAppointment = false
+        }
+        resolve(res)
+      })
     })
     })
   },
   },
 
 
@@ -44,18 +50,21 @@ Page({
       title: '加载中...',
       title: '加载中...',
       mask: true
       mask: true
     })
     })
-    myReportList({}).then(res => {
-      wx.hideLoading()
-      var response = res.data || []
-      that.setData({
-        reportList: response
-      })
-    }).catch(e => {
-      wx.hideLoading()
-      wx.showModal({
-        content: e,
-        confirmColor: '#333',
-        showCancel: false
+    return new Promise((resolve, rejetct) => {
+      myReportList({}).then(res => {
+        wx.hideLoading()
+        var response = res.data || []
+        that.setData({
+          reportList: response
+        })
+        resolve(res)
+      }).catch(e => {
+        wx.hideLoading()
+        wx.showModal({
+          content: e,
+          confirmColor: '#333',
+          showCancel: false
+        })
       })
       })
     })
     })
   },
   },