import { fetchUserCenter } from '../../services/usercenter/fetchUsercenter'; import Toast from 'tdesign-miniprogram/toast/index'; const USER_CENTER_TTL = 30000; const menuData = [ [{ title: '我发布的需求', tit: '', url: '', icon: '', type: 'publish', }, { title: '我投标的需求', tit: '', url: '', icon: '', type: 'quote', }, { title: '优惠券', tit: '', url: '', type: 'coupon', }, { title: '余额', tit: '', url: '', type: 'balance', }, ], [{ title: '帮助中心', tit: '', url: '', type: 'help-center', }, { title: '客服热线', tit: '', url: '', type: 'service', icon: 'service', }, ], ]; // 会员角色组名为 Promoter 时才展示推广中心 const PROMOTER_GROUP = 'Promoter'; const promoterMenu = { title: '推广中心', tit: '', url: '', icon: '', type: 'promoter', }; const orderTagInfos = [{ title: '待付款', iconName: 'wallet', orderNum: 0, tabType: 10, status: 1, }, { title: '待发货', iconName: 'deliver', orderNum: 0, tabType: 20, status: 1, }, { title: '待收货', iconName: 'package', orderNum: 0, tabType: 30, status: 1, }, { title: '待评价', iconName: 'comment', orderNum: 0, tabType: 40, status: 1, }, { title: '退款/售后', iconName: 'exchang', orderNum: 0, tabType: 0, status: 1, }, ]; function cloneMenu(list) { return list.map((row) => row.map((item) => ({ ...item }))); } /** 按角色组装菜单,每次返回新数组,避免改到模块上的模板 */ function buildMenu(userInfo = {}, countsData = []) { const menu = cloneMenu(menuData); menu[0] = menu[0].filter((item) => item.type !== 'quote' && item.type !== 'promoter'); if (userInfo.role === 'dev') { menu[0].splice(1, 0, { title: '我投标的需求', tit: '', url: '', icon: '', type: 'quote' }); } if (userInfo.group_name === PROMOTER_GROUP) { const balanceIndex = menu[0].findIndex((item) => item.type === 'balance'); menu[0].splice(balanceIndex === -1 ? menu[0].length : balanceIndex + 1, 0, { ...promoterMenu }); } const counts = Array.isArray(countsData) ? countsData : []; menu[0].forEach((item) => { const matched = counts.find((count) => count.type === item.type); if (matched) { item.tit = matched.num; } }); return menu; } const getDefaultData = () => ({ showMakePhone: false, userInfo: { avatarUrl: '', nickName: '正在登录...', phoneNumber: '', }, menuData: cloneMenu(menuData), orderTagInfos: orderTagInfos.map((item) => ({ ...item })), customerServiceInfo: {}, currAuthStep: 1, showKefu: true, versionNo: '', }); Page({ data: getDefaultData(), onLoad() { this.getVersionInfo(); }, onShow() { this.getTabBar().init(); const app = getApp(); const appShowAt = app && app.globalData ? app.globalData.appShowAt : 0; const fromBackground = appShowAt !== this._seenAppShowAt; this._seenAppShowAt = appShowAt; const stale = !this._loadedAt || Date.now() - this._loadedAt > USER_CENTER_TTL; // 从后台回来,或距离上次成功超过 30 秒,才重新拉用户中心 if (fromBackground || stale) { this.init(); } }, onPullDownRefresh() { if (this._loading) { wx.stopPullDownRefresh(); return; } this.fetUseriInfoHandle(); }, init() { this.fetUseriInfoHandle(); }, fetUseriInfoHandle() { if (this._loading) { return; } this._loading = true; fetchUserCenter().then((res) => { if (!res || res.code !== 1 || !res.data) { wx.showToast({ title: (res && res.msg) || '加载失败', icon: 'none' }); return; } const { userInfo, countsData, orderTagInfos: orderTags, customerServiceInfo, } = res.data; const info = orderTagInfos.map((item, index) => ({ ...item, ...(orderTags && orderTags[index] ? orderTags[index] : {}), })); this._loadedAt = Date.now(); this.setData({ userInfo, menuData: buildMenu(userInfo, countsData), orderTagInfos: info, customerServiceInfo, currAuthStep: 2, }); }).catch((err) => { console.error('fetchUserCenter failed:', err); wx.showToast({ title: '网络异常,请重试', icon: 'none' }); }).then(() => { this._loading = false; wx.stopPullDownRefresh(); }); }, onClickCell({ currentTarget }) { const { type } = currentTarget.dataset; switch (type) { case 'address': { wx.navigateTo({ url: '/pages/usercenter/address/list/index' }); break; } case 'publish': { wx.navigateTo({ url: '/pages/demand/list/index?opType=my' }); break; } case 'quote': { wx.navigateTo({ url: '/pages/demand/list/index?opType=myQuote' }); break; } case 'service': { this.openMakePhone(); break; } case 'help-center': { // Toast({ // context: this, // selector: '#t-toast', // message: '你点击了帮助中心', // icon: '', // duration: 1000, // }); break; } case 'balance': { wx.navigateTo({ url: '/pages/usercenter/balance/index' }); break; } case 'promoter': { wx.navigateTo({ url: '/pages/promoter/index/index' }); break; } case 'coupon': { // wx.navigateTo({ // url: '/pages/coupon/coupon-list/index' // }); break; } default: { Toast({ context: this, selector: '#t-toast', message: '未知跳转', icon: '', duration: 1000, }); break; } } }, jumpNav(e) { const status = e.detail.tabType; if (status === 0) { wx.navigateTo({ url: '/pages/order/after-service-list/index' }); } else { wx.navigateTo({ url: `/pages/order/order-list/index?status=${status}` }); } }, jumpAllOrder() { wx.navigateTo({ url: '/pages/order/order-list/index' }); }, openMakePhone() { this.setData({ showMakePhone: true }); }, closeMakePhone() { this.setData({ showMakePhone: false }); }, call() { wx.makePhoneCall({ phoneNumber: this.data.customerServiceInfo.servicePhone, }); }, gotoUserEditPage() { const { currAuthStep } = this.data; if (currAuthStep === 2) { wx.navigateTo({ url: '/pages/usercenter/person-info/index' }); } else { this.fetUseriInfoHandle(); } }, getVersionInfo() { const versionInfo = wx.getAccountInfoSync(); const { version, envVersion = __wxConfig } = versionInfo.miniProgram; this.setData({ versionNo: envVersion === 'release' ? version : envVersion, }); }, });