处理 socket 问题

szh_zidingyibiaoti
张同海 2 years ago
parent ace6026c6a
commit 33f6293969

@ -28,7 +28,7 @@
:collapsible="true" :collapsible="true"
></side-menu> ></side-menu>
<div :style="{minWidth:placeholderDivMinWidth}" v-if="!isMobile()"></div> <div :style="{ minWidth: placeholderDivMinWidth }" v-if="!isMobile()"></div>
<a-layout :class="[layoutMode, `content-width-${contentWidth}`]" :style="{ minHeight: '100vh' }"> <a-layout :class="[layoutMode, `content-width-${contentWidth}`]" :style="{ minHeight: '100vh' }">
<!-- layout header --> <!-- layout header -->
<global-header <global-header
@ -53,10 +53,8 @@
<a-layout-footer> <a-layout-footer>
<global-footer /> <global-footer />
</a-layout-footer> </a-layout-footer>
</a-layout> </a-layout>
</a-layout> </a-layout>
</template> </template>
<script> <script>
@ -78,7 +76,7 @@ export default {
GlobalHeader, GlobalHeader,
GlobalFooter GlobalFooter
}, },
data () { data() {
return { return {
collapsed: false, collapsed: false,
menus: [] menus: []
@ -89,7 +87,7 @@ export default {
// //
mainMenu: state => state.permission.addRouters mainMenu: state => state.permission.addRouters
}), }),
contentPaddingLeft () { contentPaddingLeft() {
if (!this.fixSidebar || this.isMobile()) { if (!this.fixSidebar || this.isMobile()) {
return '0' return '0'
} }
@ -99,26 +97,26 @@ export default {
return '80px' return '80px'
}, },
placeholderDivMinWidth() { placeholderDivMinWidth() {
let width = this.collapsed ? '80px' : '230px'; let width = this.collapsed ? '80px' : '230px'
return width; return width
} }
}, },
watch: { watch: {
sidebarOpened (val) { sidebarOpened(val) {
this.collapsed = !val this.collapsed = !val
}, },
// //
mainMenu (val) { mainMenu(val) {
this.setMenus() this.setMenus()
} }
}, },
created () { created() {
this.setMenus() this.setMenus()
/* const routes = convertRoutes(this.mainMenu.find(item => item.path === '/')) /* const routes = convertRoutes(this.mainMenu.find(item => item.path === '/'))
this.menus = (routes && routes.children) || [] */ this.menus = (routes && routes.children) || [] */
this.collapsed = !this.sidebarOpened this.collapsed = !this.sidebarOpened
}, },
mounted () { mounted() {
const userAgent = navigator.userAgent const userAgent = navigator.userAgent
if (userAgent.indexOf('Edge') > -1) { if (userAgent.indexOf('Edge') > -1) {
this.$nextTick(() => { this.$nextTick(() => {
@ -129,35 +127,46 @@ export default {
}) })
} }
console.log('== 1111进入页面 == ,这里添加获取订舱下拉数据 ') console.log('== 1111进入页面 == ,这里添加获取订舱下拉数据 ')
this.setBookingInfo() // this.setBookingInfo()
}, },
methods: { methods: {
...mapActions(['setSidebar', 'setCarrierList', 'setYardList', 'setPackageList', 'setIssuetypeList', 'setBlfrtList', 'setCtnallList', 'setLineList', 'setBookingSourcePage', 'setBookingInitData', 'setCodeCountryList']), ...mapActions([
'setSidebar',
'setCarrierList',
'setYardList',
'setPackageList',
'setIssuetypeList',
'setBlfrtList',
'setCtnallList',
'setLineList',
'setBookingSourcePage',
'setBookingInitData',
'setCodeCountryList'
]),
// //
setMenus () { setMenus() {
const routes = convertRoutes(this.mainMenu.find(item => item.path === '/')) const routes = convertRoutes(this.mainMenu.find(item => item.path === '/'))
this.menus = (routes && routes.children) || [] this.menus = (routes && routes.children) || []
}, },
toggle () { toggle() {
this.collapsed = !this.collapsed this.collapsed = !this.collapsed
this.setSidebar(!this.collapsed) this.setSidebar(!this.collapsed)
triggerWindowResizeEvent() triggerWindowResizeEvent()
}, },
paddingCalc () { paddingCalc() {
let left = '' let left = ''
if (this.sidebarOpened) { if (this.sidebarOpened) {
left = this.isDesktop() ? '230px' : '80px' left = this.isDesktop() ? '230px' : '80px'
} else { } else {
left = (this.isMobile() && '0') || ((this.fixSidebar && '80px') || '0') left = (this.isMobile() && '0') || (this.fixSidebar && '80px') || '0'
} }
return left return left
}, },
menuSelect () { menuSelect() {},
}, drawerClose() {
drawerClose () {
this.collapsed = false this.collapsed = false
}, },
setBookingInfo () { setBookingInfo() {
this.setCarrierList() this.setCarrierList()
this.setYardList() this.setYardList()
this.setPackageList() this.setPackageList()

@ -29,7 +29,7 @@ import VXETable from 'vxe-table'
import 'vxe-table/lib/style.css' import 'vxe-table/lib/style.css'
import 'vue-easytable/libs/theme-default/index.css' import 'vue-easytable/libs/theme-default/index.css'
import VueEasytable from 'vue-easytable' import VueEasytable from 'vue-easytable'
import qs from 'qs'; import qs from 'qs'
Vue.use(VXETable) Vue.use(VXETable)
Vue.use(VueContextMenu) Vue.use(VueContextMenu)
@ -44,7 +44,7 @@ Vue.prototype.hasPerm = hasBtnPermission
Vue.prototype.applocation = sysApplication Vue.prototype.applocation = sysApplication
Vue.prototype.$bus = new Vue() Vue.prototype.$bus = new Vue()
Vue.config.productionTip = false Vue.config.productionTip = false
Vue.prototype.$qs = qs; Vue.prototype.$qs = qs
new Vue({ new Vue({
router, router,

@ -46,45 +46,45 @@ class SocketConnection extends EventEmitter {
} }
} }
async start(token) { // async start(token) {
// 组件重新加载时, 如果 socket 存在, 不需要新建 // // 组件重新加载时, 如果 socket 存在, 不需要新建
if (!this.socket) { // if (!this.socket) {
this.socket = new SignalR.HubConnectionBuilder() // this.socket = new SignalR.HubConnectionBuilder()
.configureLogging(SignalR.LogLevel.Information) // .configureLogging(SignalR.LogLevel.Information)
.withUrl( // .withUrl(
`/hubs/chathub`, { // `/hubs/chathub`, {
accessTokenFactory: () => token, // accessTokenFactory: () => token,
skipNegotiation: true, // skipNegotiation: true,
transport: SignalR.HttpTransportType.WebSockets // transport: SignalR.HttpTransportType.WebSockets
} // }
) // )
.build() // .build()
this.socket.on('ForceExist', () => { //可以做相关业务逻辑 // this.socket.on('ForceExist', () => { //可以做相关业务逻辑
store.dispatch("Logout").then(()=>{ // store.dispatch("Logout").then(()=>{
window.location.reload() // window.location.reload()
}).catch(err=>{ // }).catch(err=>{
this.$message.error({ // this.$message.error({
title: '错误', // title: '错误',
description: err.message // description: err.message
}) // })
}) // })
}) // })
this.socket.on('AppendNotice', (notice) => { // this.socket.on('AppendNotice', (notice) => {
store.commit("APPEND_NOTICE",notice) // store.commit("APPEND_NOTICE",notice)
}) // })
this.socket.onclose(async () => { // this.socket.onclose(async () => {
this.offline = true // this.offline = true
this.emit('onclose') // this.emit('onclose')
await this._initialize() // await this._initialize()
}) // })
await this._initialize() // await this._initialize()
} // }
} // }
async authenticate(token) { async authenticate(token) {
await this.start(token) await this.start(token)
@ -92,11 +92,11 @@ class SocketConnection extends EventEmitter {
listen(method) { listen(method) {
if (this.offline) return if (this.offline) return
if (this.listened.some((v) => v === method)) return if (this.listened.some(v => v === method)) return
this.listened.push(method) this.listened.push(method)
this.one('onstart', () => { this.one('onstart', () => {
this.listened.forEach((method) => { this.listened.forEach(method => {
this.socket.on(method, (data) => { this.socket.on(method, data => {
if (this.options.log) { if (this.options.log) {
console.log({ console.log({
type: 'receive', type: 'receive',
@ -143,7 +143,7 @@ class SocketConnection extends EventEmitter {
} }
// eslint-disable-next-line no-async-promise-executor // eslint-disable-next-line no-async-promise-executor
return new Promise(async (resolve) => this.one('onstart', () => resolve(this.socket.invoke(methodName, ...args)))) return new Promise(async resolve => this.one('onstart', () => resolve(this.socket.invoke(methodName, ...args))))
} }
} }
@ -173,16 +173,16 @@ function install(Vue, connection) {
if (this.$options.sockets) { if (this.$options.sockets) {
const methods = Object.getOwnPropertyNames(this.$options.sockets) const methods = Object.getOwnPropertyNames(this.$options.sockets)
methods.forEach((method) => { methods.forEach(method => {
Socket.listen(method) Socket.listen(method)
Socket.one(method, (data) => this.$options.sockets[method].call(this, data)) Socket.one(method, data => this.$options.sockets[method].call(this, data))
}) })
} }
if (this.$options.subscribe) { if (this.$options.subscribe) {
Socket.one('authenticated', () => { Socket.one('authenticated', () => {
this.$options.subscribe.forEach((channel) => { this.$options.subscribe.forEach(channel => {
Socket.invoke('join', channel) Socket.invoke('join', channel)
}) })
}) })

Loading…
Cancel
Save