Bläddra i källkod

feature:更新壁纸的md5流程

zeng.chen 6 månader sedan
förälder
incheckning
eb460247fc
3 ändrade filer med 43 tillägg och 81 borttagningar
  1. 1 1
      devices/cmd_key_event.js
  2. 40 78
      pages/piano/cropper/cropper.js
  3. 2 2
      utils/event_bus.js

+ 1 - 1
devices/cmd_key_event.js

@@ -370,7 +370,7 @@ class CmdEvent {
         return event;
     }
     static wallPaperMD5({ value, kind }) {
-        const event = new CmdEvent({ cmdEvent: EnumCmdEvent.wallpaperMD5 });
+        const event = new CmdEvent({ cmdEvent: EnumCmdEvent.wallPaperMD5 });
         console.log("wallPaperMD5", value, kind)
         event.wallpaper = value;
         event.heiJiaoKind = kind;

+ 40 - 78
pages/piano/cropper/cropper.js

@@ -27,6 +27,7 @@ Page({
     _imgUrl: null,
     _imgMD5: null,
     _imgIndex: null,
+    _imgNext: 0,
   },
   cropper: null,
   callback() {
@@ -157,47 +158,7 @@ Page({
       }
     })
   },
-  testUpload(url) {
-    const fs = wx.getFileSystemManager();
-    wx.showLoading({
-      title: '壁纸裁剪中',
-    })
-    let _this = this
-    // app.globalData.imgSrc = obj.url;
-    console.log("裁剪壁纸:", url);
-    _this.data._imgUrl = url
-    fs.readFile({
-      filePath: url,
-      encoding: '', // 不指定编码以获取原始二进制数据
-      success: (obj) => {
-
-        fs.getFileInfo({
-          "filePath": url, "digestAlgorithm": "md5", success: (res) => {
-            console.log("md5:", res)
-            console.log("加载文件成功:", obj.data.byteLength, obj.data.length, obj.getFileInfo)
-
-            let binData = obj.data;
-            _this.sliceDataIntoChunks(binData, 64);
-
-            console.log("加载文件成功2:", binData.byteLength, binData.length)
-            _this.data._imageBufferLength = binData.byteLength ?? binData.length
-            wx.hideLoading();
-            wx.showLoading({
-              title: '开始传输壁纸',
-            })
-            _this.data._imgMD5 = res.digest
-            console.log("md5 2:", _this.data._imgMD5)
-            _this.startImage()
-          }
-        })
-
 
-      },
-      fail: (err) => {
-        console.error('读取 .bin 文件失败:', err);
-      },
-    });
-  },
   loadimage(e) {
     wx.hideLoading();
     console.log('壁纸', e);
@@ -240,7 +201,7 @@ Page({
               console.log("加载文件成功:", obj.data.byteLength, obj.data.length, obj.getFileInfo)
 
               let binData = obj.data;
-              _this.sliceDataIntoChunks(binData, 64);
+              _this.sliceDataIntoChunks(binData, 128);
 
               console.log("加载文件成功2:", binData.byteLength, binData.length)
               _this.data._imageBufferLength = binData.byteLength ?? binData.length
@@ -249,6 +210,7 @@ Page({
                 title: '开始传输壁纸',
               })
               _this.data._imgMD5 = res.digest
+              _this.data._imgIndex = 0
               console.log("md5 2:", _this.data._imgMD5)
               _this.startImage()
             }, fail: (err) => {
@@ -398,48 +360,28 @@ Page({
       }
     });
   },
-  async sendImage(i) {
+  async sendImage(index) {
     let _this = this
 
     wx.hideLoading()
     let chunks = _this.data._chunks
-    let next = 0;
+
     let total = _this.data._imageBufferLength;
     let btHelper = BtHelper.getInstance();
     // let i = 0;
     // _this.data._timer = setInterval(async () => {
-    if (i > chunks.length - 1) {
-      wx.showModal({
-        title: '壁纸上传成功:' + i + "总:" + chunks.length,
-        showCancel: false,
-        success(res) {
-          if (res.confirm) {
-            const pages = getCurrentPages();
-            // 获取上一级页面实例
-            const prevPage = pages[pages.length - 2];
-            // 传递参数
-            prevPage.setData({
-              topImg: {
-                "pic": _this.data._imgUrl,
-              }
-            });
-            wx.navigateBack({
-              delta: 1,
-            })
-          }
-        }
-      })
+    if (index > chunks.length - 1) {
       _this.endImage(0)
       return;
     }
 
-    const chunk = chunks[i];
-    next += (chunk.byteLength ?? chunk.length);
+    const chunk = chunks[index];
+    _this.data._imgNext += (chunk.byteLength ?? chunk.length);
     // let uint8Array = new Uint8Array(chunk);
     // let unit16 = uint8Array.map(item => {
     //   return item.toString(16)
     // });
-    console.log("发送壁纸数据1:", i, ":", next, ":", chunk.length, ":", chunk.byteLength, ":", total, chunks.length)
+    // console.log("发送壁纸数据1:", index, ":", _this.data._imgNext, ":", chunk.length, ":", chunk.byteLength, ":", total, chunks.length)
 
     let res = await btHelper.wallPaperSyncData(chunk);
     // let res = true;
@@ -452,10 +394,9 @@ Page({
       })
       _this.endImage(2)
     }
-    _this.updateProgress(next, total);
 
-    i++;
-    _this.data._imgIndex = i;
+    _this.data._imgIndex += 1;
+    _this.updateProgress(_this.data._imgNext, total);
     // }, 30);
   },
   async delay(ms) {
@@ -473,6 +414,9 @@ Page({
       showProgress: false
     })
     _this.data._chunks = []
+    _this.data._imgNext = 0
+    _this.data._imgMD5 = null
+    _this.data._imgIndex = 0
     BtHelper.getInstance().wallPaper(value);
   },
   startProgress() {
@@ -498,9 +442,9 @@ Page({
   failSend(showToast) {
     let _this = this
     _this.data._chunks = null
-    _this.data._imgUrl = null
     _this.data._imgMD5 = null
     _this.data._imgIndex = 0
+    _this.data._imgNext = 0
     wx.hideLoading()
     if (showToast) {
       wx.showModal({
@@ -528,13 +472,13 @@ Page({
     let _this = this;
     EventManager.addNotification(CmdEvent.eventName, function (event) {
       let name = event.cmdEvent;
-      console.log("裁剪页:", EnumCmdEvent)
       let value = event.wallpaper;
+      let kind = event.heiJiaoKind;
+      console.log("裁剪页:", name, value, kind)
 
       switch (name) {
         case EnumCmdEvent.wallpaper:
-          let kind = event.heiJiaoKind;
-          console.log("裁剪页:", value, kind)
+          //0x78
           // 小程序:0x78,  1。 开启壁纸
           // 黑胶回:0x78, 2, 1 ,1。  0x78, 2, 0, 2 。 1成功,2失败   
 
@@ -559,14 +503,31 @@ Page({
           } else if (value === 0 && kind == 1) {
             // 发送完成
             _this.failSend(false)
-            wx.showToast({
-              title: '壁纸发送成功',
-              icon: 'success'
+            wx.showModal({
+              title: '上传壁纸成功',
+              showCancel: false,
+              success(res) {
+                if (res.confirm) {
+                  const pages = getCurrentPages();
+                  // 获取上一级页面实例
+                  const prevPage = pages[pages.length - 2];
+                  // 传递参数
+                  prevPage.setData({
+                    topImg: {
+                      "pic": _this.data._imgUrl,
+                    }
+                  });
+                  wx.navigateBack({
+                    delta: 1,
+                  })
+                }
+              }
             })
           }
           break;
         case EnumCmdEvent.wallPaperMD5:
-          console.log("开始发送MD5")
+          // 0x80
+          console.log("开始发送MD5回复", kind, value)
           // 收到回复md5,开始发送图片
           if (kind == 1 && value === 1) {
             _this.sendImage(0)
@@ -575,6 +536,7 @@ Page({
           }
           break;
         case EnumCmdEvent.wallPaperData:
+          // 0x79
           // 收到回复md5,开始发送图片
           if (kind == 1 && value === 1) {
             _this.sendImage(_this.data._imgIndex)

+ 2 - 2
utils/event_bus.js

@@ -118,7 +118,7 @@ function removeNotification(name, observer) {
 function fire(cmdEvent) {
     let name = "cmdEventNotification";
     let info = cmdEvent;
-    console.log("fire:" + name);
+    // console.log("fire:" + name);
     if (__notices.length == 0) {
         console.log("fire error: u hadn't add any notice.");
         return;
@@ -134,7 +134,7 @@ function fire(cmdEvent) {
 
 function fireName(name, cmdEvent) {
     let info = cmdEvent;
-    console.log("fire:" + name);
+    // console.log("fire:" + name);
     if (__notices.length == 0) {
         console.log("fire error: u hadn't add any notice.");
         return;