zy-react-library 1.0.130 → 1.0.132

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.
@@ -1,5 +1,6 @@
1
1
  import { Button, Col, Form, Input, Modal, Row, Select, Spin } from "antd";
2
2
  import { useEffect, useRef, useState } from "react";
3
+ import { dynamicLoadJs } from "../../utils";
3
4
 
4
5
  /**
5
6
  * 定位组件弹窗
@@ -34,8 +35,7 @@ const MapSelector = (props) => {
34
35
  // 初始化地图
35
36
  const initMap = async () => {
36
37
  if (!window.BMapGL) {
37
- console.error("BMapGL is not loaded");
38
- return;
38
+ await dynamicLoadJs("https://api.map.baidu.com/api?v=1.0&type=webgl&ak=OElqFYoKiAH8KFtph8ftLKF5NlNrbCUr");
39
39
  }
40
40
 
41
41
  setLoading(true);
@@ -45,11 +45,14 @@ const Map = (props) => {
45
45
  <Form.Item name={latitudeProps} noStyle rules={[{ required, message: "请选择纬度" }]}>
46
46
  <Input disabled />
47
47
  </Form.Item>
48
- <Button type="primary" onClick={() => {
49
- setMapVisible(true);
50
- setCurrentLongitude(form.getFieldValue(longitudeProps));
51
- setCurrentLatitude(form.getFieldValue(latitudeProps));
52
- }}>
48
+ <Button
49
+ type="primary"
50
+ onClick={() => {
51
+ setMapVisible(true);
52
+ setCurrentLongitude(form.getFieldValue(longitudeProps));
53
+ setCurrentLatitude(form.getFieldValue(latitudeProps));
54
+ }}
55
+ >
53
56
  地图定位
54
57
  </Button>
55
58
  </div>
@@ -0,0 +1,42 @@
1
+ import type { ForwardRefExoticComponent, RefAttributes } from "react";
2
+
3
+ export interface AliPlayerProps {
4
+ /** 播放地址 */
5
+ source?: string | string[];
6
+ /** 阿里的 vid */
7
+ vid?: string;
8
+ /** 阿里的 playAuth */
9
+ playAuth?: string;
10
+ /** 封面地址 */
11
+ cover?: string;
12
+ /** 播放器宽度,默认 100% */
13
+ width?: string;
14
+ /** 播放器高度,默认 600px */
15
+ height?: string;
16
+ /** 是否自动播放 */
17
+ autoplay?: boolean;
18
+ /** 是否显示进度条 */
19
+ showProgress?: boolean;
20
+ /** 是否直播模式 */
21
+ isLive?: boolean;
22
+ /** 播放开始时间 */
23
+ playTime?: number;
24
+ /** 播放结束事件 */
25
+ onEnded?: () => void;
26
+ /** 播放进度更新事件 */
27
+ onTimeupdate?: (currentTime: number) => void;
28
+ }
29
+
30
+ export interface AliPlayerRef {
31
+ play: () => void;
32
+ pause: () => void;
33
+ }
34
+
35
+ /**
36
+ * 视频播放组件
37
+ */
38
+ declare const AliPlayer: ForwardRefExoticComponent<
39
+ AliPlayerProps & RefAttributes<AliPlayerRef>
40
+ >;
41
+
42
+ export default AliPlayer;
@@ -0,0 +1,160 @@
1
+ import { useDocumentVisibility } from "ahooks";
2
+ import { uniqueId } from "lodash-es";
3
+ import { forwardRef, useEffect, useImperativeHandle, useRef } from "react";
4
+ import { dynamicLoadCss, dynamicLoadJs } from "../../utils";
5
+
6
+ /**
7
+ * 视频播放组件
8
+ */
9
+ const AliPlayer = forwardRef(({
10
+ source = "",
11
+ vid = "",
12
+ playAuth = "",
13
+ cover = "",
14
+ width = "100%",
15
+ height = "600px",
16
+ autoplay = true,
17
+ showProgress = true,
18
+ isLive = false,
19
+ playTime = 0,
20
+ onEnded,
21
+ onTimeupdate,
22
+ }, ref) => {
23
+ const playerRef = useRef(null);
24
+ const containerRef = useRef(null);
25
+ const visibility = useDocumentVisibility();
26
+ const className = useRef(uniqueId("_")).current;
27
+
28
+ useImperativeHandle(ref, () => ({
29
+ play: () => {
30
+ playerRef.current && playerRef.current.play();
31
+ },
32
+ pause: () => {
33
+ playerRef.current && playerRef.current.pause();
34
+ },
35
+ }));
36
+
37
+ const onDisposeAliPlayer = () => {
38
+ if (!playerRef.current)
39
+ return;
40
+ playerRef.current.dispose();
41
+ playerRef.current = null;
42
+ };
43
+
44
+ const onCreateAliPlayer = async () => {
45
+ if (!containerRef.current)
46
+ return;
47
+ if (!window.Aliplayer) {
48
+ await dynamicLoadJs("https://g.alicdn.com/apsara-media-box/imp-web-player/2.16.3/aliplayer-min.js");
49
+ await dynamicLoadCss("https://g.alicdn.com/apsara-media-box/imp-web-player/2.16.3/skins/default/aliplayer-min.css");
50
+ }
51
+
52
+ onDisposeAliPlayer();
53
+
54
+ const skinLayout = [
55
+ { name: "bigPlayButton", align: "blabs", x: 30, y: 80 },
56
+ { name: "H5Loading", align: "cc" },
57
+ { name: "errorDisplay", align: "tlabs", x: 0, y: 0 },
58
+ { name: "infoDisplay" },
59
+ { name: "tooltip", align: "blabs", x: 0, y: 56 },
60
+ { name: "thumbnail" },
61
+ {
62
+ name: "controlBar",
63
+ align: "blabs",
64
+ x: 0,
65
+ y: 0,
66
+ children: [
67
+ { name: "playButton", align: "tl", x: 15, y: 12 },
68
+ { name: "timeDisplay", align: "tl", x: 10, y: 7 },
69
+ { name: "fullScreenButton", align: "tr", x: 10, y: 12 },
70
+ { name: "setting", align: "tr", x: 15, y: 12 },
71
+ { name: "volume", align: "tr", x: 5, y: 10 },
72
+ ],
73
+ },
74
+ ];
75
+
76
+ if (showProgress) {
77
+ skinLayout[skinLayout.length - 1].children.unshift({
78
+ name: "progress",
79
+ align: "blabs",
80
+ x: 0,
81
+ y: 44,
82
+ });
83
+ }
84
+
85
+ playerRef.current = new window.Aliplayer(
86
+ {
87
+ id: className,
88
+ ...(source
89
+ ? { source }
90
+ : {
91
+ vid,
92
+ playauth: playAuth,
93
+ qualitySort: "asc",
94
+ format: "m3u8",
95
+ encryptType: 1,
96
+ mediaType: "video",
97
+ isLive: true,
98
+ rePlay: false,
99
+ playsinline: true,
100
+ controlBarVisibility: "hover",
101
+ }),
102
+ cover,
103
+ width,
104
+ height,
105
+ autoplay,
106
+ isLive,
107
+ useH5Prism: true,
108
+ skinLayout,
109
+ },
110
+ (player) => {
111
+ if (autoplay) {
112
+ player.play();
113
+ }
114
+
115
+ player.on("ended", () => {
116
+ onEnded && onEnded();
117
+ });
118
+
119
+ player.on("timeupdate", () => {
120
+ onTimeupdate && onTimeupdate(player.getCurrentTime());
121
+ });
122
+
123
+ if (playTime > 0) {
124
+ player.seek(playTime);
125
+ }
126
+ },
127
+ );
128
+ };
129
+
130
+ useEffect(() => {
131
+ if (source || (vid && playAuth)) {
132
+ onCreateAliPlayer();
133
+ }
134
+
135
+ return () => {
136
+ onDisposeAliPlayer();
137
+ };
138
+ }, [source, vid, playAuth]);
139
+
140
+ useEffect(() => {
141
+ if (visibility === "hidden") {
142
+ playerRef.current && playerRef.current.pause();
143
+ }
144
+ else {
145
+ playerRef.current && playerRef.current.play();
146
+ }
147
+ }, [visibility]);
148
+
149
+ return (
150
+ <div
151
+ ref={containerRef}
152
+ id={className}
153
+ style={{ width, height }}
154
+ />
155
+ );
156
+ });
157
+
158
+ AliPlayer.displayName = "AliPlayer";
159
+
160
+ export default AliPlayer;
@@ -0,0 +1,20 @@
1
+ import type { ForwardRefExoticComponent, RefAttributes } from "react";
2
+ import type { AliPlayerProps, AliPlayerRef } from "./AliPlayer";
3
+
4
+ export interface VideoProps extends Omit<AliPlayerProps, "onEnded" | "onTimeupdate"> {
5
+ /** 是否显示弹窗 */
6
+ visible?: boolean;
7
+ /** 是否内联模式 */
8
+ inline?: boolean;
9
+ /** 弹窗关闭事件 */
10
+ onCancel?: () => void;
11
+ }
12
+
13
+ /**
14
+ * 视频播放组件
15
+ */
16
+ declare const Video: ForwardRefExoticComponent<
17
+ VideoProps & RefAttributes<AliPlayerRef>
18
+ >;
19
+
20
+ export default Video;
@@ -0,0 +1,89 @@
1
+ import { Modal } from "antd";
2
+ import { useEffect, useRef, useState } from "react";
3
+ import { getFileUrl } from "../../utils";
4
+ import AliPlayer from "./AliPlayer";
5
+
6
+ /**
7
+ * 视频播放组件
8
+ */
9
+ const Video = ({
10
+ source = "",
11
+ vid = "",
12
+ playAuth = "",
13
+ cover = "",
14
+ autoplay = true,
15
+ showProgress = true,
16
+ playTime = 0,
17
+ inline = false,
18
+ isLive = false,
19
+ width = "100%",
20
+ height = "600px",
21
+ title = "视频",
22
+ visible: externalVisible = false,
23
+ onCancel,
24
+ ...restProps
25
+ }) => {
26
+ const [internalVisible, setInternalVisible] = useState(false);
27
+ const playerRef = useRef(null);
28
+
29
+ const visible = onCancel ? externalVisible : internalVisible;
30
+ const setVisible = onCancel || setInternalVisible;
31
+
32
+ const getSource = () => {
33
+ if (!source)
34
+ return;
35
+ if (source.includes("http") || source.includes("https"))
36
+ return source;
37
+ else return getFileUrl() + source;
38
+ };
39
+
40
+ useEffect(() => {
41
+ if (!inline) {
42
+ if (visible) {
43
+ playerRef.current && playerRef.current.play();
44
+ }
45
+ else {
46
+ playerRef.current && playerRef.current.pause();
47
+ }
48
+ }
49
+ }, [visible, inline]);
50
+
51
+ const playerElement = (
52
+ <AliPlayer
53
+ ref={playerRef}
54
+ source={getSource()}
55
+ vid={vid}
56
+ playAuth={playAuth}
57
+ cover={cover}
58
+ autoplay={autoplay}
59
+ showProgress={showProgress}
60
+ playTime={playTime}
61
+ isLive={isLive}
62
+ width={width}
63
+ height={height}
64
+ />
65
+ );
66
+
67
+ if (!inline) {
68
+ return (
69
+ <Modal
70
+ open={visible}
71
+ title={title}
72
+ footer={[
73
+ <button key="cancel" onClick={() => setVisible(false)}>
74
+ 取消
75
+ </button>,
76
+ ]}
77
+ maskClosable={false}
78
+ onCancel={() => setVisible(false)}
79
+ {...restProps}
80
+ >
81
+ {playerElement}
82
+ </Modal>
83
+ );
84
+ }
85
+
86
+ return playerElement;
87
+ };
88
+
89
+ export default Video;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "zy-react-library",
3
3
  "private": false,
