feat(策略编辑): 实现策略创建功能并优化API请求

添加策略创建的表单验证和API调用逻辑
移除API请求中对userId的硬编码依赖
使用环境变量配置API基础URL
This commit is contained in:
niannian zheng 2026-03-02 15:13:15 +08:00
parent dc857a98a3
commit 58cf092753
2 changed files with 86 additions and 38 deletions

View File

@ -113,7 +113,10 @@
</template>
<script setup>
import { ref, computed } from 'vue';
import { ref, computed, getCurrentInstance } from 'vue';
const { proxy } = getCurrentInstance();
const api = proxy.$api;
const currentType = ref('weight'); //
@ -181,17 +184,68 @@ const onMaTypeChange = (e) => {
formData.value.maType = types[e.detail.value];
};
const submit = () => {
console.log('保存策略:', {
const submit = async () => {
if (!formData.value.alias) {
uni.showToast({ title: '请输入模型别名', icon: 'none' });
return;
}
const strategyData = {
type: currentType.value,
config: formData.value
});
alias: formData.value.alias,
config: {}
};
switch (currentType.value) {
case 'weight':
if (!formData.value.threshold) {
uni.showToast({ title: '请输入偏离阈值', icon: 'none' });
return;
}
strategyData.config = {
period: formData.value.period,
threshold: parseFloat(formData.value.threshold)
};
break;
case 'ma':
if (!formData.value.fastPeriod || !formData.value.slowPeriod) {
uni.showToast({ title: '请输入快线和慢线周期', icon: 'none' });
return;
}
strategyData.config = {
fastPeriod: parseInt(formData.value.fastPeriod),
slowPeriod: parseInt(formData.value.slowPeriod),
maType: formData.value.maType
};
break;
case 'chandelier':
if (!formData.value.atrPeriod || !formData.value.atrMultiplier) {
uni.showToast({ title: '请输入ATR周期和倍数', icon: 'none' });
return;
}
strategyData.config = {
atrPeriod: parseInt(formData.value.atrPeriod),
atrMultiplier: parseFloat(formData.value.atrMultiplier),
trendMa: formData.value.trendMa ? parseInt(formData.value.trendMa) : null
};
break;
}
console.log('保存策略:', strategyData);
uni.showLoading({ title: '保存中' });
setTimeout(() => {
try {
const res = await api.strategies.createStrategy(strategyData);
console.log('策略创建成功:', res);
uni.hideLoading();
uni.showToast({ title: '策略已保存', icon: 'success' });
setTimeout(() => uni.navigateBack(), 1500);
}, 800);
} catch (error) {
console.error('策略创建失败:', error);
uni.hideLoading();
uni.showToast({ title: '保存失败,请重试', icon: 'none' });
}
};
</script>

View File

@ -5,7 +5,7 @@
console.log('📦 api.js 模块加载成功')
// API基础URL
const BASE_URL = 'https://localhost:7040/';
const BASE_URL = import.meta.env.VITE_API_BASE_URL || 'https://localhost:7040/';
// 请求超时时间
const TIMEOUT = 10000;
@ -65,20 +65,8 @@ const doWechatLogin = async () => {
console.log('🔐 后端登录接口响应:', res);
if (res.statusCode === 200 && res.data?.code === 200 && res.data?.data?.token) {
console.log('✅ 微信登录成功获取到token和userId');
console.log('✅ 微信登录成功获取到token');
uni.setStorageSync('token', res.data.data.token);
// 存储userId到本地存储
const userId = res.data.data.userId || res.data.data.user?.userId;
if (userId) {
uni.setStorageSync('userId', userId);
console.log('✅ userId已存储:', userId);
// 验证存储是否成功
const storedUserId = uni.getStorageSync('userId');
console.log('✅ 验证userId存储结果:', storedUserId);
} else {
console.warn('⚠️ 登录响应中未找到userId');
console.log('登录响应数据:', res.data.data);
}
return res.data;
} else {
console.error('❌ 微信登录失败:', res.data?.message || '登录失败');
@ -168,10 +156,9 @@ const requestWithRetry = async (url, method = 'GET', data = {}, headers = {}, re
if (res.statusCode === 200) {
return res.data;
} else if (res.statusCode === 401) {
// 未授权清除token和userId并重新登录(使用登录锁防止并发)
// 未授权清除token并重新登录(使用登录锁防止并发)
console.log('🔒 登录已过期,开始重新登录...');
uni.removeStorageSync('token');
uni.removeStorageSync('userId');
// 重新登录后重试请求
if (retryCount < 3) {
@ -266,35 +253,42 @@ console.log('📦 api.js 开始导出api对象')
export const api = {
assets: {
getAssetData: () => {
const userId = uni.getStorageSync('userId');
console.log('📤 发起 getAssetData 请求userId:', userId);
return get('/api/v1/portfolio/assets', { userId });
console.log('📤 发起 getAssetData 请求');
return get('/api/v1/portfolio/assets');
},
getHoldings: () => {
const userId = uni.getStorageSync('userId');
console.log('📤 发起 getHoldings 请求userId:', userId);
return get('/api/v1/portfolio', { userId });
console.log('📤 发起 getHoldings 请求');
return get('/api/v1/portfolio');
}
},
strategies: {
getStrategies: () => {
const userId = uni.getStorageSync('userId');
console.log('📤 发起 getStrategies 请求userId:', userId);
return get('/api/v1/strategies', { userId });
console.log('📤 发起 getStrategies 请求');
return get('/api/v1/strategies');
},
createStrategy: (data) => {
console.log('📤 发起 createStrategy 请求:', data);
return post('/api/v1/strategies', data);
},
updateStrategy: (id, data) => {
console.log('📤 发起 updateStrategy 请求:', id, data);
return post(`/api/v1/strategies/${id}`, data);
},
deleteStrategy: (id) => {
console.log('📤 发起 deleteStrategy 请求:', id);
return post(`/api/v1/strategies/${id}/delete`, {});
}
},
user: {
getUserInfo: () => {
const userId = uni.getStorageSync('userId');
console.log('📤 发起 getUserInfo 请求userId:', userId);
return get('/api/user/info', { userId });
console.log('📤 发起 getUserInfo 请求');
return get('/api/user/info');
},
getUserStats: () => {
const userId = uni.getStorageSync('userId');
console.log('📤 发起 getUserStats 请求userId:', userId);
return get('/api/user/stats', { userId });
console.log('📤 发起 getUserStats 请求');
return get('/api/user/stats');
}
}
};