@@ -1,5 +1,13 @@
|
||||
import { createPage, request, ensureRoleOnCenterPage } from '../../utils/base-page.js';
|
||||
import { getOrderStatusText } from '../../utils/api-helper.js';
|
||||
import {
|
||||
STAFF_API, isStaffMode, isMerchantPortalUser, refreshStaffContext, syncStaffUi,
|
||||
restoreStaffContextAfterAuth, applyStaffStatsToPage, getStaffContext,
|
||||
} from '../../utils/staff-api.js';
|
||||
import { ensurePhoneAuth } from '../../utils/phone-auth.js';
|
||||
import { fetchGonggaoLunbo, isGonggaoCacheValid } from '../../utils/display-config';
|
||||
import { ICON_KEYS, resolveMiniappIcon } from '../../utils/miniapp-icons.js';
|
||||
import { fetchMerchantOrderStats, applyOrderStatsToHome } from '../../utils/merchant-order-stats.js';
|
||||
|
||||
const app = getApp();
|
||||
|
||||
@@ -12,46 +20,106 @@ Page(createPage({
|
||||
statusBar: 20,
|
||||
navBar: 44,
|
||||
isShangjia: false,
|
||||
isStaffMode: false,
|
||||
staffRoleName: '',
|
||||
quotaAvailable: '0.00',
|
||||
canDispatch: true,
|
||||
canStaffManage: true,
|
||||
lunboList: [],
|
||||
lunbozhanwei: '/images/lunbozhanwei.jpg',
|
||||
gonggao: '',
|
||||
swiperCurrent: 0,
|
||||
imgUrls: {
|
||||
noticeIco: 'https://bintao.xmxym88.com/xcx/bintao/38.png',
|
||||
statCardBg: 'https://bintao.xmxym88.com/xcx/bintao/3.png',
|
||||
kefuKeyBtn: 'https://bintao.xmxym88.com/xcx/bintao/13.png',
|
||||
kefuListBtn: 'https://bintao.xmxym88.com/xcx/bintao/21.png',
|
||||
noticeIco: '',
|
||||
statCardBg: '',
|
||||
kefuKeyBtn: '',
|
||||
kefuListBtn: '',
|
||||
iconRegular: '',
|
||||
iconCustom: '',
|
||||
},
|
||||
stats: {
|
||||
pendingCount: 0,
|
||||
pendingAmount: '0.00',
|
||||
todayDispatch: 0,
|
||||
todayRefund: 0,
|
||||
completedCount: 0,
|
||||
completedAmount: '0.00',
|
||||
refundCount: 0,
|
||||
refundAmount: '0.00',
|
||||
dispatchCount: 0,
|
||||
dispatchAmount: '0.00',
|
||||
},
|
||||
recentOrders: [],
|
||||
recentLoading: false,
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
async onLoad() {
|
||||
const sys = wx.getSystemInfoSync();
|
||||
this.setData({
|
||||
statusBar: sys.statusBarHeight || 20,
|
||||
navBar: (sys.statusBarHeight || 20) + 44,
|
||||
lunbozhanwei: app.globalData.lunbozhanwei || '/images/lunbozhanwei.jpg',
|
||||
});
|
||||
if (wx.getStorageSync('token')) {
|
||||
await restoreStaffContextAfterAuth();
|
||||
}
|
||||
this.checkRole();
|
||||
this.waitForConfigAndLoadBanner();
|
||||
syncStaffUi(this);
|
||||
this.setupImageUrls();
|
||||
await this.waitForConfigAndLoadBanner();
|
||||
if (this.data.isShangjia) {
|
||||
await this.loadDashboard(false);
|
||||
}
|
||||
this._merchantHomeSessionReady = true;
|
||||
this._skipShowRefresh = true;
|
||||
this.checkColdStartPopup('shangjiaduan');
|
||||
},
|
||||
|
||||
onShow() {
|
||||
async onShow() {
|
||||
if (!app.globalData._shangjiaPhoneChecked) {
|
||||
const phoneOk = await ensurePhoneAuth({ redirect: true, role: 'shangjia' });
|
||||
if (!phoneOk) return;
|
||||
app.globalData._shangjiaPhoneChecked = true;
|
||||
}
|
||||
|
||||
this.registerNotificationComponent();
|
||||
// 先用本地缓存渲染,再向服务端同步子客服身份
|
||||
this.checkRole();
|
||||
syncStaffUi(this);
|
||||
if (app.globalData.ossImageUrl || app.globalData.miniappIcons) {
|
||||
this.setupImageUrls();
|
||||
}
|
||||
const token = wx.getStorageSync('token');
|
||||
const isOwner = Number(wx.getStorageSync('shangjiastatus')) === 1;
|
||||
if (token && !isOwner) {
|
||||
await restoreStaffContextAfterAuth();
|
||||
this.checkRole();
|
||||
syncStaffUi(this);
|
||||
} else if (isStaffMode()) {
|
||||
await refreshStaffContext(request);
|
||||
this.checkRole();
|
||||
syncStaffUi(this);
|
||||
applyStaffStatsToPage(this, getStaffContext());
|
||||
}
|
||||
|
||||
if (this.data.isShangjia) {
|
||||
ensureRoleOnCenterPage(this, 'shangjia');
|
||||
this.loadDashboard();
|
||||
}
|
||||
this.loadBannerData();
|
||||
|
||||
if (this._skipShowRefresh) {
|
||||
this._skipShowRefresh = false;
|
||||
return;
|
||||
}
|
||||
|
||||
const silent = this._merchantHomeSessionReady;
|
||||
if (this.data.isShangjia) {
|
||||
this.loadDashboard(silent);
|
||||
}
|
||||
if (silent) {
|
||||
this.loadBannerData(true, true);
|
||||
} else {
|
||||
this.loadBannerData(false);
|
||||
}
|
||||
},
|
||||
|
||||
onPullDownRefresh() {
|
||||
@@ -65,10 +133,36 @@ Page(createPage({
|
||||
|
||||
waitForConfigAndLoadBanner() {
|
||||
if (app.globalData.ossImageUrl || app.globalData.apiBaseUrl) {
|
||||
this.loadBannerData(true);
|
||||
return;
|
||||
this.setupImageUrls();
|
||||
return this.loadBannerData(true);
|
||||
}
|
||||
setTimeout(() => this.waitForConfigAndLoadBanner(), 100);
|
||||
return new Promise((resolve) => {
|
||||
const tick = () => {
|
||||
if (app.globalData.ossImageUrl || app.globalData.apiBaseUrl) {
|
||||
this.setupImageUrls();
|
||||
this.loadBannerData(true).finally(resolve);
|
||||
} else {
|
||||
setTimeout(tick, 100);
|
||||
}
|
||||
};
|
||||
tick();
|
||||
});
|
||||
},
|
||||
|
||||
setupImageUrls() {
|
||||
const ossBase = app.globalData.ossImageUrl || '';
|
||||
const homeDir = `${ossBase}beijing/shangjiaduan/home/`;
|
||||
const icon = (key, fallback) => resolveMiniappIcon(app, key, fallback);
|
||||
this.setData({
|
||||
imgUrls: {
|
||||
noticeIco: icon(ICON_KEYS.MERCHANT_HOME_NOTICE, `${homeDir}notice.png`),
|
||||
statCardBg: icon(ICON_KEYS.MERCHANT_HOME_STAT_BG, `${ossBase}beijing/shangjiaduan/stat_card_bg.png`),
|
||||
kefuKeyBtn: icon(ICON_KEYS.MERCHANT_HOME_KEFU_KEY, `${homeDir}kefu_key.png`),
|
||||
kefuListBtn: icon(ICON_KEYS.MERCHANT_HOME_KEFU_LIST, `${homeDir}kefu_list.png`),
|
||||
iconRegular: icon(ICON_KEYS.MERCHANT_HOME_REGULAR, `${homeDir}regular_dispatch.png`),
|
||||
iconCustom: icon(ICON_KEYS.MERCHANT_HOME_CUSTOM, `${homeDir}custom_dispatch.png`),
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
processImageUrls(urlList) {
|
||||
@@ -78,14 +172,13 @@ Page(createPage({
|
||||
},
|
||||
|
||||
checkRole() {
|
||||
const ok = Number(wx.getStorageSync('shangjiastatus')) === 1;
|
||||
this.setData({ isShangjia: ok });
|
||||
syncStaffUi(this);
|
||||
},
|
||||
|
||||
loadBannerData(forceFetch) {
|
||||
loadBannerData(forceFetch, silent) {
|
||||
const g = app.globalData || {};
|
||||
const cachedList = g.shangpinlunbo || [];
|
||||
if (!forceFetch && cachedList.length) {
|
||||
if (!forceFetch && isGonggaoCacheValid(app) && cachedList.length) {
|
||||
this.setData({
|
||||
lunboList: this.processImageUrls(cachedList),
|
||||
gonggao: g.shangpingonggao || '',
|
||||
@@ -93,45 +186,30 @@ Page(createPage({
|
||||
});
|
||||
return Promise.resolve();
|
||||
}
|
||||
if (!forceFetch && g.shangpingonggao) {
|
||||
return fetchGonggaoLunbo(app, 'merchant_home').then((data) => {
|
||||
if (!data) return;
|
||||
this.setData({
|
||||
lunboList: this.processImageUrls(cachedList),
|
||||
gonggao: g.shangpingonggao,
|
||||
lunboList: this.processImageUrls(app.globalData.shangpinlunbo),
|
||||
gonggao: app.globalData.shangpingonggao || '',
|
||||
});
|
||||
return Promise.resolve();
|
||||
}
|
||||
const apiBase = g.apiBaseUrl || '';
|
||||
if (!apiBase) return Promise.resolve();
|
||||
return new Promise((resolve) => {
|
||||
wx.request({
|
||||
url: `${apiBase}/peizhi/shangpingonggao/`,
|
||||
method: 'POST',
|
||||
header: { 'content-type': 'application/json' },
|
||||
success: (res) => {
|
||||
if (res.statusCode === 200 && res.data) {
|
||||
const data = res.data;
|
||||
app.globalData.shangpingonggao = data.shangpingonggao || '';
|
||||
app.globalData.shangpinlunbo = data.shangpinlunbo || [];
|
||||
this.setData({
|
||||
lunboList: this.processImageUrls(app.globalData.shangpinlunbo),
|
||||
gonggao: app.globalData.shangpingonggao,
|
||||
});
|
||||
}
|
||||
},
|
||||
complete: resolve,
|
||||
});
|
||||
});
|
||||
}).catch(() => {});
|
||||
},
|
||||
|
||||
async loadDashboard() {
|
||||
async loadDashboard(silent = false) {
|
||||
await Promise.all([
|
||||
this.loadShangjiaBrief(),
|
||||
this.loadPendingStats(),
|
||||
this.loadRecentOrders(),
|
||||
this.loadRecentOrders(silent),
|
||||
]);
|
||||
},
|
||||
|
||||
async loadShangjiaBrief() {
|
||||
if (isStaffMode()) {
|
||||
const ctx = await refreshStaffContext(request);
|
||||
syncStaffUi(this);
|
||||
applyStaffStatsToPage(this, ctx);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const res = await request({
|
||||
url: '/yonghu/shangjiaxinxi',
|
||||
@@ -155,39 +233,24 @@ Page(createPage({
|
||||
|
||||
async loadPendingStats() {
|
||||
try {
|
||||
const res = await request({
|
||||
url: '/dingdan/sjdingdanhq',
|
||||
method: 'POST',
|
||||
data: { zhuangtai_list: [8], page: 1, page_size: 20 },
|
||||
});
|
||||
if (res.data && (res.data.code === 0 || res.data.code === 200)) {
|
||||
const data = res.data.data || {};
|
||||
const list = data.list || [];
|
||||
let amount = 0;
|
||||
list.forEach((item) => {
|
||||
const v = parseFloat(item.jine || item.dingdan_jine || 0);
|
||||
if (!Number.isNaN(v)) amount += v;
|
||||
});
|
||||
this.setData({
|
||||
stats: {
|
||||
...this.data.stats,
|
||||
pendingCount: data.pending_count != null ? data.pending_count : list.length,
|
||||
pendingAmount: amount.toFixed(2),
|
||||
},
|
||||
});
|
||||
}
|
||||
const data = await fetchMerchantOrderStats(request, {});
|
||||
applyOrderStatsToHome(this, data);
|
||||
} catch (e) {
|
||||
console.error('待结算统计失败', e);
|
||||
console.warn('待结算统计失败', e);
|
||||
}
|
||||
},
|
||||
|
||||
/** 最近订单:同订单页「全部」筛选,取最新若干条 */
|
||||
async loadRecentOrders() {
|
||||
if (this.data.recentLoading) return;
|
||||
this.setData({ recentLoading: true });
|
||||
async loadRecentOrders(silent = false) {
|
||||
if (this.data.recentLoading && !silent) return;
|
||||
if (silent && this._recentOrdersRequesting) return;
|
||||
if (!silent) {
|
||||
this.setData({ recentLoading: true });
|
||||
}
|
||||
this._recentOrdersRequesting = true;
|
||||
try {
|
||||
const res = await request({
|
||||
url: '/dingdan/sjdingdanhq',
|
||||
url: isStaffMode() ? STAFF_API.orderList : '/dingdan/sjdingdanhq',
|
||||
method: 'POST',
|
||||
data: {
|
||||
zhuangtai_list: ALL_ORDER_ZHUANGTAI,
|
||||
@@ -206,7 +269,7 @@ Page(createPage({
|
||||
return {
|
||||
dingdan_id: item.dingdan_id,
|
||||
title: item.jieshao || '暂无描述',
|
||||
time: item.create_time || '',
|
||||
time: item.create_time || item.CreateTime || '',
|
||||
status: getOrderStatusText(item.zhuangtai),
|
||||
money: item.jine || '0.00',
|
||||
raw,
|
||||
@@ -217,7 +280,10 @@ Page(createPage({
|
||||
} catch (e) {
|
||||
console.error('最近订单加载失败', e);
|
||||
} finally {
|
||||
this.setData({ recentLoading: false });
|
||||
this._recentOrdersRequesting = false;
|
||||
if (!silent) {
|
||||
this.setData({ recentLoading: false });
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -225,12 +291,24 @@ Page(createPage({
|
||||
this.setData({ swiperCurrent: e.detail.current });
|
||||
},
|
||||
|
||||
goDispatch() {
|
||||
goRegularDispatch() {
|
||||
wx.navigateTo({ url: '/pages/merchant-regular-dispatch/merchant-regular-dispatch' });
|
||||
},
|
||||
|
||||
goLinkDispatch() {
|
||||
wx.navigateTo({ url: '/pages/express-order/express-order' });
|
||||
},
|
||||
|
||||
goCustomDispatch() {
|
||||
wx.navigateTo({ url: '/pages/merchant-dispatch/merchant-dispatch' });
|
||||
},
|
||||
|
||||
goDispatch() {
|
||||
this.goCustomDispatch();
|
||||
},
|
||||
|
||||
goExpress() {
|
||||
wx.navigateTo({ url: '/pages/express-order/express-order' });
|
||||
this.goLinkDispatch();
|
||||
},
|
||||
|
||||
goOrdersTab() {
|
||||
|
||||
Reference in New Issue
Block a user