Explorar o código

新增 sendError

wangzhihui %!s(int64=4) %!d(string=hai) anos
pai
achega
dbe7d2e2b3
Modificáronse 1 ficheiros con 120 adicións e 51 borrados
  1. 120 51
      miniprogram/pages/cachePage/cachePage.js

+ 120 - 51
miniprogram/pages/cachePage/cachePage.js

@@ -74,67 +74,136 @@ Page({
     })
   },
   async getPhoneNumber(e) {
-    if (e.detail.errMsg == "getPhoneNumber:ok") {
-      wx.showLoading({
-        title: '正在登录...',
-      })
-      let session_key = wx.getStorageSync('session_key')
-      let {
-        result
-      } = await cloudApi("getWxPhoneNumber", {
-        ...e.detail,
-        session_key
-      })
+    try {
 
-      let {
-        phone,
-      } = result
-      let {
-        shipName,
-        shipMmsi
-      } = this.data
 
-      console.log(phone,
-        shipName,
-        shipMmsi, )
-      let res = await uploadFile(wx.getStorageSync('file'), {
-        openId: wx.getStorageSync('openId'),
-        phone,
-        shipName,
-        shipMmsi,
-        type: wx.getStorageSync('type'),
-        location: `${this.data.longitude},${this.data.latitude}`
-      }, 1)
-      wx.setStorageSync('shareImageUrl', res.result.mediaInfo.viewUrl)
-      if (res.status == 0) {
+      if (e.detail.errMsg == "getPhoneNumber:ok") {
+        wx.showLoading({
+          title: '正在登录...',
+          mask: true
+        })
+        let session_key = wx.getStorageSync('session_key')
         let {
-          shipInfo,
-          userInfo
-        } = res.result
+          result
+        } = await cloudApi("getWxPhoneNumber", {
+          ...e.detail,
+          session_key
+        })
+        let {
+          phone,
+        } = result
+        if (phone) {
+          wx.setStorageSync('phone', phone)
+          let {
+            shipName,
+            shipMmsi
+          } = this.data
+          try {
+            let res = await uploadFile(wx.getStorageSync('file'), {
+              openId: wx.getStorageSync('openId'),
+              phone,
+              shipName,
+              shipMmsi,
+              type: wx.getStorageSync('type'),
+              location: `${this.data.longitude},${this.data.latitude}`
+            }, 1)
+            if (res.status == 0) {
+              wx.setStorageSync('shareImageUrl', res.result.mediaInfo.viewUrl)
+              let {
+                shipInfo,
+                userInfo
+              } = res.result
+
+              let data = {
+                ...shipInfo,
+                ...userInfo
+              }
+              Object.keys(data).forEach(function (key) {
+                wx.setStorageSync(key, data[key])
+              })
+              wx.showToast({
+                title: res.msg,
+              })
+              wx.downloadFile({
+                url: res.result.mediaInfo.viewUrl,
+                success: e => {
+                  console.log("下载调用!", e)
+                  wx.setStorageSync('cacheImage', e.tempFilePath)
+                  wx.saveImageToPhotosAlbum({
+                    filePath: e.tempFilePath,
+                    success: e => {
+                      console.log(5, e)
 
-        let data = {
-          ...shipInfo,
-          ...userInfo
+                      if (e.errMsg == "saveImageToPhotosAlbum:ok") {
+                        wx.showToast({
+                          title: '保存成功!',
+                        })
+                        wx.removeStorageSync('cacheImage')
+                      }
+                    },
+                    fail: async e => {
+                      console.log("6", e)
+                      await cloudApi('sendError', {
+                        e,
+                        flag: 4,
+                        msg: "保存失败"
+                      })
+                      this.setData({
+                        authModal: true,
+                        modalText: "文件存储"
+                      })
+                    }
+                  })
+                  wx.navigateTo({
+                    url: '/pages/takePhoto/success/success',
+                  })
+                },
+                fail: async e => {
+                  await cloudApi('sendError', {
+                    e,
+                    msg: "下载失败",
+                    flag: 3
+                  })
+                }
+              })
+            } else {
+              await cloudApi('sendError', {
+                res,
+                flag: 2
+              })
+              wx.showToast({
+                title: res.msg,
+                icon: "error"
+              })
+            }
+          } catch (error) {
+            await cloudApi('sendError', {
+              error,
+              flag: 1
+            })
+          }
+        } else {
+          wx.showToast({
+            title: '获取手机号失败',
+            duration: 5000
+          })
+          await cloudApi('sendError', {
+            msg: "获取手机号失败"
+          })
         }
-        Object.keys(data).forEach(function (key) {
-          wx.setStorageSync(key, data[key])
-        })
-        wx.showToast({
-          title: res.msg,
-        })
-        wx.redirectTo({
-          url: '/pages/takePhoto/takePhoto',
-        })
       } else {
         wx.showToast({
-          title: res.msg,
+          title: '请授权以登录',
           icon: "error"
         })
+        await cloudApi('sendError', {
+          msg: "未授权手机号"
+        })
       }
-    } else {
-      wx.showToast({
-        title: '请授权以登录',
-        icon: "error"
+    } catch (error) {
+      await cloudApi('sendError', {
+        msg: "全局trycatch",
+        error
       })
     }