cc-tools-utils 0.0.2 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,7 @@
1
- 一个由 chencheng)开发与维护的个人 JavaScript/Node.js 工具函数库,旨在为日常开发提供常用的、高质量的辅助工具。
1
+ 一个由 chencheng 开发与维护的个人工具函数库,旨在为日常开发提供常用的、高质量的辅助工具。
2
2
 
3
3
  ✨ 特性
4
+
4
5
  开箱即用:简单导入,无需复杂配置
5
6
 
6
7
  轻量无依赖:保持核心功能的纯净,不引入第三方依赖
@@ -9,4 +10,27 @@
9
10
 
10
11
  模块化:按需引入,支持 Tree Shaking
11
12
 
12
- 良好测试:核心功能均有单元测试覆盖
13
+ 良好测试:核心功能均有单元测试覆盖
14
+
15
+ ## 📦 安装
16
+
17
+ ```bash
18
+ npm install cc-tools-utils
19
+ # 或
20
+ yarn add cc-tools-utils
21
+ # 或
22
+ pnpm add cc-tools-utils
23
+ ```
24
+
25
+
26
+ ## 🚀 使用
27
+
28
+ ```javascript
29
+ import { cesiumHooks, tools } from 'cc-tools-utils';
30
+
31
+ // 使用 Cesium hooks
32
+ const { viewer } = cesiumHooks.initCesium('cesiumContainer');
33
+
34
+ // 使用工具函数
35
+ const debouncedFn = tools.debounce(300, () => console.log('debounced'));
36
+ ```
package/index.js ADDED
@@ -0,0 +1,12 @@
1
+ // import * as img from "./lib/img/Transform.js";
2
+ import * as tools from "./lib/js/tools.js";
3
+ import * as cesiumHooks from "./lib/js/cesiumHooks.js";
4
+
5
+ // 命名导出
6
+ export { tools, cesiumHooks };
7
+
8
+ // 默认导出
9
+ export default {
10
+ tools,
11
+ cesiumHooks,
12
+ };
@@ -20,7 +20,6 @@ import {
20
20
  Color,
21
21
  } from "cesium";
22
22
  import * as Cesium from "cesium";
23
- import { ref } from "vue";
24
23
 
25
24
  // 默认镜头高度
26
25
  export const DEFAULT_HEIGHT = 25000000;
@@ -28,13 +27,18 @@ export const DEFAULT_HEIGHT = 25000000;
28
27
  export const DEFAULT_POSITION = [109, 35.5, DEFAULT_HEIGHT];
29
28
  // 模型显示的距离阈值(米)
30
29
  export const MODEL_DISPLAY_DISTANCE = 5000000;
31
- // 慧居公司主体 Access_Tokens
32
- export const Cesium_Access_Tokens =
33
- "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiI2Mzg2ZTAwNy0zODczLTQxYjUtODE3OS0zYzI5ZmM1MTU3MGIiLCJpZCI6Mzc4NzcwLCJpYXQiOjE3Njg0NDQyODd9.u9gpkF-dD_GHOX9dbnNR5k7Zv6oVpUcNm5WOeqvxzu0";
34
- // 所有的实体ids
35
- export const allEntitiesIds = ref([]);
36
- // 赋值accessToken
37
- Cesium.Ion.defaultAccessToken = Cesium_Access_Tokens;
30
+
31
+ /**
32
+ * 设置Cesium Ion访问令牌
33
+ * @param {string} token - Cesium Ion访问令牌
34
+ */
35
+ export const setCesiumAccessToken = (token) => {
36
+ if (!token || typeof token !== "string") {
37
+ console.warn("setCesiumAccessToken: token必须是非空字符串");
38
+ return;
39
+ }
40
+ Cesium.Ion.defaultAccessToken = token;
41
+ };
38
42
 
39
43
  // 判断是否是同一个对象
