feat: 接口错误处理

This commit is contained in:
“张嘉宁” 2023-04-03 10:44:47 +08:00
parent 0e58843896
commit f6e393b3d2
1 changed files with 124 additions and 112 deletions

View File

@ -608,7 +608,8 @@ export function Home() {
// setting // setting
const [openSettings, setOpenSettings] = useState(false); const [openSettings, setOpenSettings] = useState(false);
const config = useChatStore((state) => state.config); const config = useChatStore((state) => state.config);
const [isAllow, setIsAllow] = useState(true); const [isAllow, setIsAllow] = useState(true); // 白名单状态
const [isRequestErr, setIsRequestErr] = useState(false); // 接口报错状态
useEffect(() => { useEffect(() => {
const urlParams = new URLSearchParams(window.location.search); const urlParams = new URLSearchParams(window.location.search);
@ -620,7 +621,12 @@ export function Home() {
const { code, msg } = data; const { code, msg } = data;
setIsAllow(code === 0); setIsAllow(code === 0);
}) })
.catch((error) => console.error(error)); // 接口错误处理
.catch((error) => {
setIsAllow(false);
setIsRequestErr(true);
console.error(error);
});
}, []); }, []);
//const isWorkWechat = () => { //const isWorkWechat = () => {
@ -636,7 +642,7 @@ export function Home() {
// return false; // return false;
//} //}
//}; //};
const isWorkWechat = () => { const isWorkWechat = () => {
// 获取user-agent标识头 // 获取user-agent标识头
const userAgent = window.navigator.userAgent; const userAgent = window.navigator.userAgent;
@ -649,15 +655,12 @@ const isWorkWechat = () => {
const ua = userAgent.toLowerCase(); const ua = userAgent.toLowerCase();
// 判断ua和微信浏览器的标识头是否匹配 // 判断ua和微信浏览器的标识头是否匹配
if ( if (/micromessenger/i.test(ua) && /wxwork/i.test(ua)) {
/micromessenger/i.test(ua) &&
/wxwork/i.test(ua)
) {
return true; return true;
} else { } else {
return false; return false;
} }
}; };
useSwitchTheme(); useSwitchTheme();
@ -677,9 +680,15 @@ const isWorkWechat = () => {
: styles.container : styles.container
}`} }`}
> >
{!isWorkWechat() ? ( {
// 接口报错
isRequestErr ? (
<h2>~</h2>
) : // 不在企业微信
!isWorkWechat() ? (
<h2 className="not-wx-work">使ChatGPT</h2> <h2 className="not-wx-work">使ChatGPT</h2>
) : typeof isAllow === "boolean" && !isAllow ? ( ) : // 不在白名单
!isAllow ? (
<h2 className="not-allow"> <h2 className="not-allow">
<span className="text">使Gpt~</span> <span className="text">使Gpt~</span>
<span onClick={goApply} className={styles["button"]}> <span onClick={goApply} className={styles["button"]}>
@ -715,7 +724,9 @@ const isWorkWechat = () => {
<div className={styles["sidebar-tail"]}> <div className={styles["sidebar-tail"]}>
<div className={styles["sidebar-actions"]}> <div className={styles["sidebar-actions"]}>
<div className={styles["sidebar-action"] + " " + styles.mobile}> <div
className={styles["sidebar-action"] + " " + styles.mobile}
>
<IconButton <IconButton
icon={<CloseIcon />} icon={<CloseIcon />}
onClick={() => { onClick={() => {
@ -770,7 +781,8 @@ const isWorkWechat = () => {
)} )}
</div> </div>
</> </>
)} )
}
</div> </div>
); );
} }