对接API

This commit is contained in:
weipengfei 2023-09-18 13:43:58 +08:00
parent ee7b9a341e
commit 4a7f2945f8
3 changed files with 317 additions and 225 deletions

View File

@ -5,12 +5,15 @@
<view class="placeholder"></view>
<view class="box-1" id="list-box" ref="box">
<view class="talk-list">
<scroll-view :scroll-y="true" style="height: 100%;overflow: hidden;" :scroll-top="scrollTop"
:scroll-with-animation="true" @scrolltoupper="bindScroll">
<view v-for="(item,index) in talkList" :key="index" :id="`msg-${item.id}`">
<view class="item flex_col" :class=" item.type == 1 ? 'push':'pull' ">
<image :src="item.pic" mode="aspectFill" class="pic"></image>
<view v-if="talkList.length-1==index" class="content multiline-text">
<!-- <rich-text :nodes="item.content"></rich-text> -->
<bing-math :key="`math-${item.id}`" class="bing-math" :latex="c_content"></bing-math>
<bing-math v-if="c_content!=''" :key="`math-${item.id}`" class="bing-math" :latex="c_content"></bing-math>
<view v-else class="blinking-box">|</view>
</view>
<view v-else class="content multiline-text">
<!-- <rich-text :nodes="item.content"></rich-text> -->
@ -18,9 +21,9 @@
</view>
</view>
</view>
</scroll-view>
</view>
</view>
<!-- <view v-show="showplc" :style="{'min-height': (keyboardHeight+200)+'px'}" class="placeholder">显示</view> -->
<view class="box-2">
<view class="flex_col">
<view class="flex_grow">
@ -30,6 +33,7 @@
<button class="send" @tap="send">发送</button>
</view>
</view>
<!-- <view v-show="showplc" :style="{'min-height': (keyboardHeight+200)+'px'}" class="placeholder">显示</view> -->
</view>
</template>
@ -61,13 +65,11 @@
c_content: '',
n_content: '',
timer: '',
TEXT: '你好,我的名字叫大',
APPID: '2eda6c2e', //
APIKey: '12ec1f9d113932575fc4b114a2f60ffd',
APISecret: 'MDEyMzE5YTc5YmQ5NjMwOTU1MWY4N2Y2',
sparkResult: '',
socketTask: {},
TEXT: '',
historyTextList: [], // token12000,使
tempRes: '', //
scrollTop: 0
}
},
mounted() {
@ -80,9 +82,14 @@
if(e.height==0)h *= -1;
else h = e.height;
uni.createSelectorQuery().selectViewport().scrollOffset(function(res) {
const scrollTop = res.scrollTop; //
let scrollTop = res.scrollTop; //
// let os = uni.getSystemInfoSync();
// // = 使 - -
// const viewportHeight = os.windowHeight - os.statusBarHeight - (os.platform == "android" ? 50 : 45)-10;
// if(scrollTop<viewportHeight)scrollTop=viewportHeight;
scrollTop+=h;
uni.pageScrollTo({
scrollTop: scrollTop + h, //
scrollTop: scrollTop, //
duration: 0 // 300ms300ms
});
}).exec();
@ -112,15 +119,6 @@
uni.pageScrollTo({
scrollTop: 999999,
})
// uni.createSelectorQuery()
// .in(this)
// .select('.box-1')
// .boundingClientRect((rect) => {
// const height = rect?.height;
// console.log(height, rect);
// })
// .exec();
})
} else {
clearInterval(this.timer);
@ -151,7 +149,6 @@
return; //
}
// ES7 async/await 使
let get = async () => {
this.hideLoadTips();
this.ajax.flag = false;
@ -197,7 +194,7 @@
}
get();
},
//
//
joinHistoryMsg() {
let join = () => {
let arr = [];
@ -269,6 +266,23 @@
})
return;
}
try{
clearInterval(this.timer);
this.talkList[this.talkList.length-1].content = this.c_content+'';
this.c_content = '';
this.n_content = '';
this.socketTask.close({
success:(res)=> {
console.log('关闭成功', res);
this.wsLiveFlag = false;
},
fail(err) {
console.log('关闭失败', err)
}
})
}catch(e){
//TODO handle the exception
}
//
let data = {
"id": new Date().getTime(),
@ -286,6 +300,7 @@
"type": 2,
"pic": "/static/avatar.png"
});
// return ;
this.$nextTick(() => {
//
this.content = '';
@ -298,16 +313,15 @@
},
async sendToSpark() {
let myUrl = await this.getWebSocketUrl();
// let myUrl = await this.getWebSocketUrl();
this.tempRes = "";
// this.sparkResult = "";
let realThis = this;
this.socketTask = uni.connectSocket({
//url: encodeURI(encodeURI(myUrl).replace(/\+/g, '%2B')),
url: myUrl,
url: 'wss://chat.lihaink.cn/chat',
method: 'GET',
success: res => {
console.log(res, "ws成功连接...", myUrl)
console.log(res, "ws成功连接...")
realThis.wsLiveFlag = true;
}
})
@ -319,31 +333,27 @@
"role": "user",
"content": this.TEXT
})
console.info("wss的onOpen成功执行...", res)
// ..........................................
console.log('open成功...')
let params = {
"header": {
"app_id": this.APPID,
"uid": "aef9f963-7"
},
"parameter": {
"chat": {
"domain": "generalv2",
"temperature": 0.5,
"max_tokens": 1024
}
},
"payload": {
"message": {
"text": this.historyTextList
}
}
};
console.log("请求的params" + JSON.stringify(params))
this.sparkResult = this.sparkResult + "\r\n我" + this.TEXT + "\r\n"
this.sparkResult = this.sparkResult + "大模型:"
console.log("发送第一帧...", params)
console.log('连接成功...')
// let params = {
// "header": {
// "app_id": this.APPID,
// "uid": "aef9f963-7"
// },
// "parameter": {
// "chat": {
// "domain": "generalv2",
// "temperature": 0.5,
// "max_tokens": 1024
// }
// },
// "payload": {
// "message": {
// "text": this.historyTextList
// }
// }
// };
let params = this.TEXT
realThis.socketTask.send({ // uni
data: JSON.stringify(params),
success() {
@ -359,12 +369,10 @@
// console.log(""+obj.payload);
let dataArray = obj.payload.choices.text;
for (let i = 0; i < dataArray.length; i++) {
realThis.sparkResult = realThis.sparkResult + dataArray[i].content;
this.talkList[this.talkList.length - 1].content += dataArray[i].content;
this.n_content = this.talkList[this.talkList.length - 1].content;
realThis.tempRes = realThis.tempRes + dataArray[i].content
}
// realThis.sparkResult =realThis.sparkResult+
let temp = JSON.parse(res.data)
// console.log("0726",temp.header.code)
if (temp.header.code !== 0) {
@ -381,16 +389,10 @@
}
if (temp.header.code === 0) {
if (res.data && temp.header.status === 2) {
realThis.sparkResult = realThis.sparkResult +
"\r\n**********************************************"
this.historyTextList.push({
"role": "assistant",
"content": this.tempRes
})
/* let dataArray= obj.payload.choices.text;
for(let i=0;i<dataArray.length;i++){
realThis.sparkResult =realThis.sparkResult+ dataArray[i].content
} */
setTimeout(() => {
realThis.socketTask.close({
success(res) {
@ -428,6 +430,18 @@
resolve(url);
});
},
//
bindScroll() {
if (this.userId == 0) {
this.getHistory();
this.getproductInfo();
this.getOrderInfo();
this.getRefundDetail();
this.getStoreDetail();
} else {
this.getMerHistory();
}
},
}
}
</script>
@ -440,6 +454,20 @@
font-size: 28rpx;
}
@keyframes blink {
0% { opacity: 1; }
50% { opacity: 0; }
100% { opacity: 1; }
}
.blinking-box {
background-color: #333;
color: #333;
animation: blink 1s infinite;
width: 2px;
overflow: hidden;
}
.wrapper {
height: auto !important;
}

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long