This commit is contained in:
jia 2023-10-18 18:40:58 +08:00
parent 9df9beb336
commit 7b6a516048
2 changed files with 248 additions and 543 deletions

View File

@ -1,6 +1,6 @@
{
"name" : "农业咨询",
"appid" : "__UNI__3A527D1",
"name" : "教育咨询",
"appid" : "__UNI__7AA205C",
"description" : "",
"versionName" : "1.0.0",
"versionCode" : "100",
@ -17,11 +17,7 @@
"delay" : 0
},
/* */
"modules" : {
"Record" : {},
"Speech" : {},
"Camera" : {}
},
"modules" : {},
/* */
"distribute" : {
/* android */
@ -51,9 +47,7 @@
},
/* SDK */
"sdkConfigs" : {
"ad" : {},
"speech" : {},
"share" : {}
"ad" : {}
}
}
},
@ -61,7 +55,7 @@
"quickapp" : {},
/* */
"mp-weixin" : {
"appid" : "wx3267f87bc81f2890",
"appid" : "",
"setting" : {
"urlCheck" : false
},

View File

@ -22,20 +22,20 @@
您好
</view>
<view class="talk-list-con-title1">
您可以和我说任何有关农业的问题<br />
您可以和我说任何有关教育的问题<br />
例如下面的问题赶快试试吧
</view>
<view class="talk-list-con-title2">
<text style="margin-top: 15rpx;"> 下方输入</text>
<view class="" @click="sendDetail('冬季怎么保存蔬菜')">
冬季怎么保存蔬菜
<view class="" @click="sendDetail('怎么教育孩子')">
怎么教育孩子
<image src="../../static/icon/airight.png" mode=""></image>
</view>
</view>
</view>
<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.type==1?avatar:item.pic" mode="aspectFill" class="pic" v-if="item.pic">
</image>
@ -60,10 +60,10 @@
</view>
<view class="contenta" v-if="c_content!=''&&index==nums">
<view class="contenta-img" v-if='palystatus' @click="palyaudio(item)">
<view class="contenta-img" v-if='palystatus' @click="palyaudio(item,index)">
<image src="@/static/icon/ai7.png" mode="aspectFit"></image>
</view>
<view class="contenta-img" v-else @click="palyaudio(item)">
<view class="contenta-img" v-else @click="palyaudio(item,index)">
<image src="@/static/icon/ai8.png" mode="aspectFit"></image>
</view>
<view class="contenta-ai" v-show="palystatus==false">
@ -112,8 +112,7 @@
</view>
<view class="flex_c_txt" v-else>
<view class="con" @touchend="end" @touchmove="move" @touchstart="start" @longpress="longpress"
@click="longclick">
<view class="con" @touchend="end" @touchmove="move" @touchstart="start">
{{btnStatus==0? "按住说话":btnStatus==1? "说话中...": btnStatus==2? "松开手指发送录音": "上划取消"}}
</view>
<!-- <view :class=" btnStatus==0? 'footerBtn': btnStatus==1?'footerBtn_star':'footerBtn_move'">
@ -182,8 +181,7 @@
</view>
</uni-popup>
<qiqbshare ref="child" ></qiqbshare>
<qiqbshare ref="child"></qiqbshare>
</view>
</template>
@ -194,7 +192,9 @@
iatWss,
debounce,
throttle,
ttWss
fileToBase64,
dataURLtoBlob,
ttWss,
} from "@/api/api.js"
import {
HTTP_REQUEST_URL
@ -214,7 +214,8 @@
const recorderManager = uni.getRecorderManager()
// module
const sr = uni.requireNativePlugin("YL-SpeechRecognition")
const innerAudioContext = uni.createInnerAudioContext()
export default {
@ -226,8 +227,8 @@
},
data() {
return {
wssType: 'nongye', //
avatarType: "/static/icon/ny.png", // AI
wssType: 'jiaoyu', //
avatarType: "/static/icon/jy.png", // AI
talkList: [],
ajax: {
rows: 15, //
@ -273,16 +274,15 @@
touchMoveX: 0, // x
touchMoveY: 0, // y
risshow: false,
touchT: null,
touchE: null,
islong: false,
//
isDraw: false,
imageSize: {
}
},
audio_file: [],
currentIndex: 0,
wv: null
}
},
mounted() {
@ -302,7 +302,20 @@
})
})
//
sr.init("2eda6c2e");
//
sr.createTts(code => {});
//
sr.createIat(code => {
console.log(code, '1111111111')
});
sr.setVadBos(10 * 1000); //ms10s,
sr.setVadEos(10 * 1000); //ms10s,
},
beforeDestroy() {
sr.destroy();
},
onLoad() {
let avatar = uni.getStorageSync('avatar');
@ -311,6 +324,9 @@
this.avatar = e.avatar;
})
},
beforeDestroy() {
// #ifdef APP-PLUS
@ -322,24 +338,7 @@
this.getHistoryMsg();
}
},
created() {
//
// uni.setInnerAudioOption({
// obeyMuteSwitch: false
// })
let that = this;
recorderManager.onStop(function(res) {
// ,
that.$nextTick(() => {
// console.log('recorder stop' + JSON.stringify(res));
that.audioDuration = res.duration;
that.voicePath = res.tempFilePath;
})
});
this.list()
},
watch: {
n_content(n, o) {
@ -373,19 +372,20 @@
methods: {
//
cavshare(data) {
let arry = []
let arry1 = []
let a;
let b;
if(data.length>2){
a=data.length-2
b=data.length-1
}else{
a=0
b=1
}
if (data.length > 2) {
a = data.length - 2
b = data.length - 1
} else {
a = 0
b = 1
}
for (let i in data[a].content) {
if (data[a].content.slice(i * 22, (i + 1) * 22)) {
@ -402,9 +402,9 @@
}
// console.log(arry1.length,arry.length)
this.imageSize.height=(arry1.length*20)+(arry.length*20)+100+150
this.imageSize.height = (arry1.length * 20) + (arry.length * 20) + 100 + 150
this.$refs.child.refresh(this.talkList,this.imageSize.height,(arry.length*20)+100);
this.$refs.child.refresh(this.talkList, this.imageSize.height, (arry.length * 20) + 100);
},
@ -524,12 +524,7 @@
})
},
//
list() {
iatWss().then((res) => {
this.audioUrl = res.data.iat_wss
})
},
//
addgn() {
@ -541,69 +536,7 @@
this.$refs.popup.close()
},
//
uploadFile(tempFilePath) {
let that = this
// console.log(tempFilePath)
uni.uploadFile({
url: HTTP_REQUEST_URL + '/api/xun_fei/iat', //
filePath: tempFilePath, //
name: 'audio', // key
header: {
'content-type': 'multipart/form-data'
},
success: function(res) {
// console.log(res)
let data = JSON.parse(res.data)
if (data.code == 1) {
uni.showToast({
title: '语音识别成功'
});
that.talkList = that.talkList.filter(item => item.id !== '@');
if (data.data.words.length > 0) {
that.content = data.data.words
that.send1()
}
// console.log( that.talkList)
// if (that.talkList.length > 0) {
// that.talkList.splice(that.talkList.length - 1, 1)
// } else {
// that.talkList.splice(0, 1)
// }
that.voicePath = ''
// if (that.content.length > 0) {
// that.send()
// }
} else {
that.talkList = that.talkList.filter(item => item.id !== '@');
uni.showToast({
title: '语音识别失败'
});
}
},
fail: function(error) {
//
that.deletespeech()
console.log(error, 11);
uni.hideLoading();
}
})
},
//
onFeedTap() {
let platform = uni.getSystemInfoSync().platform
@ -628,169 +561,174 @@
}
// #endif
},
//
txtspeech(val, i) {
// xuiat({
// text: val
// }).then(res => {
// if (res.code == 1) {
// this.talkList[i].audio_file = res.data.audio_file
// this.nums = this.talkList.length - 1
// // this.palyaudio(res.data)
// }
// }).catch((err) => {
// console.log(err)
// })
ttWss({
data: val
xuiat({
text: val
}).then(res => {
if (res.code == 0) {
this.talkList[i].audio_file = res.data.mp3
this.nums = this.talkList.length - 1
if (res.code == 1) {
this.talkList[i].audio_file = res.data.audio_file
this.nums = this.talkList.length - 1
// this.palyaudio(res.data)
}
}).catch((err) => {
console.log(err)
})
//
// let arry1;
// if (val.indexOf(',') == -1) {
// arry1 = val.split(',')
// } else if (val.indexOf('') == -1) {
// arry1 = val.split('')
// } else {
// arry1.push(val)
// }
// const temp = []
// for (let i = 0; i < arry1.length; i++) {
// temp.push(
// // 使Promise
// new Promise((resolve, reject) => {
// ttWss({
// data: arry1[i]
// }).then(res => {
// return resolve(res.data.mp3)
// }).catch(err => {
// return reject(err)
// })
// })
// )
// }
// Promise.all(temp).then(res => {
// // console.log(res,'2222222')
// this.audio_file = res
// this.ScanAudio(res, this.currentIndex, 0)
// })
},
//
longclick() {
if (this.touchE - this.touchT < 1000) {
console.log('点击');
//
ScanAudio(urls, currentIndex, delayInSeconds) {
if (currentIndex >= urls.length) {
this.palystatus = true;
return false;
}
},
longpress(e) {
// console.log(e.touches[0].pageX)
this.onFeedTap()
this.touchT = new Date().getTime();
this.islong = true
this.risshow = false
this.$refs.popup.open('center')
//
this.timer = setInterval(() => {
this.num++
}, 1000)
this.btnStatus = 1
recorderManager.start({
//5,
duration: 300000
const music = uni.createInnerAudioContext();
music.src = urls[currentIndex];
console.log(urls[currentIndex])
console.log('播放成功', this.palystatus)
if (this.palystatus) {
this.palystatus = false
music.play();
} else {
music.pause();
}
music.onEnded(() => {
music.destroy();
setTimeout(() => {
this.ScanAudio(urls, currentIndex + 1, delayInSeconds);
}, delayInSeconds * 1000);
});
this.touchStartX = e.touches[0].pageX; //
this.touchStartY = e.touches[0].pageY; //
},
//
voiceToText() {
let that = this;
sr.voiceToText(data => {
// console.log(JSON.stringify(data));
if (data.code == 1001) {
// that.toTextStatus = "..."
} else if (data.code == 1006) {
// this.toTextStatus = ""
//
// sr.getIatPath(path => {
// that.path = path;
// });
} else if (data.code == 1007) {
// this.toTextStatus = ""
} else if (data.code == 1008) {
// this.volume = data.msg;
} else if (data.code == 1009) { //
//islast
// this.transText = data.msg;
if (data.isLast) {
if (data.msg.length > 0 && this.risshow == false) {
this.content = data.msg
this.send()
} else {
this.content = ''
this.stopVoiceToText()
}
}
} else if (data.code == 1010) { //error
//data.msg
if (data.msg) {
this.transText = data.msg;
}
}
})
},
//
stopVoiceToText() {
//10071009 ios 1007 1006 1009 1010
sr.stopListening();
},
start(e) {
// console.log(e.touches[0].pageX)
this.touchStartX = e.touches[0].pageX; //
this.touchStartY = e.touches[0].pageY; //
clearTimeout(this.timer); //
let that=this
this.timer = setTimeout(function() {
//
// alert("")
that.onFeedTap()
that.islong = true
that.risshow = false
that.$refs.popup.open('center')
that.btnStatus = 1
that.voiceToText()
}.bind(this), 1500);
},
end() {
this.touchE = new Date().getTime();
if (this.islong) {
this.closePop()
// clearInterval(this.timer1)
// this.btnStatus = 0
// //
// innerAudioContext.stop()
// //,,,
// setTimeout(() => {
// let obj = {
// status: false,
// path: [{
// src: this.voicePath,
// switchStatus: false,
// soundTime: this.num
// }]
// }
// console.log(1111, this.voicePath)
// // if (this.voicePath.length > 0) {
// // uni.$u.throttle(this.uploadFile(this.voicePath), 500)
// // }
// this.num = 0
// // pathToBase64(this.voicePath)
// // .then(base64 => {
// // const arrayBuffer = new Uint8Array(base64)
// // const base1 = uni.arrayBufferToBase64(arrayBuffer)
// // let params = {
// // common: {
// // app_id: '2eda6c2e',
// // },
// // business: {
// // language: "zh_cn",
// // domain: "iat",
// // accent: "mandarin",
// // vad_eos: 5000,
// // dwa: "wpgs",
// // },
// // data: {
// // status: 2,
// // format: "audio/L16;rate=16000",
// // encoding: "raw",
// // audio: base64.split(',')[1]
// // },
// // };
// // this.audioTxt = JSON.stringify(params)
// // this.audiosckt()
// // })
// // .catch(error => {
// // console.error(error)
// // })
// // this.socketaudio.send(JSON.stringify(params))
// }, 500)
this.islong = false
clearInterval(this.timer)
this.btnStatus = 0
recorderManager.stop();
// console.log(this.voicePath, "888");
// this.closePop()
if (this.risshow == false && this.touchE - this.touchT > 3000) {
let data = {
"id": '@',
"content": '加载完毕',
"type": 2,
"pic": this.avatarType
}
this.talkList.push(data);
// ,,,
setTimeout(() => {
let obj = {
status: false,
path: [{
src: this.voicePath,
switchStatus: false,
soundTime: this.num
}]
}
clearInterval(this.timer) //
this.btnStatus = 0;
this.stopVoiceToText()
this.num = 0
if (this.voicePath.length > 0 && this.risshow == false) {
// this.huashow=true
this.TabItemTap()
throttle(this.uploadFile(this.voicePath), 1000)
}
this.num = 0
}, 500)
} else {
if (!this.risshow) {
uni.showModal({
title: "录音时长不超过3s"
})
}
}
}
@ -799,36 +737,7 @@
},
toBase64() {
pathToBase64(this.voicePath)
.then(base64 => {
const arrayBuffer = new Uint8Array(base64)
const base1 = uni.arrayBufferToBase64(arrayBuffer.split(0, 1280))
let params = {
common: {
app_id: '2eda6c2e',
},
business: {
language: "zh_cn",
domain: "iat",
accent: "mandarin",
vad_eos: 5000,
dwa: "wpgs",
},
data: {
status: 2,
format: "audio/L16;rate=16000",
encoding: "raw",
audio: base1
},
};
this.audioTxt = JSON.stringify(params)
})
.catch(error => {
console.error(error)
})
},
//
deletespeech() {
@ -873,9 +782,31 @@
},
//
palyaudio(item) {
palyaudio1(item, i) {
this.palystatus = !this.palystatus
console.log(this.audio_file, '111')
if (!this.palystatus) {
this.ScanAudio(this.audio_file, this.currentIndex, 0)
} else {
this.ScanAudio(this.audio_file, this.currentIndex, 0)
// this.txtspeech(item.content, i)
}
// innerAudioContext.onError((res) => {
// console.log(res.errMsg);
// console.log(res.errCode);
// this.palystatus = true
// });
innerAudioContext.onEnded((res) => {
this.palystatus = true
});
},
//
palyaudio(item, i) {
this.palystatus = !this.palystatus
if (!this.palystatus) {
innerAudioContext.src = item.audio_file;
innerAudioContext.play()
@ -1051,250 +982,29 @@
let that = this;
let moveX = this.touchMoveX - e.touches[0].pageX;
let moveY = this.touchMoveY - e.touches[0].pageY;
// console.log(-115 > moveX && -240 < moveX,moveY)
let platform = uni.getSystemInfoSync().platform;
if (platform === 'ios') {
if (-115 > moveX && -240 < moveX && moveY < -620 && moveY > -730) {
this.risshow = true
this.deletespeech()
if (-115 > moveX && -240 < moveX && moveY < -359 && moveY > -480) {
this.risshow = true
this.deletespeech()
} else {
this.risshow = false
}
} else {
this.risshow = false
if (-115 > moveX && -240 < moveX && moveY < -359 && moveY > -480) {
this.risshow = true
this.deletespeech()
} else {
this.risshow = false
}
}
},
audiosckt() {
this.socketaudio = uni.connectSocket({
//url: encodeURI(encodeURI(myUrl).replace(/\+/g, '%2B')),
url: this.audioUrl,
method: 'GET',
token: '',
success: res => {
console.log(res, "ws成功连接...")
}
})
// ,
this.socketaudio.onOpen((res) => {
console.log("WebSocket连接正常打开中...");
//
let params = {
common: {
app_id: '2eda6c2e',
},
business: {
language: "zh_cn",
domain: "iat",
accent: "mandarin",
vad_eos: 5000,
dwa: "wpgs",
},
data: {
status: 0,
format: "audio/L16;rate=16000",
encoding: "raw",
},
};
this.socketaudio.send({
data: JSON.stringify(params),
async success(res) {
console.log("消息发送成功", '1');
that.audioTxt = ''
},
});
//
this.handlerInterval = setInterval(() => {
//
console.log(this.audioTxt)
let that = this
this.socketaudio.send({
data: JSON.stringify({
data: {
status: 1,
format: 'audio/L16;rate=16000',
encoding: 'raw',
audio: this.audioTxt
}
}),
async success(res) {
console.log("消息发送成功", that.audioTxt, '2');
that.audioTxt = ''
},
});
if (this.audioTxt.length === 0) {
this.socketaudio.send({
data: JSON.stringify({
data: {
status: 2,
format: 'audio/L16;rate=16000',
encoding: 'raw',
audio: ''
}
}),
async success(res) {
console.log("消息发送成功", '3');
clearInterval(that.handlerInterval);
return false;
},
});
};
}, 40);
//
// this.socketaudio.send({
// data: JSON.stringify(params),
// async success(res) {
// console.log("", res);
// },
// });
//
})
// socket
// this.socketaudio.onClose(() => {
// console.log("")
// })
this.socketaudio.onMessage((res) => {
console.log("收到服务器内容:" + res.data);
});
},
send1() {
this.showStop = true;
//
let data = {
"id": new Date().getTime(),
"content": this.content,
"type": 1,
"pic": this.avatar
}
this.TEXT = this.content,
this.talkList.push(data);
this.talkList.push({
"id": new Date().getTime(),
"content": '',
"type": 2,
"pic": this.avatarType
});
this.n_content = '';
this.c_content = '';
this.socketing = true;
// return ;
this.$nextTick(() => {
//
this.content = '';
})
this.sendToSpark1();
},
async sendToSpark1() {
this.tempRes = "";
let realThis = this;
this.socketTask = uni.connectSocket({
//url: encodeURI(encodeURI(myUrl).replace(/\+/g, '%2B')),
url: `wss://chat.lihaink.cn/chat?type=${this.wssType}&timestamp=${Date.now()}`,
method: 'GET',
token: '',
success: res => {
console.log(res, "ws成功连接...")
realThis.wsLiveFlag = true;
}
})
realThis.socketTask.onError((res) => {
console.log("连接发生错误请检查appid是否填写", res)
})
realThis.socketTask.onOpen((res) => {
this.historyTextList.push({
"role": "user",
"content": this.TEXT
})
// ..........................................
console.log('连接成功...')
if (this.historyTextList.length > 9) this.params = JSON.parse(JSON.stringify(this
.historyTextList
.splice(-9)));
else this.params = JSON.parse(JSON.stringify(this.historyTextList));
this.isSurpass();
realThis.socketTask.send({ // uni
data: JSON.stringify(this.params),
success() {
console.log('第一帧发送成功');
}
});
});
//
realThis.socketTask.onMessage((res) => {
// console.log('API', res.data);
let obj = JSON.parse(res.data)
// console.log(""+obj.payload);
if (!realThis.wsLiveFlag) return;
let dataArray = obj.payload.choices.text;
for (let i = 0; i < dataArray.length; i++) {
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
}
let temp = JSON.parse(res.data)
// console.log("0726",temp.header.code)
if (temp.header.code !== 0) {
this.socketing = false;
console.log(`${temp.header.code}:${temp.message}`);
realThis.socketTask.close({
success(res) {
console.log('关闭成功', res)
realThis.wsLiveFlag = false;
},
fail(err) {
console.log('关闭失败', err)
}
})
}
if (temp.header.code === 0) {
if (res.data && temp.header.status === 2) {
this.socketing = false;
this.historyTextList.push({
"role": "assistant",
"content": this.tempRes
})
setTimeout(() => {
let that = this
realThis.socketTask.close({
success(res) {
console.log('关闭成功', res)
that.nums = that.talkList.length - 1
that.txtspeech(that.n_content, that.talkList.length -
1)
realThis.wsLiveFlag = false;
},
fail(err) {
// console.log('', err)
}
})
}, 1000)
}
}
})
},
//
send() {
@ -1311,7 +1021,8 @@
"id": new Date().getTime(),
"content": this.content,
"type": 1,
"pic": this.avatar
// "pic": this.avatar
"pic": this.avatarType
}
// console.log(data);
this.TEXT = this.content;
@ -1427,15 +1138,15 @@
"role": "assistant",
"content": this.tempRes
})
this.nums = this.talkList.length - 1
this.txtspeech(this.n_content, this.talkList.length -
1)
setTimeout(() => {
let that = this
realThis.socketTask.close({
success(res) {
console.log('关闭成功', res)
that.nums = that.talkList.length - 1
that.txtspeech(that.n_content, that.talkList.length -
1)
realThis.wsLiveFlag = false;
@ -1513,7 +1224,7 @@
page {
// background-color: #f5f5f5;
height: 100vh;
background: url('@/static/icon/bg1.png') no-repeat;
// background: url('@/static/icon/bg1.png') no-repeat;
background-size: 100% 100%;
background-attachment: fixed;
background-repeat: no-repeat;
@ -1630,7 +1341,7 @@
width: 100%;
padding-top: var(--status-bar-height);
padding-bottom: 30rpx;
background: url('@/static/icon/bg1.png');
// background: url('@/static/icon/bg1.png');
background-size: cover;
text-align: center;
background-color: transparent;