dingdanquanliucheng/frontend/mini-app/utils/badge.js

96 lines
2.2 KiB
JavaScript

/**
* TabBar 角标管理工具
* 负责查询待处理数量并在 TabBar 上显示角标提醒。
*/
var request = require("./request");
var auth = require("./auth");
/**
* 更新 TabBar 角标
* 根据当前用户角色查询待处理数量并显示角标
*/
function updateTabBadge() {
var app = getApp();
var role = app.globalData.userRole || auth.getUserRole();
if (role === "driver") {
_updateDriverBadge();
} else if (role === "admin" || role === "manager") {
_updateManagerBadge();
}
}
/**
* 更新管理层角色的 TabBar 角标
* 显示待审批订单数量
*/
function _updateManagerBadge() {
request({
url: "/api/orders?order_status=pending_approve&page_no=1&page_size=1",
})
.then(function (data) {
var count = data.total || 0;
if (count > 0) {
// 审批 Tab 在管理层的索引是 1
wx.setTabBarBadge({
index: 1,
text: count > 99 ? "99+" : String(count),
});
} else {
wx.removeTabBarBadge({ index: 1 });
}
})
.catch(function (err) {
console.warn("[Badge] 获取待审批数失败:", err);
});
}
/**
* 更新司机角色的 TabBar 角标
* 显示待处理任务数量
*/
function _updateDriverBadge() {
request({
url: "/api/driver/tasks",
})
.then(function (data) {
var tasks = data.tasks || data.list || [];
var pendingCount = tasks.filter(function (t) {
return t.status === "pending";
}).length;
if (pendingCount > 0) {
// 任务 Tab 在司机的索引是 0
wx.setTabBarBadge({
index: 0,
text: pendingCount > 99 ? "99+" : String(pendingCount),
});
} else {
wx.removeTabBarBadge({ index: 0 });
}
})
.catch(function (err) {
console.warn("[Badge] 获取待处理任务数失败:", err);
});
}
/**
* 清除所有角标
*/
function clearAllBadge() {
try {
wx.removeTabBarBadge({ index: 0 });
wx.removeTabBarBadge({ index: 1 });
wx.removeTabBarBadge({ index: 2 });
wx.removeTabBarBadge({ index: 3 });
} catch (e) {
// ignore
}
}
module.exports = {
updateTabBadge: updateTabBadge,
clearAllBadge: clearAllBadge,
};