对话调用接口

This commit is contained in:
caicai8 2019-12-19 18:24:00 +08:00
parent 1bdbf1a0d8
commit 0076759583
1 changed files with 88 additions and 10 deletions

View File

@ -69,18 +69,33 @@
var html = '<div class="mWord"><span></span><p>'+value+'</p></div>';
$(".b_body").append(html);
var answer = "";
for(var i = 0 ;i<array.length;i++){
if( array[i].input.indexOf(value) > -1 ){
answer = '<div class="rotWord"><span></span><p id="member">'+array[i].output+'</p></div>';
continue;
var args= {
type : "get",
url:"https://api.ownthink.com/bot",
data : {"appid" : "xiaosi", "spoken" : value},
success : function(redata)//回调函数
{
var my_data = $.parseJSON(redata)
var array= [my_data.data.info.text];
if(my_data.data.info.hasOwnProperty("heuristic"))
{
for (var i=0; i < my_data.data.info.heuristic.length; i++)
{
array.push(my_data.data.info.heuristic[i]);
}
}
answer = answer || '<div class="rotWord"><span></span><p id="member">你说什么?能再说一遍吗?</p></div>';
setTimeout(() => {
$(".b_body").append(answer);
for (var i=0; i < array.length; i++)
{
var result = array[i];
$(".b_body").append("<div class='rotWord'><span></span> <p id='member'>" + result + "</p></div>");
$(".b_body").scrollTop(10000000);
}, 500);
}
}
}
ajax(args);
$(".b_body").scrollTop(10000000);
text.val('');
}
@ -95,6 +110,69 @@
action(text.val());
}
});
function ajax(mJson)
{
var type=mJson.type||'get';
var url=mJson.url;
var data=mJson.data;
var success=mJson.success;
var error=mJson.error;
var dataStr='';
//console.log(data);
//请求数据封装
if(data)
{
var arr = Object.keys(data);//获取key数组以便获取长度
var len = arr.length;
var i = 0;
for (var key in data)
{
dataStr+=key+'='+data[key];
if (++i<len)
{
dataStr+='&';
}
}
if(type.toLowerCase()=='get')
{
url+='?'+dataStr;
}
// dataStr+=new Date().getTime();//设计时需要发送时间。。。
// if(type.toLowerCase()=='get')
// {
// url+='?'+dataStr;
// }
}
//console.log(url);
var xhr=new XMLHttpRequest();
xhr.open(type,url,true);
xhr.setRequestHeader('content-type' , 'application/x-www-form-urlencoded');
xhr.send(null);
xhr.onreadystatechange=function()
{
if(xhr.readyState==4)
{
if(xhr.status>=200&&xhr.status<300)
{
success&&success(xhr.responseText);
}
else
{
error&&error(xhr.status);
}
}
}
}
</script>
</body>
</html>