页面优化
This commit is contained in:
@@ -3,6 +3,8 @@ import {
|
||||
} from '../../services/usercenter/fetchUsercenter';
|
||||
import Toast from 'tdesign-miniprogram/toast/index';
|
||||
|
||||
const USER_CENTER_TTL = 30000;
|
||||
|
||||
const menuData = [
|
||||
[{
|
||||
title: '我发布的需求',
|
||||
@@ -94,6 +96,31 @@ const orderTagInfos = [{
|
||||
},
|
||||
];
|
||||
|
||||
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: {
|
||||
@@ -101,8 +128,8 @@ const getDefaultData = () => ({
|
||||
nickName: '正在登录...',
|
||||
phoneNumber: '',
|
||||
},
|
||||
menuData,
|
||||
orderTagInfos,
|
||||
menuData: cloneMenu(menuData),
|
||||
orderTagInfos: orderTagInfos.map((item) => ({ ...item })),
|
||||
customerServiceInfo: {},
|
||||
currAuthStep: 1,
|
||||
showKefu: true,
|
||||
@@ -118,10 +145,22 @@ Page({
|
||||
|
||||
onShow() {
|
||||
this.getTabBar().init();
|
||||
this.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() {
|
||||
this.init();
|
||||
if (this._loading) {
|
||||
wx.stopPullDownRefresh();
|
||||
return;
|
||||
}
|
||||
this.fetUseriInfoHandle();
|
||||
},
|
||||
|
||||
init() {
|
||||
@@ -129,44 +168,40 @@ Page({
|
||||
},
|
||||
|
||||
fetUseriInfoHandle() {
|
||||
fetchUserCenter().then(
|
||||
(res) => {
|
||||
const {
|
||||
userInfo,
|
||||
countsData,
|
||||
orderTagInfos: orderTags,
|
||||
customerServiceInfo,
|
||||
} = res.data;
|
||||
// eslint-disable-next-line no-unused-expressions
|
||||
menuData[0] = menuData[0].filter((item) => item.type !== 'quote' && item.type !== 'promoter');
|
||||
if (userInfo.role === 'dev') {
|
||||
menuData[0].splice(1, 0, { title: '我投标的需求', tit: '', url: '', icon: '', type: 'quote' });
|
||||
}
|
||||
if (userInfo.group_name === PROMOTER_GROUP) {
|
||||
const balanceIndex = menuData[0].findIndex((item) => item.type === 'balance');
|
||||
menuData[0].splice(balanceIndex === -1 ? menuData[0].length : balanceIndex + 1, 0, { ...promoterMenu });
|
||||
}
|
||||
menuData[0].forEach((v) => {
|
||||
countsData.forEach((counts) => {
|
||||
if (counts.type === v.type) {
|
||||
v.tit = counts.num;
|
||||
}
|
||||
});
|
||||
});
|
||||
const info = orderTagInfos.map((v, index) => ({
|
||||
...v,
|
||||
...orderTags[index],
|
||||
}));
|
||||
this.setData({
|
||||
userInfo,
|
||||
menuData,
|
||||
orderTagInfos: info,
|
||||
customerServiceInfo,
|
||||
currAuthStep: 2,
|
||||
});
|
||||
wx.stopPullDownRefresh();
|
||||
},
|
||||
);
|
||||
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({
|
||||
|
||||
Reference in New Issue
Block a user