gaoshp
2024-06-23 fbee7228e2f6e43b417d4c3f03020704831261cd
src/utils/tool.js
@@ -1,8 +1,8 @@
/*
 * @Descripttion: 工具集
 * @version: 1.2
 * @LastEditors: sakuya
 * @LastEditTime: 2022年5月24日00:28:56
 * @LastEditors: lzhe lzhe@example.com
 * @LastEditTime: 2024-06-17 18:21:44
 */
import CryptoJS from 'crypto-js';
@@ -10,6 +10,16 @@
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; // 获取路径名
if (process.env.NODE_ENV=='development') {
   var path = "120.46.212.231:84";
} else {
   var path = hostname+":"+port;
}
/* localStorage */
tool.data = {
   set(key, data, datetime = 0) {
@@ -114,6 +124,36 @@
   }
}
/* 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);
@@ -218,4 +258,15 @@
   }
}
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