|
|
@@ -20,3 +20,34 @@ export const copyByUniappApi = (data, msg = '已复制到剪贴板') => {
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+// 全局更新购物车角标
|
|
|
+export const updateCartBadge = () => {
|
|
|
+ // 检查是否登录,未登录不请求
|
|
|
+ const token = uni.getStorageSync('token');
|
|
|
+ if (!token) {
|
|
|
+ uni.removeTabBarBadge({ index: 2 });
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 确保 uView http 可用
|
|
|
+ if (uni.$u && uni.$u.http) {
|
|
|
+ uni.$u.http.get('/token/shop/cart/getCount').then(res => {
|
|
|
+ if (res.code == 200) {
|
|
|
+ const count = res.data;
|
|
|
+ if (count > 0) {
|
|
|
+ uni.setTabBarBadge({
|
|
|
+ index: 2,
|
|
|
+ text: String(count)
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ uni.removeTabBarBadge({
|
|
|
+ index: 2
|
|
|
+ });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).catch(e => {
|
|
|
+ console.log('更新购物车角标失败', e);
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|