From abca5bd2f90f30c94fdba1dcec2cf701c64f96e6 Mon Sep 17 00:00:00 2001 From: BTMuli Date: Thu, 2 Nov 2023 11:07:44 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20=E4=BF=AE=E5=A4=8D=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E4=BF=9D=E5=AD=98=E5=A4=B1=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fix #56 --- src/utils/TGClient.ts | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/utils/TGClient.ts b/src/utils/TGClient.ts index b47bad85..77ae1e34 100644 --- a/src/utils/TGClient.ts +++ b/src/utils/TGClient.ts @@ -1,7 +1,7 @@ /** * @file utils/TGClient.ts * @desc 负责米游社客户端的 callback 处理 - * @since Beta v0.3.4 + * @since Beta v0.3.5 */ import { event, invoke, path } from "@tauri-apps/api"; @@ -420,7 +420,7 @@ class TGClient { /** * @func onClickImg - * @since Beta v0.3.4 + * @since Beta v0.3.5 * @desc 点击图片,下载到本地 * @param {unknown} payload - 请求参数 * @returns {void} - 无返回值 @@ -428,24 +428,27 @@ class TGClient { async onClickImg(payload: any): Promise { const url = payload.image_list[0].url; const savePath = `${await path.downloadDir()}${path.sep}${Date.now().toString()}.png`; - const executeJS = - "javascript:(function(){" + - " window.__TAURI__.dialog.save({" + - " title: '保存图片'," + - " filters: [{ name: '图片', extensions: ['png'] }]," + - ` defaultPath: '${savePath}',` + - " }).then((res) => {" + - " fetch('" + - url + - "')" + - " .then((response) => response.blob())" + - " .then((blob) => {" + - " window.__TAURI__.fs.writeBinaryFile(res, blob).then(() => {" + - " alert('保存成功');" + - " });" + - " });" + - " });" + - "})();"; + const executeJS = `javascript:(async function() { + const _t = window.__TAURI__; + const savePath = await _t.dialog.save({ + title: '保存图片', + filters: [{ name: '图片', extensions: ['png'] }], + defaultPath: '${savePath}', + }); + if (savePath) { + const resBlob = await _t.http.fetch('${url}',{ + method: 'GET', + responseType: _t.http.ResponseType.Binary + }); + const buffer = new Uint8Array(resBlob.data); + const blob = new Blob([buffer], { type: 'image/png' }); + await _t.fs.writeBinaryFile({ + contents: blob, + path: savePath, + }); + alert('保存成功'); + } + })();`; await invoke("execute_js", { label: "mhy_client", js: executeJS }); } }