feat(M08-A): 接入顾客端个人中心
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
const { request, ensureLogin, cents } = require('../../utils/api.js')
|
||||
|
||||
Page({
|
||||
data: {
|
||||
loading: false,
|
||||
errorMessage: '',
|
||||
profile: null,
|
||||
recentLedger: [],
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.loadProfile()
|
||||
},
|
||||
|
||||
async loadProfile() {
|
||||
this.setData({ loading: true, errorMessage: '' })
|
||||
try {
|
||||
await ensureLogin()
|
||||
const response = await request('/profile')
|
||||
const profile = formatProfile(response.data)
|
||||
this.setData({
|
||||
profile,
|
||||
recentLedger: (response.data.recentLedger || []).map(formatLedger),
|
||||
})
|
||||
} catch (error) {
|
||||
this.setData({ errorMessage: error.message || '个人中心加载失败' })
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
}
|
||||
},
|
||||
|
||||
openOrders() {
|
||||
wx.navigateTo({ url: '/pages/orders/list' })
|
||||
},
|
||||
})
|
||||
|
||||
function formatProfile(profile) {
|
||||
return {
|
||||
...profile,
|
||||
registeredText: formatDate(profile.registeredAt),
|
||||
lastLoginText: profile.lastLoginAt ? formatDate(profile.lastLoginAt) : '暂无',
|
||||
wallet: {
|
||||
...profile.wallet,
|
||||
cashText: cents(profile.wallet.cashBalanceCents),
|
||||
giftText: cents(profile.wallet.giftBalanceCents),
|
||||
totalText: cents(profile.wallet.totalBalanceCents),
|
||||
},
|
||||
benefits: {
|
||||
...profile.benefits,
|
||||
packageAmountText: cents(profile.benefits.packageAmountCents),
|
||||
},
|
||||
recharge: {
|
||||
...profile.recharge,
|
||||
creditedText: cents(profile.recharge.creditedRechargeCents),
|
||||
giftedText: cents(profile.recharge.giftedRechargeCents),
|
||||
},
|
||||
orders: {
|
||||
...profile.orders,
|
||||
paidAmountText: cents(profile.orders.paidAmountCents),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function formatLedger(item) {
|
||||
return {
|
||||
...item,
|
||||
deltaText: `${signedCents(item.cashDeltaCents)} / ${signedCents(item.giftDeltaCents)}`,
|
||||
balanceText: `${cents(item.cashBalanceAfterCents)} / ${cents(item.giftBalanceAfterCents)}`,
|
||||
createdText: formatDate(item.createdAt),
|
||||
}
|
||||
}
|
||||
|
||||
function signedCents(value) {
|
||||
const amount = Number(value || 0)
|
||||
return `${amount >= 0 ? '+' : '-'}${cents(Math.abs(amount))}`
|
||||
}
|
||||
|
||||
function formatDate(value) {
|
||||
const date = new Date(value)
|
||||
const pad = (input) => String(input).padStart(2, '0')
|
||||
return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`
|
||||
}
|
||||
Reference in New Issue
Block a user