apt
This commit is contained in:
413
src/config/getData2.js
Normal file
413
src/config/getData2.js
Normal file
@@ -0,0 +1,413 @@
|
||||
/* eslint-disable */
|
||||
import {post} from '@config/http.js'
|
||||
// import store from '../vuex/store'
|
||||
|
||||
// 开发者选项
|
||||
export const getIP = () => post('api/developer/getIP', {})
|
||||
export const getLogList = () => post('api/developer/getLogList', {})
|
||||
export const getROSNodeList = () => post('api/developer/getROSNodeList', {})
|
||||
export const temperature = () => post('api/developer/temperature', {})
|
||||
export const debugInfo = () => post('api/developer/debugInfo', {})
|
||||
export const softwareVersion = () => post('api/developer/softwareVersion', {})
|
||||
export const parameterSetting = (num, word) => post('api/developer/parameterSetting', {
|
||||
maxTaskNun: num,
|
||||
password: word
|
||||
})
|
||||
|
||||
// new
|
||||
// 登录
|
||||
export const authlogin = (username, password) => post('auth/login', {
|
||||
username: username,
|
||||
password: password
|
||||
})
|
||||
|
||||
export const queryHead = () => post('api/home/queryHead', {
|
||||
})
|
||||
// 车辆状态
|
||||
// 1.1查询车辆状态
|
||||
export const queryVehicleStatus = (username, password) => post('api/vehicle/queryVehicleStatus', {
|
||||
})
|
||||
// 1.2查询传感器状态
|
||||
export const querrySensor = () => post('api/vehicle/querrySensor', {})
|
||||
// 1.3软启动
|
||||
export const softStart = () => post('api/vehicle/softStart', {})
|
||||
// 1.4跳过起点
|
||||
export const skipStartPoint = () => post('api/vehicle/skipStartPoint', {})
|
||||
// 1.5关机,显示屏控制、TCS控制、RC控制、Joy控制
|
||||
export const ShutDown = (type, bool) => post('api/vehicle/Shut_down', {
|
||||
type: type,
|
||||
bool: bool
|
||||
})
|
||||
// 1.6查询初始化坐标
|
||||
export const queryRestPoint = () => post('api/vehicle/queryRestPoint', {})
|
||||
// 1.7初始化坐标确定
|
||||
export const restCoordinate = (code, name, x, y, t) => post('api/vehicle/restCoordinate', {
|
||||
point_code: code,
|
||||
point_name: name,
|
||||
x: x,
|
||||
y: y,
|
||||
t: t
|
||||
})
|
||||
// 1.8查询agv的状态
|
||||
export const queryAgvStatus = () => post('api/vehicle/queryAgv_Status', {})
|
||||
// 1.9退出
|
||||
export const quitNow = () => post('api/vehicle/quitNow', {})
|
||||
|
||||
// 系统管理
|
||||
// 1.1 用户列表
|
||||
export const usersQuery = (page, size) => post('api/users/query', {
|
||||
page: page,
|
||||
size: size
|
||||
})
|
||||
// 1.2 添加用户
|
||||
export const usersAdd = (username, personName, gender, phone, rolesIds, password) => post('api/users/add', {
|
||||
username: username,
|
||||
personName: personName,
|
||||
gender: gender,
|
||||
phone: phone,
|
||||
rolesIds: rolesIds,
|
||||
password: password
|
||||
})
|
||||
// 1.3 修改用户、重置密码
|
||||
export const usersEdit = (obj) => post('api/users/edit', obj)
|
||||
// 1.4 删除用户
|
||||
export const usersDelete = (userId) => post('api/users/delete', userId)
|
||||
|
||||
// 角色管理
|
||||
// 1.1 角色列表
|
||||
export const sysRoleQuery = (page, size) => post('api/sysRole/query', {
|
||||
page: page,
|
||||
size: size
|
||||
})
|
||||
// 1.2 添加角色
|
||||
export const sysRoleAdd = (name, remark) => post('api/sysRole/add', {
|
||||
name: name,
|
||||
remark: remark
|
||||
})
|
||||
// 1.3 修改角色
|
||||
export const sysRoleEdit = (roleId, name, remark) => post('api/sysRole/edit', {
|
||||
roleId: roleId,
|
||||
name: name,
|
||||
remark: remark
|
||||
})
|
||||
// 1.4 删除角色
|
||||
export const sysRoleDelete = (roleId) => post('api/sysRole/delete', roleId)
|
||||
// 1.5 保存菜单
|
||||
export const sysRoleMenu = (roleId, menus, menus1) => post('api/sysRole/menu', {
|
||||
roleId: roleId,
|
||||
menus: menus,
|
||||
menus1: menus1
|
||||
})
|
||||
// 1.6 查询菜单树
|
||||
export const menuQuery = (roleId, menus) => post('api/sysRole/menuQuery', {
|
||||
})
|
||||
|
||||
// 系统参数
|
||||
// 1.1 参数列表
|
||||
export const paramQuery = (page, size) => post('api/param/query', {
|
||||
page: page,
|
||||
size: size
|
||||
})
|
||||
// 1.2 添加参数
|
||||
export const paramAdd = (code, name, value, remark) => post('api/param/add', {
|
||||
code: code,
|
||||
name: name,
|
||||
value: value,
|
||||
remark: remark
|
||||
})
|
||||
// 1.3 修改参数
|
||||
export const paramEdit = (id, code, name, value, remark) => post('api/param/edit', {
|
||||
id: id,
|
||||
code: code,
|
||||
name: name,
|
||||
value: value,
|
||||
remark: remark
|
||||
})
|
||||
// 1.4 删除参数
|
||||
export const paramDelete = (paramsIds) => post('api/param/delete', paramsIds)
|
||||
|
||||
// 修改密码
|
||||
export const updatePass = (newPass, oldPass) => post('api/users/updatePass', {
|
||||
newPass: newPass,
|
||||
oldPass: oldPass
|
||||
})
|
||||
|
||||
// 用户登录获取菜单
|
||||
export const sysMenuBuild = () => post('api/sysMenu/build', {
|
||||
})
|
||||
// export const sysMenuBuild = () => {
|
||||
// let res = [
|
||||
// {
|
||||
// "menuId": "1654807438270009344",
|
||||
// "pid": "1324783485328",
|
||||
// "label": "首页",
|
||||
// "router": "",
|
||||
// "index": "1",
|
||||
// "children": [
|
||||
// {
|
||||
// "menuId": "1654807784312672256",
|
||||
// "pid": "1654807438270009344",
|
||||
// "label": "首页",
|
||||
// "router": "/index/home",
|
||||
// "index": "1",
|
||||
// "children": null,
|
||||
// "title": "首页",
|
||||
// "zhTitle": "首页",
|
||||
// "enTitle": "Index",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "首页",
|
||||
// "en_title": "Index",
|
||||
// "in_title": null
|
||||
// }
|
||||
// ],
|
||||
// "title": "首页",
|
||||
// "zhTitle": "首页",
|
||||
// "enTitle": "Index",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "首页",
|
||||
// "en_title": "Index",
|
||||
// "in_title": null
|
||||
// },
|
||||
// {
|
||||
// "menuId": "1654808014131171328",
|
||||
// "pid": "1324783485328",
|
||||
// "label": "任务",
|
||||
// "router": "",
|
||||
// "index": "2",
|
||||
// "children": [
|
||||
// {
|
||||
// "menuId": "1654808296076480512",
|
||||
// "pid": "1654808014131171328",
|
||||
// "label": "搬运起点",
|
||||
// "router": "/index/carrypoint",
|
||||
// "index": "1",
|
||||
// "children": null,
|
||||
// "title": "搬运起点",
|
||||
// "zhTitle": "搬运起点",
|
||||
// "enTitle": "Carry Point",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "搬运起点",
|
||||
// "en_title": "Carry\nPoint",
|
||||
// "in_title": null
|
||||
// }
|
||||
// ],
|
||||
// "title": "任务",
|
||||
// "zhTitle": "任务",
|
||||
// "enTitle": "Task",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "任务",
|
||||
// "en_title": "Task",
|
||||
// "in_title": null
|
||||
// },
|
||||
// {
|
||||
// "menuId": "1654808791742550016",
|
||||
// "pid": "1324783485328",
|
||||
// "label": "车辆",
|
||||
// "router": "",
|
||||
// "index": "4",
|
||||
// "children": [
|
||||
// {
|
||||
// "menuId": "1654808864970903552",
|
||||
// "pid": "1654808791742550016",
|
||||
// "label": "车辆状态",
|
||||
// "router": "/index/vehiclestatus",
|
||||
// "index": "1",
|
||||
// "children": null,
|
||||
// "title": "车辆状态",
|
||||
// "zhTitle": "车辆状态",
|
||||
// "enTitle": "Vehicle State ",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "车辆状态",
|
||||
// "en_title": "Vehicle\nState",
|
||||
// "in_title": null
|
||||
// },
|
||||
// {
|
||||
// "menuId": "1654808966481448960",
|
||||
// "pid": "1654808791742550016",
|
||||
// "label": "车辆控制",
|
||||
// "router": "/index/vehiclecontrol",
|
||||
// "index": "2",
|
||||
// "children": null,
|
||||
// "title": "车辆控制",
|
||||
// "zhTitle": "车辆控制",
|
||||
// "enTitle": "Vehicle Control",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "车辆控制",
|
||||
// "en_title": "Vehicle\nControl",
|
||||
// "in_title": null
|
||||
// },
|
||||
// {
|
||||
// "menuId": "1654808966481448961",
|
||||
// "pid": "1654808791742550016",
|
||||
// "label": "恢复定位",
|
||||
// "router": "/index/slam",
|
||||
// "index": "3",
|
||||
// "children": null,
|
||||
// "title": "恢复定位",
|
||||
// "zhTitle": "恢复定位",
|
||||
// "enTitle": "Vehicle Control",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "恢复定位",
|
||||
// "en_title": "Vehicle\nControl",
|
||||
// "in_title": null
|
||||
// }
|
||||
// ],
|
||||
// "title": "车辆",
|
||||
// "zhTitle": "车辆",
|
||||
// "enTitle": "Vehicle",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "车辆",
|
||||
// "en_title": "Vehicle",
|
||||
// "in_title": null
|
||||
// },
|
||||
// {
|
||||
// "menuId": "1654808657013116928",
|
||||
// "pid": "1324783485328",
|
||||
// "label": "故障",
|
||||
// "router": "",
|
||||
// "index": "3",
|
||||
// "children": [
|
||||
// {
|
||||
// "menuId": "1654808721102082048",
|
||||
// "pid": "1654808657013116928",
|
||||
// "label": "故障信息",
|
||||
// "router": "/index/errorinfo",
|
||||
// "index": "1",
|
||||
// "children": null,
|
||||
// "title": "故障信息",
|
||||
// "zhTitle": "故障信息",
|
||||
// "enTitle": "Fault Information",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "故障信息",
|
||||
// "en_title": "Fault\nInformation",
|
||||
// "in_title": null
|
||||
// }
|
||||
// ],
|
||||
// "title": "故障",
|
||||
// "zhTitle": "故障",
|
||||
// "enTitle": "Fault ",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "故障",
|
||||
// "en_title": "Fault",
|
||||
// "in_title": null
|
||||
// },
|
||||
// {
|
||||
// "menuId": "1654809097821884416",
|
||||
// "pid": "1324783485328",
|
||||
// "label": "示教",
|
||||
// "router": "/index/teach",
|
||||
// "index": "5",
|
||||
// "children": null,
|
||||
// "title": "示教",
|
||||
// "zhTitle": "示教",
|
||||
// "enTitle": "Teach",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "示教",
|
||||
// "en_title": "Teach",
|
||||
// "in_title": null
|
||||
// },
|
||||
// {
|
||||
// "menuId": "1664808014131171329",
|
||||
// "pid": "1324783485328",
|
||||
// "label": "系统",
|
||||
// "router": "",
|
||||
// "index": "6",
|
||||
// "children": [
|
||||
// {
|
||||
// "menuId": "1664808014131171330",
|
||||
// "pid": "1664808014131171329",
|
||||
// "label": "用户管理",
|
||||
// "router": "/index/user",
|
||||
// "index": "1",
|
||||
// "children": null,
|
||||
// "title": "用户管理",
|
||||
// "zhTitle": "用户管理",
|
||||
// "enTitle": "User Management",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "用户管理",
|
||||
// "en_title": "User\nManagement",
|
||||
// "in_title": null
|
||||
// },
|
||||
// {
|
||||
// "menuId": "1664808014131171331",
|
||||
// "pid": "1664808014131171329",
|
||||
// "label": "角色管理",
|
||||
// "router": "/index/role",
|
||||
// "index": "2",
|
||||
// "children": null,
|
||||
// "title": "角色管理",
|
||||
// "zhTitle": "角色管理",
|
||||
// "enTitle": "Role Management",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "角色管理",
|
||||
// "en_title": "Role\nManagement",
|
||||
// "in_title": null
|
||||
// },
|
||||
// {
|
||||
// "menuId": "1664808014131171335",
|
||||
// "pid": "1664808014131171329",
|
||||
// "label": "系统管理",
|
||||
// "router": "/index/system",
|
||||
// "index": "3",
|
||||
// "children": null,
|
||||
// "title": "系统管理",
|
||||
// "zhTitle": "系统管理",
|
||||
// "enTitle": "System Param",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "系统管理",
|
||||
// "en_title": "System\nParam",
|
||||
// "in_title": null
|
||||
// },
|
||||
// {
|
||||
// "menuId": "1664808014131171334",
|
||||
// "pid": "1664808014131171329",
|
||||
// "label": "开发者选项",
|
||||
// "router": "/index/developer",
|
||||
// "index": "4",
|
||||
// "children": null,
|
||||
// "title": "开发者选项",
|
||||
// "zhTitle": "开发者选项",
|
||||
// "enTitle": "Developer Param",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "开发者选项",
|
||||
// "en_title": "Developer\nParam",
|
||||
// "in_title": null
|
||||
// },
|
||||
// {
|
||||
// "menuId": "1664808014131171336",
|
||||
// "pid": "1664808014131171329",
|
||||
// "label": "远程支持",
|
||||
// "router": "/index/remote",
|
||||
// "index": "5",
|
||||
// "children": null,
|
||||
// "title": "远程支持",
|
||||
// "zhTitle": "远程支持",
|
||||
// "enTitle": "Remote Support",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "远程支持",
|
||||
// "en_title": "Remote\nSupport",
|
||||
// "in_title": null
|
||||
// }
|
||||
// ],
|
||||
// "title": "系统",
|
||||
// "zhTitle": "系统",
|
||||
// "enTitle": "System",
|
||||
// "inTitle": null,
|
||||
// "zh_title": "系统",
|
||||
// "en_title": "System",
|
||||
// "in_title": null
|
||||
// }
|
||||
// ]
|
||||
// return res
|
||||
// }
|
||||
|
||||
// 用户退出
|
||||
export const authLogout = () => post('auth/logout', {
|
||||
})
|
||||
|
||||
/**
|
||||
* 远程支持
|
||||
*/
|
||||
// 获取远程码接口
|
||||
export const queryRemotelnfo = () => post('api/developer/queryRemoteInfo', {
|
||||
})
|
||||
67
src/config/http.js
Normal file
67
src/config/http.js
Normal file
@@ -0,0 +1,67 @@
|
||||
import axios from 'axios'
|
||||
// import { Dialog } from './utils.js'
|
||||
import store from '../vuex/store'
|
||||
import router from '@/router'
|
||||
import i18n from '../i18n/i18n'
|
||||
|
||||
axios.defaults.timeout = 50000
|
||||
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
|
||||
axios.interceptors.request.use(
|
||||
config => {
|
||||
let token = ''
|
||||
if (store.getters.userInfo !== '') {
|
||||
token = JSON.parse(store.getters.userInfo).token
|
||||
}
|
||||
token && (config.headers.Authorization = token)
|
||||
if (config.method === 'post') {
|
||||
if (!config.data.flag) {
|
||||
config.data = config.data
|
||||
} else {
|
||||
config.data = config.data.formData
|
||||
}
|
||||
}
|
||||
config.headers.post['Accept-Language'] = i18n.locale === 'en-us' ? 'en' : 'zh'
|
||||
return config
|
||||
},
|
||||
error => {
|
||||
// Dialog('错误的传参')
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
|
||||
axios.interceptors.response.use(
|
||||
response => {
|
||||
return Promise.resolve(response)
|
||||
},
|
||||
error => {
|
||||
if (error && error.response) {
|
||||
switch (error.response.status) {
|
||||
case 400:
|
||||
// Dialog(error.message)
|
||||
break
|
||||
case 401:
|
||||
store.dispatch('setSignOut')
|
||||
router.push('/login')
|
||||
break
|
||||
}
|
||||
return Promise.reject(error.response.data)
|
||||
} else {
|
||||
return Promise.reject(error)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
export const post = (sevmethod, params) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
axios.post(`${store.getters.baseUrl}/` + sevmethod, params)
|
||||
.then(response => {
|
||||
resolve(response.data)
|
||||
}, error => {
|
||||
// Dialog(error.message)
|
||||
reject(error)
|
||||
})
|
||||
.catch((error) => {
|
||||
reject(error)
|
||||
})
|
||||
})
|
||||
}
|
||||
112
src/config/isOperate.js
Normal file
112
src/config/isOperate.js
Normal file
@@ -0,0 +1,112 @@
|
||||
// 用户长时间未操作 退出登录
|
||||
import store from '../vuex/store'
|
||||
import router from '@/router'
|
||||
var timer = null
|
||||
|
||||
clearInterval(timer)
|
||||
|
||||
export function isOperateFun () {
|
||||
var lastTime = new Date().getTime() // 最后一次点击时间
|
||||
var currentTime = new Date().getTime() // 当前时间
|
||||
var timeOut = 3 * 60 * 1000 // 允许最长未操作时间
|
||||
// var i = 1 // 辅助作用
|
||||
|
||||
function handleReset () { // 重新赋值最后一次点击时间,清除定时器,重新开始定时器
|
||||
// console.log('又点击了!!!!!!')
|
||||
// i = 1
|
||||
|
||||
lastTime = new Date().getTime()
|
||||
|
||||
if (timer) {
|
||||
clearInterval(timer)
|
||||
timer = null
|
||||
}
|
||||
|
||||
if (!timer) {
|
||||
// console.log('真好!重新开始')
|
||||
handleInterval()
|
||||
}
|
||||
}
|
||||
|
||||
// document.onclick = () => { // 单击事件
|
||||
// handleReset()
|
||||
// }
|
||||
|
||||
// document.ondblclick = () => { // 双击事件
|
||||
// handleReset()
|
||||
// }
|
||||
|
||||
// document.onmousedown = () => { // 按下鼠标键时触发
|
||||
// handleReset()
|
||||
// }
|
||||
|
||||
// document.onmouseup = () => { // 释放按下的鼠标键时触发
|
||||
// handleReset()
|
||||
// }
|
||||
|
||||
// document.onmousemove = () => { // 鼠标移动事件
|
||||
// handleReset()
|
||||
// }
|
||||
|
||||
// document.onmouseover = () => { // 移入事件
|
||||
// handleReset()
|
||||
// }
|
||||
|
||||
// document.onmouseout = () => { // 移出事件
|
||||
// handleReset()
|
||||
// }
|
||||
|
||||
// document.onmouseenter = () => { // 移入事件
|
||||
// handleReset()
|
||||
// }
|
||||
|
||||
// document.onmouseleave = () => { // 移出事件
|
||||
// handleReset()
|
||||
// }
|
||||
|
||||
// document.touchstart = () => {
|
||||
// alert('touchstart')
|
||||
// handleReset()
|
||||
// }
|
||||
|
||||
// document.touchend = () => {
|
||||
// alert('touchend')
|
||||
// handleReset()
|
||||
// }
|
||||
|
||||
// document.touchmove = () => {
|
||||
// alert('touchend')
|
||||
// handleReset()
|
||||
// }
|
||||
|
||||
window.onload = function startup () {
|
||||
document.addEventListener('touchstart', handleReset, false)
|
||||
document.addEventListener('touchend', handleReset, false)
|
||||
document.addEventListener('touchmove', handleReset, false)
|
||||
}
|
||||
|
||||
function handleInterval () { // 定时器
|
||||
timer = setInterval(() => {
|
||||
currentTime = new Date().getTime() // 当前时间
|
||||
|
||||
// console.log(`${i++}-currentTime`, currentTime)
|
||||
// console.log('最后一次点击时间', lastTime)
|
||||
|
||||
if (currentTime - lastTime > timeOut) {
|
||||
// console.log('长时间未操作')
|
||||
|
||||
clearInterval(timer) // 清除定时器
|
||||
|
||||
store.dispatch('setSignOut').then(() => { // 执行退出并跳转到首页
|
||||
const path = window.location.href.split('#')[1]
|
||||
|
||||
if (path !== '/home') { // 判断当前路由不是首页 则跳转至首页
|
||||
router.push('/home')
|
||||
}
|
||||
})
|
||||
}
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
handleInterval() // 一开始程序 默认执行定制器
|
||||
}
|
||||
16
src/config/rem.js
Normal file
16
src/config/rem.js
Normal file
@@ -0,0 +1,16 @@
|
||||
// 在 main.js 或其他全局初始化文件中
|
||||
function setRemUnit () {
|
||||
const screenWidth = window.innerWidth // 获取屏幕宽度
|
||||
const baseWidth = 1024 // 设计稿宽度
|
||||
const baseFontSize = 100 // 设计稿对应的字体大小
|
||||
|
||||
// 计算根元素的字体大小
|
||||
const rem = (screenWidth / baseWidth) * baseFontSize
|
||||
document.documentElement.style.fontSize = `${rem}px`
|
||||
}
|
||||
|
||||
// 页面加载时设置
|
||||
setRemUnit()
|
||||
|
||||
// 窗口大小改变时重新设置
|
||||
window.addEventListener('resize', setRemUnit)
|
||||
57
src/config/utils.js
Normal file
57
src/config/utils.js
Normal file
@@ -0,0 +1,57 @@
|
||||
/**
|
||||
* 存储localStorage
|
||||
*/
|
||||
export const setStore = (name, content) => {
|
||||
if (!name) return
|
||||
if (typeof content !== 'string') {
|
||||
content = JSON.stringify(content)
|
||||
}
|
||||
window.localStorage.setItem(name, content)
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取localStorage
|
||||
*/
|
||||
export const getStore = name => {
|
||||
if (!name) return
|
||||
return window.localStorage.getItem(name)
|
||||
}
|
||||
|
||||
/**
|
||||
* yy-mm-dd
|
||||
*/
|
||||
export const dateFtt = date => {
|
||||
if (date == null) {
|
||||
return ''
|
||||
}
|
||||
let year = date.getFullYear()
|
||||
let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
|
||||
let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
|
||||
return `${year}-${month}-${day}`
|
||||
}
|
||||
|
||||
/**
|
||||
* yy-mm-dd hh:mm:ss
|
||||
*/
|
||||
export const dateTimeFtt = date => {
|
||||
if (date == null) {
|
||||
return ''
|
||||
}
|
||||
let year = date.getFullYear()
|
||||
let month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
|
||||
let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
|
||||
let hh = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
|
||||
let mm = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
|
||||
let ss = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
|
||||
return `${year}-${month}-${day} ${hh}:${mm}:${ss}`
|
||||
}
|
||||
|
||||
/**
|
||||
* 将字符串形式的日期转换成日期对象
|
||||
*/
|
||||
export const dateNew = date => {
|
||||
if (date === undefined || date === 'undefined') {
|
||||
return new Date()
|
||||
}
|
||||
return new Date(Date.parse(date))
|
||||
}
|
||||
114
src/config/websocket.js
Normal file
114
src/config/websocket.js
Normal file
@@ -0,0 +1,114 @@
|
||||
// import { Message } from 'element-ui'
|
||||
import { toast } from './utils.js'
|
||||
// import { getToken } from '@/utils/authToken' // 与后端的协商,websocket请求需要带上token参数
|
||||
let websock = null
|
||||
let messageCallback = null
|
||||
let errorCallback = null
|
||||
let wsUrl = ''
|
||||
// let tryTime = 0
|
||||
|
||||
// 接收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')
|
||||
// Message.error('ws连接异常,请稍候重试')
|
||||
// errorCallback()
|
||||
}
|
||||
}, 500)
|
||||
}
|
||||
|
||||
// 关闭ws连接
|
||||
function websocketclose (e) {
|
||||
// e.code === 1000 表示正常关闭。 无论为何目的而创建, 该链接都已成功完成任务。
|
||||
// e.code !== 1000 表示非正常关闭。
|
||||
if (e && e.code !== 1000) {
|
||||
toast('server error')
|
||||
errorCallback()
|
||||
// // 如果需要设置异常重连则可替换为下面的代码,自行进行测试
|
||||
// if (tryTime < 10) {
|
||||
// setTimeout(function() {
|
||||
// websock = null
|
||||
// tryTime++
|
||||
// initWebSocket()
|
||||
// console.log(`第${tryTime}次重连`)
|
||||
// }, 3 * 1000)
|
||||
// } else {
|
||||
// Message.error('重连失败!请稍后重试')
|
||||
// }
|
||||
}
|
||||
}
|
||||
// 建立ws连接
|
||||
function websocketOpen (e) {
|
||||
// console.log('ws连接成功')
|
||||
}
|
||||
|
||||
// 初始化weosocket
|
||||
function initWebSocket () {
|
||||
if (typeof (WebSocket) === 'undefined') {
|
||||
toast('您的浏览器不支持WebSocket,无法获取数据')
|
||||
return false
|
||||
}
|
||||
// ws请求完整地址
|
||||
// const token = 'JWT=' + getToken()
|
||||
// const requstWsUrl = wsUrl + '?' + token
|
||||
const requstWsUrl = wsUrl
|
||||
websock = new WebSocket(requstWsUrl)
|
||||
|
||||
websock.onmessage = function (e) {
|
||||
websocketonmessage(e)
|
||||
}
|
||||
websock.onopen = function () {
|
||||
websocketOpen()
|
||||
}
|
||||
websock.onerror = function () {
|
||||
// Message.error('ws连接异常,请稍候重试')
|
||||
// errorCallback()
|
||||
}
|
||||
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