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