广州市综治平台前端
liuwh
6 days ago bd4e6eb3d29de84c00f5e448f5839300873a6abe
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
 * @Company: hugeInfo
 * @Author: ldh
 * @Date: 2022-03-24 09:22:19
 * @LastEditTime: 2022-11-29 18:09:21
 * @LastEditors: ldh
 * @Version: 1.0.0
 * @Description: 司法确认动态
 */
import React, { useState, useRef, useEffect } from 'react';
import { useVirtual } from 'react-virtual';
import { Space } from 'antd';
import { mediationRoom } from '../../../assets/images/icon';
import mediateDynamicStatus from '../../../status/mediateDynamicStatus';
import * as $$ from '../../../utils/utility';
import timer from '../../../utils/timer';
 
// 语音转写文字
function getContentApi(submitData) {
    $$.ax.ax.defaults.headers.common['Authorization'] =
        'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIxMDAwMDAwMDAxIiwidGltZSI6Ik1vbiBNYXkgMjMgMDk6NDY6MjggQ1NUIDIwMjIifQ.W0dCrVnsNEV0Yws9ccvUIG_lAeOxqG6BF52qBTD_fn8';
    return $$.ax.ax.post('https://voipconference.xfyun.cn/api/v1/asr/getContent?appKey=1000000001&userId=tjy', submitData).then((response) => {
        return response.data;
    });
}
 
// 虚拟滚动组件
function RowVirtualizerDynamic({ rows, recordRef }) {
    const rowVirtualizer = useVirtual({
        size: rows.length,
        parentRef: recordRef,
    });
 
    return (
        <div className="mediationWindow-dynamic-record" ref={recordRef}>
            {rows.length === 0 && $$.MyEmpty()}
            <div style={{ height: rowVirtualizer.totalSize, width: '100%', position: 'relative' }}>
                {rowVirtualizer.virtualItems.map((virtualRow) => {
                    let type = rows[virtualRow.index].userId;
                    return (
                        <div
                            key={virtualRow.index}
                            ref={virtualRow.measureRef}
                            style={{
                                position: 'absolute',
                                top: 0,
                                left: 0,
                                width: '100%',
                                transform: `translateY(${virtualRow.start}px)`,
                            }}
                        >
                            <div style={{ minHeight: 60, paddingBottom: 8 }}>
                                <div
                                    className={`mediationWindow-dynamic-record-line animated faster ${type === 'tjy' ? 'fadeInRight' : 'fadeInLeft'}`}
                                    style={{ flexDirection: type === 'tjy' && 'row-reverse' }}
                                >
                                    <div
                                        className={`mediationWindow-dynamic-record-name mediationWindow-dynamic-record-${
                                            type === 'bgdsr' ? 'bei' : type === 'ygdsr' ? 'shen' : 'tiao'
                                        }`}
                                    >
                                        {type === 'bgdsr' ? '被' : type === 'ygdsr' ? '申' : '调'}
                                    </div>
                                    <div className={`mediationWindow-dynamic-record-bubble mediationWindow-dynamic-record-bubble-${type === 'tjy' ? 'right' : 'left'}`}>
                                        {rows[virtualRow.index].txt}
                                    </div>
                                </div>
                            </div>
                        </div>
                    );
                })}
            </div>
        </div>
    );
}
 
