143 lines
4.4 KiB
JavaScript
143 lines
4.4 KiB
JavaScript
const app = getApp()
|
|
const { trackClick } = require('../../utils/trackClick')
|
|
|
|
Page({
|
|
data: {
|
|
statusBarHeight: 44,
|
|
balance: 0,
|
|
balanceText: '0.00',
|
|
transactions: [],
|
|
loading: true,
|
|
rechargeAmounts: [10, 30, 50, 100],
|
|
selectedAmount: 30,
|
|
auditMode: false,
|
|
},
|
|
|
|
onLoad() {
|
|
this.setData({
|
|
statusBarHeight: app.globalData.statusBarHeight || 44,
|
|
auditMode: app.globalData.auditMode || false,
|
|
})
|
|
this.loadBalance()
|
|
this.loadTransactions()
|
|
},
|
|
|
|
onShow() {
|
|
this.setData({ auditMode: app.globalData.auditMode || false })
|
|
},
|
|
|
|
async loadBalance() {
|
|
if (!app.globalData.isLoggedIn || !app.globalData.userInfo) return
|
|
const userId = app.globalData.userInfo.id
|
|
try {
|
|
const res = await app.request({ url: `/api/miniprogram/balance?userId=${userId}`, silent: true })
|
|
if (res && res.data) {
|
|
this.setData({
|
|
balance: res.data.balance || 0,
|
|
balanceText: (res.data.balance || 0).toFixed(2),
|
|
loading: false,
|
|
})
|
|
}
|
|
} catch (e) {
|
|
this.setData({ loading: false })
|
|
}
|
|
},
|
|
|
|
async loadTransactions() {
|
|
if (!app.globalData.isLoggedIn || !app.globalData.userInfo) return
|
|
const userId = app.globalData.userInfo.id
|
|
try {
|
|
const res = await app.request({ url: `/api/miniprogram/balance/transactions?userId=${userId}`, silent: true })
|
|
if (res && res.data) {
|
|
const list = (res.data || []).map(t => ({
|
|
...t,
|
|
amountText: Math.abs(t.amount || 0).toFixed(2),
|
|
amountSign: (t.amount || 0) >= 0 ? '+' : '-',
|
|
description: t.type === 'recharge' ? '充值' : t.type === 'consume' ? '阅读消费' : t.type === 'refund' ? '退款' : '其他',
|
|
createdAt: t.createdAt ? new Date(t.createdAt).toLocaleString('zh-CN') : '--',
|
|
}))
|
|
this.setData({ transactions: list })
|
|
}
|
|
} catch (e) {
|
|
console.warn('[Wallet] load transactions failed', e)
|
|
}
|
|
},
|
|
|
|
selectAmount(e) {
|
|
trackClick('wallet', 'tab_click', '选择金额' + (e.currentTarget.dataset.amount || ''))
|
|
this.setData({ selectedAmount: parseInt(e.currentTarget.dataset.amount) })
|
|
},
|
|
|
|
async handleRecharge() {
|
|
trackClick('wallet', 'btn_click', '充值')
|
|
if (!app.globalData.isLoggedIn || !app.globalData.userInfo) {
|
|
wx.showToast({ title: '请先登录', icon: 'none' })
|
|
return
|
|
}
|
|
const userId = app.globalData.userInfo.id
|
|
const amount = this.data.selectedAmount
|
|
|
|
let openId = app.globalData.openId
|
|
if (!openId) {
|
|
openId = await app.getOpenId()
|
|
}
|
|
if (!openId) {
|
|
wx.showToast({ title: '获取支付凭证失败,请重新登录', icon: 'none', duration: 2500 })
|
|
return
|
|
}
|
|
|
|
wx.showLoading({ title: '创建订单...' })
|
|
try {
|
|
const res = await app.request({
|
|
url: '/api/miniprogram/balance/recharge',
|
|
method: 'POST',
|
|
data: { userId, amount }
|
|
})
|
|
wx.hideLoading()
|
|
if (res && res.data && res.data.orderSn) {
|
|
const payRes = await app.request({
|
|
url: '/api/miniprogram/pay',
|
|
method: 'POST',
|
|
data: {
|
|
openId: openId,
|
|
productType: 'balance_recharge',
|
|
productId: res.data.orderSn,
|
|
amount: amount,
|
|
description: `余额充值 ¥${amount}`,
|
|
userId: userId,
|
|
}
|
|
})
|
|
const params = (payRes && payRes.data && payRes.data.payParams) ? payRes.data.payParams : (payRes && payRes.payParams ? payRes.payParams : null)
|
|
if (params) {
|
|
wx.requestPayment({
|
|
...params,
|
|
success: async () => {
|
|
await app.request({
|
|
url: '/api/miniprogram/balance/recharge/confirm',
|
|
method: 'POST',
|
|
data: { orderSn: res.data.orderSn }
|
|
})
|
|
wx.showToast({ title: '充值成功', icon: 'success' })
|
|
this.loadBalance()
|
|
this.loadTransactions()
|
|
},
|
|
fail: () => {
|
|
wx.showToast({ title: '支付取消', icon: 'none' })
|
|
}
|
|
})
|
|
} else {
|
|
wx.showToast({ title: payRes?.error || '创建支付失败', icon: 'none' })
|
|
}
|
|
}
|
|
} catch (e) {
|
|
wx.hideLoading()
|
|
console.error('[Wallet] recharge error', e)
|
|
wx.showToast({ title: '充值失败:' + (e.message || e.errMsg || '网络异常'), icon: 'none', duration: 3000 })
|
|
}
|
|
},
|
|
|
|
goBack() {
|
|
wx.navigateBack()
|
|
},
|
|
})
|