曹睿
2025-04-25 ec1bef3a37e8dcdf22f1bf52e7c272a18306f4b9
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
<template>
  <view class="settings-container">
    <wd-cell-group>
      <wd-cell title="账号和安全" icon="secured" is-link @click="navigateToAccount" />
      <wd-cell title="主题设置" icon="brush" is-link @click="navigateToTheme" />
      <wd-cell title="用户协议" icon="user" is-link @click="navigateToUserAgreement" />
      <wd-cell title="隐私政策" icon="folder" is-link @click="navigateToPrivacy" />
    </wd-cell-group>
 
    <wd-cell-group custom-style="margin-top:40rpx">
      <wd-cell title="网络测试" icon="wifi" is-link @click="navigateToNetworkTest" />
      <wd-cell
        title="清空缓存"
        icon="delete1"
        :value="cacheSize"
        clickable
        @click="handleClearCache"
      />
    </wd-cell-group>
 
    <wd-cell-group v-if="isLogin" custom-style="margin-top:40rpx">
      <wd-cell title="退出" icon="logout" is-link @click="handleLogout" />
    </wd-cell-group>
 
    <!-- 全屏 loading -->
    <view v-if="clearing" class="loading-mask">
      <view class="loading-content">
        <view class="loading-icon" />
        <text class="loading-text">正在清理...</text>
      </view>
    </view>
  </view>
</template>
 
<script lang="ts" setup>
import { useUserStore } from "@/store/modules/user";
import { checkLogin } from "@/utils/auth";
 
const userStore = useUserStore();
 
const isLogin = computed(() => !!userStore.userInfo);
 
// 账号和安全
const navigateToAccount = () => {
  uni.navigateTo({ url: "/pages/mine/settings/account/index" });
};
 
// 用户协议
const navigateToUserAgreement = () => {
  uni.navigateTo({ url: "/pages/mine/settings/agreement/index" });
};
// 隐私政策
const navigateToPrivacy = () => {
  uni.navigateTo({ url: "/pages/mine/settings/privacy/index" });
};
// 主题设置
const navigateToTheme = () => {
  uni.navigateTo({ url: "/pages/mine/settings/theme/index" });
};
// 网络测试
const navigateToNetworkTest = () => {
  uni.navigateTo({ url: "/pages/mine/settings/network/index" });
};
 
// 是否正在清理
const clearing = ref(false);
// 缓存大小
const cacheSize = ref<any>("计算中...");
// 获取缓存大小
const getCacheSize = async () => {
  try {
    // #ifdef MP-WEIXIN
    const res = await uni.getStorageInfo();
    cacheSize.value = formatSize(res.currentSize);
    // #endif
    // #ifdef H5
    cacheSize.value = formatSize(
      Object.keys(localStorage).reduce((size, key) => size + localStorage[key].length, 0)
    );
    // #endif
    if (!cacheSize.value) {
      cacheSize.value = "0B";
    }
  } catch (error) {
    console.error("获取缓存大小失败:", error);
    cacheSize.value = "获取失败";
  }
};
 
// 格式化存储大小
const formatSize = (size: number) => {
  if (size < 1024) {
    return size + "B";
  } else if (size < 1024 * 1024) {
    return (size / 1024).toFixed(2) + "KB";
  } else {
    return (size / 1024 / 1024).toFixed(2) + "MB";
  }
};
 
// 处理清除缓存
const handleClearCache = async () => {
  if (cacheSize.value === "获取失败") {
    uni.showToast({
      title: "获取缓存信息失败,请稍后重试",
      icon: "none",
      duration: 2000,
    });
    return;
  }
  if (cacheSize.value === "0B") {
    uni.showToast({
      title: "暂无缓存需要清理",
      icon: "none",
      duration: 2000,
    });
    return;
  }
  if (clearing.value) {
    return;
  }
 
  try {
    clearing.value = true;
    // 模拟清理过程
    await new Promise((resolve) => setTimeout(resolve, 1500));
    // 清除缓存
    await uni.clearStorage();
    // 更新缓存大小显示
    await getCacheSize();
    // 提示清理成功
    uni.showToast({
      title: "清理成功",
      icon: "success",
    });
  } catch {
    uni.showToast({
      title: "清理失败",
      icon: "error",
    });
  } finally {
    clearing.value = false;
  }
};
 
// 退出登录
const handleLogout = () => {
  userStore.logout().then(() => {
    uni.showToast({ title: "已退出登录", icon: "success" });
    // 跳转到登录页
    uni.reLaunch({ url: "/pages/mine/index" });
  });
};
 
// 检查登录状态
onLoad(() => {
  if (!checkLogin()) return;
 
  getCacheSize();
});
</script>
<style lang="scss" scoped>
.settings-container {
  padding: 20px;
 
  .loading-mask {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: rgba(0, 0, 0, 0.4);
 
    .loading-content {
      padding: 30rpx 40rpx;
      text-align: center;
      background-color: rgba(255, 255, 255, 0.95);
      border-radius: 12rpx;
      box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15);
 
      .loading-icon {
        width: 60rpx;
        height: 60rpx;
        margin: 0 auto 20rpx;
        border: 4rpx solid #f3f3f3;
        border-top: 4rpx solid #409eff;
        border-radius: 50%;
        animation: spin 1s linear infinite;
      }
 
      .loading-text {
        font-size: 28rpx;
        font-weight: 500;
        color: #333;
      }
    }
  }
}
</style>