const MediateDynamic = () => {
    const recordRef = useRef();
 
    // 气泡弹出
    const [voiceBubble, setVoiceBubble] = useState(false);
 
    // 语音记录
    const [callRecord, setCallRecord] = useState([]);
 
    // tab当前选择
    const [tabActive, setTabActive] = useState(1);
 
    // 语音记录实时添加
    function addCallRecord(speakData) {
        let dom = recordRef?.current;
        if (dom) {
            let scrollClientHeight = dom.scrollHeight - dom.clientHeight;
            callRecord.push(speakData);
            setCallRecord([...callRecord]);
            // 判断滚动条位置从下往上处于三分之一的区域时添加新的记录置底
            if (dom.scrollTop > scrollClientHeight - scrollClientHeight / 3) {
                recordRef.current.scrollTop = recordRef.current.scrollHeight + 100;
                return;
            }
            if (callRecord.length < 30 && dom.scrollTop < 30) {
                recordRef.current.scrollTop = recordRef.current.scrollHeight;
            }
        }
    }
 
    // 获取语音历史记录
    async function getContent() {
        let roomNum = $$.myTimeFormat(new Date(), 'YYYYMMDD');
        const { code, data } = await getContentApi({ roomNum, appid: '1000000001' });
        if (code === 200) {
            let speakArr = [],
                speakObj = {};
            // 排序获取最新的说话的人是谁
            data.asrList.forEach((x) => {
                speakArr.push({
                    userId: x.userId,
                    createTime: x.historyTxts[x.historyTxts.length - 1].createTime,
                    txt: x.historyTxts[x.historyTxts.length - 1].txt,
                });
                speakObj[x.userId] = x.historyTxts;
            });
            speakArr.sort((a, b) => a.createTime - b.createTime);
            let positionItem = speakArr[speakArr.length - 1];
            // 通过保存的时间戳获取下标
            let index = 0;
            if (mediateDynamicStatus.legObj[positionItem.userId]) {
                loop: for (let i = 0; i < speakObj[positionItem.userId].length; i++) {
                    if (speakObj[positionItem.userId][i].txt.indexOf(mediateDynamicStatus.legObj[positionItem.userId]) !== -1) {
                        index = i;
                        break loop;
                    }
                }
            }
            // 将最新的说话的人的过往语句拼接起来
            let speakArrOne = [];
            speakObj[positionItem.userId].splice(index).forEach((x) => speakArrOne.push(x.txt));
            if (speakArrOne.length !== 0) {
                // 保存当前截取的时间戳
                if (positionItem.txt !== mediateDynamicStatus.legObj[positionItem.userId]) {
                    mediateDynamicStatus.setTime(positionItem.userId, positionItem.txt);
                    addCallRecord({ userId: positionItem.userId, txt: speakArrOne });
                    if (voiceBubble) {
                        setVoiceBubble(false);
                    }
                    setTimeout(() => {
                        setVoiceBubble(true);
                    }, 1000);
                }
            }
        }
    }
 
    useEffect(() => {
        setTabActive(1);
        setVoiceBubble(false);
        timer.setInterval('loop', () => getContent(), 5000);
        return () => {
            mediateDynamicStatus.clear();
            timer.clearInterval('loop');
        };
    }, []);
 
    const lastSpeak = callRecord.length !== 0 ? callRecord[callRecord.length - 1] : {};
 
    return (
        <>
            <div className="mediationWindow-dynamic animated faster flipInY">
                {tabActive === 1 ? (
                    <div className="mediationWindow-dynamic-realTime">
                        {voiceBubble ? (
                            <div className="mediationWindow-dynamic-realTime-bubble animated faster fadeInUp">
                                <div
                                    className={`mediationWindow-dynamic-realTime-box mediationWindow-dynamic-realTime-${
                                        lastSpeak.userId === 'ygdsr' ? 'boxLeft' : lastSpeak.userId === 'bgdsr' ? 'boxRight' : 'boxCenter'
                                    }`}
                                >
                                    <div className="mediationWindow-dynamic-realTime-content">{lastSpeak.txt}</div>
                                </div>
                            </div>
                        ) : (
                            <div className="mediationWindow-dynamic-realTime-bubble" />
                        )}
                        <img src={mediationRoom} className="mediationWindow-dynamic-realTime-img" alt="" />
                    </div>
                ) : (
                    <RowVirtualizerDynamic rows={callRecord} recordRef={recordRef} />
                )}
            </div>
            <Space className="mediationWindow-dynamic-tabs" direction="vertical">
                {[
                    ['实', '时'],
                    ['历', '史'],
                ].map((x, t) => (
                    <div
                        onClick={() => setTabActive(t + 1)}
                        className={`mediationWindow-dynamic-tabs-item ${t + 1 === tabActive ? 'mediationWindow-dynamic-tabs-itemActive' : ''}`}
                        key={t}
                    >
                        <div>{x[0]}</div>
                        <div>{x[1]}</div>
                    </div>
                ))}
            </Space>
        </>
    );
};
 
export default MediateDynamic;