wangzhibo
4 天以前 10595d8632f959d0954d1939243196f4eed02372
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import QRCode from "qrcode";
 
const QRCODE_KEY = "recycleQrcodeBase64";
 
export function generateQrcode(text: string): Promise<string> {
    return new Promise((resolve) => {
        QRCode.toDataURL(
            text,
            {
                width: 300,
                margin: 2,
            },
            (err, url) => {
                if (err) {
                    console.error("❌ 二维码生成失败", err);
                    resolve("");
                    return;
                }
                resolve(url);
            },
        );
    });
}
 
export function generateRecycleQrcode(text: string): Promise<string> {
    return new Promise((resolve) => {
        const cached = uni.getStorageSync(QRCODE_KEY);
        if (cached) return resolve(cached);
 
        generateQrcode(text).then((url) => {
            if (url) {
                uni.setStorageSync(QRCODE_KEY, url);
            }
            resolve(url);
        });
    });
}
 
/**
 * 清除指定二维码缓存
 * @param key
 */
export function clearQrcodeCache(key: string) {
    uni.removeStorageSync(key);
}
 
/**
 * 清除所有二维码缓存
 */
export function clearAllQrcodeCache() {
    uni.clearStorageSync();
}