170 lines
5.0 KiB
JavaScript
170 lines
5.0 KiB
JavaScript
/**
|
|
* 管理层仪表盘页面
|
|
* 职责:展示待审批和待发厂订单数量统计,以及最近订单列表。
|
|
*/
|
|
|
|
var badge = require("../../../utils/badge");
|
|
|
|
function mapOrderStatus(status) {
|
|
var app = getApp();
|
|
return app.mapOrderStatus(status);
|
|
}
|
|
|
|
function getStatusBadge(status) {
|
|
var map = {
|
|
pending_approve: "badge-orange", cancel_pending: "badge-orange",
|
|
approved: "badge-green",
|
|
pending_driver: "badge-blue", accepted: "badge-blue",
|
|
picked_up: "badge-blue", delivered: "badge-green",
|
|
rejected: "badge-red", canceled: "badge-red",
|
|
};
|
|
return map[status] || "badge-gray";
|
|
}
|
|
|
|
Page({
|
|
data: {
|
|
statusBarHeight: 20,
|
|
loading: true,
|
|
message: "",
|
|
summary: { pendingApproveCount: 0, pendingDriverCount: 0 },
|
|
recentOrders: [],
|
|
serviceBound: true,
|
|
},
|
|
|
|
// 提醒事件处理函数
|
|
_onReminder: null,
|
|
_serviceBindPromptShown: false,
|
|
|
|
onPullDownRefresh: function () {
|
|
this.loadDashboard().then(function () { wx.stopPullDownRefresh(); });
|
|
},
|
|
|
|
async onShow() {
|
|
var app = getApp();
|
|
this.setData({ statusBarHeight: app.globalData.statusBarHeight });
|
|
var tab = this.getTabBar();
|
|
if (tab) {
|
|
tab.setData({ selected: 0 });
|
|
tab.updateRole(app.globalData.userRole);
|
|
}
|
|
await this.loadDashboard();
|
|
await this.checkServiceBind();
|
|
// 更新TabBar角标
|
|
badge.updateTabBadge();
|
|
|
|
// 监听 WebSocket 提醒事件
|
|
var that = this;
|
|
this._onReminder = function () {
|
|
console.log("[Dashboard] 收到提醒,刷新数据");
|
|
that.loadDashboard();
|
|
badge.updateTabBadge();
|
|
};
|
|
app.on("reminder", this._onReminder);
|
|
},
|
|
|
|
onHide: function () {
|
|
// 页面隐藏时移除监听
|
|
var app = getApp();
|
|
if (this._onReminder) {
|
|
app.off("reminder", this._onReminder);
|
|
this._onReminder = null;
|
|
}
|
|
},
|
|
|
|
onUnload: function () {
|
|
// 页面卸载时移除监听
|
|
var app = getApp();
|
|
if (this._onReminder) {
|
|
app.off("reminder", this._onReminder);
|
|
this._onReminder = null;
|
|
}
|
|
},
|
|
|
|
async loadDashboard() {
|
|
var app = getApp();
|
|
this.setData({ loading: true, message: "" });
|
|
try {
|
|
var results = await Promise.all([
|
|
app.request({ url: "/api/orders?order_status=pending_approve&page_no=1&page_size=50", method: "GET" }),
|
|
]);
|
|
var pendingList = results[0].list || [];
|
|
var recentOrders = pendingList.slice(0, 8).map(function (item) {
|
|
return {
|
|
orderId: item.order_id,
|
|
orderNo: item.order_no,
|
|
customerName: item.customer_name,
|
|
statusText: mapOrderStatus(item.order_status),
|
|
badgeClass: getStatusBadge(item.order_status),
|
|
amount: Number(item.contract_amount || item.sale_price_total || 0).toFixed(2),
|
|
createdAt: item.created_at ? item.created_at.slice(0, 10) : "",
|
|
};
|
|
});
|
|
this.setData({
|
|
summary: { pendingApproveCount: pendingList.length, pendingDriverCount: 0 },
|
|
recentOrders: recentOrders,
|
|
});
|
|
} catch (error) {
|
|
this.setData({ message: error.message || "加载失败" });
|
|
} finally {
|
|
this.setData({ loading: false });
|
|
}
|
|
},
|
|
|
|
async checkServiceBind() {
|
|
var app = getApp();
|
|
var rolesNeedBinding = ["admin", "manager", "salesman"];
|
|
if (!app.globalData.authToken || rolesNeedBinding.indexOf(app.globalData.userRole) === -1) return true;
|
|
try {
|
|
var res = await app.request({ url: "/api/auth/service-bind-status" });
|
|
var bound = !!res.bound;
|
|
this.setData({ serviceBound: bound });
|
|
if (!bound && !this._serviceBindPromptShown) {
|
|
this._serviceBindPromptShown = true;
|
|
this.promptBindService();
|
|
}
|
|
return bound;
|
|
} catch (e) {
|
|
return this.data.serviceBound !== false;
|
|
}
|
|
},
|
|
|
|
promptBindService: function () {
|
|
wx.showModal({
|
|
title: "请关注服务号",
|
|
content: "登录后请先关注并绑定微信服务号,以便接收客户和订单提醒。",
|
|
confirmText: "去绑定",
|
|
confirmColor: "#2563eb",
|
|
success: function (res) {
|
|
if (res.confirm) {
|
|
wx.navigateTo({ url: "/pages/bind-service/bind-service" });
|
|
}
|
|
},
|
|
});
|
|
},
|
|
|
|
goApproveList: function () { wx.switchTab({ url: "/pages/manager/approve-list/approve-list" }); },
|
|
goSupplierText: function () { wx.navigateTo({ url: "/pages/manager/supplier-text/supplier-text" }); },
|
|
goTaskCreate: function () { wx.navigateTo({ url: "/pages/manager/task-create/task-create" }); },
|
|
goOrderList: function () { wx.switchTab({ url: "/pages/manager/order-list/order-list" }); },
|
|
|
|
handleOrderTap: function (event) {
|
|
var id = event.currentTarget.dataset.id;
|
|
wx.navigateTo({ url: "/pages/manager/approve-detail/approve-detail?id=" + id });
|
|
},
|
|
|
|
// 分享给好友
|
|
onShareAppMessage: function () {
|
|
return {
|
|
title: "订单全流程管理系统",
|
|
path: "/pages/manager/dashboard/dashboard",
|
|
};
|
|
},
|
|
|
|
// 分享到朋友圈
|
|
onShareTimeline: function () {
|
|
return {
|
|
title: "订单全流程管理系统",
|
|
};
|
|
},
|
|
});
|