283e6651aa
Co-authored-by: Cursor <cursoragent@cursor.com>
32 lines
1002 B
TypeScript
32 lines
1002 B
TypeScript
import { isHttpImageUrl } from '@dukang/weixin-sdk';
|
|
import { toastSuccess } from './toast';
|
|
import { isWechatEnv, weixinSdk } from './weixin';
|
|
|
|
export { isHttpImageUrl };
|
|
|
|
export function blobToDataUrl(blob: Blob): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
reader.onload = () => resolve(String(reader.result || ''));
|
|
reader.onerror = () => reject(new Error('读取图片失败'));
|
|
reader.readAsDataURL(blob);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 微信内用 previewImage 打开可公网访问的图片,用户长按即可保存。
|
|
* blob / data URL 不能走该接口。
|
|
*/
|
|
export async function previewSaveableImage(url: string): Promise<boolean> {
|
|
const href = url.trim();
|
|
if (!isWechatEnv() || !isHttpImageUrl(href)) return false;
|
|
try {
|
|
await weixinSdk.previewImage(href);
|
|
toastSuccess('请长按图片保存到相册');
|
|
return true;
|
|
} catch {
|
|
toastSuccess('请长按上方图片保存到相册');
|
|
return false;
|
|
}
|
|
}
|