diff --git a/common/manageServer.js b/common/manageServer.js index 5d7885f..aa23bdd 100644 --- a/common/manageServer.js +++ b/common/manageServer.js @@ -1,4 +1,4 @@ -import { goto } from "./untool"; +import { goto, getImageTempFilePath } from "./untool"; import { showToast, showLoading, hideLoading, downloadFile } from "./untool" import { setSessionXToken, getSessionXToken } from "@/session" import { textEncoder, textDecoder } from "./util" @@ -77,7 +77,8 @@ async function handleResponse({ // 图片类型数据特殊处理 if((header['content-type'] || header['Content-Type'])?.indexOf?.("image/") > -1) { const base64 = "data:image/png;base64," + uni.arrayBufferToBase64(data) - resolve({ data: { fileUrl: base64 } }) + let fileUrl = await getImageTempFilePath(base64) + resolve({ data: { fileUrl } }) return } data = data || {} diff --git a/common/untool.js b/common/untool.js index f25cd68..6ad601d 100644 --- a/common/untool.js +++ b/common/untool.js @@ -142,6 +142,68 @@ const saveFile = ({ tempFilePath })=> { }); }) } + +const getImageInfo = (src)=> { + return new Promise(async (resolve, reject)=> { + uni.getImageInfo({ + src, + success(res) { + resolve(res) + }, + fail(e) { + console.log("获取图片信息失败=======", e) + reject(e) + } + }) + }) +} + +// 微信小程序 有该api +const writeFile = (params = {}, fileEndFix = '.png')=> { + return new Promise((resolve, reject)=> { + //声明文件系统 + const fs = wx.getFileSystemManager(); + var times = new Date().getTime(); + var filePath = wx.env.USER_DATA_PATH + '/' + times + fileEndFix; + //将base64图片写入 + var that = this; + fs.writeFile({ + filePath: filePath, + ...params, + success: (res) => { + resolve({ filePath }) + }, + fail: (err)=> { + console.log("writeFile:错误==", err) + reject(err) + } + }); + }) +} + +const getImageTempFilePath = (base64File) => { + return new Promise(async (resolve, reject) => { + let tempFile = base64File + + // // #ifdef MP-WEIXIN + // let wxRes = await writeFile({ data: base64File, encoding: 'base64' }) + // tempFile = wxRes.filePath + // // #endif + + // // #ifdef H5 + // let webRes = await downloadFile(base64File) + // tempFile = webRes.tempFilePath + // // #endif + + // // #ifdef APP-PLUS + // let appRes = await downloadFile(tempFile) + // console.log("===appRes===", appRes) + // // #endif + + resolve(tempFile) + }) + +} export { @@ -158,4 +220,5 @@ export { downloadFile, saveNetFile, saveFile, + getImageTempFilePath, } diff --git a/components/tpx-bluetooth-print/tpx-bluetooth-print.vue b/components/tpx-bluetooth-print/tpx-bluetooth-print.vue index 69eba2f..41cf074 100644 --- a/components/tpx-bluetooth-print/tpx-bluetooth-print.vue +++ b/components/tpx-bluetooth-print/tpx-bluetooth-print.vue @@ -132,6 +132,10 @@ export default { showToast('获取图片信息异常:getImageZip') reject('获取图片信息异常:getImageZip') } + }, + fail(err){ + showToast('获取图片信息异常:getImageZip') + console.log("canvasGetImageData:失败====", err); } }) }) @@ -146,14 +150,17 @@ export default { uni.getImageInfo({ src: filePath, success(res) { + console.log("获取图片信息成功=====", res) // 按原图比例算出 新的画图高度 let height = that.canvasHeight if(!height) { height = parseInt(that.canvasWidth * (res.height / res.width)) that.$emit("update:canvasHeight", height) } - cxt.drawImage(filePath, 0, 0, that.canvasWidth, that.canvasHeight); + cxt.drawImage(res.path, 0, 0, that.canvasWidth, that.canvasHeight); + console.log("开始绘制图片===") cxt.draw(true, ()=> { + console.log("绘制图片结束===") setTimeout( async ()=> { let data = await that.getImageZip({ canvasId: that.canvasId, width: that.canvasWidth, height, }) resolve(data) @@ -161,8 +168,8 @@ export default { }); }, fail(e) { + console.log("获取图片信息失败=====", e) reject(e) - console.log(e) } }) })