Feedback.js
7.54 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* eslint-disable */
import React, { useEffect, useRef, useState } from "react";
import { Button, Badge } from "antd-v4";
import { MessageOutlined } from "@ant-design/icons";
import Draggable from "react-draggable";
import styles from "./index.less";
const _this = {};
const Feedback = props => {
const { app } = props;
const { userinfo } = app;
const [showState, setShowState] = useState(false);
const iframeRef = useRef(null);
const [unReadCount, setUnReadCount] = useState(0);
const [unReadCountColor, setUnReadCountColor] = useState(0);
const iframeId = useRef(
Math.random()
.toString(36)
.substring(2)
);
useEffect(() => {
const recMessage = e => {
if (e && e.data) {
const { payload, action, iCount, color } = e.data;
if (payload === "commonFeedback") {
switch (action) {
case "closeModal":
setTimeout(() => {
setShowState(false);
}, 500);
break;
case "unReadCountChange":
setUnReadCount(iCount);
setUnReadCountColor(color);
break;
default:
break;
}
}
}
};
window.addEventListener("message", recMessage);
return () => {
window.removeEventListener("message", recMessage);
};
}, []);
const sendMessage = () => {
setShowState(true);
iframeRef.current.contentWindow.postMessage(
{
payload: "commonFeedback",
action: "openModal",
currentPane: JSON.parse(localStorage.getItem("xlybusinesscurrentPane")),
app: { userinfo }
},
"*"
);
};
// 设置按钮活动范围
const [positionOffset, setPositionOffset] = useState({ x: 0, y: -200 });
const [tempClient, setTempClient] = useState([0, 0]);
const [bounds, setBounds] = useState(null);
const btnRef = useRef(null);
const [showBtn, setShowBtn] = useState(true); // 按钮是否显示
useEffect(() => {
const handleResize = () => {
setShowBtn(false);
const btnWith = 32;
const btnHeight = 32;
const feedbackBtnRecord =
sessionStorage.getItem("feedbackBtnRecord") || "0,0";
const diffX = Number(feedbackBtnRecord.split(",")[0]);
const diffY = Number(feedbackBtnRecord.split(",")[1]);
let newX = positionOffset.x + diffX;
let newY = positionOffset.y + diffY;
newX = Math.max(-window.innerWidth + btnWith, newX);
newX = Math.min(newX, 0);
newY = Math.max(-window.innerHeight + btnHeight + 53, newY);
newY = Math.min(newY, 0);
sessionStorage.setItem(
"feedbackBtnRecord",
`${newX - positionOffset.x},${newY - positionOffset.y}`
);
const positionOffsetNew = {
x: newX,
y: newY
};
setPositionOffset(positionOffsetNew);
const { x: offsetX, y: offsetY } = positionOffsetNew;
setBounds({
top: -(window.innerHeight + offsetY - 53 - btnHeight), // 按钮上方可移动范围
left: -(window.innerWidth + offsetX - btnWith), // 按钮左边可移动用范围
right: -offsetX, // 按钮右边可移动用范围
bottom: -offsetY // 按钮下边可移动范围
});
setTimeout(() => {
setShowBtn(true);
hideEdgeBtn();
}, 100);
};
handleResize();
window.addEventListener("resize", handleResize);
return () => {
window.removeEventListener("resize", handleResize);
};
}, []);
// 记录按钮位置
const recordBtnPostion = (diffX, diffY) => {
const feedbackBtnRecord = sessionStorage.getItem("feedbackBtnRecord");
let feedbackBtnRecordNew = `${diffX},${diffY}`;
if (feedbackBtnRecord) {
feedbackBtnRecordNew = `${Number(feedbackBtnRecord.split(",")[0]) +
diffX},${Number(feedbackBtnRecord.split(",")[1]) + diffY}`;
}
sessionStorage.setItem("feedbackBtnRecord", feedbackBtnRecordNew);
};
// 隐藏边缘按钮
const timer = useRef(null);
const [badgeClass, setBadgeClass] = useState(null);
const hideEdgeBtn = () => {
if (_this.draging) {
return;
}
const feedbackBtnRecord =
sessionStorage.getItem("feedbackBtnRecord") || "0,0";
const diffX = Number(feedbackBtnRecord.split(",")[0]);
const diffY = Number(feedbackBtnRecord.split(",")[1]);
clearTimeout(timer.current);
timer.current = null;
if (Math.abs(diffX) < 1) {
setTimeout(() => {
// document.querySelector("html").style.overflowX = "hidden";
setBadgeClass(styles.hideBtnToRight);
}, 1500);
} else if (window.innerWidth - Math.abs(diffX) - 32 < 1) {
setTimeout(() => {
// document.querySelector("html").style.overflowX = "hidden";
setBadgeClass(styles.hideBtnToLeft);
}, 1500);
} else if (Math.abs(diffY - 200) < 1) {
setTimeout(() => {
// document.querySelector("html").style.overflowY = "hidden";
setBadgeClass(styles.hideBtnToBottom);
}, 1500);
}
};
return (
<>
<div
style={{
position: "absolute",
bottom: 0,
right: 0,
zIndex: 998
}}
>
{showBtn && (
<Draggable
bounds={bounds}
positionOffset={positionOffset}
onStart={() => {
const { x, y } = btnRef.current.getBoundingClientRect();
setTempClient([x, y]);
}}
onDrag={() => {
_this.draging = true;
clearTimeout(timer.current);
timer.current = null;
setBadgeClass("");
}}
onStop={() => {
_this.draging = false;
const { x, y } = btnRef.current.getBoundingClientRect();
const diffX = x - tempClient[0];
const diffY = y - tempClient[1];
recordBtnPostion(diffX, diffY);
hideEdgeBtn();
if (Math.abs(diffX) <= 2 && Math.abs(diffY) <= 2) {
sendMessage();
}
}}
>
<div>
<Badge
className={badgeClass}
count={unReadCount}
offset={badgeClass === styles.hideBtnToLeft ? [0, 5] : [-30, 5]}
color={unReadCountColor}
onMouseOver={() => {
clearTimeout(timer.current);
timer.current = null;
setBadgeClass("");
// document.querySelector("html").style.overflowX = "auto";
// document.querySelector("html").style.overflowY = "auto";
}}
onMouseLeave={() => {
hideEdgeBtn();
}}
>
<Button ref={btnRef} type="primary" shape="circle" size="large">
<MessageOutlined />
</Button>
</Badge>
</div>
</Draggable>
)}
</div>
<div
style={{
position: "absolute",
top: 0,
left: 0,
width: "100vw",
height: "100vh",
zIndex: 1001,
overflow: "hidden",
display: showState ? "" : "none"
}}
>
<iframe
ref={iframeRef}
// src={`http://localhost:8000/commonFeedback?iframeId=${
// iframeId.current
// }`}
src={`http://project.xlyprint.cn/commonFeedback?iframeId=${
iframeId.current
}`}
title="问题反馈"
width="100%"
height="100%"
frameborder="0"
/>
</div>
</>
);
};
export default Feedback;