40
44
  export function isArrayEqual(arr1, arr2) {
@@ -61,6 +65,38 @@ export const getHeading = (pointA, pointB) => {
61
65
  return CesiumMath.TWO_PI - CesiumMath.zeroToTwoPi(heading);
62
66
  };
63
67
 
68
+ /**
69
+ * 计算两点间的恒向线方位角(Rhumb Bearing)
70
+ * @param {Array} pos1 - 起点 [经度, 纬度]
71
+ * @param {Array} pos2 - 终点 [经度, 纬度]
72
+ * @returns {number} 方位角(度数)
73
+ */
74
+ export const getRhumbBearing = (pos1, pos2) => {
75
+ if (!Array.isArray(pos1) || !Array.isArray(pos2)) {
76
+ throw new Error("getRhumbBearing: 参数必须是数组");
77
+ }
78
+ if (pos1.length < 2 || pos2.length < 2) {
79
+ throw new Error("getRhumbBearing: 参数数组至少需要两个元素[经度, 纬度]");
80
+ }
81
+
82
+ const [lon1, lat1] = pos1;
83
+ const [lon2, lat2] = pos2;
84
+
85
+ // 将度数转换为弧度
86
+ const φ1 = CesiumMath.toRadians(lat1);
87
+ const φ2 = CesiumMath.toRadians(lat2);
88
+ const Δλ = CesiumMath.toRadians(lon2 - lon1);
89
+
90
+ // 计算恒向线方位角
91
+ const Δψ = Math.log(
92
+ Math.tan(φ2 / 2 + Math.PI / 4) / Math.tan(φ1 / 2 + Math.PI / 4),
93
+ );
94
+ const θ = Math.atan2(Δλ, Δψ);
95
+
96
+ // 转换为度数并标准化到0-360范围
97
+ return (CesiumMath.toDegrees(θ) + 360) % 360;
98
+ };
99
+
64
100
  // 获取俯仰角
65
101
  export const getPitch = (pointA, pointB) => {
66
102
  const transform = Transforms.eastNorthUpToFixedFrame(pointA);
@@ -118,20 +154,39 @@ export const transformCartesian3ToWGS84 = (cartesian3) => {
118
154
 
119
155
  // 颜色
120
156
  export const toCesiumColor = (color) => {
121
- return Cesium.Color.fromCssColorString(color);
157
+ if (!color || typeof color !== "string") {
158
+ console.warn("toCesiumColor: color必须是字符串,使用默认颜色");
159
+ return Cesium.Color.WHITE;
160
+ }
161
+ try {
162
+ return Cesium.Color.fromCssColorString(color);
163
+ } catch (error) {
164
+ console.warn(`toCesiumColor: 无效的颜色值 ${color},使用默认颜色`);
165
+ return Cesium.Color.WHITE;
166
+ }
122
167
  };
123
168
 
124
169
  // 位置
125
170
  export const toCesiumPosition = (position) => {
126
- if (Array.isArray(position)) {
127
- return Cesium.Cartesian3.fromDegrees(...position);
128
- } else {
129
- throw new Error("Position must be an array of numbers.");
171
+ if (!Array.isArray(position)) {
172
+ throw new Error("toCesiumPosition: position必须是数组");
173
+ }
174
+ if (position.length < 2) {
175
+ throw new Error(
176
+ "toCesiumPosition: position数组至少需要两个元素[经度, 纬度]",
177
+ );
178
+ }
179
+ if (position.some((v) => typeof v !== "number" || isNaN(v))) {
180
+ throw new Error("toCesiumPosition: position数组元素必须是有效数字");
130
181
  }
182
+ return Cesium.Cartesian3.fromDegrees(...position);
131
183
  };
132
184
 
133
185
  // 返回中国视角
134
186
  export const backToChina = (viewer, callback, time) => {
187
+ if (!viewer) {
188
+ throw new Error("backToChina: viewer参数是必需的");
189
+ }
135
190
  viewer.camera.flyTo({
136
191
  destination: toCesiumPosition(DEFAULT_POSITION),
137
192
  duration: time || 1.5,
@@ -143,6 +198,9 @@ export const backToChina = (viewer, callback, time) => {
143
198
 
144
199
  // 初始化 一切
145
200
  export const initCesium = (boxID, doSomething) => {
201
+ if (!boxID) {
202
+ throw new Error("initCesium: boxID参数是必需的");
203
+ }
146
204
  // 1. 创建Viewer
147
205
  const viewer = new Cesium.Viewer(boxID, {
148
206
  contextOptions: {
@@ -228,11 +286,13 @@ export const divergingWaves = (
228
286
  speed = 1,
229
287
  defaultColor = "#ff0000",
230
288
  id = "",
231
- },
289
+ } = {},
232
290
  ) => {
233
291
  if (!Cesium.defined(viewer)) {
234
- console.error("Viewer is not defined");
235
- return;
292
+ throw new Error("divergingWaves: viewer参数是必需的");
293
+ }
294
+ if (!Array.isArray(defaultPosition) || defaultPosition.length < 2) {
295
+ throw new Error("divergingWaves: defaultPosition必须是有效的坐标数组");
236
296
  }
237
297
  const entity = viewer.entities.add({
238
298
  id,
@@ -607,8 +667,14 @@ export const addPointer = (
607
667
  nearDistance = 1000,
608
668
  farDistance = 400000000,
609
669
  hoverImage,
610
- },
670
+ } = {},
611
671
  ) => {
672
+ if (!viewer) {
673
+ throw new Error("addPointer: viewer参数是必需的");
674
+ }
675
+ if (!position) {
676
+ throw new Error("addPointer: position参数是必需的");
677
+ }
612
678
  // 构建实体配置
613
679
  const entityConfig = {
614
680
  id,
@@ -737,13 +803,14 @@ export const initEvents = ({ viewer, scene, camera, entities, clock }) => {
737
803
  viewer.cesiumWidget.screenSpaceEventHandler.removeInputAction(
738
804
  ScreenSpaceEventType.LEFT_DOUBLE_CLICK,
739
805
  );
806
+
807
+ let allEntitiesIds;
740
808
  // 监听实体添加
741
809
  entities.collectionChanged.addEventListener((collection, added, removed) => {
742
- let old = allEntitiesIds.value;
810
+ let old = allEntitiesIds;
743
811
  let current = collection.values.map((item) => item.id);
744
812
  if (!isArrayEqual(old, current)) {
745
- allEntitiesIds.value = entities.values.map((item) => item.id);
746
- // console.log(" 添加了 实体", allEntitiesIds.value);
813
+ allEntitiesIds = entities.values.map((item) => item.id);
747
814
  }
748
815
  });
749
816
 
@@ -814,6 +881,8 @@ export const initEvents = ({ viewer, scene, camera, entities, clock }) => {
814
881
  // console.log("鼠标位置经纬度:", log_String, lat_String, alti_String);
815
882
  }
816
883
  }, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
884
+
885
+ return allEntitiesIds;
817
886
  };
818
887
 
819
888
  // 渲染更新 位置
@@ -1052,3 +1121,7 @@ export const addHover = (viewer, onHover) => {
1052
1121
  if (hoverTimer) clearTimeout(hoverTimer);
1053
1122
  };
1054
1123
  };
1124
+
1125
+ export const cc = () => {
1126
+ return 1;
1127
+ };
@@ -0,0 +1,36 @@
1
+ // 防抖函数
2
+ export const debounce = (time, fn) => {
3
+ if (typeof fn !== "function") {
4
+ throw new Error("debounce的第二个参数必须是函数");
5
+ }
6
+ if (typeof time !== "number" || time < 0) {
7
+ throw new Error("debounce的第一个参数必须是非负数");
8
+ }
9
+ let timeoutID = null;
10
+ return function () {
11
+ if (timeoutID) clearTimeout(timeoutID);
12
+ timeoutID = setTimeout(() => {
13
+ fn.apply(this, arguments);
14
+ timeoutID = null;
15
+ }, time);
16
+ };
17
+ };
18
+
19
+ // 节流
20
+ export const throttle = (time, fn) => {
21
+ if (typeof fn !== "function") {
22
+ throw new Error("throttle的第二个参数必须是函数");
23
+ }
24
+ if (typeof time !== "number" || time < 0) {
25
+ throw new Error("throttle的第一个参数必须是非负数");
26
+ }
27
+ let isRunning = false;
28
+ return function () {
29
+ if (isRunning) return;
30
+ isRunning = true;
31
+ fn.apply(this, arguments);
32
+ setTimeout(() => {
33
+ isRunning = false;
34
+ }, time);
35
+ };
36
+ };
package/package.json CHANGED
@@ -1,58 +1,55 @@
1
1
  {
2
2
  "name": "cc-tools-utils",
3
- "version": "0.0.2",
4
- "description": "A collection of useful JavaScript utilities including animation tools, image processing, and event bus",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
3
+ "version": "0.0.4",
4
+ "description": "Cesium工具库,包含常用的Cesium hooks和工具函数",
5
+ "main": "index.js",
6
+ "types": "index.d.ts",
7
7
  "scripts": {
8
- "type-check": "tsc --noEmit",
8
+ "type-check": "false",
9
9
  "test": "echo \"Error: no test specified\" && exit 1",
10
- "build": "webpack --config webpack.dev.js",
11
- "dev": "webpack --config webpack.dev.js --mode development"
10
+ "build": "webpack --config webpack.dev.js "
12
11
  },
13
12
  "keywords": [
14
- "utilities",
13
+ "cesium",
15
14
  "tools",
16
- "animation",
17
- "image-processing",
18
- "event-bus",
19
- "javascript",
20
- "typescript"
15
+ "utils",
16
+ "hooks"
21
17
  ],
22
- "author": "chencheng",
23
- "license": "MIT",
18
+ "author": "",
19
+ "license": "ISC",
24
20
  "repository": {
25
21
  "type": "git",
26
22
  "url": "https://gitee.com/chen-cheng-1998/learn_own.git"
27
23
  },
28
24
  "files": [
29
- "dist",
30
25
  "lib",
31
26
  "README.md",
32
- "LICENSE"
27
+ "LICENSE",
28
+ "index.js",
29
+ "index.d.ts",
30
+ "vue-config-helper.js",
31
+ "CONSUMER_PROJECT_CONFIG.md"
33
32
  ],
34
- "dependencies": {
33
+ "peerDependencies": {
34
+ "cesium": ">=1.100.0",
35
+ "vue": ">=3.0.0"
36
+ },
37
+ "devDependencies": {
35
38
  "@vue/compiler-sfc": "^3.5.11",
36
- "cesium": "^1.137.0",
37
39
  "css-loader": "^7.1.2",
38
40
  "style-loader": "^4.0.0",
39
- "vue": "3.2.13",
40
- "vue-loader": "^17.4.2"
41
- },
42
- "devDependencies": {
41
+ "vue-loader": "^17.4.2",
43
42
  "@babel/core": "^7.18.0",
43
+ "@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
44
44
  "@babel/plugin-proposal-optional-chaining": "^7.17.12",
45
45
  "@babel/preset-env": "^7.18.2",
46
- "@babel/preset-react": "^7.17.12",
47
46
  "@types/node": "^22.7.5",
48
47
  "babel-loader": "^8.2.5",
49
48
  "clean-webpack-plugin": "^4.0.0",
50
- "copy-webpack-plugin": "^11.0.0",
51
49
  "ts-loader": "^9.2.6",
52
50
  "typescript": "^4.5.4",
53
51
  "webpack": "^5.65.0",
54
- "webpack-bundle-analyzer": "^4.5.0",
55
52
  "webpack-cli": "^4.9.1",
56
53
  "webpack-parallel-uglify-plugin": "^2.0.0"
57
54
  }
58
- }
55
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * cc-tools-utils 的 Vue CLI 配置辅助工具
3
+ *
4
+ * 使用方法:
5
+ * 在消费项目的 vue.config.js 中:
6
+ *
7
+ * const ccToolsConfig = require('cc-tools-utils/vue-config-helper');
8
+ * module.exports = ccToolsConfig;
9
+ *
10
+ * 或者合并配置:
11
+ * const { defineConfig } = require('@vue/cli-service');
12
+ * const ccToolsConfig = require('cc-tools-utils/vue-config-helper');
13
+ *
14
+ * module.exports = defineConfig({
15
+ * ...ccToolsConfig,
16
+ * // 你的其他配置
17
+ * });
18
+ */
19
+
20
+ const { defineConfig } = require("@vue/cli-service");
21
+
22
+ module.exports = defineConfig({
23
+ transpileDependencies: ["cesium", /@cesium/],
24
+
25
+ configureWebpack: {
26
+ module: {
27
+ rules: [
28
+ {
29
+ test: /\.m?js$/,
30
+ include: /node_modules[\\/](cesium|@cesium)/,
31
+ use: {
32
+ loader: "babel-loader",
33
+ options: {
34
+ presets: [
35
+ [
36
+ "@babel/preset-env",
37
+ {
38
+ targets: {
39
+ browsers: ["> 1%", "last 2 versions", "not ie <= 8"],
40
+ },
41
+ },
42
+ ],
43
+ ],
44
+ plugins: [
45
+ "@babel/plugin-proposal-nullish-coalescing-operator",
46
+ "@babel/plugin-proposal-optional-chaining",
47
+ ],
48
+ },
49
+ },
50
+ },
51
+ ],
52
+ },
53
+ },
54
+ });
package/dist/index.d.ts DELETED
@@ -1,11 +0,0 @@
1
- import * as img from "./lib/img/Transform";
2
- import * as js from "./lib/js/tools";
3
- import * as bus from "./lib/js/bus";
4
- import * as cesiumHooks from "./lib/js/cesiumHooks";
5
- declare const _default: {
6
- img: typeof img;
7
- js: typeof js;
8
- bus: typeof bus;
9
- cesiumHooks: typeof cesiumHooks;
10
- };
11
- export default _default;