script export default data return prompt responseData response loading false ; methods submit if thisprompt !== thissubmitData; else unishowToast
在submitData方法中,将返回的responseData字符串按照换行符分割成数组,然后使用定时器逐步将数组中的每个元素添加到response变量中,实现一点渲染一点的效果。代码如下:
submitData() { uni.showLoading({ title: "请耐心等待回答结果..." }); // 显示等待框 this.loading = true; uni.request({ url: "https://svip1-api.cveoy.top/v3/completions", // 假设接口地址为 http://localhost:3000/data method: "POST", data: { prompt: this.prompt, keys: "sk-eae8a6f4e46c49fb9c1c8e933b2a33e7" }, success: res => { let responseData = res.data; responseData = responseData.replace("你当前使用的模型为gpt-3.5-turbo!", "欢迎使用Clever Helmet智慧问答系统\n"); console.log(responseData); this.responseData = responseData; let responseArray = responseData.split("\n"); let i = 0; let timer = setInterval(() => { if (i < responseArray.length) { this.response += responseArray[i] + "\n"; i++; } else { clearInterval(timer); } }, 1000); }, complete: () => { uni.hideLoading(); // 隐藏等待框 this.loading = false; } });
原文地址: https://www.cveoy.top/t/topic/ffLQ 著作权归作者所有。请勿转载和采集!