Răsfoiți Sursa

去掉又拍云服务,添加OSS文件md5校验

zhoupeng 5 ani în urmă
părinte
comite
d0ca7b59a0

+ 2 - 1
package.json

@@ -11,6 +11,7 @@
     "typescript": "^3.3.3"
   },
   "dependencies": {
-    "miniprogram-api-typings": "^2.4.2"
+    "js-md5": "^0.7.3",
+    "miniprogram-api-typings": "^2.10.3"
   }
 }

+ 88 - 73
pages/index/component/AudioPanel/index.js

@@ -57,6 +57,66 @@ Component({
       this.startRecord()
     },
 
+    uploadSuccess(accessUrl) {
+      const that = this
+      //文件上传成功后通知服务端
+      API.notifyServer(that.data.paper.examRecordDataId, that.data.paper.examStudentId, that.data.paper.questionOrder, accessUrl, 'AUDIO').then(res => {
+        // 轮询答案处理结果
+        const _startTime = new Date().getTime()
+        var times = 10
+        const handler = setInterval(function () {
+          API.getNotifyResult(res)
+            .then(res => {
+              console.log(res)
+              if (res == 'CONFIRMED') {
+                clearInterval(handler)
+                wx.hideLoading()
+                
+                wx.showToast({
+                  title: '上传成功,继续扫描下一题',
+                  icon: 'none',
+                  duration: 2500
+                })
+
+                wx.reLaunch({
+                  url: '/pages/index/index'
+                })
+              } else {
+                throw '答案未同步'
+              }
+            }).catch(exceptions => {
+              times--
+              if (new Date().getTime() - _startTime >= 1000 * 10 || times < 0) {
+                clearInterval(handler)
+                wx.hideLoading()
+                wx.showToast({
+                  title: '上传失败,请重新上传',
+                  icon: 'none'
+                })
+              }
+            })
+        }, 1000)
+      }).catch(exceptions => {
+        console.log('catch', exceptions)
+        that.uploadFail(exceptions)
+      })
+    },
+
+    uploadFail(exceptions) {
+      var error = '上传失败,请重新上传'
+      try {
+        if (exceptions.statusCode == 500) {
+          error = exceptions.data.desc
+        }
+      } catch (e) {
+
+      }
+      wx.hideLoading()
+      wx.showToast({
+        title: error,
+        icon: 'none'
+      })
+    },
     uploadAudio(e) {
       // 正在录音 禁止操作
       if (this.data.isRecord) {
@@ -73,87 +133,42 @@ Component({
       const duration = filepath
       wx.getFileInfo({
         filePath: filepath,
-        success: function (res) {
-          console.log(res)
+        success: function (fileRes) {
+          console.log(fileRes)
           var accessUrl = ''
           // 获取上传签名
           API.getSign(that.data.paper.examRecordDataId, that.data.paper.examStudentId,
-            that.data.paper.questionOrder, res.digest, 'mp3')
-            .then(res => {
-              accessUrl = res.accessUrl
+            that.data.paper.questionOrder, fileRes.digest, 'mp3')
+            .then(signRes => {
+              console.log(signRes)
               // 上传文件
-              console.log(res)
-              return API.uploadFile(filepath, res.formParams, res.formUrl)
-            }).then(res => {
-              console.log(res)
-              //文件上传成功后通知服务端
-              API.notifyServer(that.data.paper.examRecordDataId, that.data.paper.examStudentId, that.data.paper.questionOrder, accessUrl, 'AUDIO').then(res => {
-                // 轮询答案处理结果
-                const _startTime = new Date().getTime()
-                var times = 10
-                const handler = setInterval(function () {
-                  API.getNotifyResult(res)
-                    .then(res => {
-                      console.log(res)
-                      if (res == 'CONFIRMED') {
-                        clearInterval(handler)
-                        wx.hideLoading()
-                        
-                        wx.showToast({
-                          title: '上传成功,继续扫描下一题',
-                          icon: 'none',
-                          duration: 2500
-                        })
-
-                        wx.reLaunch({
-                          url: '/pages/index/index'
-                        })
-                      } else {
-                        throw '答案未同步'
-                      }
-                    }).catch(exceptions => {
-                      times--
-                      if (new Date().getTime() - _startTime >= 1000 * 10 || times < 0) {
-                        clearInterval(handler)
-                        wx.hideLoading()
-                        wx.showToast({
-                          title: '上传失败,请重新上传',
-                          icon: 'none'
-                        })
-                      }
-                    })
-                }, 1000)
-              }).catch(exceptions => {
-                console.log('catch', exceptions)
-                var error = '上传失败,请重新上传'
-                try {
-                  if (exceptions.statusCode == 500) {
-                    error = exceptions.data.desc
-                  }
-                } catch (e) {
-
+              const c_md5 = wx.arrayBufferToBase64(new Uint8Array(fileRes.digest.match(/[\da-f]{2}/gi).map(function (h) {
+                return parseInt(h, 16)
+              })))
+              console.log('uploadFile::start upload')
+              const task = wx.uploadFile({
+                url: signRes.formUrl,
+                filePath: filepath,
+                name: 'file',
+                formData: signRes.formParams,
+                success: taskRes => {
+                  console.log('uploadFile::upload success', taskRes)
+                  that.uploadSuccess(signRes.accessUrl)
+                },
+                fail: error => {
+                  console.log('uploadFile:uploadFile:catch', exceptions)
+                  that.uploadFail(exceptions)
                 }
-                wx.hideLoading()
-                wx.showToast({
-                  title: error,
-                  icon: 'none'
-                })
               })
-            }).catch(exceptions => {
-              console.log('catch', exceptions)
-              var error = '上传失败,请重新上传'
-              try {
-                if (exceptions.statusCode == 500) {
-                  error = exceptions.data.desc
+              task.onHeadersReceived(res => {
+                console.log('task header::', res, res.header['Content-MD5'], c_md5)
+                if (c_md5 != res.header['Content-MD5']) {
+                  that.uploadFail('文件破损,请重新上传')
                 }
-              } catch (e) {
-
-              }
-              wx.hideLoading()
-              wx.showToast({
-                title: error,
-                icon: 'none'
               })
+            }).catch(exceptions => {
+              console.log('catch', exceptions)
+              that.uploadFail(exceptions)
             })
         }
       })

+ 52 - 27
pages/index/component/PicturePanel/index.js

@@ -4,6 +4,7 @@ const app = getApp()
 const API = app.globalData.api
 const SUFFIX = ["jpg", "jpeg", "png"]
 const MASK = false
+const MD5 = require('js-md5')
 Component({
   /**
    * 组件的属性列表
@@ -33,6 +34,7 @@ Component({
     enableChoose: true,
     gridLoading: false,
     willSubmit: false, // 确定按钮是否已点击
+    mmd: MD5
   },
 
   timeoutHandler: null,
@@ -376,9 +378,23 @@ Component({
       }
     },
 
+    uploadFail(that, filepath, md5, formParams, formUrl, accessUrl, index, retry) {
+      var pic = that.data.pictures
+      pic[index]['status'] = 'FAIL'
+      pic[index]['retry'] = 3 - retry
+      that.setData({
+        pictures: pic
+      })
+      that.checkFailed()
+      retry--
+      if (retry > 0) {
+        that.uploadFile(filepath, md5, formParams, md5, formUrl, accessUrl, index, retry)
+      }
+    },
+
     //上传文件,支持配置重试次数
-    uploadFile(filepath, formParams, formUrl, accessUrl, index, retry) {
-      console.log('uploadFile:cancelAll', this.data.cancelAll)
+    uploadFile(filepath, md5, formParams, formUrl, accessUrl, index, retry) {
+      console.log('uploadFile')
       if (this.data.cancelAll) {
         return
       }
@@ -386,26 +402,35 @@ Component({
         return
       }
       const that = this
-      API.uploadFile(filepath, formParams, formUrl).then(res => {
-        var pic = that.data.pictures
-        pic[index]['url'] = accessUrl
-        pic[index]['status'] = 'SUCCESS'
-        that.setData({
-          pictures: pic
-        })
-        that.checkUploadComplete()
-      }).catch(exceptions => {
-        console.log('uploadFile:uploadFile:catch', exceptions)
-        var pic = that.data.pictures
-        pic[index]['status'] = 'FAIL'
-        pic[index]['retry'] = 3 - retry
-        that.setData({
-          pictures: pic
-        })
-        that.checkFailed()
-        retry--
-        if (retry > 0) {
-          that.uploadFile(filepath, formParams, formUrl, accessUrl, index, retry)
+
+      const c_md5 = wx.arrayBufferToBase64(new Uint8Array(md5.match(/[\da-f]{2}/gi).map(function (h) {
+        return parseInt(h, 16)
+      })))
+      console.log('uploadFile::start upload')
+      const task = wx.uploadFile({
+        url: formUrl,
+        filePath: filepath,
+        name: 'file',
+        formData: formParams,
+        success: taskRes => {
+          console.log('uploadFile::upload success', taskRes)
+          var pic = that.data.pictures
+          pic[index]['url'] = accessUrl
+          pic[index]['status'] = 'SUCCESS'
+          that.setData({
+            pictures: pic
+          })
+          that.checkUploadComplete()
+        },
+        fail: error => {
+          console.log('uploadFile:uploadFile:catch', error)
+          that.uploadFail(that, filepath, md5, formParams, formUrl, accessUrl, index, retry)
+        }
+      })
+      task.onHeadersReceived(res => {
+        console.log('task header::', res, res.header['Content-MD5'], c_md5)
+        if (c_md5 != res.header['Content-MD5']) {
+          that.uploadFail(that, filepath, md5, formParams, formUrl, accessUrl, index, retry)
         }
       })
     },
@@ -427,15 +452,15 @@ Component({
             console.log('uploadImage:compressImage:success',cf)
             wx.getFileInfo({
               filePath: cf.tempFilePath,
-              success: function (res) {
-                console.log('uploadImage:compressImage:success>>getFileInfo:success', res)
+              success: function (fileRes) {
+                console.log('uploadImage:compressImage:success>>getFileInfo:success', fileRes)
                 // 获取上传签名
                 API.getSign(that.data.paper.examRecordDataId, that.data.paper.examStudentId,
-                  that.data.paper.questionOrder, res.digest, 'jpeg')
+                  that.data.paper.questionOrder, fileRes.digest, 'jpeg')
                   .then(signResult => {
                     // 上传文件
                     var _retry = 3
-                    that.uploadFile(cf.tempFilePath, signResult.formParams, signResult.formUrl, signResult.accessUrl, imageIndex, _retry)
+                    that.uploadFile(cf.tempFilePath, fileRes.digest, signResult.formParams, signResult.formUrl, signResult.accessUrl, imageIndex, _retry)
                   }).catch(exceptions => {
                     var pic = that.data.pictures
                     pic[imageIndex]['status'] = 'FAIL'
@@ -444,7 +469,7 @@ Component({
                       pictures: pic
                     })
                     that.checkFailed()
-                    console.log('uploadImage:exceptions:', new Date(), pic)
+                    console.log('uploadImage:exceptions:', new Date(), pic, exceptions)
                     that.uploadImage(imageIndex, --retry)
                   })
               }