feat(策略编辑): 实现策略创建功能并优化API请求
添加策略创建的表单验证和API调用逻辑 移除API请求中对userId的硬编码依赖 使用环境变量配置API基础URL
This commit is contained in:
parent
dc857a98a3
commit
58cf092753
@ -113,7 +113,10 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup>
|
<script setup>
|
||||||
import { ref, computed } from 'vue';
|
import { ref, computed, getCurrentInstance } from 'vue';
|
||||||
|
|
||||||
|
const { proxy } = getCurrentInstance();
|
||||||
|
const api = proxy.$api;
|
||||||
|
|
||||||
const currentType = ref('weight'); // 当前选中的策略类型
|
const currentType = ref('weight'); // 当前选中的策略类型
|
||||||
|
|
||||||
@ -181,17 +184,68 @@ const onMaTypeChange = (e) => {
|
|||||||
formData.value.maType = types[e.detail.value];
|
formData.value.maType = types[e.detail.value];
|
||||||
};
|
};
|
||||||
|
|
||||||
const submit = () => {
|
const submit = async () => {
|
||||||
console.log('保存策略:', {
|
if (!formData.value.alias) {
|
||||||
|
uni.showToast({ title: '请输入模型别名', icon: 'none' });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const strategyData = {
|
||||||
type: currentType.value,
|
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: '保存中' });
|
uni.showLoading({ title: '保存中' });
|
||||||
setTimeout(() => {
|
|
||||||
|
try {
|
||||||
|
const res = await api.strategies.createStrategy(strategyData);
|
||||||
|
console.log('策略创建成功:', res);
|
||||||
|
|
||||||
uni.hideLoading();
|
uni.hideLoading();
|
||||||
uni.showToast({ title: '策略已保存', icon: 'success' });
|
uni.showToast({ title: '策略已保存', icon: 'success' });
|
||||||
setTimeout(() => uni.navigateBack(), 1500);
|
setTimeout(() => uni.navigateBack(), 1500);
|
||||||
}, 800);
|
} catch (error) {
|
||||||
|
console.error('策略创建失败:', error);
|
||||||
|
uni.hideLoading();
|
||||||
|
uni.showToast({ title: '保存失败,请重试', icon: 'none' });
|
||||||
|
}
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
56
utils/api.js
56
utils/api.js
@ -5,7 +5,7 @@
|
|||||||
console.log('📦 api.js 模块加载成功')
|
console.log('📦 api.js 模块加载成功')
|
||||||
|
|
||||||
// API基础URL
|
// API基础URL
|
||||||
const BASE_URL = 'https://localhost:7040/';
|
const BASE_URL = import.meta.env.VITE_API_BASE_URL || 'https://localhost:7040/';
|
||||||
|
|
||||||
// 请求超时时间
|
// 请求超时时间
|
||||||
const TIMEOUT = 10000;
|
const TIMEOUT = 10000;
|
||||||
@ -65,20 +65,8 @@ const doWechatLogin = async () => {
|
|||||||
console.log('🔐 后端登录接口响应:', res);
|
console.log('🔐 后端登录接口响应:', res);
|
||||||
|
|
||||||
if (res.statusCode === 200 && res.data?.code === 200 && res.data?.data?.token) {
|
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);
|
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;
|
return res.data;
|
||||||
} else {
|
} else {
|
||||||
console.error('❌ 微信登录失败:', res.data?.message || '登录失败');
|
console.error('❌ 微信登录失败:', res.data?.message || '登录失败');
|
||||||
@ -168,10 +156,9 @@ const requestWithRetry = async (url, method = 'GET', data = {}, headers = {}, re
|
|||||||
if (res.statusCode === 200) {
|
if (res.statusCode === 200) {
|
||||||
return res.data;
|
return res.data;
|
||||||
} else if (res.statusCode === 401) {
|
} else if (res.statusCode === 401) {
|
||||||
// 未授权,清除token和userId并重新登录(使用登录锁防止并发)
|
// 未授权,清除token并重新登录(使用登录锁防止并发)
|
||||||
console.log('🔒 登录已过期,开始重新登录...');
|
console.log('🔒 登录已过期,开始重新登录...');
|
||||||
uni.removeStorageSync('token');
|
uni.removeStorageSync('token');
|
||||||
uni.removeStorageSync('userId');
|
|
||||||
|
|
||||||
// 重新登录后重试请求
|
// 重新登录后重试请求
|
||||||
if (retryCount < 3) {
|
if (retryCount < 3) {
|
||||||
@ -266,35 +253,42 @@ console.log('📦 api.js 开始导出api对象')
|
|||||||
export const api = {
|
export const api = {
|
||||||
assets: {
|
assets: {
|
||||||
getAssetData: () => {
|
getAssetData: () => {
|
||||||
const userId = uni.getStorageSync('userId');
|
console.log('📤 发起 getAssetData 请求');
|
||||||
console.log('📤 发起 getAssetData 请求,userId:', userId);
|
return get('/api/v1/portfolio/assets');
|
||||||
return get('/api/v1/portfolio/assets', { userId });
|
|
||||||
},
|
},
|
||||||
getHoldings: () => {
|
getHoldings: () => {
|
||||||
const userId = uni.getStorageSync('userId');
|
console.log('📤 发起 getHoldings 请求');
|
||||||
console.log('📤 发起 getHoldings 请求,userId:', userId);
|
return get('/api/v1/portfolio');
|
||||||
return get('/api/v1/portfolio', { userId });
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
strategies: {
|
strategies: {
|
||||||
getStrategies: () => {
|
getStrategies: () => {
|
||||||
const userId = uni.getStorageSync('userId');
|
console.log('📤 发起 getStrategies 请求');
|
||||||
console.log('📤 发起 getStrategies 请求,userId:', userId);
|
return get('/api/v1/strategies');
|
||||||
return get('/api/v1/strategies', { userId });
|
},
|
||||||
|
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: {
|
user: {
|
||||||
getUserInfo: () => {
|
getUserInfo: () => {
|
||||||
const userId = uni.getStorageSync('userId');
|
console.log('📤 发起 getUserInfo 请求');
|
||||||
console.log('📤 发起 getUserInfo 请求,userId:', userId);
|
return get('/api/user/info');
|
||||||
return get('/api/user/info', { userId });
|
|
||||||
},
|
},
|
||||||
getUserStats: () => {
|
getUserStats: () => {
|
||||||
const userId = uni.getStorageSync('userId');
|
console.log('📤 发起 getUserStats 请求');
|
||||||
console.log('📤 发起 getUserStats 请求,userId:', userId);
|
return get('/api/user/stats');
|
||||||
return get('/api/user/stats', { userId });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user