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
+3
View File
@@ -0,0 +1,3 @@
import { createPinia } from 'pinia';
const store = createPinia();;
export default store;
+51
View File
@@ -0,0 +1,51 @@
import { defineStore } from 'pinia';
import { ref } from 'vue';
import { getToken } from '@/common/common.js'
import { get_base_url } from '@/api/request.js'
import SocketClass from '@/api/socket.js'
export const useSocketStore = defineStore('socketStore',{
state:()=>({
SocketObj: null
}),
getters:{
taskSocket(state){
return state.SocketObj
}
},
actions:{
creatSocket(){
if(this.SocketObj && this.SocketObj?.close) {
closeSocket()
}
this.SocketObj = new SocketClass(get_base_url() + '/trastudy/trasoket/open?summary=' + getToken());
},
closeSocket: () => {
if(!this.SocketObj || !this.SocketObj?.close) return
this.SocketObj && this.SocketObj?.close?.()
},
setOnmessageCb: (cb) => {
if(!this.SocketObj || !this.SocketObj?.on) return
this.SocketObj.on('message', cb)
}
}
})
// () => {
// const SocketObj = ref()
// const creatSocket = () => {
// console.log(data)
// if(SocketObj.value && SocketObj.value?.close) {
// closeSocket()
// }
// SocketObj.value = new SocketClass(get_base_url() + '/trastudy/trasoket/open?summary=' + getToken());
// }
// const closeSocket = () => {
// if(!SocketObj.value || !SocketObj.value?.close) return
// SocketObj.value && SocketObj.value?.close?.()
// }
// const setOnmessageCb = (cb) => {
// if(!SocketObj.value || !SocketObj.value?.on) return
// SocketObj.value.on('message', cb)
// }
// return { SocketObj, creatSocket, closeSocket, setOnmessageCb }
// }