4
- "version": "1.0.130",
4
+ "version": "1.0.132",
5
5
  "type": "module",
6
6
  "description": "",
7
7
  "author": "LiuJiaNan",
package/utils/index.d.ts CHANGED
@@ -332,3 +332,13 @@ export function processTreeDataForOnlyLastLevel(
332
332
  export function validatorEndTime(timeStart: string): {
333
333
  validator: (_: any, value: any) => Promise<void | string>;
334
334
  };
335
+
336
+ /**
337
+ * 动态加载js资源
338
+ */
339
+ export function dynamicLoadJs(url: string): Promise<void>;
340
+
341
+ /**
342
+ * 动态加载css资源
343
+ */
344
+ export function dynamicLoadCss(url: string): Promise<void>;
package/utils/index.js CHANGED
@@ -523,6 +523,35 @@ export const validatorEndTime = (timeStart) => {
523
523
  }
524
524
  }
525
525
 
526
+ /**
527
+ * 动态加载js资源
528
+ */
529
+ export function dynamicLoadJs(url) {
530
+ return new Promise((resolve, reject) => {
531
+ const script = document.createElement("script");
532
+ script.type = "text/javascript";
533
+ script.src = url;
534
+ script.onload = resolve;
535
+ script.onerror = reject;
536
+ document.body.appendChild(script);
537
+ });
538
+ }
539
+
540
+ /**
541
+ * 动态加载css资源
542
+ */
543
+ export function dynamicLoadCss(url) {
544
+ return new Promise((resolve, reject) => {
545
+ const link = document.createElement("link");
546
+ link.rel = "stylesheet";
547
+ link.type = "text/css";
548
+ link.href = url;
549
+ link.onload = resolve;
550
+ link.onerror = reject;
551
+ document.head.appendChild(link);
552
+ });
553
+ }
554
+
526
555
  /**
527
556
  * 获取文件url
528
557
  */