feat :init
This commit is contained in:
481
miniprogram/pages/index/index.js
Normal file
481
miniprogram/pages/index/index.js
Normal file
@@ -0,0 +1,481 @@
|
||||
// pages/index/index.js
|
||||
const app = getApp();
|
||||
const { memberAPI } = require('../../utils/api');
|
||||
const {
|
||||
formatRelativeTime,
|
||||
getMemberLevelText,
|
||||
calculateMemberExpire,
|
||||
showError,
|
||||
showSuccess,
|
||||
debounce
|
||||
} = require('../../utils/util');
|
||||
|
||||
Page({
|
||||
/**
|
||||
* 页面的初始数据
|
||||
*/
|
||||
data: {
|
||||
// 用户信息
|
||||
userInfo: {},
|
||||
isLoggedIn: false,
|
||||
|
||||
// 会员信息
|
||||
memberInfo: {},
|
||||
|
||||
// 快捷权益
|
||||
quickBenefits: [],
|
||||
|
||||
// 功能菜单
|
||||
menuItems: [
|
||||
{
|
||||
id: 'member-center',
|
||||
name: '会员中心',
|
||||
icon: '/images/menu/member.png',
|
||||
path: '/pages/member/center/center'
|
||||
},
|
||||
{
|
||||
id: 'member-bind',
|
||||
name: '绑定会员',
|
||||
icon: '/images/menu/bind.png',
|
||||
path: '/pages/member/bind/bind'
|
||||
},
|
||||
{
|
||||
id: 'benefits',
|
||||
name: '权益说明',
|
||||
icon: '/images/menu/benefits.png',
|
||||
path: '/pages/member/benefits/benefits'
|
||||
},
|
||||
{
|
||||
id: 'upgrade',
|
||||
name: '升级会员',
|
||||
icon: '/images/menu/upgrade.png',
|
||||
path: '/pages/member/upgrade/upgrade'
|
||||
},
|
||||
{
|
||||
id: 'history',
|
||||
name: '使用记录',
|
||||
icon: '/images/menu/history.png',
|
||||
path: '/pages/member/history/history'
|
||||
},
|
||||
{
|
||||
id: 'invite',
|
||||
name: '邀请好友',
|
||||
icon: '/images/menu/invite.png',
|
||||
path: '/pages/member/invite/invite'
|
||||
},
|
||||
{
|
||||
id: 'support',
|
||||
name: '客服支持',
|
||||
icon: '/images/menu/support.png',
|
||||
path: '/pages/support/support'
|
||||
},
|
||||
{
|
||||
id: 'settings',
|
||||
name: '设置',
|
||||
icon: '/images/menu/settings.png',
|
||||
path: '/pages/settings/settings'
|
||||
}
|
||||
],
|
||||
|
||||
// 最近活动
|
||||
recentActivities: [],
|
||||
|
||||
// 升级提示
|
||||
showUpgradePrompt: false,
|
||||
|
||||
// 权益弹窗
|
||||
showBenefitModal: false,
|
||||
selectedBenefit: {},
|
||||
|
||||
// 加载状态
|
||||
loading: false,
|
||||
refreshing: false
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面加载
|
||||
*/
|
||||
onLoad(options) {
|
||||
this.initPage();
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面显示
|
||||
*/
|
||||
onShow() {
|
||||
this.refreshUserInfo();
|
||||
this.refreshMemberInfo();
|
||||
},
|
||||
|
||||
/**
|
||||
* 页面相关事件处理函数--监听用户下拉动作
|
||||
*/
|
||||
onPullDownRefresh() {
|
||||
this.refreshPage();
|
||||
},
|
||||
|
||||
/**
|
||||
* 页面上拉触底事件的处理函数
|
||||
*/
|
||||
onReachBottom() {
|
||||
// 可以在这里加载更多数据
|
||||
},
|
||||
|
||||
/**
|
||||
* 用户点击右上角分享
|
||||
*/
|
||||
onShareAppMessage() {
|
||||
return {
|
||||
title: '小智AI会员 - 享受专属特权',
|
||||
path: '/pages/index/index',
|
||||
imageUrl: '/images/share-cover.png'
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* 初始化页面
|
||||
*/
|
||||
initPage() {
|
||||
this.setData({ loading: true });
|
||||
|
||||
// 检查登录状态
|
||||
this.checkLoginStatus();
|
||||
|
||||
// 初始化菜单徽章
|
||||
this.updateMenuBadges();
|
||||
|
||||
this.setData({ loading: false });
|
||||
},
|
||||
|
||||
/**
|
||||
* 检查登录状态
|
||||
*/
|
||||
checkLoginStatus() {
|
||||
const isLoggedIn = app.checkLoginStatus();
|
||||
const userInfo = app.globalData.userInfo || {};
|
||||
|
||||
this.setData({
|
||||
isLoggedIn,
|
||||
userInfo
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 刷新页面数据
|
||||
*/
|
||||
refreshPage: debounce(function() {
|
||||
this.setData({ refreshing: true });
|
||||
|
||||
Promise.all([
|
||||
this.refreshUserInfo(),
|
||||
this.refreshMemberInfo(),
|
||||
this.loadRecentActivities()
|
||||
]).finally(() => {
|
||||
this.setData({ refreshing: false });
|
||||
wx.stopPullDownRefresh();
|
||||
});
|
||||
}, 1000),
|
||||
|
||||
/**
|
||||
* 刷新用户信息
|
||||
*/
|
||||
async refreshUserInfo() {
|
||||
if (!this.data.isLoggedIn) return;
|
||||
|
||||
try {
|
||||
const result = await memberAPI.getUserInfo();
|
||||
if (result.code === 0) {
|
||||
const userInfo = result.data;
|
||||
app.globalData.userInfo = userInfo;
|
||||
this.setData({ userInfo });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取用户信息失败:', error);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 刷新会员信息
|
||||
*/
|
||||
async refreshMemberInfo() {
|
||||
if (!this.data.isLoggedIn) return;
|
||||
|
||||
try {
|
||||
const result = await memberAPI.getMemberInfo();
|
||||
if (result.code === 0) {
|
||||
const memberData = result.data;
|
||||
const memberInfo = this.processMemberInfo(memberData);
|
||||
|
||||
app.globalData.memberInfo = memberInfo;
|
||||
this.setData({ memberInfo });
|
||||
|
||||
// 加载会员权益
|
||||
this.loadMemberBenefits();
|
||||
|
||||
// 检查是否显示升级提示
|
||||
this.checkUpgradePrompt(memberInfo);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取会员信息失败:', error);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理会员信息
|
||||
*/
|
||||
processMemberInfo(memberData) {
|
||||
const { level, expireTime } = memberData;
|
||||
const levelText = getMemberLevelText(level);
|
||||
const expireInfo = calculateMemberExpire(expireTime);
|
||||
|
||||
return {
|
||||
...memberData,
|
||||
levelText,
|
||||
expireInfo
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* 加载会员权益
|
||||
*/
|
||||
async loadMemberBenefits() {
|
||||
try {
|
||||
const result = await memberAPI.getMemberBenefits();
|
||||
if (result.code === 0) {
|
||||
const benefits = result.data || [];
|
||||
// 只显示前4个权益作为快捷入口
|
||||
const quickBenefits = benefits.slice(0, 4).map(benefit => ({
|
||||
...benefit,
|
||||
icon: benefit.icon || '/images/benefits/default.png'
|
||||
}));
|
||||
|
||||
this.setData({ quickBenefits });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取会员权益失败:', error);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 加载最近活动
|
||||
*/
|
||||
async loadRecentActivities() {
|
||||
if (!this.data.isLoggedIn) return;
|
||||
|
||||
try {
|
||||
const result = await memberAPI.getBenefitUsage(1, 5);
|
||||
if (result.code === 0) {
|
||||
const activities = (result.data.list || []).map(activity => ({
|
||||
...activity,
|
||||
timeText: formatRelativeTime(activity.createTime),
|
||||
statusClass: this.getActivityStatusClass(activity.status),
|
||||
statusText: this.getActivityStatusText(activity.status),
|
||||
icon: activity.icon || '/images/activity/default.png'
|
||||
}));
|
||||
|
||||
this.setData({ recentActivities: activities });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取最近活动失败:', error);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取活动状态样式类
|
||||
*/
|
||||
getActivityStatusClass(status) {
|
||||
const classMap = {
|
||||
'success': 'text-success',
|
||||
'failed': 'text-danger',
|
||||
'pending': 'text-warning'
|
||||
};
|
||||
return classMap[status] || 'text-muted';
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取活动状态文本
|
||||
*/
|
||||
getActivityStatusText(status) {
|
||||
const textMap = {
|
||||
'success': '成功',
|
||||
'failed': '失败',
|
||||
'pending': '处理中'
|
||||
};
|
||||
return textMap[status] || '未知';
|
||||
},
|
||||
|
||||
/**
|
||||
* 检查升级提示
|
||||
*/
|
||||
checkUpgradePrompt(memberInfo) {
|
||||
const { level, expireInfo } = memberInfo;
|
||||
|
||||
// 普通用户或会员即将过期时显示升级提示
|
||||
const showPrompt = level === 'free' ||
|
||||
(expireInfo && !expireInfo.isExpired && expireInfo.daysLeft <= 7);
|
||||
|
||||
this.setData({ showUpgradePrompt: showPrompt });
|
||||
},
|
||||
|
||||
/**
|
||||
* 更新菜单徽章
|
||||
*/
|
||||
updateMenuBadges() {
|
||||
const menuItems = this.data.menuItems.map(item => {
|
||||
// 这里可以根据实际需求添加徽章逻辑
|
||||
if (item.id === 'member-bind' && !this.data.isLoggedIn) {
|
||||
item.badge = 'NEW';
|
||||
}
|
||||
return item;
|
||||
});
|
||||
|
||||
this.setData({ menuItems });
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理登录
|
||||
*/
|
||||
handleLogin() {
|
||||
wx.navigateTo({
|
||||
url: '/pages/login/login'
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理菜单点击
|
||||
*/
|
||||
handleMenuClick(e) {
|
||||
const { menu } = e.currentTarget.dataset;
|
||||
|
||||
// 检查是否需要登录
|
||||
if (!this.data.isLoggedIn && this.needLogin(menu.id)) {
|
||||
this.handleLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
// 特殊处理
|
||||
switch (menu.id) {
|
||||
case 'support':
|
||||
this.handleCustomerService();
|
||||
break;
|
||||
default:
|
||||
wx.navigateTo({
|
||||
url: menu.path
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 检查功能是否需要登录
|
||||
*/
|
||||
needLogin(menuId) {
|
||||
const loginRequiredMenus = [
|
||||
'member-center', 'member-bind', 'upgrade',
|
||||
'history', 'invite'
|
||||
];
|
||||
return loginRequiredMenus.includes(menuId);
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理客服
|
||||
*/
|
||||
handleCustomerService() {
|
||||
wx.makePhoneCall({
|
||||
phoneNumber: '400-123-4567',
|
||||
fail: () => {
|
||||
showError('拨打客服电话失败');
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 处理权益点击
|
||||
*/
|
||||
handleBenefitClick(e) {
|
||||
const { benefit } = e.currentTarget.dataset;
|
||||
this.setData({
|
||||
selectedBenefit: benefit,
|
||||
showBenefitModal: true
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 关闭权益弹窗
|
||||
*/
|
||||
closeBenefitModal() {
|
||||
this.setData({
|
||||
showBenefitModal: false,
|
||||
selectedBenefit: {}
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 阻止事件冒泡
|
||||
*/
|
||||
stopPropagation() {
|
||||
// 阻止事件冒泡
|
||||
},
|
||||
|
||||
/**
|
||||
* 使用权益
|
||||
*/
|
||||
async useBenefit() {
|
||||
const { selectedBenefit } = this.data;
|
||||
|
||||
try {
|
||||
const result = await memberAPI.verifyBenefit(selectedBenefit.type);
|
||||
if (result.code === 0) {
|
||||
showSuccess('权益使用成功');
|
||||
this.closeBenefitModal();
|
||||
this.loadMemberBenefits(); // 刷新权益信息
|
||||
} else {
|
||||
showError(result.message || '权益使用失败');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('使用权益失败:', error);
|
||||
showError('权益使用失败');
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 导航到会员中心
|
||||
*/
|
||||
navigateToCenter() {
|
||||
if (!this.data.isLoggedIn) {
|
||||
this.handleLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
wx.switchTab({
|
||||
url: '/pages/member/center/center'
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 导航到升级页面
|
||||
*/
|
||||
navigateToUpgrade() {
|
||||
if (!this.data.isLoggedIn) {
|
||||
this.handleLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
wx.navigateTo({
|
||||
url: '/pages/member/upgrade/upgrade'
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* 导航到历史记录
|
||||
*/
|
||||
navigateToHistory() {
|
||||
if (!this.data.isLoggedIn) {
|
||||
this.handleLogin();
|
||||
return;
|
||||
}
|
||||
|
||||
wx.navigateTo({
|
||||
url: '/pages/member/history/history'
|
||||
});
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user