| | |
| | | /* |
| | | * @Descripttion: 工具集 |
| | | * @version: 1.2 |
| | | * @LastEditors: sakuya |
| | | * @LastEditTime: 2022年5月24日00:28:56 |
| | | * @LastEditors: lzhe lzhe@example.com |
| | | * @LastEditTime: 2024-10-10 10:17:47 |
| | | */ |
| | | |
| | | import CryptoJS from 'crypto-js'; |
| | |
| | | |
| | | const tool = {} |
| | | |
| | | var currentLocation = window.location.href; // 获取完整的URL |
| | | var protocol = window.location.protocol; // 获取协议(如http:或https:) |
| | | var hostname = window.location.hostname; // 获取主机名 |
| | | var port = window.location.port; // 获取端口号 |
| | | var pathname = window.location.pathname; // 获取路径名 |
| | | |
| | | console.log(hostname,port,pathname) |
| | | if (process.env.NODE_ENV=='development') { |
| | | //var path = "120.46.212.231:85"; //如果是本地联调,写成120.46.212.231:85,发版写成hostname+":"+port; |
| | | var path = hostname+":"+port; |
| | | } else { |
| | | var path = hostname+":"+port; |
| | | } |
| | | /* localStorage */ |
| | | tool.data = { |
| | | set(key, data, datetime = 0) { |
| | |
| | | } |
| | | } |
| | | |
| | | /* socket */ |
| | | tool.socket = { |
| | | url:'ws://'+path+'/ws/info', |
| | | websocket: null, |
| | | connectToWebSocket(token) { //建立链接 |
| | | this.websocket = new WebSocket(this.url + "?access_token=" + token); |
| | | }, |
| | | sendDataToWebSocket(data) { //发送 |
| | | if(!data) return; //没有入参不发送 |
| | | if (this.websocket.readyState === this.websocket.OPEN) { |
| | | this.websocket.send(JSON.stringify(data)); |
| | | } |
| | | // this.websocket.onmessage = function(event) { |
| | | // // 当从服务器收到消息时 |
| | | // console.error("WebSocket Error: ", event.data); |
| | | // }; |
| | | |
| | | // this.websocket.onerror = function(error) { |
| | | // // 当WebSocket连接发生错误时 |
| | | // console.error("WebSocket Error: ", error); |
| | | // }; |
| | | |
| | | // this.websocket.onclose = function(event) { |
| | | // // 当WebSocket连接关闭时 |
| | | // console.log("WebSocket is closed now."); |
| | | // // 可以在这里重试连接或其他逻辑... |
| | | // }; |
| | | return this; |
| | | } |
| | | } |
| | | /* Fullscreen */ |
| | | tool.screen = function (element) { |
| | | var isFull = !!(document.webkitIsFullScreen || document.mozFullScreen || document.msFullscreenElement || document.fullscreenElement); |
| | |
| | | } |
| | | } |
| | | |
| | | tool.qsStringify = function(obj) { |
| | | return Object.keys(obj).map(key => { |
| | | if (Array.isArray(obj[key])) { |
| | | return obj[key] |
| | | .map(arrayValue => `${encodeURIComponent(key)}=${encodeURIComponent(arrayValue)}`) |
| | | .join('&'); |
| | | } |
| | | return `${encodeURIComponent(key)}=${encodeURIComponent(obj[key])}`; |
| | | }).join('&'); |
| | | } |
| | | |
| | | export default tool |