Просмотр исходного кода

pref: await/async handle request

wangyuan 2 лет назад
Родитель
Сommit
299f4615db
3 измененных файлов с 106 добавлено и 84 удалено
  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
     wx.showLoading({
       title: '加载中...',
       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 () {
@@ -250,7 +253,6 @@ Page({
         status: 1,
         currentPage: 1
       }).then(res => {
-        reslove(res)
         wx.hideNavigationBarLoading()
         if (res.data.vos.length !== 0) {
           const response = res.data.vos || []
@@ -268,6 +270,7 @@ Page({
             topBarObj: null
           })
         }
+        reslove(res)
       }).catch(e => {
         reject(e)
         wx.hideNavigationBarLoading()
@@ -282,11 +285,11 @@ Page({
       appGetConfigImg({
         'imgType': '5'
       }).then(configRes => {
-        reslove(configRes)
         wx.hideNavigationBarLoading()
         that.setData({
           homepageImg: configRes.data[0].imgUrl
         })
+        reslove(configRes)
       }).catch(e => {
         reject(e)
         wx.hideNavigationBarLoading()

+ 43 - 33
pages/my/my.js

@@ -75,9 +75,7 @@ Page({
 
   onShow () {
     if (!this.data.isFirstGetCount) {
-      this.getAccountNmberFn()
-      this.appointmentListFn()
-      this.getAppGetConfigImg()
+      this.initMyPage()
     }
   },
 
@@ -94,22 +92,28 @@ Page({
       appHeadImg: headImg,
       appUserName: userName
     })
-    this.getAccountNmberFn()
-    this.appointmentListFn()
-    this.getAppGetConfigImg()
+    this.initMyPage()
+  },
+
+  async initMyPage () {
+    await this.getAccountNmberFn()
+    await this.appointmentListFn()
+    await this.getAppGetConfigImg()
   },
 
   getAppGetConfigImg() {
     const that = this
     wx.showNavigationBarLoading()
     // 类型  【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 () {
     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 () {
     var that = this
     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() {
     app.globalData.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 () {
-    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: '加载中...',
       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
+        })
       })
     })
   },