feat(P0): 替换全局Toast/Modal为uView组件,样式统一

This commit is contained in:
claw_bot 2026-03-12 11:46:42 +00:00
parent 2b6c8b4575
commit 0b09c9eed8
3 changed files with 38 additions and 18 deletions

22
App.vue
View File

@ -1,3 +1,15 @@
<script setup>
// u-toast
import { useToast } from 'uview-plus'
const toast = useToast()
//
uni.$u = {
toast,
showModal: useToast().showModal
}
</script>
<script> <script>
export default { export default {
onLaunch: function() { onLaunch: function() {
@ -12,6 +24,16 @@ export default {
} }
</script> </script>
<template>
<view>
<router-view />
<!-- 全局toast组件 -->
<u-toast ref="uToast" />
<!-- 全局modal组件 -->
<u-modal ref="uModal" />
</view>
</template>
<style> <style>
/* 每个页面公共css */ /* 每个页面公共css */
page { page {

View File

@ -410,7 +410,7 @@ const fetchPortfolioData = async () => {
} }
} catch (error) { } catch (error) {
console.error('获取投资组合数据失败:', error); console.error('获取投资组合数据失败:', error);
uni.showToast({ title: '加载失败,请重试', icon: 'none' }); uni.$u.toast.error('加载失败,请重试');
} }
}; };
@ -430,7 +430,7 @@ const fetchTransactions = async () => {
} }
} catch (error) { } catch (error) {
console.error('获取交易记录失败:', error); console.error('获取交易记录失败:', error);
uni.showToast({ title: '加载交易记录失败', icon: 'none' }); uni.$u.toast.error('加载交易记录失败');
} }
}; };
@ -488,18 +488,18 @@ const resetTransactionForm = () => {
const submitTransaction = async () => { const submitTransaction = async () => {
// //
if (!transactionForm.value.stockCode) { if (!transactionForm.value.stockCode) {
return uni.showToast({ title: transactionType.value === 'sell' ? '请选择要卖出的持仓' : '请输入股票代码', icon: 'none' }); return uni.$u.toast.error(transactionType.value === 'sell' ? '请选择要卖出的持仓' : '请输入股票代码');
} }
const amount = parseFloat(transactionForm.value.amount); const amount = parseFloat(transactionForm.value.amount);
if (!amount || amount <= 0) { if (!amount || amount <= 0) {
return uni.showToast({ title: '请输入有效的数量', icon: 'none' }); return uni.$u.toast.error('请输入有效的数量');
} }
// //
if (transactionType.value === 'sell' && amount > maxSellAmount.value) { if (transactionType.value === 'sell' && amount > maxSellAmount.value) {
return uni.showToast({ title: `卖出数量不能超过持仓数量 ${maxSellAmount.value}`, icon: 'none' }); return uni.$u.toast.error(`卖出数量不能超过持仓数量 ${maxSellAmount.value}`);
} }
if (!transactionForm.value.price || parseFloat(transactionForm.value.price) <= 0) { if (!transactionForm.value.price || parseFloat(transactionForm.value.price) <= 0) {
return uni.showToast({ title: '请输入有效的价格', icon: 'none' }); return uni.$u.toast.error('请输入有效的价格');
} }
const transactionData = { const transactionData = {
@ -513,13 +513,12 @@ const submitTransaction = async () => {
remark: transactionForm.value.remark remark: transactionForm.value.remark
}; };
uni.showLoading({ title: '提交中...' }); uni.$u.toast.loading('提交中...');
try { try {
const response = await api.assets.createTransaction(transactionData); const response = await api.assets.createTransaction(transactionData);
if (response.code === 200) { if (response.code === 200) {
uni.hideLoading(); uni.$u.toast.success('交易提交成功');
uni.showToast({ title: '交易提交成功', icon: 'success' });
showTransactionForm.value = false; showTransactionForm.value = false;
// //
@ -529,19 +528,20 @@ const submitTransaction = async () => {
} }
} catch (error) { } catch (error) {
console.error('创建交易失败:', error); console.error('创建交易失败:', error);
uni.hideLoading(); uni.$u.toast.error('提交失败,请重试');
uni.showToast({ title: '提交失败,请重试', icon: 'none' });
} }
}; };
// //
const deletePortfolio = async () => { const deletePortfolio = async () => {
uni.showModal({ uni.$u.showModal({
title: '确认删除', title: '确认删除',
content: '删除后所有持仓和交易记录都会丢失,确定要删除这个组合吗?', content: '删除后所有持仓和交易记录都会丢失,确定要删除这个组合吗?',
confirmText: '删除',
confirmColor: '#EF4444',
success: async (res) => { success: async (res) => {
if (res.confirm) { if (res.confirm) {
uni.showLoading({ title: '删除中' }); uni.$u.toast.loading('删除中');
try { try {
// //
const response = await uni.request({ const response = await uni.request({
@ -553,15 +553,13 @@ const deletePortfolio = async () => {
}); });
if (response.statusCode === 200) { if (response.statusCode === 200) {
uni.hideLoading(); uni.$u.toast.success('删除成功');
uni.showToast({ title: '删除成功', icon: 'success' });
setTimeout(() => uni.switchTab({ url: '/pages/index/index' }), 1500); setTimeout(() => uni.switchTab({ url: '/pages/index/index' }), 1500);
} else { } else {
throw new Error('删除失败'); throw new Error('删除失败');
} }
} catch (error) { } catch (error) {
uni.hideLoading(); uni.$u.toast.error('删除失败,请重试');
uni.showToast({ title: '删除失败,请重试', icon: 'none' });
} }
} }
} }

View File

@ -199,7 +199,7 @@ const requestWithRetry = async (url, method = 'GET', data = {}, headers = {}, re
return await requestWithRetry(url, method, data, headers, retryCount + 1); return await requestWithRetry(url, method, data, headers, retryCount + 1);
} else { } else {
console.error('❌ 达到最大重试次数'); console.error('❌ 达到最大重试次数');
uni.showToast({ title: '系统异常,请稍后重试', icon: 'none', duration: 2000 }); uni.$u.toast.error('系统异常,请稍后重试');
throw new Error('登录已过期,重试次数超限'); throw new Error('登录已过期,重试次数超限');
} }
} else { } else {