wujingjing
2025-04-08 c320220c0f882d36d4b5c571b35deb201f7807ac
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import axios from 'axios';
import { ElMessage } from 'element-plus';
import EZUIKit from 'ezuikit-js';
import { YingShiControl } from './types';
 
export type YingShiCameraOptions = {
    /**
     * @description 播放器容器 DOM 的 id
     */
    domId: string;
    /** @description 设备序列号 */
    deviceSerial: string;
    /**
     * @description 视频宽度,默认值为容器容器 DOM 宽度
     */
    width?: number;
    /**
     * @description 视频高度,默认值为容器容器 DOM 高度
     */
    height?: number;
};
export class YingShiCamera {
    domId: string;
    deviceSerial: string;
    width?: number;
    height?: number;
 
    accessToken: string;
    liveUrl: string;
 
    private static readonly APP_KEY = '4dd1ebea34544429b55d0e12fef86156';
 
    private static readonly SECRET = '151b699b9150fe1939af7b2fca29503c';
 
    private static readonly BASE_URL = 'https://open.ys7.com/api';
 
    constructor(options: YingShiCameraOptions) {
        this.domId = options.domId;
        this.deviceSerial = options.deviceSerial;
        this.width = options.width;
        this.height = options.height;
    }
 
    private static async executeCommand(deviceSerial: string, accessToken: string, command: YingShiControl) {
        const res = await axios({
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            url: 'https://open.ys7.com/api/lapp/device/ptz/start',
            method: 'post',
            data: {
                accessToken: accessToken,
                deviceSerial: deviceSerial,
                channelNo: 1,
                ...command,
            },
        });
        const data = (res as any).data.data;
        return data;
    }
 
    private static async stopCommand(deviceSerial: string, accessToken: string, command: Omit<YingShiControl, 'speed'>) {
        const res = await axios({
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            url: 'https://open.ys7.com/api/lapp/device/ptz/start',
            method: 'post',
            data: {
                accessToken: accessToken,
                deviceSerial: deviceSerial,
                channelNo: 1,
                ...command,
            },
        });
        const data = (res as any).data.data;
        return data;
    }
 
    /**
     * 获取 accessToken
     * @returns Promise<string>
     */
    private static async getAccessToken() {
        const res = await axios({
            method: 'post',
            url: `${YingShiCamera.BASE_URL}/lapp/token/get`,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            data: {
                appKey: YingShiCamera.APP_KEY,
                appSecret: YingShiCamera.SECRET,
            },
        }).catch((err) => {
            ElMessage.error(err);
        });
        const token = (res as any).data.data.accessToken;
        return token;
    }
 
    /**
     * 获取直播地址
     * @param accessToken
     * @param deviceSerial
     * @returns
     */
    private static async getLiveUrl(accessToken: string, deviceSerial: string) {
        const res = await axios({
            method: 'post',
            url: `${YingShiCamera.BASE_URL}/lapp/v2/live/address/get`,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            data: {
                accessToken: accessToken,
                deviceSerial: deviceSerial, //设备序列号,一个序列号对应一个设备
            },
        });
        const url = (res as any).data.data.url;
        return url;
    }
 
    private async initEZUIKitPlayer() {
        const UIKitDEMO = new EZUIKit.EZUIKitPlayer({
            id: this.domId,
            url: this.liveUrl,
            accessToken: this.accessToken,
            width: this.width,
            height: this.height,
        });
 
        await UIKitDEMO.play().catch((err) => {
            ElMessage.error(err);
        });
        UIKitDEMO.openPtz();
    }
 
    async init() {
        this.accessToken = await YingShiCamera.getAccessToken();
        this.liveUrl = await YingShiCamera.getLiveUrl(this.accessToken, this.deviceSerial);
        this.initEZUIKitPlayer();
    }
    async control(command: YingShiControl) {
        const data = await YingShiCamera.executeCommand(this.deviceSerial, this.accessToken, command);
    }
 
    async stopControl(command: Omit<YingShiControl, 'speed'>) {
        const data = await YingShiCamera.stopCommand(this.deviceSerial, this.accessToken, command);
    }
}