[add] 同步前端代码
This commit is contained in:
67
plus-ui/src/utils/batchUpload.ts
Normal file
67
plus-ui/src/utils/batchUpload.ts
Normal file
@ -0,0 +1,67 @@
|
||||
// 文件路径:utils/BatchUploader.ts
|
||||
|
||||
/**
|
||||
* BatchUploader 批量上传工具类
|
||||
* 用于将大量数据分批发送到后端,避免单次请求数据过大导致失败。
|
||||
* 支持设置每批数据大小、批次间延迟、上传函数和完成回调。
|
||||
*/
|
||||
/**
|
||||
* BatchUploader 批量上传工具类(安全简洁版)
|
||||
* 用于将大量数据分批上传,避免一次性请求数据过大导致失败。
|
||||
*/
|
||||
export class BatchUploader<T> {
|
||||
private dataList: T[];
|
||||
private chunkSize: number;
|
||||
private delay: number;
|
||||
private uploadFunc: (chunk: T[], batchIndex: number, totalBatches: number) => Promise<void>;
|
||||
private onComplete?: () => void;
|
||||
|
||||
constructor(options: {
|
||||
dataList: T[]; // 统一使用一维数组类型
|
||||
chunkSize?: number;
|
||||
delay?: number;
|
||||
uploadFunc: (chunk: T[], batchIndex: number, totalBatches: number) => Promise<void>;
|
||||
onComplete?: () => void;
|
||||
}) {
|
||||
const { dataList, chunkSize = 8000, delay = 200, uploadFunc, onComplete } = options;
|
||||
|
||||
this.dataList = dataList;
|
||||
this.chunkSize = chunkSize;
|
||||
this.delay = delay;
|
||||
this.uploadFunc = uploadFunc;
|
||||
this.onComplete = onComplete;
|
||||
}
|
||||
|
||||
public async start() {
|
||||
const total = Math.ceil(this.dataList.length / this.chunkSize);
|
||||
|
||||
for (let i = 0; i < total; i++) {
|
||||
const chunk = this.dataList.slice(i * this.chunkSize, (i + 1) * this.chunkSize);
|
||||
|
||||
console.log(`正在上传第 ${i + 1}/${total} 批,chunk 大小: ${chunk.length}`);
|
||||
|
||||
await this.uploadFunc(chunk, i + 1, total);
|
||||
|
||||
if (this.delay > 0 && i + 1 < total) {
|
||||
await new Promise((res) => setTimeout(res, this.delay));
|
||||
}
|
||||
}
|
||||
|
||||
this.onComplete?.();
|
||||
}
|
||||
}
|
||||
|
||||
//使用示例
|
||||
// const uploader = new BatchUploader({
|
||||
// dataList: features,
|
||||
// chunkSize: 15000,
|
||||
// delay: 200,
|
||||
// uploadFunc: async (chunk, batchNum, totalBatch) => {
|
||||
// await addProjectPilePoint();//异步方法
|
||||
// },
|
||||
// onComplete: () => {
|
||||
// console.log('桩点上传完成');
|
||||
// }
|
||||
// });
|
||||
|
||||
// await uploader.start();
|
||||
22
plus-ui/src/utils/bus.ts
Normal file
22
plus-ui/src/utils/bus.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import mitt from 'mitt';
|
||||
|
||||
const emitter = mitt();
|
||||
|
||||
export default {
|
||||
install(app) {
|
||||
// 发送事件
|
||||
app.config.globalProperties.$sendChanel = (event, payload) => {
|
||||
emitter.emit(event, payload);
|
||||
};
|
||||
|
||||
// 监听事件(返回取消函数)
|
||||
app.config.globalProperties.$recvChanel = (event, handler) => {
|
||||
emitter.on(event, handler);
|
||||
|
||||
// 可选:返回解绑函数,方便使用
|
||||
return () => {
|
||||
emitter.off(event, handler);
|
||||
};
|
||||
};
|
||||
}
|
||||
};
|
||||
1737074
plus-ui/src/utils/geo.json
Normal file
1737074
plus-ui/src/utils/geo.json
Normal file
File diff suppressed because it is too large
Load Diff
196
plus-ui/src/utils/lassoSelect.ts
Normal file
196
plus-ui/src/utils/lassoSelect.ts
Normal file
@ -0,0 +1,196 @@
|
||||
import { Map as OLMap } from 'ol';
|
||||
import VectorSource from 'ol/source/Vector';
|
||||
import VectorLayer from 'ol/layer/Vector';
|
||||
import { LineString, Polygon } from 'ol/geom';
|
||||
import { Feature } from 'ol';
|
||||
import { Style, Stroke, Fill } from 'ol/style';
|
||||
import GeoJSON from 'ol/format/GeoJSON';
|
||||
import { polygon as turfPolygon, booleanIntersects } from '@turf/turf';
|
||||
import { toLonLat } from 'ol/proj';
|
||||
import DragPan from 'ol/interaction/DragPan';
|
||||
import MouseWheelZoom from 'ol/interaction/MouseWheelZoom';
|
||||
|
||||
export class LassoSelector {
|
||||
private map: OLMap;
|
||||
private drawLayer: VectorLayer<VectorSource>;
|
||||
private drawSource: VectorSource;
|
||||
private overlaySource: VectorSource;
|
||||
private overlayLayer: VectorLayer<VectorSource>;
|
||||
private drawing = false;
|
||||
private coordinates: [number, number][] = [];
|
||||
private targetSource: VectorSource;
|
||||
private isShiftKeyDown = false;
|
||||
private onSelectCallback: (selected: Feature[], isInvert?: boolean) => void;
|
||||
private dragPanInteraction: DragPan | null = null;
|
||||
private mouseWheelZoomInteraction: MouseWheelZoom | null = null;
|
||||
|
||||
constructor(map: OLMap, targetSource: VectorSource, onSelect: (selected: Feature[], isInvert?: boolean) => void) {
|
||||
this.map = map;
|
||||
this.targetSource = targetSource;
|
||||
this.onSelectCallback = onSelect;
|
||||
|
||||
// 找出拖动和滚轮缩放交互
|
||||
this.dragPanInteraction = this.map
|
||||
.getInteractions()
|
||||
.getArray()
|
||||
.find((interaction) => interaction instanceof DragPan) as DragPan;
|
||||
|
||||
this.mouseWheelZoomInteraction = this.map
|
||||
.getInteractions()
|
||||
.getArray()
|
||||
.find((interaction) => interaction instanceof MouseWheelZoom) as MouseWheelZoom;
|
||||
|
||||
this.drawSource = new VectorSource();
|
||||
this.drawLayer = new VectorLayer({
|
||||
source: this.drawSource,
|
||||
style: new Style({
|
||||
stroke: new Stroke({
|
||||
color: '#ff0000',
|
||||
width: 2
|
||||
})
|
||||
})
|
||||
});
|
||||
this.map.addLayer(this.drawLayer);
|
||||
|
||||
this.overlaySource = new VectorSource();
|
||||
this.overlayLayer = new VectorLayer({
|
||||
source: this.overlaySource,
|
||||
style: new Style({
|
||||
stroke: new Stroke({
|
||||
color: 'rgba(255, 0, 0, 0.8)',
|
||||
width: 2
|
||||
}),
|
||||
fill: new Fill({
|
||||
color: 'rgba(255, 0, 0, 0.3)'
|
||||
})
|
||||
})
|
||||
});
|
||||
this.map.addLayer(this.overlayLayer);
|
||||
|
||||
this.bindEvents();
|
||||
}
|
||||
|
||||
private bindEvents() {
|
||||
// 禁用默认右键菜单
|
||||
this.map.getViewport().addEventListener('contextmenu', (e) => e.preventDefault());
|
||||
|
||||
// pointerdown 捕获左键按下
|
||||
this.map.getViewport().addEventListener('pointerdown', (e) => {
|
||||
if (e.button === 0 && !this.drawing) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
this.isShiftKeyDown = e.shiftKey;
|
||||
this.drawing = true;
|
||||
this.coordinates = [];
|
||||
this.drawSource.clear();
|
||||
this.overlaySource.clear();
|
||||
|
||||
// 禁用拖动和缩放
|
||||
if (this.dragPanInteraction) this.dragPanInteraction.setActive(false);
|
||||
if (this.mouseWheelZoomInteraction) this.mouseWheelZoomInteraction.setActive(false);
|
||||
}
|
||||
});
|
||||
|
||||
// pointermove 画线
|
||||
this.map.on('pointermove', (evt) => {
|
||||
if (!this.drawing) return;
|
||||
const coord = evt.coordinate as [number, number];
|
||||
this.coordinates.push(coord);
|
||||
this.renderLine();
|
||||
this.renderPolygon();
|
||||
});
|
||||
|
||||
// pointerup 捕获左键抬起
|
||||
this.map.getViewport().addEventListener('pointerup', (e) => {
|
||||
if (e.button === 0 && this.drawing) {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
|
||||
this.drawing = false;
|
||||
this.handleDrawEnd();
|
||||
|
||||
// 恢复拖动和缩放
|
||||
if (this.dragPanInteraction) this.dragPanInteraction.setActive(true);
|
||||
if (this.mouseWheelZoomInteraction) this.mouseWheelZoomInteraction.setActive(true);
|
||||
}
|
||||
});
|
||||
|
||||
// 防止拖动导致意外事件
|
||||
this.map.getViewport().addEventListener('pointercancel', (e) => {
|
||||
if (this.drawing) {
|
||||
this.drawing = false;
|
||||
this.drawSource.clear();
|
||||
this.overlaySource.clear();
|
||||
|
||||
if (this.dragPanInteraction) this.dragPanInteraction.setActive(true);
|
||||
if (this.mouseWheelZoomInteraction) this.mouseWheelZoomInteraction.setActive(true);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private renderLine() {
|
||||
this.drawSource.clear();
|
||||
if (this.coordinates.length >= 2) {
|
||||
const line = new LineString(this.coordinates);
|
||||
const feature = new Feature({ geometry: line });
|
||||
this.drawSource.addFeature(feature);
|
||||
}
|
||||
}
|
||||
|
||||
private renderPolygon() {
|
||||
this.overlaySource.clear();
|
||||
if (this.coordinates.length < 3) return;
|
||||
|
||||
const polygonCoords = [...this.coordinates, this.coordinates[0]];
|
||||
const polygon = new Polygon([polygonCoords]);
|
||||
const feature = new Feature({ geometry: polygon });
|
||||
this.overlaySource.addFeature(feature);
|
||||
}
|
||||
|
||||
private handleDrawEnd() {
|
||||
if (this.coordinates.length < 3) {
|
||||
this.drawSource.clear();
|
||||
this.overlaySource.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
const first = this.coordinates[0];
|
||||
const last = this.coordinates[this.coordinates.length - 1];
|
||||
if (first[0] !== last[0] || first[1] !== last[1]) {
|
||||
this.coordinates.push([...first]);
|
||||
}
|
||||
|
||||
const coords4326 = this.coordinates.map((c) => toLonLat(c));
|
||||
const turfPoly = turfPolygon([coords4326]);
|
||||
|
||||
const geojson = new GeoJSON();
|
||||
const selected: Feature[] = [];
|
||||
|
||||
this.targetSource.getFeatures().forEach((feature) => {
|
||||
const geom = feature.getGeometry();
|
||||
if (!geom) return;
|
||||
|
||||
const geomObj = geojson.writeGeometryObject(geom, {
|
||||
featureProjection: 'EPSG:3857',
|
||||
dataProjection: 'EPSG:4326'
|
||||
}) as any;
|
||||
|
||||
if ((geomObj.type === 'Polygon' || geomObj.type === 'MultiPolygon') && booleanIntersects(turfPoly, geomObj)) {
|
||||
selected.push(feature);
|
||||
}
|
||||
});
|
||||
|
||||
if (selected.length) {
|
||||
this.onSelectCallback(selected, this.isShiftKeyDown);
|
||||
}
|
||||
|
||||
this.drawSource.clear();
|
||||
this.overlaySource.clear();
|
||||
}
|
||||
|
||||
destroy() {
|
||||
this.map.removeLayer(this.drawLayer);
|
||||
this.map.removeLayer(this.overlayLayer);
|
||||
}
|
||||
}
|
||||
40
plus-ui/src/utils/moveDiv.ts
Normal file
40
plus-ui/src/utils/moveDiv.ts
Normal file
@ -0,0 +1,40 @@
|
||||
export function setMove(downId, moveID) {
|
||||
let moveDiv = document.getElementById(downId)
|
||||
moveDiv.style.cssText += ';cursor:move;'
|
||||
let informationBox = document.getElementById(moveID)
|
||||
const sty = (() => {
|
||||
if (window.document.currentStyle) {
|
||||
return (dom, attr) => dom.currentStyle[attr];
|
||||
} else {
|
||||
return (dom, attr) => getComputedStyle(dom, false)[attr];
|
||||
}
|
||||
})()
|
||||
moveDiv.onmousedown = (e) => {
|
||||
// 鼠标按下,计算当前元素距离可视区的距离
|
||||
const disX = e.clientX - moveDiv.offsetLeft;
|
||||
const disY = e.clientY - moveDiv.offsetTop;
|
||||
// 获取到的值带px 正则匹配替换
|
||||
let styL = sty(informationBox, 'left');
|
||||
let styT = sty(informationBox, 'top');
|
||||
// 第一次获取到的值为组件自带50% 移动之后赋值为px
|
||||
if (styL.includes('%')) {
|
||||
styL = +document.body.clientWidth * (+styL.replace(/\%/g, '') / 100);
|
||||
styT = +document.body.clientHeight * (+styT.replace(/\%/g, '') / 100);
|
||||
} else {
|
||||
styL = +styL.replace(/\px/g, '');
|
||||
styT = +styT.replace(/\px/g, '');
|
||||
}
|
||||
document.onmousemove = function (e) {
|
||||
// 通过事件委托,计算移动的距离
|
||||
let left = e.clientX - disX;
|
||||
let top = e.clientY - disY;
|
||||
// 移动当前元素
|
||||
informationBox.style.cssText += `;left:${left + styL}px;top:${top + styT}px;`;
|
||||
};
|
||||
document.onmouseup = function (e) {
|
||||
document.onmousemove = null;
|
||||
document.onmouseup = null;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
19
plus-ui/src/utils/projectTeam.ts
Normal file
19
plus-ui/src/utils/projectTeam.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import $cache from '@/plugins/cache';
|
||||
//获取班组列表
|
||||
import { listProjectTeam } from '@/api/project/projectTeam';
|
||||
import { ProjectTeamVO } from '@/api/project/projectTeam/types';
|
||||
export const getProjectTeam = async () => {
|
||||
const { id } = $cache.local.getJSON('selectedProject');
|
||||
const res = await listProjectTeam({
|
||||
pageNum: 1,
|
||||
pageSize: 20,
|
||||
orderByColumn: 'createTime',
|
||||
isAsc: 'desc',
|
||||
projectId: id
|
||||
});
|
||||
const list = res.rows.map((projectTeam: ProjectTeamVO) => ({
|
||||
value: projectTeam.id,
|
||||
label: projectTeam.teamName
|
||||
}));
|
||||
$cache.local.setJSON('ProjectTeamList', list);
|
||||
};
|
||||
382
plus-ui/src/utils/reconnecting-websocket.js
Normal file
382
plus-ui/src/utils/reconnecting-websocket.js
Normal file
@ -0,0 +1,382 @@
|
||||
// MIT License:
|
||||
//
|
||||
// Copyright (c) 2010-2012, Joe Walnes
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
/**
|
||||
* This behaves like a WebSocket in every way, except if it fails to connect,
|
||||
* or it gets disconnected, it will repeatedly poll until it successfully connects
|
||||
* again.
|
||||
*
|
||||
* It is API compatible, so when you have:
|
||||
* ws = new WebSocket('ws://....');
|
||||
* you can replace with:
|
||||
* ws = new ReconnectingWebSocket('ws://....');
|
||||
*
|
||||
* The event stream will typically look like:
|
||||
* onconnecting
|
||||
* onopen
|
||||
* onmessage
|
||||
* onmessage
|
||||
* onclose // lost connection
|
||||
* onconnecting
|
||||
* onopen // sometime later...
|
||||
* onmessage
|
||||
* onmessage
|
||||
* etc...
|
||||
*
|
||||
* It is API compatible with the standard WebSocket API, apart from the following members:
|
||||
*
|
||||
* - `bufferedAmount`
|
||||
* - `extensions`
|
||||
* - `binaryType`
|
||||
*
|
||||
* Latest version: https://github.com/joewalnes/reconnecting-websocket/
|
||||
* - Joe Walnes
|
||||
*
|
||||
* Syntax
|
||||
* ======
|
||||
* var socket = new ReconnectingWebSocket(url, protocols, options);
|
||||
*
|
||||
* Parameters
|
||||
* ==========
|
||||
* url - The url you are connecting to.
|
||||
* protocols - Optional string or array of protocols.
|
||||
* options - See below
|
||||
*
|
||||
* Options
|
||||
* =======
|
||||
* Options can either be passed upon instantiation or set after instantiation:
|
||||
*
|
||||
* var socket = new ReconnectingWebSocket(url, null, { debug: true, reconnectInterval: 4000 });
|
||||
*
|
||||
* or
|
||||
*
|
||||
* var socket = new ReconnectingWebSocket(url);
|
||||
* socket.debug = true;
|
||||
* socket.reconnectInterval = 4000;
|
||||
*
|
||||
* debug
|
||||
* - Whether this instance should log debug messages. Accepts true or false. Default: false.
|
||||
*
|
||||
* automaticOpen
|
||||
* - Whether or not the websocket should attempt to connect immediately upon instantiation. The socket can be manually opened or closed at any time using ws.open() and ws.close().
|
||||
*
|
||||
* reconnectInterval
|
||||
* - The number of milliseconds to delay before attempting to reconnect. Accepts integer. Default: 1000.
|
||||
*
|
||||
* maxReconnectInterval
|
||||
* - The maximum number of milliseconds to delay a reconnection attempt. Accepts integer. Default: 30000.
|
||||
*
|
||||
* reconnectDecay
|
||||
* - The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. Accepts integer or float. Default: 1.5.
|
||||
*
|
||||
* timeoutInterval
|
||||
* - The maximum time in milliseconds to wait for a connection to succeed before closing and retrying. Accepts integer. Default: 2000.
|
||||
*
|
||||
*/
|
||||
// (function (global, factory) {
|
||||
// if (typeof define === 'function' && define.amd) {
|
||||
// define([], factory);
|
||||
// } else if (typeof module !== 'undefined' && module.exports){
|
||||
// module.exports = factory();
|
||||
// } else {
|
||||
// global.ReconnectingWebSocket = factory();
|
||||
// }
|
||||
// })(this, function () {
|
||||
//
|
||||
// if (!('WebSocket' in window)) {
|
||||
// return;
|
||||
// }
|
||||
|
||||
function ReconnectingWebSocket(url, protocols, options) {
|
||||
|
||||
// Default settings
|
||||
var settings = {
|
||||
|
||||
/** Whether this instance should log debug messages. */
|
||||
debug: false,
|
||||
|
||||
/** Whether or not the websocket should attempt to connect immediately upon instantiation. */
|
||||
automaticOpen: true,
|
||||
|
||||
/** The number of milliseconds to delay before attempting to reconnect. */
|
||||
reconnectInterval: 1000,
|
||||
/** The maximum number of milliseconds to delay a reconnection attempt. */
|
||||
maxReconnectInterval: 30000,
|
||||
/** The rate of increase of the reconnect delay. Allows reconnect attempts to back off when problems persist. */
|
||||
reconnectDecay: 1.5,
|
||||
|
||||
/** The maximum time in milliseconds to wait for a connection to succeed before closing and retrying. */
|
||||
timeoutInterval: 2000,
|
||||
|
||||
/** The maximum number of reconnection attempts to make. Unlimited if null. */
|
||||
maxReconnectAttempts: null,
|
||||
|
||||
/** The binary type, possible values 'blob' or 'arraybuffer', default 'blob'. */
|
||||
binaryType: 'blob'
|
||||
}
|
||||
if (!options) {
|
||||
options = {};
|
||||
}
|
||||
|
||||
// Overwrite and define settings with options if they exist.
|
||||
for (var key in settings) {
|
||||
if (typeof options[key] !== 'undefined') {
|
||||
this[key] = options[key];
|
||||
} else {
|
||||
this[key] = settings[key];
|
||||
}
|
||||
}
|
||||
|
||||
// These should be treated as read-only properties
|
||||
|
||||
/** The URL as resolved by the constructor. This is always an absolute URL. Read only. */
|
||||
this.url = url;
|
||||
|
||||
/** The number of attempted reconnects since starting, or the last successful connection. Read only. */
|
||||
this.reconnectAttempts = 0;
|
||||
|
||||
/**
|
||||
* The current state of the connection.
|
||||
* Can be one of: WebSocket.CONNECTING, WebSocket.OPEN, WebSocket.CLOSING, WebSocket.CLOSED
|
||||
* Read only.
|
||||
*/
|
||||
this.readyState = WebSocket.CONNECTING;
|
||||
|
||||
/**
|
||||
* A string indicating the name of the sub-protocol the server selected; this will be one of
|
||||
* the strings specified in the protocols parameter when creating the WebSocket object.
|
||||
* Read only.
|
||||
*/
|
||||
this.protocol = null;
|
||||
|
||||
// Private state variables
|
||||
|
||||
var self = this;
|
||||
var ws;
|
||||
var forcedClose = false;
|
||||
var timedOut = false;
|
||||
var eventTarget = document.createElement('div');
|
||||
|
||||
// Wire up "on*" properties as event handlers
|
||||
|
||||
eventTarget.addEventListener('open', function (event) {
|
||||
self.onopen(event);
|
||||
});
|
||||
eventTarget.addEventListener('close', function (event) {
|
||||
self.onclose(event);
|
||||
});
|
||||
eventTarget.addEventListener('connecting', function (event) {
|
||||
self.onconnecting(event);
|
||||
});
|
||||
eventTarget.addEventListener('message', function (event) {
|
||||
self.onmessage(event);
|
||||
});
|
||||
eventTarget.addEventListener('error', function (event) {
|
||||
self.onerror(event);
|
||||
});
|
||||
|
||||
// Expose the API required by EventTarget
|
||||
|
||||
this.addEventListener = eventTarget.addEventListener.bind(eventTarget);
|
||||
this.removeEventListener = eventTarget.removeEventListener.bind(eventTarget);
|
||||
this.dispatchEvent = eventTarget.dispatchEvent.bind(eventTarget);
|
||||
|
||||
/**
|
||||
* This function generates an event that is compatible with standard
|
||||
* compliant browsers and IE9 - IE11
|
||||
*
|
||||
* This will prevent the error:
|
||||
* Object doesn't support this action
|
||||
*
|
||||
* http://stackoverflow.com/questions/19345392/why-arent-my-parameters-getting-passed-through-to-a-dispatched-event/19345563#19345563
|
||||
* @param s String The name that the event should use
|
||||
* @param args Object an optional object that the event will use
|
||||
*/
|
||||
function generateEvent(s, args) {
|
||||
var evt = document.createEvent("CustomEvent");
|
||||
evt.initCustomEvent(s, false, false, args);
|
||||
return evt;
|
||||
};
|
||||
|
||||
this.open = function (reconnectAttempt) {
|
||||
ws = new WebSocket(self.url, protocols || []);
|
||||
ws.binaryType = this.binaryType;
|
||||
|
||||
if (reconnectAttempt) {
|
||||
if (this.maxReconnectAttempts && this.reconnectAttempts > this.maxReconnectAttempts) {
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
eventTarget.dispatchEvent(generateEvent('connecting'));
|
||||
this.reconnectAttempts = 0;
|
||||
}
|
||||
|
||||
if (self.debug || ReconnectingWebSocket.debugAll) {
|
||||
console.debug('ReconnectingWebSocket', 'attempt-connect', self.url);
|
||||
}
|
||||
|
||||
var localWs = ws;
|
||||
var timeout = setTimeout(function () {
|
||||
if (self.debug || ReconnectingWebSocket.debugAll) {
|
||||
console.debug('ReconnectingWebSocket', 'connection-timeout', self.url);
|
||||
}
|
||||
timedOut = true;
|
||||
localWs.close();
|
||||
timedOut = false;
|
||||
}, self.timeoutInterval);
|
||||
|
||||
ws.onopen = function (event) {
|
||||
clearTimeout(timeout);
|
||||
if (self.debug || ReconnectingWebSocket.debugAll) {
|
||||
console.debug('ReconnectingWebSocket', 'onopen', self.url);
|
||||
}
|
||||
self.protocol = ws.protocol;
|
||||
self.readyState = WebSocket.OPEN;
|
||||
self.reconnectAttempts = 0;
|
||||
var e = generateEvent('open');
|
||||
e.isReconnect = reconnectAttempt;
|
||||
reconnectAttempt = false;
|
||||
eventTarget.dispatchEvent(e);
|
||||
};
|
||||
|
||||
ws.onclose = function (event) {
|
||||
clearTimeout(timeout);
|
||||
ws = null;
|
||||
if (forcedClose) {
|
||||
self.readyState = WebSocket.CLOSED;
|
||||
eventTarget.dispatchEvent(generateEvent('close'));
|
||||
} else {
|
||||
self.readyState = WebSocket.CONNECTING;
|
||||
var e = generateEvent('connecting');
|
||||
e.code = event.code;
|
||||
e.reason = event.reason;
|
||||
e.wasClean = event.wasClean;
|
||||
eventTarget.dispatchEvent(e);
|
||||
if (!reconnectAttempt && !timedOut) {
|
||||
if (self.debug || ReconnectingWebSocket.debugAll) {
|
||||
console.debug('ReconnectingWebSocket', 'onclose', self.url);
|
||||
}
|
||||
eventTarget.dispatchEvent(generateEvent('close'));
|
||||
}
|
||||
|
||||
var timeout = self.reconnectInterval * Math.pow(self.reconnectDecay, self.reconnectAttempts);
|
||||
setTimeout(function () {
|
||||
self.reconnectAttempts++;
|
||||
self.open(true);
|
||||
}, timeout > self.maxReconnectInterval ? self.maxReconnectInterval : timeout);
|
||||
}
|
||||
};
|
||||
ws.onmessage = function (event) {
|
||||
if (self.debug || ReconnectingWebSocket.debugAll) {
|
||||
console.debug('ReconnectingWebSocket', 'onmessage', self.url, event.data);
|
||||
}
|
||||
var e = generateEvent('message');
|
||||
e.data = event.data;
|
||||
eventTarget.dispatchEvent(e);
|
||||
};
|
||||
ws.onerror = function (event) {
|
||||
if (self.debug || ReconnectingWebSocket.debugAll) {
|
||||
console.debug('ReconnectingWebSocket', 'onerror', self.url, event);
|
||||
}
|
||||
eventTarget.dispatchEvent(generateEvent('error'));
|
||||
};
|
||||
}
|
||||
|
||||
// Whether or not to create a websocket upon instantiation
|
||||
if (this.automaticOpen == true) {
|
||||
this.open(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transmits data to the server over the WebSocket connection.
|
||||
*
|
||||
* @param data a text string, ArrayBuffer or Blob to send to the server.
|
||||
*/
|
||||
this.send = function (data) {
|
||||
if (ws) {
|
||||
if (self.debug || ReconnectingWebSocket.debugAll) {
|
||||
console.debug('ReconnectingWebSocket', 'send', self.url, data);
|
||||
}
|
||||
return ws.send(data);
|
||||
} else {
|
||||
throw 'INVALID_STATE_ERR : Pausing to reconnect websocket';
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Closes the WebSocket connection or connection attempt, if any.
|
||||
* If the connection is already CLOSED, this method does nothing.
|
||||
*/
|
||||
this.close = function (code, reason) {
|
||||
// Default CLOSE_NORMAL code
|
||||
if (typeof code == 'undefined') {
|
||||
code = 1000;
|
||||
}
|
||||
forcedClose = true;
|
||||
if (ws) {
|
||||
ws.close(code, reason);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Additional public API method to refresh the connection if still open (close, re-open).
|
||||
* For example, if the app suspects bad data / missed heart beats, it can try to refresh.
|
||||
*/
|
||||
this.refresh = function () {
|
||||
if (ws) {
|
||||
ws.close();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* An event listener to be called when the WebSocket connection's readyState changes to OPEN;
|
||||
* this indicates that the connection is ready to send and receive data.
|
||||
*/
|
||||
ReconnectingWebSocket.prototype.onopen = function (event) {
|
||||
};
|
||||
/** An event listener to be called when the WebSocket connection's readyState changes to CLOSED. */
|
||||
ReconnectingWebSocket.prototype.onclose = function (event) {
|
||||
};
|
||||
/** An event listener to be called when a connection begins being attempted. */
|
||||
ReconnectingWebSocket.prototype.onconnecting = function (event) {
|
||||
};
|
||||
/** An event listener to be called when a message is received from the server. */
|
||||
ReconnectingWebSocket.prototype.onmessage = function (event) {
|
||||
};
|
||||
/** An event listener to be called when an error occurs. */
|
||||
ReconnectingWebSocket.prototype.onerror = function (event) {
|
||||
};
|
||||
|
||||
/**
|
||||
* Whether all instances of ReconnectingWebSocket should log debug messages.
|
||||
* Setting this to true is the equivalent of setting all instances of ReconnectingWebSocket.debug to true.
|
||||
*/
|
||||
ReconnectingWebSocket.debugAll = false;
|
||||
|
||||
ReconnectingWebSocket.CONNECTING = WebSocket.CONNECTING;
|
||||
ReconnectingWebSocket.OPEN = WebSocket.OPEN;
|
||||
ReconnectingWebSocket.CLOSING = WebSocket.CLOSING;
|
||||
ReconnectingWebSocket.CLOSED = WebSocket.CLOSED;
|
||||
|
||||
// return ReconnectingWebSocket;
|
||||
// });
|
||||
55
plus-ui/src/utils/requset2.ts
Normal file
55
plus-ui/src/utils/requset2.ts
Normal file
@ -0,0 +1,55 @@
|
||||
import $modal from '@/plugins/modal';
|
||||
import axios from 'axios';
|
||||
|
||||
// 创建一个 Axios 实例并设置默认配置
|
||||
const axiosInstance = axios.create({
|
||||
baseURL: import.meta.env.VITE_APP_BASE_DRONE_API,
|
||||
timeout: 5000
|
||||
});
|
||||
|
||||
// 请求拦截器,添加 token 到每个请求头
|
||||
axiosInstance.interceptors.request.use(
|
||||
(config) => {
|
||||
let air = localStorage.getItem('airToken');
|
||||
let token = '';
|
||||
if (air) {
|
||||
token = JSON.parse(air).access_token; // 假设 token 存储在 localStorage 中
|
||||
}
|
||||
if (token) {
|
||||
config.headers['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
},
|
||||
(error) => {
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
// 处理消息显示
|
||||
function showMessage(message, type = 'error') {
|
||||
$modal.msg({
|
||||
message: message,
|
||||
type: type,
|
||||
duration: 5 * 1000
|
||||
});
|
||||
}
|
||||
|
||||
export function request(params) {
|
||||
// 如果有取消请求的需求,可以在此处添加 CancelToken
|
||||
// const source = axios.CancelToken.source();
|
||||
// params.cancelToken = source.token;
|
||||
|
||||
// 确保 URL 是完整的
|
||||
// if (!params.url.startsWith(process.env.AIR)) {
|
||||
// params.url = process.env.AIR;
|
||||
// }
|
||||
|
||||
return axiosInstance(params).then((res) => {
|
||||
if (res.data.code === 200) {
|
||||
return res.data;
|
||||
} else {
|
||||
if (!res.data.code) return res.data;
|
||||
showMessage(res.data.message || res.data.msg);
|
||||
return Promise.reject(res.data);
|
||||
}
|
||||
});
|
||||
}
|
||||
58
plus-ui/src/utils/setMapCenter.ts
Normal file
58
plus-ui/src/utils/setMapCenter.ts
Normal file
@ -0,0 +1,58 @@
|
||||
// MapViewFitter.ts
|
||||
import { Map as OlMap } from 'ol';
|
||||
import GeoJSON from 'ol/format/GeoJSON';
|
||||
import { FeatureCollection } from 'geojson';
|
||||
import { bbox as turfBbox, bboxPolygon as turfBboxPolygon } from '@turf/turf';
|
||||
import type { Geometry } from 'ol/geom';
|
||||
|
||||
export class MapViewFitter {
|
||||
private map: OlMap;
|
||||
private format: GeoJSON;
|
||||
|
||||
constructor(map: OlMap) {
|
||||
this.map = map;
|
||||
this.format = new GeoJSON();
|
||||
}
|
||||
|
||||
/**
|
||||
* 使地图视图自动适应传入的 GeoJSON FeatureCollection 范围
|
||||
* @param featureCollection GeoJSON FeatureCollection
|
||||
* @param padding 四周留白,默认为 [10, 10, 10, 10]
|
||||
* @param duration 动画持续时间,默认为 1000 毫秒
|
||||
*/
|
||||
fit(featureCollection: FeatureCollection, padding: number[] = [10, 10, 10, 10], duration: number = 1000) {
|
||||
if (!featureCollection?.features?.length) return;
|
||||
|
||||
const bbox = turfBbox(featureCollection); // [minX, minY, maxX, maxY]
|
||||
const bboxPolygon = turfBboxPolygon(bbox); // Feature<Polygon>
|
||||
|
||||
const geometry: Geometry = this.format.readGeometry(bboxPolygon.geometry, {
|
||||
dataProjection: 'EPSG:4326',
|
||||
featureProjection: 'EPSG:3857'
|
||||
});
|
||||
|
||||
const extent = geometry.getExtent();
|
||||
|
||||
this.map.getView().fit(extent, {
|
||||
padding,
|
||||
duration
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//示例
|
||||
// import { MapViewFitter } from '@/utils/setMapCenter'; // 确保路径正确
|
||||
|
||||
// const fitter = new MapViewFitter(map); // 传入你的 OpenLayers 地图实例
|
||||
// const features = xxx;//features数组
|
||||
|
||||
// if (features?.length) {
|
||||
// const featureCollection = {
|
||||
// type: 'FeatureCollection',
|
||||
// features
|
||||
// };
|
||||
|
||||
// fitter.fit(featureCollection);
|
||||
// }
|
||||
|
||||
68
plus-ui/src/utils/snowflake.ts
Normal file
68
plus-ui/src/utils/snowflake.ts
Normal file
@ -0,0 +1,68 @@
|
||||
export class SnowflakeIdGenerator {
|
||||
private readonly startTimeStamp = 1609459200000; // 起始时间戳,这里设置为 2021-01-01 00:00:00
|
||||
private readonly workerIdBits = 5;
|
||||
private readonly dataCenterIdBits = 5;
|
||||
private readonly sequenceBits = 12;
|
||||
|
||||
private readonly maxWorkerId = -1 ^ (-1 << this.workerIdBits);
|
||||
private readonly maxDataCenterId = -1 ^ (-1 << this.dataCenterIdBits);
|
||||
|
||||
private readonly workerIdShift = this.sequenceBits;
|
||||
private readonly dataCenterIdShift = this.sequenceBits + this.workerIdBits;
|
||||
private readonly timestampLeftShift = this.sequenceBits + this.workerIdBits + this.dataCenterIdBits;
|
||||
private readonly sequenceMask = -1 ^ (-1 << this.sequenceBits);
|
||||
|
||||
private workerId: number;
|
||||
private dataCenterId: number;
|
||||
private sequence = 0;
|
||||
private lastTimestamp = -1;
|
||||
|
||||
constructor(workerId: number, dataCenterId: number) {
|
||||
if (workerId > this.maxWorkerId || workerId < 0) {
|
||||
throw new Error(`Worker ID 必须在 0 到 ${this.maxWorkerId} 之间`);
|
||||
}
|
||||
if (dataCenterId > this.maxDataCenterId || dataCenterId < 0) {
|
||||
throw new Error(`数据中心 ID 必须在 0 到 ${this.maxDataCenterId} 之间`);
|
||||
}
|
||||
this.workerId = workerId;
|
||||
this.dataCenterId = dataCenterId;
|
||||
}
|
||||
|
||||
private waitNextMillis(lastTimestamp: number): number {
|
||||
let timestamp = this.getCurrentTimestamp();
|
||||
while (timestamp <= lastTimestamp) {
|
||||
timestamp = this.getCurrentTimestamp();
|
||||
}
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
private getCurrentTimestamp(): number {
|
||||
return Date.now();
|
||||
}
|
||||
|
||||
nextId(): number {
|
||||
let timestamp = this.getCurrentTimestamp();
|
||||
|
||||
if (timestamp < this.lastTimestamp) {
|
||||
throw new Error('时钟回拨,拒绝生成 ID 达 ' + (this.lastTimestamp - timestamp) + ' 毫秒');
|
||||
}
|
||||
|
||||
if (timestamp === this.lastTimestamp) {
|
||||
this.sequence = (this.sequence + 1) & this.sequenceMask;
|
||||
if (this.sequence === 0) {
|
||||
timestamp = this.waitNextMillis(this.lastTimestamp);
|
||||
}
|
||||
} else {
|
||||
this.sequence = 0;
|
||||
}
|
||||
|
||||
this.lastTimestamp = timestamp;
|
||||
|
||||
return (
|
||||
((timestamp - this.startTimeStamp) << this.timestampLeftShift) |
|
||||
(this.dataCenterId << this.dataCenterIdShift) |
|
||||
(this.workerId << this.workerIdShift) |
|
||||
this.sequence
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user