283e6651aa
Co-authored-by: Cursor <cursoragent@cursor.com>
34 lines
1006 B
TypeScript
34 lines
1006 B
TypeScript
import { ensureJssdkReady } from './jssdk';
|
|
import type { WeixinSdkConfig } from './types';
|
|
|
|
export function isHttpImageUrl(url?: string | null): url is string {
|
|
return !!url && /^https?:\/\//i.test(url.trim());
|
|
}
|
|
|
|
/** 微信内预览图片,便于长按保存到相册(须是可公网访问的 http(s) 地址) */
|
|
export async function previewWechatImage(config: WeixinSdkConfig, url: string): Promise<void> {
|
|
const href = url.trim();
|
|
if (!isHttpImageUrl(href)) {
|
|
throw new Error('图片地址无效');
|
|
}
|
|
|
|
await ensureJssdkReady({
|
|
apiBase: config.apiBase ?? '/api/v1',
|
|
clientApp: config.clientApp,
|
|
getAccessToken: config.getAccessToken,
|
|
});
|
|
|
|
if (!window.wx?.previewImage) {
|
|
throw new Error('微信预览图片不可用');
|
|
}
|
|
|
|
await new Promise<void>((resolve, reject) => {
|
|
window.wx!.previewImage!({
|
|
current: href,
|
|
urls: [href],
|
|
success: () => resolve(),
|
|
fail: (res) => reject(new Error(res?.errMsg || '预览失败')),
|
|
});
|
|
});
|
|
}
|