import { getWebsocketUrl } from '@/utils/websocket/config'
|
import { useConnectWebSocket } from '@/utils/websocket/connect-websocket'
|
import { useStore } from 'vuex'
|
import dayjs from 'dayjs'
|
import { aLinkDownloadUtil } from '@/utils/util'
|
import EventBus from '@/utils/eventBus'
|
import { computed, onBeforeUnmount, onMounted } from 'vue';
|
import { setStore,getStore } from '@/utils/store';
|
import { getDownloadStatusApi } from '@/api/dataCenter/dataCenter';
|
|
export const useDownloadWs = () => {
|
const store = useStore()
|
const loginUserInfo = computed(() => store.state.user.userInfo)
|
const downloadProgress = computed(() => store.state.common.downloadProgress)
|
let currentWebSocket = null
|
function messageHandler(payload) {
|
const {type,status,progress,download_url} = payload
|
let setProgress = 0
|
if (status === 'PENDING'){
|
setProgress = 1
|
}else if(status === 'CANCELLED'){
|
setProgress = 100
|
}else if(status !== 'COMPLETED' && progress === 100){
|
setProgress = 99
|
}else{
|
setProgress = progress
|
}
|
store.commit('setDownloadProgress', {
|
...downloadProgress.value,
|
[type]: setProgress,
|
})
|
if (status === 'COMPLETED' && ['htsjzx','htlsrwxq'].includes(type)) {
|
const name = `数据中心-${dayjs().format('YYYYMMDDHHmmss')}.zip`
|
const currentUrl = getStore({name: 'downloadUrl'})
|
if (currentUrl === download_url && download_url!== undefined) return
|
aLinkDownloadUtil(download_url, name)
|
setStore({ name: 'downloadUrl', content: download_url })
|
}
|
}
|
|
onMounted(() => {
|
let webSocketUrl = getWebsocketUrl() + `&model_type=3&workspace-id=${loginUserInfo.value.user_id}`
|
currentWebSocket = useConnectWebSocket((result)=>{
|
const payload = JSON.parse(result)
|
messageHandler(payload.data)
|
}, webSocketUrl)
|
EventBus.on('useDownloadWs-messageHandler',messageHandler)
|
})
|
|
onBeforeUnmount(() => {
|
currentWebSocket?.close()
|
EventBus.off('useDownloadWs-messageHandler',messageHandler)
|
})
|
return {}
|
}
|