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
import { onShow } from "@dcloudio/uni-app";
import { defineStore } from "pinia";
import { ref } from "vue";
 
// 缓存
export const useCache = defineStore("app.cache", () => {
    // 缓存大小
    const size = ref("0KB");
 
    // 获取缓存
    function get() {
        // #ifdef APP
        // @ts-ignore
        plus.cache.calculate(function (s: number) {
            //size是多少个字节单位是b
            if (s < 1024) {
                size.value = s + "B";
            } else if (s / 1024 >= 1 && s / 1024 / 1024 < 1) {
                size.value = Math.floor((s / 1024) * 100) / 100 + "KB";
            } else if (s / 1024 / 1024 >= 1) {
                size.value = Math.floor((s / 1024 / 1024) * 100) / 100 + "M";
            }
        });
        // #endif
    }
 
    // 清空缓存
    function clear() {
        // #ifdef APP
        // @ts-ignore
        plus.cache.clear(function () {
            get();
        });
        // #endif
    }
 
    onShow(() => {
        get();
    });
 
    return {
        size,
        get,
        clear,
    };
});