xiaokuaisong-xiaochengxu/uniapp04/pages/testAI/testAI.vue
2025-04-11 14:42:29 +08:00

89 lines
2.0 KiB
Vue

<template>
<view class="container">
<input
class="input"
v-model="inputText"
placeholder="请输入您的问题"
@confirm="sendQuestion"
/>
<button class="button" @click="sendQuestion">发送提问</button>
<view class="answer-box" v-if="answer">
<text class="answer-text">{{ answer }}</text>
</view>
</view>
</template>
<script>
export default {
data() {
return {
inputText: '',
answer: ''
};
},
methods: {
async sendQuestion() {
try {
const res = await uni.request({
url: 'https://api.siliconflow.com/v1/chat/completions',
method: 'POST',
header: {
'Content-Type': 'application/json',
'Authorization': 'Bearer sk-rbbgeretfnwjpnmqyzutorjfwvzysnuykeowudkhuaeuokdm'
},
data: {
model: 'deepseek-ai/DeepSeek-R1',
messages: [{ role: "user", content: this.inputText }]
}
});
if (res.statusCode === 200) {
this.answer = res.data.choices[0].message.content;
} else {
const errorMsg = res.data?.error?.message || 'API返回未知错误';
this.answer = `请求失败:${errorMsg} (状态码:${res.statusCode})`;
console.error('API错误详情:', res.data); // 调试用
}
} catch (error) {
this.answer = `网络异常:${error.errMsg || '请检查网络连接'}`;
console.error('请求异常:', error); // 调试用
} finally {
uni.hideLoading();
}
}
}
}
</script>
<style>
.container {
padding: 20rpx;
}
.input {
height: 100rpx;
border: 1rpx solid #eee;
border-radius: 16rpx;
padding: 0 20rpx;
margin-bottom: 20rpx;
}
.button {
background-color: #1677FF;
color: white;
margin-bottom: 40rpx;
}
.answer-box {
padding: 20rpx;
background-color: #f9f9f9;
border-radius: 12rpx;
}
.answer-text {
font-size: 28rpx;
line-height: 1.6;
}
</style>