@cqsjjb/jjb-react-admin-component 3.3.1-beta.6 → 3.3.1-beta.8

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/AMap/index.js ADDED
@@ -0,0 +1,372 @@
1
+ import React, { useEffect, useRef, useState, useCallback } from 'react';
2
+ import { Button, Col, message, Modal, Result, Row } from 'antd';
3
+ import './index.less';
4
+ const _A_MAP_SEARCH_ = '_A_MAP_SEARCH_';
5
+ const _A_MAP_DEFAULT_POINT_ = {
6
+ lng: 116.397437,
7
+ lat: 39.909148
8
+ };
9
+ export default function AMap({
10
+ lng: propLng,
11
+ lat: propLat,
12
+ title = '高德地图',
13
+ width = 700,
14
+ onOk,
15
+ onCancel
16
+ }) {
17
+ const [lng, setLng] = useState(_A_MAP_DEFAULT_POINT_.lng);
18
+ const [lat, setLat] = useState(_A_MAP_DEFAULT_POINT_.lat);
19
+ const GL = useRef(null);
20
+ const mapRef = useRef(null);
21
+ const markerRef = useRef(null);
22
+ const autoRef = useRef(null);
23
+ const placeSearchRef = useRef(null);
24
+ useEffect(() => {
25
+ if (typeof window.AMap === 'undefined') {
26
+ message.error('未导入高德地图文件,生成地图失败!').then(() => null);
27
+ return;
28
+ }
29
+ let mounted = true;
30
+
31
+ // 保证传入的初始值是 number(避免 NaN)
32
+ const initLng = Number(propLng) || _A_MAP_DEFAULT_POINT_.lng;
33
+ const initLat = Number(propLat) || _A_MAP_DEFAULT_POINT_.lat;
34
+
35
+ // 等待容器可见且尺寸非 0(Modal 动画或隐藏态时常为 0)
36
+ const waitForVisible = (el, timeout = 3000) => new Promise(resolve => {
37
+ const start = Date.now();
38
+ function check() {
39
+ if (!el) return resolve(false);
40
+ const r = el.getBoundingClientRect();
41
+ if (r.width > 0 && r.height > 0) return resolve(true);
42
+ if (Date.now() - start > timeout) return resolve(false);
43
+ requestAnimationFrame(check);
44
+ }
45
+ check();
46
+ });
47
+ async function init() {
48
+ if (!GL.current) {
49
+ console.warn('AMap container ref not ready');
50
+ return;
51
+ }
52
+ const visible = await waitForVisible(GL.current, 3000);
53
+ if (!visible) {
54
+ // 继续也可,但记录一下。多数情况下等待后再 init 能解决 Pixel(NaN, 0)
55
+ console.warn('AMap container still not visible before init (proceeding anyway).');
56
+ }
57
+ try {
58
+ const map = new window.AMap.Map(GL.current, {
59
+ center: [initLng, initLat],
60
+ zoom: 18,
61
+ viewMode: '3D'
62
+ });
63
+ mapRef.current = map;
64
+
65
+ // 保证 state 与实际中心一致
66
+ setLng(initLng);
67
+ setLat(initLat);
68
+
69
+ // 插件加载、控件创建都做容错
70
+ window.AMap.plugin(['AMap.Scale', 'AMap.ToolBar', 'AMap.Geolocation', 'AMap.AutoComplete', 'AMap.PlaceSearch', 'AMap.Geocoder'], () => {
71
+ try {
72
+ map.addControl(new window.AMap.Scale());
73
+ } catch (e) {
74
+ // 某些构建/版本中可能不存在
75
+ console.warn('Scale control failed', e);
76
+ }
77
+ try {
78
+ map.addControl(new window.AMap.ToolBar({
79
+ position: 'RT'
80
+ }));
81
+ } catch (e) {
82
+ console.warn('ToolBar control failed', e);
83
+ }
84
+ try {
85
+ // 使用合法角落(RB/RT/LB/LT),不要自定义 'BT'
86
+ const geolocation = new window.AMap.Geolocation({
87
+ position: 'RB',
88
+ offset: new window.AMap.Pixel(Number(20), Number(20)),
89
+ showCircle: false,
90
+ showButton: true
91
+ });
92
+ map.addControl(geolocation);
93
+
94
+ // 正确的回调签名 (status, result)
95
+ geolocation.getCurrentPosition((status, result) => {
96
+ if (status !== 'complete') {
97
+ // 不做致命处理,但提示
98
+ message.error('未开启定位,定位失败!');
99
+ } else {
100
+ // 如果需要可以从 result.position 取值
101
+ try {
102
+ const pos = result && result.position;
103
+ if (pos) {
104
+ const px = typeof pos.getLng === 'function' ? pos.getLng() : pos.lng;
105
+ const py = typeof pos.getLat === 'function' ? pos.getLat() : pos.lat;
106
+ if (!Number.isNaN(Number(px)) && !Number.isNaN(Number(py))) {
107
+ setLng(Number(px));
108
+ setLat(Number(py));
109
+ map.setCenter([Number(px), Number(py)]);
110
+ }
111
+ }
112
+ } catch (e) {
113
+ // 忽略
114
+ }
115
+ }
116
+ });
117
+ } catch (e) {
118
+ console.warn('Geolocation init failed', e);
119
+ }
120
+
121
+ // 初始 marker 放置
122
+ try {
123
+ markerRef.current = new window.AMap.Marker({
124
+ position: [initLng, initLat],
125
+ map
126
+ });
127
+ } catch (e) {
128
+ console.warn('Marker init failed', e);
129
+ }
130
+
131
+ // AutoComplete + PlaceSearch
132
+ try {
133
+ placeSearchRef.current = new window.AMap.PlaceSearch({
134
+ map
135
+ });
136
+ autoRef.current = new window.AMap.AutoComplete({
137
+ input: document.getElementById(_A_MAP_SEARCH_)
138
+ });
139
+ autoRef.current.on && autoRef.current.on('select', e => {
140
+ // e.poi 里可能是 AMap.LngLat 实例 / 对象 / 数组,做兼容处理
141
+ const poi = e && e.poi ? e.poi : null;
142
+ let loc = poi && (poi.location || poi.lnglat) || e && (e.location || e.lnglat) || null;
143
+ let foundLng = null;
144
+ let foundLat = null;
145
+ if (loc) {
146
+ if (typeof loc.getLng === 'function' && typeof loc.getLat === 'function') {
147
+ // AMap.LngLat 实例
148
+ foundLng = Number(loc.getLng());
149
+ foundLat = Number(loc.getLat());
150
+ } else if (typeof loc.lng === 'number' && typeof loc.lat === 'number') {
151
+ foundLng = Number(loc.lng);
152
+ foundLat = Number(loc.lat);
153
+ } else if (Array.isArray(loc)) {
154
+ foundLng = Number(loc[0]);
155
+ foundLat = Number(loc[1]);
156
+ }
157
+ }
158
+ if (foundLng != null && foundLat != null && !Number.isNaN(foundLng) && !Number.isNaN(foundLat)) {
159
+ setLng(foundLng);
160
+ setLat(foundLat);
161
+ map.setCenter([foundLng, foundLat]);
162
+ if (markerRef.current) {
163
+ markerRef.current.setPosition([foundLng, foundLat]);
164
+ } else {
165
+ markerRef.current = new window.AMap.Marker({
166
+ position: [foundLng, foundLat],
167
+ map
168
+ });
169
+ }
170
+ } else {
171
+ // 兜底:用 placeSearch 搜索 name(如果有)
172
+ const name = poi && (poi.name || poi.address) || e && (e.info || e.name);
173
+ if (name && placeSearchRef.current && placeSearchRef.current.search) {
174
+ placeSearchRef.current.search(name, (status, result) => {
175
+ // placeSearch 回调再处理,这里保持原有逻辑或按需增强
176
+ if (status === 'complete' && result && result.poiList && result.poiList.pois && result.poiList.pois.length > 0) {
177
+ const p = result.poiList.pois[0];
178
+ const loc2 = p.location || p.lnglat;
179
+ let x = null,
180
+ y = null;
181
+ if (loc2) {
182
+ if (typeof loc2.getLng === 'function') {
183
+ x = Number(loc2.getLng());
184
+ y = Number(loc2.getLat());
185
+ } else if (typeof loc2.lng === 'number') {
186
+ x = Number(loc2.lng);
187
+ y = Number(loc2.lat);
188
+ } else if (Array.isArray(loc2)) {
189
+ x = Number(loc2[0]);
190
+ y = Number(loc2[1]);
191
+ }
192
+ }
193
+ if (!Number.isNaN(x) && !Number.isNaN(y)) {
194
+ setLng(x);
195
+ setLat(y);
196
+ map.setCenter([x, y]);
197
+ if (markerRef.current) markerRef.current.setPosition([x, y]);else markerRef.current = new window.AMap.Marker({
198
+ position: [x, y],
199
+ map
200
+ });
201
+ }
202
+ }
203
+ });
204
+ }
205
+ }
206
+ });
207
+ } catch (e) {
208
+ console.warn('AutoComplete / PlaceSearch init failed', e);
209
+ }
210
+
211
+ // 点击选点
212
+ try {
213
+ map.on && map.on('click', e => {
214
+ const lnglat = e && e.lnglat;
215
+ if (!lnglat) return;
216
+ let clickLng = null;
217
+ let clickLat = null;
218
+ if (typeof lnglat.getLng === 'function') {
219
+ clickLng = Number(lnglat.getLng());
220
+ clickLat = Number(lnglat.getLat());
221
+ } else {
222
+ clickLng = Number(lnglat.lng || Array.isArray(lnglat) && lnglat[0]);
223
+ clickLat = Number(lnglat.lat || Array.isArray(lnglat) && lnglat[1]);
224
+ }
225
+ if (Number.isNaN(clickLng) || Number.isNaN(clickLat)) return;
226
+ setLng(clickLng);
227
+ setLat(clickLat);
228
+ if (markerRef.current) {
229
+ markerRef.current.setPosition([clickLng, clickLat]);
230
+ } else {
231
+ markerRef.current = new window.AMap.Marker({
232
+ position: [clickLng, clickLat],
233
+ map
234
+ });
235
+ }
236
+ });
237
+ } catch (e) {
238
+ console.warn('map.on click failed', e);
239
+ }
240
+ });
241
+ } catch (err) {
242
+ console.error('AMap init error', err);
243
+ }
244
+ }
245
+ init();
246
+
247
+ // cleanup
248
+ return () => {
249
+ mounted = false;
250
+ try {
251
+ if (mapRef.current) {
252
+ mapRef.current.destroy();
253
+ mapRef.current = null;
254
+ }
255
+ } catch (e) {
256
+ // ignore
257
+ }
258
+ markerRef.current = null;
259
+ autoRef.current = null;
260
+ placeSearchRef.current = null;
261
+ };
262
+ }, [propLng, propLat]);
263
+
264
+ // 重置、确认等操作(同样做 number guard)
265
+ const onResetMap = useCallback(() => {
266
+ const map = mapRef.current;
267
+ if (!map) return;
268
+ const x = Number(_A_MAP_DEFAULT_POINT_.lng);
269
+ const y = Number(_A_MAP_DEFAULT_POINT_.lat);
270
+ setLng(x);
271
+ setLat(y);
272
+ try {
273
+ map.setCenter([x, y]);
274
+ if (markerRef.current) {
275
+ markerRef.current.setPosition([x, y]);
276
+ } else {
277
+ markerRef.current = new window.AMap.Marker({
278
+ position: [x, y],
279
+ map
280
+ });
281
+ }
282
+ } catch (e) {
283
+ console.warn('reset map failed', e);
284
+ }
285
+ }, []);
286
+ const handleOk = useCallback(() => {
287
+ if (!mapRef.current) return;
288
+ // 确保 lng/lat 是 number
289
+ const x = Number(lng);
290
+ const y = Number(lat);
291
+ if (Number.isNaN(x) || Number.isNaN(y)) {
292
+ onOk && onOk({
293
+ lng,
294
+ lat,
295
+ comp: undefined,
296
+ compText: undefined
297
+ });
298
+ return;
299
+ }
300
+ window.AMap.plugin('AMap.Geocoder', () => {
301
+ try {
302
+ const geocoder = new window.AMap.Geocoder();
303
+ geocoder.getAddress([x, y], (status, result) => {
304
+ let comp;
305
+ if (status === 'complete' && result && result.regeocode) {
306
+ comp = result.regeocode.addressComponent;
307
+ }
308
+ onOk && onOk({
309
+ lng: x,
310
+ lat: y,
311
+ comp,
312
+ compText: comp && [comp.province, comp.city, comp.district, comp.township, comp.street, comp.streetNumber].filter(Boolean).join('') || undefined
313
+ });
314
+ });
315
+ } catch (e) {
316
+ onOk && onOk({
317
+ lng: x,
318
+ lat: y,
319
+ comp: undefined,
320
+ compText: undefined
321
+ });
322
+ }
323
+ });
324
+ }, [lng, lat, onOk]);
325
+ const hasMapScript = typeof window.AMap !== 'undefined';
326
+ return /*#__PURE__*/React.createElement(Modal, {
327
+ open: true,
328
+ destroyOnClose: true,
329
+ title: title,
330
+ width: width,
331
+ footer: hasMapScript && /*#__PURE__*/React.createElement(Row, {
332
+ align: "middle",
333
+ justify: "space-between"
334
+ }, /*#__PURE__*/React.createElement(Col, null, "\u5750\u6807\uFF1A", [lng, lat].join('-')), /*#__PURE__*/React.createElement(Col, null, /*#__PURE__*/React.createElement(Button, {
335
+ ghost: true,
336
+ type: "primary",
337
+ style: {
338
+ marginRight: 12
339
+ },
340
+ onClick: onResetMap
341
+ }, "\u91CD\u7F6E\u5730\u56FE"), /*#__PURE__*/React.createElement(Button, {
342
+ type: "primary",
343
+ onClick: handleOk
344
+ }, "\u786E\u8BA4\u5750\u6807"))),
345
+ maskClosable: false,
346
+ onCancel: () => onCancel && onCancel()
347
+ }, hasMapScript ? /*#__PURE__*/React.createElement("div", {
348
+ style: {
349
+ position: 'relative'
350
+ }
351
+ }, /*#__PURE__*/React.createElement("div", {
352
+ ref: GL,
353
+ style: {
354
+ height: 500,
355
+ userSelect: 'none'
356
+ }
357
+ }), /*#__PURE__*/React.createElement("input", {
358
+ id: _A_MAP_SEARCH_,
359
+ type: "text",
360
+ maxLength: 30,
361
+ placeholder: "\u8BF7\u8F93\u5165\u5173\u952E\u5B57\u67E5\u8BE2",
362
+ style: {
363
+ position: 'absolute',
364
+ top: 10,
365
+ left: 10,
366
+ zIndex: 1000
367
+ }
368
+ })) : /*#__PURE__*/React.createElement(Result, {
369
+ status: "error",
370
+ title: "\u52A0\u8F7D\u5730\u56FE\u5931\u8D25\uFF0C\u7F3A\u5C11\u5FC5\u8981\u7684\u6587\u4EF6\uFF01"
371
+ }));
372
+ }
@@ -0,0 +1,199 @@
1
+ function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); }
2
+ /**
3
+ * 自适应树组件
4
+ * @param {Object} props - 组件属性
5
+ * @param {Function} [props.titleAction] - 节点操作按钮生成函数
6
+ * @param {Object} node - 当前节点数据
7
+ * @returns {Array} 操作按钮配置数组,每项包含:
8
+ * @property {ReactNode} content - 按钮内容(文本或元素)
9
+ * @property {Function} onClick - 点击事件回调
10
+ * @param {Function} [props.titleIcon] - 节点标题图标生成函数
11
+ * @param {Object} node - 当前节点数据
12
+ * @returns {ReactNode} 图标元素(如 img、Icon 组件等)
13
+ *
14
+ * @note 说明:
15
+ * 1. 自动继承全局 Ant Design 前缀配置(window.process.env.app.antd['ant-prefix'])
16
+ * 2. 节点容器默认占满父元素空间,支持纵向滚动
17
+ * 3. 节点标题超长自动换行,且不会挤压标题图标和操作图标的空间
18
+ * 4. 节点选中时,返回的节点数据为当前节点及其所有父节点id(二维数组)、选中的节点数据(一维数组)
19
+ *
20
+ * @example
21
+ * <AdaptiveTree
22
+ * treeData={[]}
23
+ * fieldNames={{
24
+ * title: 'name',
25
+ * key: 'id'
26
+ * }}
27
+ * titleAction={(node) => [
28
+ * {
29
+ * content: '添加',
30
+ * onClick: (node) => {
31
+ * console.warn("添加", node)
32
+ * }
33
+ * },
34
+ * {
35
+ * content: '编辑',
36
+ * onClick: (node) => {
37
+ * console.warn("编辑", node)
38
+ * }
39
+ * }
40
+ * ]}
41
+ * titleIcon={(node) => <FolderOpenOutlined />}
42
+ * />
43
+ */
44
+ import React, { useState } from 'react';
45
+ import { Tree, Space } from "antd";
46
+ import { styled } from 'styled-components';
47
+ export default props => {
48
+ const {
49
+ titleAction,
50
+ titleIcon,
51
+ draggable,
52
+ ...rest
53
+ } = props;
54
+ const prefixCls = window.process.env.app.antd['ant-prefix'];
55
+ const [expandedKeys, setExpandedKeys] = useState([]);
56
+ const [selectedKeys, setSelectedKeys] = useState([]);
57
+ const StyledAntdComponents = styled.div`
58
+ .${prefixCls}-tree {
59
+ width: 100%;
60
+ height: 100%;
61
+ overflow-y: auto;
62
+ }
63
+ .${prefixCls}-tree-switcher {
64
+ align-items: flex-start !important;
65
+ padding-top: 13px;
66
+ }
67
+ .${prefixCls}-tree-checkbox {
68
+ margin-top: 11px;
69
+ margin-left: 4px;
70
+ align-self: flex-start;
71
+ }
72
+ .${prefixCls}-tree-treenode {
73
+ width: 100%;
74
+ display: flex;
75
+ .${prefixCls}-tree-node-content-wrapper {
76
+ flex: 1
77
+ }
78
+ .${prefixCls}-tree-switcher {
79
+ display: flex;
80
+ align-items: center;
81
+ justify-content: flex-end;
82
+ }
83
+ .${prefixCls}-tree-switcher::before {
84
+ display: none;
85
+ }
86
+ }
87
+ .adaptive-tree-title {
88
+ width: 100%;
89
+ display: flex;
90
+ justify-content: space-between;
91
+ gap: 8px;
92
+ align-items: flex-start;
93
+ padding: 6px 0;
94
+ transition: none;
95
+ cursor: ${props?.draggable ? 'grab' : 'point'};
96
+ .title-left {
97
+ display: flex;
98
+ align-items: flex-start;
99
+ gap: 2px;
100
+ .icon {
101
+ width: 14px;
102
+ height: 14px;
103
+ }
104
+ }
105
+ .operation {
106
+ cursor: pointer;
107
+ white-space: nowrap;
108
+ }
109
+ }
110
+ `;
111
+
112
+ // 获得指定索引的节点 ID
113
+ const getLevelIndexNodeIds = (targetIndexArr, key) => {
114
+ const children = props?.fieldNames?.children || 'children';
115
+ const pathNodes = [];
116
+ const pathNodesIds = [];
117
+ let currentData = props.treeData || [];
118
+ for (let i = 0; i < targetIndexArr.length; i++) {
119
+ const index = targetIndexArr[i];
120
+ let nextNode;
121
+ if (i === 0) {
122
+ if (!Array.isArray(currentData) || index < 0 || index >= currentData.length) {
123
+ return null;
124
+ }
125
+ nextNode = currentData[index];
126
+ } else {
127
+ if (!currentData?.[children] || !Array.isArray(currentData[children]) || index < 0 || index >= currentData[children].length) {
128
+ return null;
129
+ }
130
+ nextNode = currentData?.[children][index];
131
+ }
132
+ pathNodes.push(nextNode);
133
+ pathNodesIds.push(nextNode[key]);
134
+ currentData = nextNode;
135
+ }
136
+ return [pathNodesIds, pathNodes[pathNodes.length - 1]];
137
+ };
138
+ const onCheck = (id, info) => {
139
+ const key = props?.fieldNames?.key || 'key';
140
+ const ids = [];
141
+ const nodes = [];
142
+ info.checkedNodesPositions.map(item => {
143
+ let nodeIndexArr = item.pos.split('-');
144
+ nodeIndexArr.shift();
145
+ const result = getLevelIndexNodeIds(nodeIndexArr, key);
146
+ ids.push(result[0]);
147
+ nodes.push(result[1]);
148
+ });
149
+ console.warn("onCheck", ids, nodes);
150
+ props?.onCheck && props.onCheck(ids, nodes, info);
151
+ };
152
+ const titleRender = node => {
153
+ const {
154
+ title,
155
+ key
156
+ } = props?.fieldNames || {
157
+ title: 'title',
158
+ key: 'key'
159
+ };
160
+ return /*#__PURE__*/React.createElement("div", {
161
+ className: "adaptive-tree-title",
162
+ key: node[key]
163
+ }, /*#__PURE__*/React.createElement("div", {
164
+ className: "title-left"
165
+ }, titleIcon ? /*#__PURE__*/React.createElement("div", {
166
+ className: "icon"
167
+ }, titleIcon(node)) : '', /*#__PURE__*/React.createElement("span", {
168
+ style: {
169
+ marginLeft: 6
170
+ }
171
+ }, node[title])), /*#__PURE__*/React.createElement(Space, null, titleAction ? (titleAction(node) || []).map(item => {
172
+ return /*#__PURE__*/React.createElement("div", {
173
+ onClick: () => {
174
+ item.onClick && item.onClick(node);
175
+ },
176
+ className: "operation"
177
+ }, item?.content || '');
178
+ }) : ''));
179
+ };
180
+ const onExpand = keys => {
181
+ setExpandedKeys(keys);
182
+ props.onExpand && props.onExpand(keys);
183
+ };
184
+ const onSelect = keys => {
185
+ setSelectedKeys(keys);
186
+ props.onSelect && props.onSelect(keys);
187
+ };
188
+ return /*#__PURE__*/React.createElement(StyledAntdComponents, null, /*#__PURE__*/React.createElement(Tree, _extends({
189
+ draggable: draggable ? {
190
+ icon: false
191
+ } : false,
192
+ titleRender: titleRender,
193
+ onCheck: onCheck,
194
+ expandedKeys: props?.expandedKeys || expandedKeys,
195
+ onExpand: onExpand,
196
+ selectedKeys: props?.selectedKeys || selectedKeys,
197
+ onSelect: onSelect
198
+ }, rest)));
199
+ };