init
This commit is contained in:
346
miniprogram/pages/usercenter/index.js
Normal file
346
miniprogram/pages/usercenter/index.js
Normal file
@@ -0,0 +1,346 @@
|
||||
import { fetchUserCenter } from '../../services/usercenter/fetchUsercenter';
|
||||
import Toast from 'tdesign-miniprogram/toast/index';
|
||||
|
||||
// 已登录用户的菜单数据
|
||||
const loggedInMenuData = [
|
||||
[
|
||||
{
|
||||
title: '收货地址',
|
||||
tit: '',
|
||||
url: '',
|
||||
type: 'address',
|
||||
},
|
||||
{
|
||||
title: '优惠券',
|
||||
tit: '',
|
||||
url: '',
|
||||
type: 'coupon',
|
||||
},
|
||||
{
|
||||
title: '我的积分',
|
||||
tit: '',
|
||||
url: '',
|
||||
type: 'points',
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
title: '帮助中心',
|
||||
tit: '',
|
||||
url: '',
|
||||
type: 'help-center',
|
||||
},
|
||||
{
|
||||
title: '客服热线',
|
||||
tit: '',
|
||||
url: '',
|
||||
type: 'service',
|
||||
icon: 'service',
|
||||
},
|
||||
],
|
||||
[
|
||||
{
|
||||
title: '退出登录',
|
||||
tit: '',
|
||||
url: '',
|
||||
type: 'logout',
|
||||
icon: 'logout',
|
||||
},
|
||||
],
|
||||
];
|
||||
|
||||
// 未登录用户的菜单数据
|
||||
const guestMenuData = [
|
||||
[
|
||||
{
|
||||
title: '帮助中心',
|
||||
tit: '',
|
||||
url: '',
|
||||
type: 'help-center',
|
||||
},
|
||||
{
|
||||
title: '客服热线',
|
||||
tit: '',
|
||||
url: '',
|
||||
type: 'service',
|
||||
icon: 'service',
|
||||
},
|
||||
],
|
||||
];
|
||||
|
||||
const orderTagInfos = [
|
||||
{
|
||||
title: '待付款',
|
||||
iconName: 'wallet',
|
||||
orderNum: 0,
|
||||
tabType: 1, // 修复:与订单列表页面的状态值匹配
|
||||
status: 1,
|
||||
},
|
||||
{
|
||||
title: '待发货',
|
||||
iconName: 'deliver',
|
||||
orderNum: 0,
|
||||
tabType: 3, // 修复:与订单列表页面的状态值匹配
|
||||
status: 1,
|
||||
},
|
||||
{
|
||||
title: '待收货',
|
||||
iconName: 'package',
|
||||
orderNum: 0,
|
||||
tabType: 5, // 修复:与订单列表页面的状态值匹配
|
||||
status: 1,
|
||||
},
|
||||
{
|
||||
title: '待评价',
|
||||
iconName: 'comment',
|
||||
orderNum: 0,
|
||||
tabType: 6, // 修复:与订单列表页面的状态值匹配
|
||||
status: 1,
|
||||
},
|
||||
{
|
||||
title: '退款/售后',
|
||||
iconName: 'exchang',
|
||||
orderNum: 0,
|
||||
tabType: 'refund', // 修改为refund,跳转到退款记录页面
|
||||
status: 1,
|
||||
},
|
||||
];
|
||||
|
||||
const getDefaultData = () => ({
|
||||
showMakePhone: false,
|
||||
userInfo: {
|
||||
avatarUrl: '',
|
||||
nickName: '正在登录...',
|
||||
phoneNumber: '',
|
||||
},
|
||||
menuData: guestMenuData, // 默认使用未登录菜单
|
||||
orderTagInfos,
|
||||
customerServiceInfo: {},
|
||||
currAuthStep: 1,
|
||||
showKefu: true,
|
||||
versionNo: '',
|
||||
});
|
||||
|
||||
Page({
|
||||
data: getDefaultData(),
|
||||
|
||||
onLoad() {
|
||||
this.getVersionInfo();
|
||||
},
|
||||
|
||||
onShow() {
|
||||
this.getTabBar().init();
|
||||
|
||||
// 检查登录状态
|
||||
const token = wx.getStorageSync('token');
|
||||
if (token) {
|
||||
// 已登录,获取用户信息并设置已登录菜单
|
||||
this.setData({
|
||||
menuData: loggedInMenuData
|
||||
});
|
||||
this.init();
|
||||
} else {
|
||||
// 未登录,显示未登录状态和未登录菜单
|
||||
this.setData({
|
||||
currAuthStep: 1, // 设置为未登录状态
|
||||
userInfo: {
|
||||
avatarUrl: '',
|
||||
nickName: '点击登录',
|
||||
phoneNumber: ''
|
||||
},
|
||||
menuData: guestMenuData
|
||||
});
|
||||
}
|
||||
},
|
||||
onPullDownRefresh() {
|
||||
this.init();
|
||||
},
|
||||
|
||||
init() {
|
||||
this.fetUseriInfoHandle();
|
||||
},
|
||||
|
||||
fetUseriInfoHandle() {
|
||||
fetchUserCenter().then(({ userInfo, countsData, orderTagInfos: orderInfo, customerServiceInfo }) => {
|
||||
// 获取当前菜单数据
|
||||
const currentMenuData = this.data.menuData;
|
||||
// eslint-disable-next-line no-unused-expressions
|
||||
if (currentMenuData && currentMenuData.length > 0 && currentMenuData[0]) {
|
||||
currentMenuData[0].forEach((v) => {
|
||||
// 添加数据验证,确保countsData存在且为数组
|
||||
if (Array.isArray(countsData)) {
|
||||
countsData.forEach((counts) => {
|
||||
if (counts.type === v.type) {
|
||||
// eslint-disable-next-line no-param-reassign
|
||||
v.tit = counts.num;
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
const info = orderTagInfos.map((v, index) => ({
|
||||
...v,
|
||||
...(orderInfo && orderInfo[index] ? orderInfo[index] : {}),
|
||||
}));
|
||||
this.setData({
|
||||
userInfo,
|
||||
menuData: currentMenuData,
|
||||
orderTagInfos: info,
|
||||
customerServiceInfo,
|
||||
currAuthStep: 2,
|
||||
});
|
||||
wx.stopPullDownRefresh();
|
||||
});
|
||||
},
|
||||
|
||||
onClickCell({ currentTarget }) {
|
||||
const { type } = currentTarget.dataset;
|
||||
|
||||
switch (type) {
|
||||
case 'address': {
|
||||
wx.navigateTo({ url: '/pages/user/address/list/index' });
|
||||
break;
|
||||
}
|
||||
case 'service': {
|
||||
this.openMakePhone();
|
||||
break;
|
||||
}
|
||||
case 'help-center': {
|
||||
wx.navigateTo({
|
||||
url: '/pages/help-center/index'
|
||||
});
|
||||
break;
|
||||
}
|
||||
case 'coupon': {
|
||||
wx.navigateTo({ url: '/pages/coupon/coupon-list/index' });
|
||||
break;
|
||||
}
|
||||
case 'points': {
|
||||
wx.navigateTo({ url: '/pages/points/index' });
|
||||
break;
|
||||
}
|
||||
case 'logout': {
|
||||
this.handleLogout();
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
Toast({
|
||||
context: this,
|
||||
selector: '#t-toast',
|
||||
message: '未知跳转',
|
||||
icon: '',
|
||||
duration: 1000,
|
||||
});
|
||||
break;
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
jumpNav(e) {
|
||||
const status = e.detail.tabType;
|
||||
if (status === 'refund') {
|
||||
// 跳转到退款记录页面
|
||||
wx.navigateTo({ url: '/pages/refund/refund-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;
|
||||
const token = wx.getStorageSync('token');
|
||||
|
||||
if (!token) {
|
||||
// 未登录,跳转到登录页
|
||||
wx.navigateTo({ url: '/pages/login/index' });
|
||||
} else if (currAuthStep === 2) {
|
||||
// 已登录但未授权,跳转到个人信息页
|
||||
wx.navigateTo({ url: '/pages/user/person-info/index' });
|
||||
} else {
|
||||
// 其他情况,重新获取用户信息
|
||||
this.fetUseriInfoHandle();
|
||||
}
|
||||
},
|
||||
|
||||
getVersionInfo() {
|
||||
const versionInfo = wx.getAccountInfoSync();
|
||||
const { version, envVersion = __wxConfig } = versionInfo.miniProgram;
|
||||
this.setData({
|
||||
versionNo: envVersion === 'release' ? version : envVersion,
|
||||
});
|
||||
},
|
||||
|
||||
handleLogout() {
|
||||
wx.showModal({
|
||||
title: '退出登录',
|
||||
content: '确定要退出登录吗?',
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
// 清除本地存储的用户信息
|
||||
wx.removeStorageSync('userInfo');
|
||||
wx.removeStorageSync('token');
|
||||
wx.removeStorageSync('openid');
|
||||
|
||||
// 重置用户信息
|
||||
this.setData({
|
||||
userInfo: {
|
||||
avatarUrl: '',
|
||||
nickName: '正在登录...',
|
||||
phoneNumber: '',
|
||||
},
|
||||
currAuthStep: 1,
|
||||
});
|
||||
|
||||
// 显示退出成功提示
|
||||
Toast({
|
||||
context: this,
|
||||
selector: '#t-toast',
|
||||
message: '退出登录成功',
|
||||
icon: 'check-circle',
|
||||
duration: 1500,
|
||||
});
|
||||
|
||||
// 延迟跳转到登录页
|
||||
setTimeout(() => {
|
||||
wx.reLaunch({
|
||||
url: '/pages/login/index'
|
||||
});
|
||||
}, 1500);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// 分享功能
|
||||
onShareAppMessage() {
|
||||
return {
|
||||
title: '个人中心 - 管理我的订单与信息',
|
||||
path: '/pages/usercenter/index'
|
||||
};
|
||||
},
|
||||
|
||||
// 分享到朋友圈
|
||||
onShareTimeline() {
|
||||
return {
|
||||
title: '个人中心 - 管理我的订单与信息'
|
||||
};
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user