|
@@ -12,21 +12,42 @@ Page({
|
|
|
* 页面的初始数据
|
|
|
*/
|
|
|
data: {
|
|
|
+ navbarData: {
|
|
|
+ showCapsule: 1, //是否显示左上角图标 1表示显示 0表示不显示
|
|
|
+ title: '壁纸设置', //导航栏 中间的标题
|
|
|
+
|
|
|
+ },
|
|
|
src: "",
|
|
|
width: 320,//宽度
|
|
|
height: 320,//高度
|
|
|
-
|
|
|
- max_width: 320,
|
|
|
- max_height: 320,
|
|
|
- disable_rotate: true, //是否禁用旋转
|
|
|
- disable_ratio: true, //锁定比例
|
|
|
- limit_move: true, //是否限制移动
|
|
|
showProgress: false,
|
|
|
- _imageBuffer: null,
|
|
|
+ _chunks: [],
|
|
|
progress: 0,
|
|
|
+ _timer: null,
|
|
|
+ _imgUrl: null,
|
|
|
},
|
|
|
cropper: null,
|
|
|
-
|
|
|
+ callback() {
|
|
|
+ let that = this
|
|
|
+ if (this.data._chunks.length > 0) {
|
|
|
+ wx.showModal({
|
|
|
+ title: '保存图片中,确定要中断退出吗?',
|
|
|
+ content: '',
|
|
|
+ complete: (res) => {
|
|
|
+ if (res.confirm) {
|
|
|
+ that.endImage(2)
|
|
|
+ wx.navigateBack({
|
|
|
+ delta: 1,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ wx.navigateBack({
|
|
|
+ delta: 1,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ },
|
|
|
// 图片转RGB565
|
|
|
convertToRGB565(imageData) {
|
|
|
const { data, width, height } = imageData;
|
|
@@ -63,15 +84,15 @@ Page({
|
|
|
},
|
|
|
upload() {
|
|
|
let that = this;
|
|
|
- wx.chooseImage({
|
|
|
+ wx.chooseMedia({
|
|
|
count: 1,
|
|
|
- sizeType: ['original', 'compressed'],
|
|
|
+ mediaType: ['image'],
|
|
|
sourceType: ['album'],
|
|
|
success(res) {
|
|
|
wx.showLoading({
|
|
|
title: '加载中',
|
|
|
})
|
|
|
- const tempFilePaths = res.tempFilePaths[0];
|
|
|
+ const tempFilePaths = res.tempFiles[0].tempFilePath;
|
|
|
//重置图片角度、缩放、位置
|
|
|
that.cropper.imgReset();
|
|
|
that.setData({
|
|
@@ -89,8 +110,8 @@ Page({
|
|
|
console.log("clickcut:", e.detail);
|
|
|
//图片预览
|
|
|
wx.previewImage({
|
|
|
- current: e.detail.url, // 当前显示图片的http链接
|
|
|
- urls: [e.detail.url] // 需要预览的图片http链接列表
|
|
|
+ current: e.detail.url,
|
|
|
+ urls: [e.detail.url]
|
|
|
})
|
|
|
},
|
|
|
cancel() {
|
|
@@ -104,10 +125,10 @@ Page({
|
|
|
title: '图片裁剪中',
|
|
|
})
|
|
|
const fs = wx.getFileSystemManager();
|
|
|
-
|
|
|
this.cropper.getImg((obj) => {
|
|
|
// app.globalData.imgSrc = obj.url;
|
|
|
console.log("裁剪图片:", obj);
|
|
|
+ _this.data._imgUrl = obj.url
|
|
|
fs.readFile({
|
|
|
filePath: obj.url,
|
|
|
encoding: '', // 不指定编码以获取原始二进制数据
|
|
@@ -118,13 +139,16 @@ Page({
|
|
|
height: obj.height,
|
|
|
data: res.data
|
|
|
})
|
|
|
- _this.data._imageBuffer = rgbData;
|
|
|
- console.log("下载文件成功2:", _this.data._imageBuffer)
|
|
|
+ console.log("下载文件成功2:", rgbData.byteLength)
|
|
|
+ _this.data._imageBufferLength = rgbData.byteLength
|
|
|
wx.hideLoading();
|
|
|
wx.showLoading({
|
|
|
title: '开始传输图片',
|
|
|
})
|
|
|
- _this.startImage();
|
|
|
+ _this.startImage(rgbData);
|
|
|
+ // 测试
|
|
|
+ // _this.sendImage()
|
|
|
+ // _this.startProgress()
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
console.error('读取 .bin 文件失败:', err);
|
|
@@ -135,11 +159,11 @@ Page({
|
|
|
// _this.downloadAndSaveFile("https://music-play.oss-cn-shenzhen.aliyuncs.com/backOss/file/6a909799a6924e6f86a4683e6da4fad4.bin");
|
|
|
// 55kb的
|
|
|
// _this.downloadAndSaveFile("https://music-play.oss-cn-shenzhen.aliyuncs.com/backOss/file/55d2dd22bd554eb19b71536bec4ba42c.bin");
|
|
|
+ // _this.downloadAndSaveFile("https://music-play.oss-cn-shenzhen.aliyuncs.com/backOss/file/7498f0c94c5646d0a4648f313a2fa05c.bin");
|
|
|
|
|
|
},
|
|
|
// 下载网络文件并保存到本地
|
|
|
downloadAndSaveFile(url) {
|
|
|
- // https://music-play.oss-cn-shenzhen.aliyuncs.com/backOss/file/6a909799a6924e6f86a4683e6da4fad4.bin
|
|
|
const fs = wx.getFileSystemManager();
|
|
|
let _this = this;
|
|
|
wx.downloadFile({
|
|
@@ -155,14 +179,12 @@ Page({
|
|
|
success: (res) => {
|
|
|
console.log("下载文件成功:", res.data)
|
|
|
let rgbData = res.data
|
|
|
- _this.data._imageBuffer = rgbData;
|
|
|
- // console.log("下载文件成功2:", _this.data._imageBuffer)
|
|
|
|
|
|
wx.hideLoading();
|
|
|
wx.showLoading({
|
|
|
title: '开始传输图片',
|
|
|
})
|
|
|
- _this.startImage();
|
|
|
+ _this.startImage(rgbData);
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
console.error('读取 .bin 文件失败:', err);
|
|
@@ -197,7 +219,7 @@ Page({
|
|
|
console.log('用户拒绝授权访问相册');
|
|
|
wx.showModal({
|
|
|
title: '提示',
|
|
|
- content: '您拒绝了访问相册的权限,请在设置中手动开启',
|
|
|
+ content: '您拒绝了访问相册的权限,无法上传图片',
|
|
|
showCancel: false,
|
|
|
success(res) {
|
|
|
if (res.confirm) {
|
|
@@ -207,6 +229,7 @@ Page({
|
|
|
if (settingRes.authSetting['scope.writePhotosAlbum']) {
|
|
|
console.log('用户已在设置中开启访问相册的权限');
|
|
|
// 可以在这里执行访问相册的操作
|
|
|
+ _this.upload(); //上传图片
|
|
|
} else {
|
|
|
console.log('用户仍未授权访问相册');
|
|
|
}
|
|
@@ -225,7 +248,9 @@ Page({
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
- async startImage() {
|
|
|
+ async startImage(imageBuffer) {
|
|
|
+ this.sliceDataIntoChunks(imageBuffer, 32);
|
|
|
+
|
|
|
BtHelper.sendCallBack(BtCmd.wallPaper(1), function (res) {
|
|
|
if (!res) {
|
|
|
wx.hideLoading()
|
|
@@ -244,22 +269,21 @@ Page({
|
|
|
const chunk = data.slice(i, i + chunkSize);
|
|
|
chunks.push(chunk);
|
|
|
}
|
|
|
- return chunks;
|
|
|
+ this.data._chunks = chunks;
|
|
|
},
|
|
|
- async sendImage(imageBuffer, index) {
|
|
|
+
|
|
|
+ async sendImage() {
|
|
|
let _this = this
|
|
|
|
|
|
wx.hideLoading()
|
|
|
- console.log("发送图片数据0:", index, imageBuffer.byteLength)
|
|
|
- // console.log("发送图片数据0:", index, imageBuffer.length)
|
|
|
- let chunks = this.sliceDataIntoChunks(imageBuffer, 16);
|
|
|
-
|
|
|
+ let chunks = this.data._chunks
|
|
|
let next = 0;
|
|
|
- let total = imageBuffer.byteLength;
|
|
|
- // let total = imageBuffer.length;
|
|
|
+ let total = this.data._imageBufferLength;
|
|
|
let btHelper = BtHelper.getInstance();
|
|
|
- for (let i = 0; i < chunks.length; i++) {
|
|
|
+ let i = 0;
|
|
|
+ this._timer = setInterval(async () => {
|
|
|
const chunk = chunks[i];
|
|
|
+ i++;
|
|
|
next += chunk.byteLength;
|
|
|
// next += chunk.length;
|
|
|
let uint8Array = new Uint8Array(chunk);
|
|
@@ -269,15 +293,31 @@ Page({
|
|
|
console.log("发送图片数据1:", i, next, total, unit16)
|
|
|
_this.updateProgress(next, total);
|
|
|
|
|
|
- await _this.delay(20);
|
|
|
let res = await btHelper.wallPaperSyncData(chunk);
|
|
|
+ // let res = true;
|
|
|
// btHelper.wallPaperData(chunk);
|
|
|
|
|
|
let nowDate = Date.now()
|
|
|
if (i === chunks.length - 1 && res) {
|
|
|
wx.showModal({
|
|
|
title: '图片上传成功' + i + " / " + nowDate,
|
|
|
- showCancel: false
|
|
|
+ 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,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
})
|
|
|
_this.endImage(0)
|
|
|
|
|
@@ -286,15 +326,28 @@ Page({
|
|
|
title: '图片上传失败',
|
|
|
showCancel: false
|
|
|
})
|
|
|
+
|
|
|
_this.endImage(2)
|
|
|
}
|
|
|
- }
|
|
|
+ }, 20);
|
|
|
},
|
|
|
async delay(ms) {
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
},
|
|
|
endImage(value) {
|
|
|
+ let _this = this
|
|
|
+ console.log("结束图片上传:", value)
|
|
|
+ if (_this.data._timer) {
|
|
|
+ clearInterval(_this.data._timer)
|
|
|
+ _this.data._timer = null
|
|
|
+ }
|
|
|
+ _this.data._chunks = []
|
|
|
BtHelper.getInstance().wallPaper(value);
|
|
|
+ if (value === 0) {
|
|
|
+
|
|
|
+ } else { // 获取页面栈
|
|
|
+
|
|
|
+ }
|
|
|
},
|
|
|
startProgress() {
|
|
|
this.setData({
|
|
@@ -303,7 +356,7 @@ Page({
|
|
|
})
|
|
|
},
|
|
|
updateProgress(chunk, total) {
|
|
|
- let progress = chunk / total;
|
|
|
+ let progress = parseFloat((chunk / total * 100).toFixed(2));
|
|
|
let _this = this
|
|
|
if (chunk >= total) {
|
|
|
_this.setData({
|
|
@@ -345,17 +398,20 @@ Page({
|
|
|
|
|
|
if (otaCmd === 1 && kind == 1) {
|
|
|
// 开始发送
|
|
|
- _this.sendImage(_this.data._imageBuffer, 0)
|
|
|
+ _this.sendImage()
|
|
|
_this.startProgress()
|
|
|
} else if (otaCmd === 0 && kind == 1) {
|
|
|
// 发送结束
|
|
|
- // _this.endImage(0)
|
|
|
} else if (kind == 0) {
|
|
|
wx.hideLoading()
|
|
|
wx.showModal({
|
|
|
title: '图片上传失败了',
|
|
|
showCancel: false
|
|
|
})
|
|
|
+ if (_this.data._timer) {
|
|
|
+ clearInterval(_this.data._timer)
|
|
|
+ _this.data._timer = null
|
|
|
+ }
|
|
|
}
|
|
|
break;
|
|
|
default:
|