Browse Source

feature:修改IOS裁剪上传图片的问题

zeng.chen 6 months atrás
parent
commit
585a47be1e

+ 16 - 16
devices/ble_manager.js

@@ -220,22 +220,22 @@ class bleManager {
     var that = this;
     const hex_util = require('./../utils/hex_util');
 
-    wx.getConnectedBluetoothDevices({
-      // services: ['ab00'],
-      search: ['ab00', 'ab01', 'ab02', '0000AB00-0000-1000-8000-00805F9B34FB', '0000AB01-0000-1000-8000-00805F9B34FB', '0000AB02-0000-1000-8000-00805F9B34FB'],
-      success: (res) => {
-        // ble连接上,才会有这个返回
-        console.log('根据主服务 UUID 获取已连接的蓝牙设备:', res.devices);
-        if (res.devices.length > 0) {
-          for (var i = 0; i < res.devices.length; i++) {
-            var device = res.devices[i];
-            if (device.name != "") {
-              that.callBackConnect(device);
-            }
-          }
-        }
-      }
-    })
+    // wx.getConnectedBluetoothDevices({
+    //   // services: ['ab00'],
+    //   search: ['ab00', 'ab01', 'ab02', '0000AB00-0000-1000-8000-00805F9B34FB', '0000AB01-0000-1000-8000-00805F9B34FB', '0000AB02-0000-1000-8000-00805F9B34FB'],
+    //   success: (res) => {
+    //     // ble连接上,才会有这个返回
+    //     console.log('根据主服务 UUID 获取已连接的蓝牙设备:', res.devices);
+    //     if (res.devices.length > 0) {
+    //       for (var i = 0; i < res.devices.length; i++) {
+    //         var device = res.devices[i];
+    //         if (device.name != "") {
+    //           that.callBackConnect(device);
+    //         }
+    //       }
+    //     }
+    //   }
+    // })
 
     wx.getBluetoothDevices({
       success: (res) => {

BIN
images/device/mode_bg.png


+ 44 - 24
pages/piano/cropper/cropper.js

@@ -3,10 +3,9 @@
 const { BtCmd } = require('../../../devices/bluetooth/bt_cmd');
 import EventManager from '../../../utils/event_bus'
 import { EnumCmdEvent, CmdEvent } from '../../../devices/cmd_key_event';
-import js_md5 from '../../../utils/js_md5';
-import store from '../../../utils/store';
 import route_util from '../../../utils/route_util';
 const { BtHelper } = require('../../../devices/bt_helper');
+const { uploadImage } = require('../../../request/imageRequest');
 
 Page({
 
@@ -36,6 +35,7 @@ Page({
     max_scale: 2,
     _hasPermission: false,
     _hasShowModal: false,
+    _deviceId: null,
     wallpaper: {},
   },
   cropper: null,
@@ -75,7 +75,6 @@ Page({
           title: '加载中',
         })
         const tempFilePaths = res.tempFiles[0].tempFilePath;
-        // //重置壁纸角度、缩放、位置
         that.cropper.imgReset();
         that.setData({
           src: tempFilePaths
@@ -83,8 +82,8 @@ Page({
 
         // todo 测试用
         // that.testUpload(tempFilePaths)
-      }, fail(res) {
-        console.log(res)
+      }, fail(err2) {
+        console.log(err2)
       }
     })
   },
@@ -146,8 +145,8 @@ Page({
   },
   loadimage(e) {
     wx.hideLoading();
-    console.log('壁纸', e);
-    this.cropper.imgReset();
+    console.log('加载壁纸', e);
+    // this.cropper.imgReset();
   },
   clickcut(e) {
     console.log("clickcut:", e.detail);
@@ -162,10 +161,16 @@ Page({
       delta: -1
     })
   },
+  async _uploadImgIOS(url) {
+    let _this = this;
+    let res = await uploadImage(url);
+    // 服务器的走下载图片
+    _this.data._imgTopPic = res.data;
+    console.log("IOS的走下载图片", res)
+    _this._downloadAndSaveFile(_this.data._imgTopPic)
+  },
   _uploadImage(url) {
-    wx.showLoading({
-      title: '壁纸裁剪中',
-    })
+
     const fs = wx.getFileSystemManager();
 
     let _this = this;
@@ -213,7 +218,6 @@ Page({
 
       },
       fail: (err) => {
-        s
         wx.hideLoading()
         wx.showToast({
           title: '读取该图片失败',
@@ -234,10 +238,23 @@ Page({
       _this._downloadAndSaveFile(_this.data._imgTopPic)
       return;
     }
+
+    const res = wx.getSystemInfoSync(); // 获取系统信息
+    /// android ios
+    const platform = res.platform; // 获取平台类型
+    wx.showLoading({
+      title: '壁纸裁剪中',
+    })
     // 读取裁剪的jpg图片
     this.cropper.getImg((res) => {
-      _this.data._imgTopPic = res.url
-      _this._uploadImage(res.url)
+      if (platform === 'ios') {
+        this.cropper.getImg((res) => {
+          _this._uploadImgIOS(res.url)
+        });
+      } else {
+        _this.data._imgTopPic = res.url
+        _this._uploadImage(res.url)
+      }
     });
   },
   _downloadAndSaveFile(url) {
@@ -350,14 +367,14 @@ Page({
     // }
     this.data._chunks = chunks;
   },
-  getImgMd5(buffer) {
-    let md5ctx = js_md5.md5_init();
-    js_md5.md5_update(md5ctx, buffer, buffer.byteLength ?? buffer.length);
-    let result = js_md5.md5_final(md5ctx);
-    let md5 = js_md5.md5_encode_hex(result, 16);
-    console.log("result:", md5, result);
-    return md5
-  },
+  // getImgMd5(buffer) {
+  //   let md5ctx = js_md5.md5_init();
+  //   js_md5.md5_update(md5ctx, buffer, buffer.byteLength ?? buffer.length);
+  //   let result = js_md5.md5_final(md5ctx);
+  //   let md5 = js_md5.md5_encode_hex(result, 16);
+  //   console.log("result:", md5, result);
+  //   return md5
+  // },
   sendImageMD5() {
     // let lengthCode = this.data._chunks.length
     // console.log("发送壁纸MD5 1:", lengthCode, this.data._imgMD5)
@@ -519,8 +536,8 @@ Page({
   },
   disconnect(event) {
     let _this = this;
-    console.log("断开连接", event.commonValue, event.deviceId);
-    if (event.commonValue === "offline" && !_this.data._hasShowModal) {
+    console.log("断开连接--", event.commonValue, event.deviceId);
+    if (event.deviceId === _this.data._deviceId && event.commonValue === "offline" && !_this.data._hasShowModal) {
       _this.data._hasShowModal = true
       wx.showModal({
         title: '提示',
@@ -547,7 +564,10 @@ Page({
     let image = options.param
     let json = JSON.parse(image)
     console.log("裁剪页:", image)
-    if (json.pic) {
+    let topImg = json.topImg ?? {}
+    let deviceId = json.deviceId;
+    _this.data._deviceId = deviceId
+    if (topImg.pic) {
       this.hasImage(json)
     } else {
       console.log("没有图片")

+ 14 - 22
pages/piano/wallpaper/wallpaper.js

@@ -6,21 +6,6 @@ const { deviceWallPaper } = require('../../../request/deviceListRequest')
 import route_util from '../../../utils/route_util';
 import store from '../../../utils/store';
 
-// import EventManager from '../../utils/event_bus'
-
-
-// applicationType: "[0]"
-// createTime: "2025-01-10T09:36:04.000+00:00"
-// createTimeStr: null
-// id: "1877650605435129856"
-// name: null
-// note: ""
-// pic: "https://music-play.oss-cn-shenzhen.aliyuncs.com/device/20250110173600839933880.png"
-// sort: 1
-// status: 0
-// updateTime: "2025-01-10T09:36:04.000+00:00"
-
-
 Page({
   /**
    * 页面的初始数据
@@ -35,8 +20,10 @@ Page({
     image4List: [],
     _allImageList: [],
     _netImageList: [],
+    _device: {},
     selectIndex: 0,
     _localImgPicKey: "_localImgPicListKey",
+
     navbarData: {
       showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
       title: '壁纸设置', //导航栏 中间的标题
@@ -45,12 +32,12 @@ Page({
     useSystemNavBar: true // 设置为 true 以使用系统导航栏
 
   },
-  closePage() {
-
-
-  },
   footerTap() {
-    route_util.jumpParam("../cropper/cropper", JSON.stringify(this.data.topImg ?? {}))
+    let parma = {
+      "deviceId": this.data._device.deviceId,
+      "topImg": this.data.topImg
+    }
+    route_util.jumpParam("../cropper/cropper", JSON.stringify(parma))
   },
   imageTopTap() {
     this.setData({
@@ -83,14 +70,19 @@ Page({
     let _this = this;
     deviceWallPaper().then(res => {
       let data = res ?? []
+
+      data.sort((a, b) => a.sort - b.sort);
+
       // 网络的
       _this.data._netImageList = data;
 
+
+
       let localImgList = store.getStore(_this.data._localImgPicKey);
       console.log("壁纸列表", data.length, "空", localImgList);
       let allImgs = []
       if (localImgList) {
-        console.log("壁纸列表2", res.length, "空", JSON.stringify(localImgList), "空");
+        console.log("壁纸列表2", data, "空", JSON.stringify(localImgList), "空");
         allImgs = localImgList.concat(data)
       }
       _this.data._allImageList = allImgs;
@@ -172,7 +164,7 @@ Page({
     console.log(param)
 
     this.data._localImgPicKey = this.data._localImgPicKey + param.deviceId;
-
+    this.data._device = param
     this.wallpaperList();
 
 

+ 64 - 0
request/imageRequest.js

@@ -0,0 +1,64 @@
+// const { request } = require('../utils/util.js')
+import strings from '../utils/strings.js';
+
+module.exports = {
+
+    // 上传图片到服务器
+    uploadImage: (filePath, formData = {}) => {
+        try {
+            var value = wx.getStorageSync('token') || "";
+            if (!strings.isEmpty(value)) {
+                const resData = JSON.parse(value);
+                Authorization = resData.token;
+            }
+        } catch (e) {
+            Authorization = "";
+        }
+        console.log("Authorization:", Authorization)
+
+        return new Promise((resolve, reject) => {
+            // 拼接地址
+            var baseUrl = getApp().globalData.baseUrl;
+            let url = '/mini/wx/image/getUrl';
+            let fullurl = `${baseUrl}${url}`;
+            console.log("上传图片参数:", fullurl, filePath)
+            wx.uploadFile({
+                url: fullurl, // 替换为你的服务器地址
+                filePath: filePath,
+                name: 'file', // 文件对应的 key,后端需要根据这个 key 获取文件
+                formData: formData, // 其他需要上传的表单数据,
+                header: Object.assign({
+                    "content-type": "multipart/form-data",
+                    "appid": "wx08f94a3e90881910",
+                    "Authorization": Authorization
+                },),
+                responseType: 'arraybuffer',
+                success: (res) => {
+                    console.log("上传图片返回:", res);
+                    try {
+                        // 检查响应头中的 Content-Type
+                        const contentType = res.header['Content-Type'] || res.header['content-type'];
+                        console.log("上传图片返回2:", contentType);
+                        if (contentType && contentType.includes('image/jpeg')) {
+                            // 服务器返回的是图片数据
+                            console.log("上传图片返回3:", res.data);
+                            resolve(dataUrl);
+                        } else {
+                            // 服务器返回的不是图片数据
+                            const data = JSON.parse(res.data ?? "{}");
+                            resolve(data);
+                        }
+                    } catch (e) {
+                        reject(new Error('解析服务器响应失败', e));
+                    }
+                },
+                fail: (err) => {
+                    console.log("上传图片返回2:", err);
+
+                    reject(new Error('上传失败: ' + err.errMsg));
+                }
+            });
+        });
+    }
+
+}

File diff suppressed because it is too large
+ 0 - 472
utils/js_md5.js


+ 4 - 1
utils/lexin/add.js

@@ -56,8 +56,11 @@ function updateDeviceList(list, isInit, closeAllBlue) {
 
   const app = getApp();
   var finalList = [];
+  console.log("初始化蓝牙设备", list);
+
   if (isInit) {
-    list[0].state = "offline";
+    let device = list[0] ?? {};
+    device.state = "offline";
   }
 
   /// 让所有蓝牙设备离线