pinia引入

This commit is contained in:
yanghang
2025-06-30 14:54:39 +08:00
parent 7833c0f1a5
commit e1da68c59b
10 changed files with 697 additions and 114 deletions
+74 -48
View File
@@ -49,6 +49,7 @@
:activeVoiceId="activeVoiceTalkingItemId"
@changePlay="changePlayItemId"
@nextQuestion="nextQuestionSuccess"
:id="'item' + item.id"
></TalkingItem>
<!-- <video v-if="videoUrlShow" :src="videoUrlShow" controls></video> -->
</view>
@@ -95,6 +96,13 @@ import { getCourseStudyInfoApi, queryTraStdyBatchWaitParagraphInfoApi,audioTrans
import common from '@/common/common';
import TalkingItem from '@/pages/course/components/talkingItem.vue'
import ImagePreview from '@/components/image-preview/image-preview.vue'
import { useSocketStore } from "@/store/socket.js"
import { storeToRefs } from 'pinia'
const socketStore = useSocketStore()
const { SocketObj } = storeToRefs(socketStore)
const crsNum = ref('');
const crsId = ref('');
const step = ref(0)
@@ -107,6 +115,7 @@ const activeVoiceTalkingItemId = ref('')
const getData = async () => {
try{
const { body } = await getCourseStudyInfoApi({crsId: crsId.value})
initsocketTask(1)
if(body.stdyProgressFlag === 'N'){
step.value = 1
teacherList.value = body.teacheList.map(t=>({
@@ -114,11 +123,34 @@ const getData = async () => {
voicePath: '',
voiceObj: null
}))
initsocketTask(1)
}else{
step.value = 2
choiceTeacher.value = body.choiceTeacher
initsocketTask(2)
common.show('提示信息', '此门课程已有学习历史,请选择是否继续学习?',true, '重新开始','继续学习').then((res) => {
console.log(res)
if(res) {
// 继续学习 获取学习历史接口
// 滚动到最下面
scrollToBottomNow()
choiceTeacher.value = body.choiceTeacher
sendMessage({
"commond" : 'OPEN',
"body" : {
crsId: crsId.value,
teacherNo: choiceTeacher.value
}
})
step.value = 2
getGraphInfoUnStudy()
}else{
// 重新开始
step.value = 1
teacherList.value = body.teacheList.map(t=>({
...t,
voicePath: '',
voiceObj: null
}))
}
}).finally(() => {})
}
}catch{
@@ -168,18 +200,15 @@ var socketTask = null
const nextStep = () => {
if(choiceTeacher.value){
teacherVoiceObj.destroy()
console.log(111)
if( socketTask && socketTask.readyState === 1){
sendMessage({
"commond" : 'OPEN',
"body" : {
crsId: crsId.value,
teacherNo: choiceTeacher.value
}
})
step.value = 2
getGraphInfoUnStudy()
sendMessage({
"commond" : 'OPEN',
"body" : {
crsId: crsId.value,
teacherNo: choiceTeacher.value
}
})
step.value = 2
getGraphInfoUnStudy()
}else{
uni.showToast({
title: '请选择一位教师!',
@@ -198,6 +227,7 @@ const getGraphInfoUnStudy = () => {
userInfo: {...choiceTeacherInfo.value},
id: _id
})
scrollToBottomNow()
})
}
// 继续 执行方法
@@ -210,44 +240,42 @@ const nextQuestionSuccess = (info) => {
type: 2,
id: _id
})
scrollToBottomNow()
getGraphInfoUnStudy()
}
const scrollToBottomNow = () => {
if(talkingList.value.length > 0){
let _last = talkingList.value[talkingList.value.length - 1]
uni.pageScrollTo({
selector: '#item' + _last.id,
duration: 300
})
}
}
const reconnectTimer = null // 重连timer
const isManualClose = ref(false) // 是否手动关闭
const initsocketTask = ($step) => {
socketTask = uni.connectSocket({
url: get_base_url() + '/trastudy/trasoket/open?summary=' + getToken(),
complete: function(){}
})
uni.onSocketMessage((res)=>{
let _data = JSON.parse(res.data)
console.log(_data)
})
uni.onSocketOpen((e)=>{
console.log('链接成功!')
})
uni.onSocketError((e)=>{
console.log(e,111)
})
uni.onSocketClose((e)=>{
// 重连
reconnect()
})
// socketTask.onMessage = (event) => {
// console.log(event,111)
// }
// socketTask.onOpen = () =>{
// console.log('建立链接')
// }
// socketTask.onError = (event) => {
// console.log('error',event)
// }
// socketTask.onclose = () => {
socketStore.creatSocket()
// socketTask = uni.connectSocket({
// url: get_base_url() + '/trastudy/trasoket/open?summary=' + getToken(),
// complete: function(){}
// })
// uni.onSocketMessage((res)=>{
// let _data = JSON.parse(res.data)
// console.log(_data)
// })
// uni.onSocketOpen((e)=>{
// console.log('链接成功!')
// })
// uni.onSocketError((e)=>{
// console.log(e,111)
// })
// uni.onSocketClose((e)=>{
// // 重连
// // initsocketTask()
// }
// reconnect()
// })
}
const reconnect = ()=> {
if(reconnectTimer || isManualClose.value) return
@@ -256,9 +284,7 @@ const reconnect = ()=> {
},3000)
}
const sendMessage = (data) => {
socketTask.send({
data: JSON.stringify(data)
})
unref(SocketObj).send(data)
}
const closeWebSocket = () => {
isManualClose.value = true