add ws消息推送
This commit is contained in:
291
pages/home/home.1.vue
Normal file
291
pages/home/home.1.vue
Normal file
@@ -0,0 +1,291 @@
|
||||
<template>
|
||||
<view class="home_wraper">
|
||||
<view class="zd_content">
|
||||
<view class="header">{{$t('home.title')}}</view>
|
||||
<view class="userInfo-wrap">
|
||||
<view class="userInfo">
|
||||
<text v-if="userName !== ''" class="p1">{{userName}}</text>
|
||||
<!-- <text class="p2">欢迎进入东丽WMS手持系统!</text> -->
|
||||
<text class="p2">{{$t('home.message')}}</text>
|
||||
</view>
|
||||
<view class="exit" @tap="Quit">
|
||||
<view class="icon-exit"></view>
|
||||
<view class="exit-text">{{$t('home.exit')}}</view>
|
||||
</view>
|
||||
</view>
|
||||
<view class="zd_home_wrapper">
|
||||
<view class="menus_wrap">
|
||||
<view class="fir_menu_wrap">
|
||||
<view class="fir_menu-item" v-for="(e, i) in menuList" :key="i" @tap="toPage1(e)">
|
||||
<image class="menu-img" :src="require('../../static/image/menu/' + e.path + '.png')" alt="">
|
||||
<view class="menu-name">{{e.title}}</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<view class="sec_menu_wraper" :class="show ? 'popshow' : 'pophide'">
|
||||
<view class="pop-title">{{title}}</view>
|
||||
<view class="sec_menu_w">
|
||||
<view class="sec_menu-item" :style="{'background-image': 'url(' + require('../../static/image/menu/' + icon + '.png') + ')'}" v-for="(e, i) in secM" :key="'sontree' + i" @click="toPage2(e)">
|
||||
<view class="menu-name_inner">{{e.title}}</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<view v-if="show" class="modal" @click.stop="show = false"></view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
reconnectTimer: null,
|
||||
websocket: null,
|
||||
serverUrl: this.$store.getters.baseUrl,
|
||||
userName: '',
|
||||
// menuList: [
|
||||
// {title: '库位管理', path: 'RF03', sonTree: [{title: '人工取货', path: '/pages/ftdl/man-get-goods'}, {title: '人工放货', path: '/pages/ftdl/man-load-goods'}, {title: '库存锁定/解锁', path: '/pages/ftdl/kc-manage'}]},
|
||||
// {title: '产线管理', path: 'RF04', sonTree: [{title: '产线叫料', path: '/pages/ftdl/line-call-mater'}, {title: '产线下料', path: '/pages/ftdl/line-down-mater'}]},
|
||||
// {title: '任务管理', path: 'RF07', sonTree: [{title: '作业管理', path: '/pages/ftdl/work-manage'}, {title: '定点作业', path: '/pages/ftdl/fixedpoint-work'}]},
|
||||
// {title: '进出区域登记', path: 'RF01', sonTree: [{title: '进出区域登记', path: '/pages/ftdl/inout-area-mark'}]}
|
||||
// ],
|
||||
menuList: [
|
||||
{title: this.$t('menu.kw-management'), path: 'RF03', sonTree: [{title: this.$t('menu.man-get-goods'), path: '/pages/ftdl/man-get-goods'}, {title: this.$t('menu.man-load-goods'), path: '/pages/ftdl/man-load-goods'}, {title: this.$t('menu.kc-manage'), path: '/pages/ftdl/kc-manage'}]},
|
||||
{title: this.$t('menu.line-management'), path: 'RF04', sonTree: [{title: this.$t('menu.line-call-mater'), path: '/pages/ftdl/line-call-mater'}, {title: this.$t('menu.line-down-mater'), path: '/pages/ftdl/line-down-mater'}]},
|
||||
{title: this.$t('menu.task-management'), path: 'RF07', sonTree: [{title: this.$t('menu.work-manage'), path: '/pages/ftdl/work-manage'}, {title: this.$t('menu.fixedpoint-work'), path: '/pages/ftdl/fixedpoint-work'}]},
|
||||
{title: this.$t('menu.inout-area-mark'), path: 'RF01', sonTree: [{title: this.$t('menu.inout-area-mark'), path: '/pages/ftdl/inout-area-mark'}]}
|
||||
],
|
||||
// menu: [{title: '叫料搬运', path: 'RF11', sonTree: [{title: '叫料搬运', path: '/pages/carry/call-carry'}]}],
|
||||
show: false,
|
||||
secM: [],
|
||||
icon: '',
|
||||
title: '',
|
||||
pwd: '1'
|
||||
};
|
||||
},
|
||||
// onShow () {
|
||||
// if (this.$store.getters.userInfo) {
|
||||
// const info = JSON.parse(this.$store.getters.userInfo)
|
||||
// this.userName = info.username
|
||||
// if (info.pwd_reset_user_id === '0') {
|
||||
// this.menuList = [...this.menu]
|
||||
// }
|
||||
|
||||
// }
|
||||
// },
|
||||
mounted () {
|
||||
this.initWebSocket()
|
||||
},
|
||||
methods: {
|
||||
initWebSocket () {
|
||||
if (this.reconnectTimer) clearTimeout(this.reconnectTimer)
|
||||
const wsHost = this.serverUrl.replace(/^https?:\/\//, '')
|
||||
// const lang = this.$i18n.locale.slice(0, 2)
|
||||
// const wsUrl = `ws://${wsHost}/webSocket/{sid}?lang=${lang}`
|
||||
const sid = 'sid'
|
||||
const wsUrl = `ws://${wsHost}/webSocket/${sid}`
|
||||
console.log(wsUrl, 666)
|
||||
this.closeWebSocket()
|
||||
this.websocket = new WebSocket(wsUrl)
|
||||
this.websocket.onopen = () => {}
|
||||
this.websocket.onmessage = (event) => {
|
||||
try {
|
||||
const res = JSON.parse(event.data)
|
||||
this.handleWebSocketMessage(res)
|
||||
} catch (error) {
|
||||
console.error('WebSocket消息解析失败:', error)
|
||||
}
|
||||
}
|
||||
this.websocket.onerror = (error) => {
|
||||
uni.showToast({
|
||||
title: error,
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
this.websocket.onclose = () => {
|
||||
this.reconnectWebSocket()
|
||||
}
|
||||
},
|
||||
handleWebSocketMessage (res) {
|
||||
console.log(res, 'res')
|
||||
console.log(res.msg.data, 'res.msg.data')
|
||||
uni.showToast({
|
||||
title: res.msg.data,
|
||||
icon: 'none'
|
||||
})
|
||||
},
|
||||
closeWebSocket () {
|
||||
if (this.websocket) {
|
||||
this.websocket.close(1000, '正常关闭')
|
||||
this.websocket = null
|
||||
}
|
||||
},
|
||||
reconnectWebSocket () {
|
||||
if (this.reconnectTimer) clearTimeout(this.reconnectTimer)
|
||||
this.reconnectTimer = setTimeout(() => {
|
||||
this.initWebSocket()
|
||||
}, 5000) // 5秒后重连
|
||||
},
|
||||
toPage1 (e) {
|
||||
if (e.sonTree.length > 0) {
|
||||
this.show = true
|
||||
this.secM = e.sonTree
|
||||
|
||||
this.icon = e.path
|
||||
this.title = e.title
|
||||
}
|
||||
},
|
||||
toPage2 (e) {
|
||||
let url = e.path + '?title=' + e.title
|
||||
if (e.active === false) {
|
||||
return
|
||||
}
|
||||
uni.redirectTo({
|
||||
url: url
|
||||
})
|
||||
},
|
||||
Quit () {
|
||||
this.$store.dispatch('delUserInfo', '')
|
||||
uni.redirectTo({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="stylus">
|
||||
@import '../../common/style/mixin.styl';
|
||||
.home_wraper
|
||||
_wh(100%, 100%)
|
||||
overflow hidden
|
||||
.zd_content
|
||||
padding 0 24rpx
|
||||
height 100%
|
||||
top 0
|
||||
padding-top calc(var(--status-bar-height) + 86rpx)
|
||||
// background linear-gradient(to bottom, #fae2ca 0%, #fff5ea 20%, #fdfdfd 100%)
|
||||
background linear-gradient(to bottom, #f5f6fb 0%, #fff 100%)
|
||||
.header
|
||||
position fixed
|
||||
left 0
|
||||
top 0
|
||||
padding-top var(--status-bar-height)
|
||||
z-index 100
|
||||
_wh(100%, calc(var(--status-bar-height) + 86rpx))
|
||||
_font(36rpx,86rpx,#333,,center)
|
||||
.userInfo-wrap
|
||||
_fj()
|
||||
_wh(100%,190rpx)
|
||||
padding 0 30rpx
|
||||
background-color $red
|
||||
_bis('../../static/image/info_bg.png',auto,100%,right,bottom)
|
||||
border-radius 12rpx
|
||||
margin-bottom 24rpx
|
||||
.userInfo
|
||||
_fj(,flex-start,column)
|
||||
.p1
|
||||
_font(34rpx,1,#fff)
|
||||
padding-bottom 18rpx
|
||||
.p2
|
||||
_font(24rpx,1,#fff)
|
||||
.exit
|
||||
_fj()
|
||||
height 50rpx
|
||||
padding 0 20rpx
|
||||
border 1px solid #FF967C
|
||||
border-radius 20rpx
|
||||
.icon-exit
|
||||
_wh(22rpx, 22rpx)
|
||||
_bis('../../static/image/exit.png',22rpx)
|
||||
.exit-text
|
||||
_font(32rpx,50rpx,#fff,,right)
|
||||
padding-left 10rpx
|
||||
.zd_home_wrapper
|
||||
_wh(100%, calc(100% - 238rpx))
|
||||
margin-bottom 24rpx
|
||||
background-color #fff
|
||||
border-radius: 12rpx;
|
||||
padding: 14rpx;
|
||||
margin-bottom 24rpx
|
||||
box-shadow: 0 4rpx 10rpx 4rpx rgba(0,0,0,.1)
|
||||
overflow-y scroll
|
||||
.menus_wrap
|
||||
width 100%
|
||||
.fir_menu_wrap
|
||||
_fj(flex-start,flex-start,,wrap)
|
||||
align-content: flex-start
|
||||
.fir_menu-item
|
||||
_fj()
|
||||
flex-direction column
|
||||
_wh(30%, auto)
|
||||
margin-bottom 40rpx
|
||||
_font(26rpx, 60rpx,#e74f1a,,center)
|
||||
&:nth-child(3n+2)
|
||||
margin-left 5%
|
||||
margin-right 5%
|
||||
::v-deep .menu-img
|
||||
_wh(100%, auto)
|
||||
img
|
||||
position relative
|
||||
opacity 1
|
||||
_wh(55%, auto)
|
||||
margin 0 auto 0.2rpx
|
||||
.menu-name
|
||||
_font(28rpx, 38rpx, #444,,center)
|
||||
padding 0 20rpx
|
||||
.sec_menu_wraper
|
||||
position fixed
|
||||
bottom 0
|
||||
left 0
|
||||
width 100%
|
||||
background-color #f4f5f5
|
||||
box-shadow 0 -8px 16px 0 rgba(28,31,33,.1)
|
||||
border-top-left-radius 20rpx
|
||||
border-top-right-radius 20rpx
|
||||
z-index 2017
|
||||
transition all .3s
|
||||
.modal
|
||||
position fixed
|
||||
bottom 0
|
||||
left 0
|
||||
_wh(100%, 100%)
|
||||
background-color rgba(0,0,0,0.8)
|
||||
z-index 2010
|
||||
.sec_menu_w
|
||||
width calc(100% - 76rpx)
|
||||
margin 30rpx auto 60rpx auto
|
||||
_fj(flex-start)
|
||||
flex-wrap wrap
|
||||
.sec_menu-item
|
||||
_wh(30%, 120rpx)
|
||||
margin-bottom 20rpx
|
||||
border-radius 10rpx
|
||||
background-size 40% auto
|
||||
background-position right 90%
|
||||
background-repeat no-repeat
|
||||
&:nth-child(3n+2)
|
||||
margin-left 5%
|
||||
margin-right 5%
|
||||
&:nth-child(5n+1) .menu-name_inner
|
||||
background linear-gradient(to right, rgba(73,102,255,0.8) 0%, rgba(117,142,255,0.8) 100%)
|
||||
&:nth-child(5n+2) .menu-name_inner
|
||||
background linear-gradient(to right, rgba(254, 168, 20,0.8) 0%, rgba(255, 177, 1,0.8) 100%)
|
||||
&:nth-child(5n+3) .menu-name_inner
|
||||
background linear-gradient(to right, rgba(0, 228, 153,0.8) 0%, rgba(0, 241, 197,0.8) 100%)
|
||||
&:nth-child(5n+4) .menu-name_inner
|
||||
background linear-gradient(to right, rgba(196, 5, 219,0.8) 0%, rgba(183,120,190,0.8) 100%)
|
||||
&:nth-child(5n+5) .menu-name_inner
|
||||
background linear-gradient(to right, rgba(146, 94, 52,0.8) 0%, rgba(162, 116, 79,0.8) 100%)
|
||||
.menu-name_inner
|
||||
_wh(100%, 100%)
|
||||
_fj(center)
|
||||
_font(28rpx, 30rpx, #fff, 700, center)
|
||||
border-radius 10rpx
|
||||
padding 10rpx
|
||||
overflow hidden
|
||||
.pop-title
|
||||
margin 60rpx 38rpx 0 38rpx
|
||||
_font(30rpx, 60rpx, #000,700,center)
|
||||
</style>
|
||||
@@ -40,6 +40,9 @@
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
reconnectTimer: null,
|
||||
websocket: null,
|
||||
serverUrl: this.$store.getters.baseUrl,
|
||||
userName: '',
|
||||
// menuList: [
|
||||
// {title: '库位管理', path: 'RF03', sonTree: [{title: '人工取货', path: '/pages/ftdl/man-get-goods'}, {title: '人工放货', path: '/pages/ftdl/man-load-goods'}, {title: '库存锁定/解锁', path: '/pages/ftdl/kc-manage'}]},
|
||||
@@ -71,7 +74,88 @@
|
||||
|
||||
// }
|
||||
// },
|
||||
mounted() {
|
||||
this.initWebSocket()
|
||||
},
|
||||
methods: {
|
||||
initWebSocket() {
|
||||
if (this.reconnectTimer) clearTimeout(this.reconnectTimer)
|
||||
const wsHost = this.serverUrl.replace(/^https?:\/\//, '')
|
||||
const sid = 'sid' + Date.now() + '_' + Math.random().toString(36).substr(2, 9)
|
||||
const wsUrl = `ws://${wsHost}/webSocket/${sid}`
|
||||
console.log(wsUrl, 666)
|
||||
|
||||
this.closeWebSocket()
|
||||
|
||||
// 使用 uni.connectSocket 创建连接
|
||||
this.socketTask = uni.connectSocket({
|
||||
url: wsUrl,
|
||||
success: () => {
|
||||
console.log('WebSocket 连接创建成功')
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('WebSocket 连接创建失败:', err)
|
||||
this.reconnectWebSocket()
|
||||
}
|
||||
})
|
||||
|
||||
// 监听 WebSocket 事件
|
||||
this.socketTask.onOpen(() => {
|
||||
console.log('WebSocket 连接已打开')
|
||||
})
|
||||
|
||||
this.socketTask.onMessage((event) => {
|
||||
try {
|
||||
const res = JSON.parse(event.data)
|
||||
this.handleWebSocketMessage(res)
|
||||
} catch (error) {
|
||||
console.error('WebSocket消息解析失败:', error)
|
||||
}
|
||||
})
|
||||
|
||||
this.socketTask.onError((error) => {
|
||||
console.error('WebSocket 错误:', error)
|
||||
uni.showToast({
|
||||
title: '连接错误',
|
||||
icon: 'none'
|
||||
})
|
||||
})
|
||||
|
||||
this.socketTask.onClose(() => {
|
||||
console.log('WebSocket 连接已关闭')
|
||||
this.reconnectWebSocket()
|
||||
})
|
||||
},
|
||||
|
||||
handleWebSocketMessage(res) {
|
||||
console.log(res, 'res')
|
||||
console.log(res.msg.data, 'res.msg.data')
|
||||
uni.showToast({
|
||||
title: res.msg.data,
|
||||
icon: 'none'
|
||||
})
|
||||
},
|
||||
|
||||
closeWebSocket() {
|
||||
if (this.socketTask) {
|
||||
this.socketTask.close({
|
||||
success: () => {
|
||||
console.log('WebSocket 已关闭')
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('关闭 WebSocket 失败:', err)
|
||||
}
|
||||
})
|
||||
this.socketTask = null
|
||||
}
|
||||
},
|
||||
|
||||
reconnectWebSocket() {
|
||||
if (this.reconnectTimer) clearTimeout(this.reconnectTimer)
|
||||
this.reconnectTimer = setTimeout(() => {
|
||||
this.initWebSocket()
|
||||
}, 5000) // 5秒后重连
|
||||
},
|
||||
toPage1 (e) {
|
||||
if (e.sonTree.length > 0) {
|
||||
this.show = true
|
||||
|
||||
99
utils/websocket.js
Normal file
99
utils/websocket.js
Normal file
@@ -0,0 +1,99 @@
|
||||
let websock = null
|
||||
let messageCallback = null
|
||||
let errorCallback = null
|
||||
let wsUrl = ''
|
||||
|
||||
// 接收ws后端返回的数据
|
||||
function websocketonmessage (e) {
|
||||
messageCallback(JSON.parse(e.data))
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起websocket连接
|
||||
* @param {Object} agentData 需要向后台传递的参数数据
|
||||
*/
|
||||
function websocketSend (agentData) {
|
||||
// 加延迟是为了尽量让ws连接状态变为OPEN
|
||||
setTimeout(() => {
|
||||
// 添加状态判断,当为OPEN时,发送消息
|
||||
if (websock.readyState === websock.OPEN) { // websock.OPEN = 1
|
||||
// 发给后端的数据需要字符串化
|
||||
websock.send(JSON.stringify(agentData))
|
||||
}
|
||||
if (websock.readyState === websock.CLOSED) { // websock.CLOSED = 3
|
||||
console.log('websock.readyState=3')
|
||||
}
|
||||
}, 500)
|
||||
}
|
||||
|
||||
// 关闭ws连接
|
||||
function websocketclose (e) {
|
||||
// e.code === 1000 表示正常关闭。 无论为何目的而创建, 该链接都已成功完成任务。
|
||||
// e.code !== 1000 表示非正常关闭。
|
||||
if (e && e.code !== 1000) {
|
||||
uni.showToast({
|
||||
title: 'server error',
|
||||
icon: 'none'
|
||||
})
|
||||
errorCallback()
|
||||
}
|
||||
}
|
||||
// 建立ws连接
|
||||
function websocketOpen (e) {
|
||||
// console.log('ws连接成功')
|
||||
}
|
||||
|
||||
// 初始化weosocket
|
||||
function initWebSocket () {
|
||||
if (typeof (WebSocket) === 'undefined') {
|
||||
uni.showToast({
|
||||
title: '您的浏览器不支持WebSocket,无法获取数据',
|
||||
icon: 'none'
|
||||
})
|
||||
return false
|
||||
}
|
||||
// ws请求完整地址
|
||||
const requstWsUrl = wsUrl
|
||||
websock = new WebSocket(requstWsUrl)
|
||||
|
||||
websock.onmessage = function (e) {
|
||||
websocketonmessage(e)
|
||||
}
|
||||
websock.onopen = function () {
|
||||
websocketOpen()
|
||||
}
|
||||
websock.onerror = function () {
|
||||
}
|
||||
websock.onclose = function (e) {
|
||||
websocketclose(e)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发起websocket请求函数
|
||||
* @param {string} url ws连接地址
|
||||
* @param {Object} agentData 传给后台的参数
|
||||
* @param {function} successCallback 接收到ws数据,对数据进行处理的回调函数
|
||||
* @param {function} errCallback ws连接错误的回调函数
|
||||
*/
|
||||
export function sendWebsocket (url, agentData, successCallback, errCallback) {
|
||||
wsUrl = url
|
||||
initWebSocket()
|
||||
messageCallback = successCallback
|
||||
errorCallback = errCallback
|
||||
websocketSend(agentData)
|
||||
}
|
||||
|
||||
/**
|
||||
* 关闭websocket函数
|
||||
*/
|
||||
export function closeWebsocket (flag) {
|
||||
if (flag) {
|
||||
websock.close()
|
||||
return
|
||||
}
|
||||
if (websock) {
|
||||
websock.close() // 关闭websocket
|
||||
websock.onclose() // 关闭websocket
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user