diff --git a/app/api/referral/data/route.ts b/app/api/referral/data/route.ts index 873ead9..1f3db8b 100644 --- a/app/api/referral/data/route.ts +++ b/app/api/referral/data/route.ts @@ -128,7 +128,8 @@ export async function GET(request: NextRequest) { // 6. 获取已转化用户列表 const convertedBindings = await query(` SELECT rb.id, rb.referee_id, rb.conversion_date, rb.commission_amount, - u.nickname, u.avatar + u.nickname, u.avatar, + (SELECT COALESCE(SUM(amount), 0) FROM orders WHERE user_id = rb.referee_id AND status = 'paid') as order_amount FROM referral_bindings rb JOIN users u ON rb.referee_id = u.id WHERE rb.referrer_id = ? AND rb.status = 'converted' @@ -136,6 +137,17 @@ export async function GET(request: NextRequest) { LIMIT 50 `, [userId]) as any[] + // 6.5 获取已过期用户列表 + const expiredBindings = await query(` + SELECT rb.id, rb.referee_id, rb.expiry_date, rb.binding_date, + u.nickname, u.avatar + FROM referral_bindings rb + JOIN users u ON rb.referee_id = u.id + WHERE rb.referrer_id = ? AND (rb.status = 'expired' OR (rb.status = 'active' AND rb.expiry_date <= NOW())) + ORDER BY rb.expiry_date DESC + LIMIT 50 + `, [userId]) as any[] + // 7. 获取收益明细 let earningsDetails: any[] = [] try { @@ -165,6 +177,8 @@ export async function GET(request: NextRequest) { visitCount: totalVisits, // 带来的付款人数 paidCount: paymentStats.paidCount, + // 已过期用户数 + expiredCount: bindingStats.expired, // === 收益数据 === // 已结算收益 @@ -201,7 +215,8 @@ export async function GET(request: NextRequest) { avatar: b.avatar, daysRemaining: Math.max(0, b.days_remaining), hasFullBook: b.has_full_book, - bindingDate: b.binding_date + bindingDate: b.binding_date, + status: 'active' })), convertedUsers: convertedBindings.map((b: any) => ({ @@ -209,7 +224,19 @@ export async function GET(request: NextRequest) { nickname: b.nickname || '用户' + b.referee_id.slice(-4), avatar: b.avatar, commission: parseFloat(b.commission_amount) || 0, - conversionDate: b.conversion_date + orderAmount: parseFloat(b.order_amount) || 0, + conversionDate: b.conversion_date, + status: 'converted' + })), + + // 已过期用户列表 + expiredUsers: expiredBindings.map((b: any) => ({ + id: b.referee_id, + nickname: b.nickname || '用户' + b.referee_id.slice(-4), + avatar: b.avatar, + bindingDate: b.binding_date, + expiryDate: b.expiry_date, + status: 'expired' })), // === 收益明细 === diff --git a/app/api/user/update/route.ts b/app/api/user/update/route.ts new file mode 100644 index 0000000..445b9b3 --- /dev/null +++ b/app/api/user/update/route.ts @@ -0,0 +1,72 @@ +// app/api/user/update/route.ts +// 更新用户信息(头像、昵称等) + +import { NextRequest, NextResponse } from 'next/server' +import { getDb } from '@/lib/db' + +export async function POST(req: NextRequest) { + try { + const { userId, nickname, avatar, phone, wechatId, address } = await req.json() + + if (!userId) { + return NextResponse.json({ error: '缺少用户ID' }, { status: 400 }) + } + + const db = await getDb() + + // 构建更新字段 + const updates: string[] = [] + const values: any[] = [] + + if (nickname !== undefined) { + updates.push('nickname = ?') + values.push(nickname) + } + if (avatar !== undefined) { + updates.push('avatar = ?') + values.push(avatar) + } + if (phone !== undefined) { + updates.push('phone = ?') + values.push(phone) + } + if (wechatId !== undefined) { + updates.push('wechat_id = ?') + values.push(wechatId) + } + if (address !== undefined) { + updates.push('address = ?') + values.push(address) + } + + if (updates.length === 0) { + return NextResponse.json({ error: '没有要更新的字段' }, { status: 400 }) + } + + updates.push('updated_at = NOW()') + values.push(userId) + + const sql = `UPDATE users SET ${updates.join(', ')} WHERE id = ?` + await db.execute(sql, values) + + // 返回更新后的用户信息 + const [rows] = await db.execute( + 'SELECT id, nickname, avatar, phone, wechat_id, address FROM users WHERE id = ?', + [userId] + ) + + const user = (rows as any[])[0] + + return NextResponse.json({ + success: true, + user + }) + + } catch (error) { + console.error('[User Update] Error:', error) + return NextResponse.json( + { error: '更新用户信息失败' }, + { status: 500 } + ) + } +} diff --git a/miniprogram/pages/my/my.js b/miniprogram/pages/my/my.js index b570100..b32adfd 100644 --- a/miniprogram/pages/my/my.js +++ b/miniprogram/pages/my/my.js @@ -105,18 +105,96 @@ Page({ // 编辑用户资料(头像和昵称) editProfile() { wx.showActionSheet({ - itemList: ['修改头像', '修改昵称'], + itemList: ['获取微信头像', '获取微信昵称', '从相册选择头像', '手动输入昵称'], success: (res) => { if (res.tapIndex === 0) { - this.chooseAvatar() + this.getWechatAvatar() } else if (res.tapIndex === 1) { + this.getWechatNickname() + } else if (res.tapIndex === 2) { + this.chooseAvatar() + } else if (res.tapIndex === 3) { this.editNickname() } } }) }, - // 选择头像 + // 获取微信头像(原生能力) + getWechatAvatar() { + // 使用chooseAvatar API(微信原生头像选择) + wx.chooseAvatar({ + success: async (res) => { + const avatarUrl = res.avatarUrl + wx.showLoading({ title: '更新中...', mask: true }) + + try { + // 更新本地显示 + const userInfo = this.data.userInfo + userInfo.avatar = avatarUrl + this.setData({ userInfo }) + app.globalData.userInfo = userInfo + wx.setStorageSync('userInfo', userInfo) + + // 同步到服务器 + await app.request('/api/user/update', { + method: 'POST', + data: { userId: userInfo.id, avatar: avatarUrl } + }) + + wx.hideLoading() + wx.showToast({ title: '头像已更新', icon: 'success' }) + } catch (e) { + wx.hideLoading() + wx.showToast({ title: '更新失败', icon: 'none' }) + } + }, + fail: () => { + wx.showToast({ title: '取消选择', icon: 'none' }) + } + }) + }, + + // 获取微信昵称(原生能力) + getWechatNickname() { + // 引导用户在弹窗中输入微信昵称 + wx.showModal({ + title: '获取微信昵称', + content: '请在下方输入您的微信昵称', + editable: true, + placeholderText: '请输入微信昵称', + success: async (res) => { + if (res.confirm && res.content) { + const nickname = res.content.trim() + if (nickname.length < 1 || nickname.length > 20) { + wx.showToast({ title: '昵称1-20个字符', icon: 'none' }) + return + } + + // 更新本地 + const userInfo = this.data.userInfo + userInfo.nickname = nickname + this.setData({ userInfo }) + app.globalData.userInfo = userInfo + wx.setStorageSync('userInfo', userInfo) + + // 同步到服务器 + try { + await app.request('/api/user/update', { + method: 'POST', + data: { userId: userInfo.id, nickname } + }) + } catch (e) { + console.log('同步昵称到服务器失败', e) + } + + wx.showToast({ title: '昵称已更新', icon: 'success' }) + } + } + }) + }, + + // 从相册选择头像 chooseAvatar() { wx.chooseMedia({ count: 1, @@ -134,7 +212,12 @@ Page({ app.globalData.userInfo = userInfo wx.setStorageSync('userInfo', userInfo) - // TODO: 上传到服务器 + // 同步到服务器 + await app.request('/api/user/update', { + method: 'POST', + data: { userId: userInfo.id, avatar: tempFilePath } + }) + wx.hideLoading() wx.showToast({ title: '头像已更新', icon: 'success' }) } catch (e) { @@ -145,7 +228,7 @@ Page({ }) }, - // 修改昵称 + // 手动输入昵称 editNickname() { wx.showModal({ title: '修改昵称', @@ -166,7 +249,16 @@ Page({ app.globalData.userInfo = userInfo wx.setStorageSync('userInfo', userInfo) - // TODO: 上传到服务器 + // 同步到服务器 + try { + await app.request('/api/user/update', { + method: 'POST', + data: { userId: userInfo.id, nickname: newNickname } + }) + } catch (e) { + console.log('同步昵称到服务器失败', e) + } + wx.showToast({ title: '昵称已更新', icon: 'success' }) } } diff --git a/miniprogram/pages/referral/referral.js b/miniprogram/pages/referral/referral.js index 005b817..381d88f 100644 --- a/miniprogram/pages/referral/referral.js +++ b/miniprogram/pages/referral/referral.js @@ -19,6 +19,8 @@ Page({ bindingCount: 0, // 绑定用户数(当前有效) visitCount: 0, // 通过链接进的人数 paidCount: 0, // 带来的付款人数 + unboughtCount: 0, // 待购买人数(绑定但未付款) + expiredCount: 0, // 已过期人数 // === 收益数据 === earnings: 0, // 已结算收益 @@ -84,7 +86,7 @@ Page({ // 使用真实数据或默认值 let activeBindings = realData?.activeUsers || [] let convertedBindings = realData?.convertedUsers || [] - let expiredBindings = [] + let expiredBindings = realData?.expiredUsers || [] // 兼容旧字段名 if (!activeBindings.length && realData?.activeBindings) { @@ -93,20 +95,40 @@ Page({ if (!convertedBindings.length && realData?.convertedBindings) { convertedBindings = realData.convertedBindings } - if (realData?.expiredBindings) { + if (!expiredBindings.length && realData?.expiredBindings) { expiredBindings = realData.expiredBindings } const expiringCount = activeBindings.filter(b => b.daysRemaining <= 7 && b.daysRemaining > 0).length + // 计算各类统计 + const bindingCount = realData?.bindingCount || activeBindings.length + const paidCount = realData?.paidCount || convertedBindings.length + const expiredCount = realData?.expiredCount || expiredBindings.length + const unboughtCount = bindingCount // 绑定中但未付款的 + + // 格式化用户数据 + const formatUser = (user, type) => ({ + id: user.id, + nickname: user.nickname || '用户' + (user.id || '').slice(-4), + avatar: user.avatar, + status: type, + daysRemaining: user.daysRemaining || 0, + bindingDate: user.bindingDate ? this.formatDate(user.bindingDate) : '--', + commission: user.commission || 0, + orderAmount: user.orderAmount || 0 + }) + this.setData({ isLoggedIn: true, userInfo, // 核心可见数据 - bindingCount: realData?.bindingCount || activeBindings.length, + bindingCount, visitCount: realData?.visitCount || 0, - paidCount: realData?.paidCount || convertedBindings.length, + paidCount, + unboughtCount, + expiredCount, // 收益数据 earnings: realData?.earnings || 0, @@ -119,10 +141,10 @@ Page({ expiringCount, referralCode, - activeBindings, - convertedBindings, - expiredBindings, - currentBindings: activeBindings, + activeBindings: activeBindings.map(u => formatUser(u, 'active')), + convertedBindings: convertedBindings.map(u => formatUser(u, 'converted')), + expiredBindings: expiredBindings.map(u => formatUser(u, 'expired')), + currentBindings: activeBindings.map(u => formatUser(u, 'active')), totalBindings: activeBindings.length + convertedBindings.length + expiredBindings.length, // 收益明细 @@ -353,14 +375,14 @@ Page({ // 分享到朋友圈 shareToMoments() { - const shareText = `🔥 发现一本超棒的创业实战书《Soul创业派对》!\n\n💡 62个真实商业案例,从私域运营到资源整合,干货满满!\n\n🎁 通过我的链接购买立享5%优惠,我是 ${this.data.userInfo?.nickname || '卡若'} 推荐!\n\n👉 ${this.data.referralCode} 是我的专属邀请码\n\n#创业派对 #私域运营 #商业案例` + const shareText = `🔥 发现一本超棒的创业实战书《Soul创业派对》!\n\n💡 62个真实商业案例,从私域运营到资源整合,干货满满!\n\n🎁 ${this.data.userInfo?.nickname || '卡若'} 推荐,通过海报扫码购买立享优惠!\n\n#创业派对 #私域运营 #商业案例` wx.setClipboardData({ data: shareText, success: () => { wx.showModal({ title: '文案已复制', - content: '请打开微信朋友圈,粘贴分享文案即可', + content: '请打开微信朋友圈,粘贴分享文案,配合推广海报一起发布效果更佳', showCancel: false, confirmText: '知道了' }) @@ -368,12 +390,12 @@ Page({ }) }, - // 提现 - 直接到微信零钱 + // 提现 - 直接到微信零钱(无门槛) async handleWithdraw() { const pendingEarnings = parseFloat(this.data.pendingEarnings) || 0 - if (pendingEarnings < 10) { - wx.showToast({ title: '满10元可提现', icon: 'none' }) + if (pendingEarnings <= 0) { + wx.showToast({ title: '暂无可提现收益', icon: 'none' }) return } @@ -480,5 +502,14 @@ Page({ goBack() { wx.navigateBack() + }, + + // 格式化日期 + formatDate(dateStr) { + if (!dateStr) return '--' + const d = new Date(dateStr) + const month = (d.getMonth() + 1).toString().padStart(2, '0') + const day = d.getDate().toString().padStart(2, '0') + return `${month}-${day}` } }) diff --git a/miniprogram/pages/referral/referral.wxml b/miniprogram/pages/referral/referral.wxml index 42b4277..b782150 100644 --- a/miniprogram/pages/referral/referral.wxml +++ b/miniprogram/pages/referral/referral.wxml @@ -43,8 +43,8 @@ 已提现: ¥{{withdrawnEarnings}} - - {{pendingEarnings < 10 ? '满10元可提现' : '申请提现'}} + + {{pendingEarnings <= 0 ? '暂无收益' : '立即提现 ¥' + pendingEarnings}} @@ -53,24 +53,31 @@ {{bindingCount}} - 绑定用户数 + 绑定中 当前有效绑定 - - {{visitCount}} - 链接进入人数 - 通过你的链接进入 - {{paidCount}} - 付款人数 - 成功转化购买 + 已付款 + 成功转化 - {{expiringCount}} - 即将过期 - 7天内到期 + {{unboughtCount}} + 待购买 + 绑定未付款 + + {{expiredCount}} + 已过期 + 绑定已失效 + + + + + + 总访问量 + {{visitCount}} + 人通过你的链接进入 diff --git a/miniprogram/pages/referral/referral.wxss b/miniprogram/pages/referral/referral.wxss index 4d2503d..4b286d6 100644 --- a/miniprogram/pages/referral/referral.wxss +++ b/miniprogram/pages/referral/referral.wxss @@ -47,9 +47,16 @@ .stat-value.brand { color: #00CED1; } .stat-value.gold { color: #FFD700; } .stat-value.orange { color: #FFA500; } +.stat-value.gray { color: #9E9E9E; } .stat-label { font-size: 24rpx; color: rgba(255,255,255,0.7); margin-top: 8rpx; display: block; font-weight: 500; } .stat-tip { font-size: 20rpx; color: rgba(255,255,255,0.4); margin-top: 4rpx; display: block; } +/* 访问量统计 */ +.visit-stat { display: flex; align-items: center; justify-content: center; gap: 12rpx; padding: 20rpx 32rpx; background: rgba(255,255,255,0.05); border-radius: 16rpx; margin-bottom: 24rpx; } +.visit-label { font-size: 24rpx; color: rgba(255,255,255,0.5); } +.visit-value { font-size: 32rpx; font-weight: 700; color: #00CED1; } +.visit-tip { font-size: 24rpx; color: rgba(255,255,255,0.5); } + /* 推广规则 */ .rules-card { background: rgba(0,206,209,0.05); border: 2rpx solid rgba(0,206,209,0.2); border-radius: 24rpx; padding: 24rpx; margin-bottom: 24rpx; width: 100%; box-sizing: border-box; } .rules-header { display: flex; align-items: center; gap: 16rpx; margin-bottom: 16rpx; }