consumer模块完成95%,在和商家端对接聊天购物闭环
This commit is contained in:
@@ -167,8 +167,9 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="uts">
|
||||
import { ref, onMounted, computed, watch } from 'vue'
|
||||
//import supa from '@/components/supadb/aksupainstance.uts'
|
||||
import { ref, computed, watch } from 'vue'
|
||||
import { onShow } from '@dcloudio/uni-app'
|
||||
import { supabaseService } from '@/utils/supabaseService.uts'
|
||||
|
||||
type WalletType = {
|
||||
id: string
|
||||
@@ -226,7 +227,7 @@ watch(activeFilter, () => {
|
||||
})
|
||||
|
||||
// 生命周期
|
||||
onMounted(() => {
|
||||
onShow(() => {
|
||||
loadWalletData()
|
||||
})
|
||||
|
||||
@@ -240,10 +241,10 @@ const resetTransactions = () => {
|
||||
// 加载钱包数据
|
||||
const loadWalletData = async () => {
|
||||
const userId = getCurrentUserId()
|
||||
if (!userId) {
|
||||
uni.navigateTo({
|
||||
url: '/pages/user/login'
|
||||
})
|
||||
if (userId == null) {
|
||||
// uni.navigateTo({
|
||||
// url: '/pages/user/login'
|
||||
// })
|
||||
return
|
||||
}
|
||||
|
||||
@@ -255,35 +256,20 @@ const loadWalletData = async () => {
|
||||
|
||||
// 加载余额信息
|
||||
const loadBalance = async () => {
|
||||
const userId = getCurrentUserId()
|
||||
if (!userId) return
|
||||
|
||||
try {
|
||||
// 使用本地模拟数据
|
||||
const mockBalance = {
|
||||
balance: 12580.00,
|
||||
total_recharge: 20000.00,
|
||||
total_consume: 7420.00,
|
||||
total_withdraw: 0.00
|
||||
}
|
||||
|
||||
// 尝试从本地存储获取
|
||||
const storedWallet = uni.getStorageSync(`wallet_${userId}`)
|
||||
const data = storedWallet ? JSON.parse(storedWallet as string) : mockBalance
|
||||
|
||||
if (data) {
|
||||
// 类型断言,处理 any 类型
|
||||
const walletData = data as any
|
||||
balance.value = Number(walletData.balance || 0)
|
||||
stats.value = {
|
||||
totalRecharge: Number(walletData.total_recharge || 0),
|
||||
totalConsume: Number(walletData.total_consume || 0),
|
||||
totalWithdraw: Number(walletData.total_withdraw || 0)
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('加载钱包异常:', err)
|
||||
}
|
||||
try {
|
||||
// 调用 Supabase 服务获取真实余额
|
||||
const realBalance = await supabaseService.getUserBalance()
|
||||
balance.value = realBalance
|
||||
|
||||
// 统计数据暂时保持 mock 或设为 0,因为后端还未实现具体统计接口
|
||||
stats.value = {
|
||||
totalRecharge: 0,
|
||||
totalConsume: 0,
|
||||
totalWithdraw: 0
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('加载钱包异常:', err)
|
||||
}
|
||||
}
|
||||
|
||||
// 加载交易记录
|
||||
@@ -295,65 +281,70 @@ const loadTransactions = async (loadMore: boolean = false) => {
|
||||
isLoading.value = true
|
||||
|
||||
try {
|
||||
const userId = getCurrentUserId()
|
||||
if (!userId) return
|
||||
const userId = getCurrentUserId()
|
||||
if (!userId) {
|
||||
isLoading.value = false
|
||||
return
|
||||
}
|
||||
|
||||
const page = loadMore ? currentPage.value + 1 : 1
|
||||
|
||||
// 模拟交易记录数据
|
||||
const mockTransactions: TransactionType[] = [
|
||||
{
|
||||
id: 't1',
|
||||
user_id: userId,
|
||||
change_amount: -128.00,
|
||||
current_balance: 12580.00,
|
||||
change_type: 'consume',
|
||||
related_id: 'ord_001',
|
||||
remark: '购买药品',
|
||||
created_at: new Date().toISOString()
|
||||
},
|
||||
{
|
||||
id: 't2',
|
||||
user_id: userId,
|
||||
change_amount: 500.00,
|
||||
current_balance: 12708.00,
|
||||
change_type: 'recharge',
|
||||
related_id: 'rec_001',
|
||||
remark: '账户充值',
|
||||
created_at: new Date(Date.now() - 86400000).toISOString()
|
||||
},
|
||||
{
|
||||
id: 't3',
|
||||
user_id: userId,
|
||||
change_amount: -58.50,
|
||||
current_balance: 12208.00,
|
||||
change_type: 'consume',
|
||||
related_id: 'ord_002',
|
||||
remark: '购买保健品',
|
||||
created_at: new Date(Date.now() - 172800000).toISOString()
|
||||
}
|
||||
]
|
||||
|
||||
// 简单模拟分页和筛选
|
||||
let filtered = mockTransactions
|
||||
if (activeFilter.value === 'income') {
|
||||
filtered = filtered.filter(t => t.change_amount > 0)
|
||||
} else if (activeFilter.value === 'expense') {
|
||||
filtered = filtered.filter(t => t.change_amount < 0)
|
||||
}
|
||||
|
||||
const newTransactions = filtered
|
||||
const page = loadMore ? currentPage.value + 1 : 1
|
||||
const limit = 20
|
||||
|
||||
// 使用 Supabase 获取真实数据
|
||||
// 注意:目前后端接口暂不支持 activeFilter 筛选,会返回所有记录
|
||||
const data = await supabaseService.getTransactions(page, limit)
|
||||
|
||||
const mappedData: TransactionType[] = []
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
const item = data[i]
|
||||
let id = ''
|
||||
let amount = 0
|
||||
let balance = 0
|
||||
let type = ''
|
||||
let remark = ''
|
||||
let createdAt = ''
|
||||
|
||||
if (item instanceof UTSJSONObject) {
|
||||
id = item.getString('id') || ''
|
||||
amount = item.getNumber('amount') || 0
|
||||
balance = item.getNumber('balance_after') || 0
|
||||
type = item.getString('type') || 'consume'
|
||||
remark = item.getString('description') || ''
|
||||
createdAt = item.getString('created_at') || ''
|
||||
} else {
|
||||
id = (item['id'] as string) || ''
|
||||
amount = (item['amount'] as number) || 0
|
||||
balance = (item['balance_after'] as number) || 0
|
||||
type = (item['type'] as string) || 'consume'
|
||||
remark = (item['description'] as string) || ''
|
||||
createdAt = (item['created_at'] as string) || ''
|
||||
}
|
||||
|
||||
mappedData.push({
|
||||
id: id,
|
||||
user_id: userId,
|
||||
change_amount: amount,
|
||||
current_balance: balance,
|
||||
change_type: type,
|
||||
related_id: null,
|
||||
remark: remark,
|
||||
created_at: createdAt
|
||||
})
|
||||
}
|
||||
|
||||
if (loadMore) {
|
||||
transactions.value.push(...newTransactions)
|
||||
currentPage.value = page
|
||||
transactions.value.push(...mappedData)
|
||||
} else {
|
||||
transactions.value = newTransactions
|
||||
currentPage.value = 1
|
||||
transactions.value = mappedData
|
||||
}
|
||||
|
||||
// 模拟没有更多数据
|
||||
hasMore.value = false
|
||||
|
||||
if (mappedData.length < limit) {
|
||||
hasMore.value = false
|
||||
} else {
|
||||
hasMore.value = true
|
||||
}
|
||||
|
||||
currentPage.value = page
|
||||
} catch (err) {
|
||||
console.error('加载交易记录异常:', err)
|
||||
} finally {
|
||||
@@ -452,21 +443,22 @@ const goToCoupons = () => {
|
||||
// 跳转到红包
|
||||
const goToRedPackets = () => {
|
||||
uni.navigateTo({
|
||||
url: '/pages/mall/consumer/red-packets'
|
||||
url: '/pages/mall/consumer/red-packets/index'
|
||||
})
|
||||
}
|
||||
|
||||
// 跳转到积分
|
||||
const goToPoints = () => {
|
||||
// 使用统一的积分页面
|
||||
uni.navigateTo({
|
||||
url: '/pages/mall/consumer/points'
|
||||
url: '/pages/mall/consumer/points/index'
|
||||
})
|
||||
}
|
||||
|
||||
// 跳转到银行卡
|
||||
const goToBankCards = () => {
|
||||
uni.navigateTo({
|
||||
url: '/pages/mall/consumer/bank-cards'
|
||||
url: '/pages/mall/consumer/bank-cards/index'
|
||||
})
|
||||
}
|
||||
|
||||
@@ -494,12 +486,32 @@ const confirmRecharge = async () => {
|
||||
const amount = parseFloat(rechargeAmount.value)
|
||||
if (isNaN(amount)) return
|
||||
|
||||
// 这里应该跳转到支付页面进行充值
|
||||
uni.navigateTo({
|
||||
url: `/pages/mall/consumer/payment?type=recharge&amount=${amount}`
|
||||
})
|
||||
|
||||
closeRechargePopup()
|
||||
uni.showLoading({ title: '处理中...' })
|
||||
try {
|
||||
const success = await supabaseService.rechargeBalance(amount)
|
||||
if (success) {
|
||||
uni.showToast({
|
||||
title: '充值成功',
|
||||
icon: 'success'
|
||||
})
|
||||
closeRechargePopup()
|
||||
// 刷新数据
|
||||
loadWalletData()
|
||||
} else {
|
||||
uni.showToast({
|
||||
title: '充值失败',
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('充值异常:', e)
|
||||
uni.showToast({
|
||||
title: '系统异常,请稍后重试',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
uni.hideLoading()
|
||||
}
|
||||
}
|
||||
|
||||
// 关闭充值弹窗
|
||||
|
||||
Reference in New Issue
Block a user