Skip to content

Commit cd30465

Browse files
authored
feat(project): support unbind wechat (#1573)
1 parent 8439088 commit cd30465

File tree

10 files changed

+130
-16
lines changed

10 files changed

+130
-16
lines changed

desktop/renderer-app/src/api-middleware/flatServer/index.ts

+23
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,26 @@ export async function bindingProcess(authUUID: string): Promise<BindingProcessRe
658658
};
659659
}
660660
}
661+
662+
export enum LoginPlatform {
663+
WeChat = "WeChat",
664+
Github = "Github",
665+
Apple = "Apple",
666+
Agora = "Agora",
667+
Google = "Google",
668+
Phone = "Phone",
669+
}
670+
671+
export interface RemoveBindingPayload {
672+
target: LoginPlatform;
673+
}
674+
675+
export interface RemoveBindingResult {
676+
token: string;
677+
}
678+
679+
export async function removeBinding(target: LoginPlatform): Promise<RemoveBindingResult> {
680+
return await post<RemoveBindingPayload, RemoveBindingResult>("user/binding/remove", {
681+
target,
682+
});
683+
}

desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/binding/WeChat.tsx

+23-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ import { message, Modal } from "antd";
66
import { WechatFilled } from "@ant-design/icons";
77

88
import { getQRCodeURL } from "../../../LoginPage/WeChatLogin";
9-
import { setBindingAuthUUID, bindingProcess } from "../../../../api-middleware/flatServer";
9+
import {
10+
setBindingAuthUUID,
11+
bindingProcess,
12+
removeBinding,
13+
LoginPlatform,
14+
} from "../../../../api-middleware/flatServer";
1015
import { FLAT_SERVER_USER_BINDING } from "../../../../api-middleware/flatServer/constants";
1116
import { useSafePromise } from "../../../../utils/hooks/lifecycle";
17+
import { GlobalStore } from "../../../../stores/global-store";
18+
import { errorTips } from "../../../../components/Tips/ErrorTips";
1219

1320
export interface BindingWeChatProps {
1421
isBind: boolean;
1522
onRefresh: () => void;
23+
globalStore: GlobalStore;
1624
}
1725

18-
export const BindingWeChat: React.FC<BindingWeChatProps> = ({ isBind, onRefresh }) => {
26+
export const BindingWeChat: React.FC<BindingWeChatProps> = ({ isBind, onRefresh, globalStore }) => {
1927
const sp = useSafePromise();
2028
const { t } = useTranslation();
2129
const [authUUID, setAuthUUID] = useState("");
@@ -57,7 +65,19 @@ export const BindingWeChat: React.FC<BindingWeChatProps> = ({ isBind, onRefresh
5765
};
5866

5967
const unbind = (): void => {
60-
message.info(t("bind-wechat-not-support-unbind"));
68+
Modal.confirm({
69+
content: t("unbind-confirm"),
70+
onOk: async () => {
71+
try {
72+
const { token } = await sp(removeBinding(LoginPlatform.WeChat));
73+
globalStore.updateUserToken(token);
74+
onRefresh();
75+
message.info(t("unbind-success"));
76+
} catch (err) {
77+
errorTips(err);
78+
}
79+
},
80+
});
6181
};
6282

6383
return (

desktop/renderer-app/src/pages/UserSettingPage/GeneralSettingPage/index.tsx

+11-5
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import "./style.less";
22

33
import React, { useContext, useEffect, useState } from "react";
44
import { Checkbox, Input, message, Radio, RadioChangeEvent } from "antd";
5-
import { UserSettingLayoutContainer } from "../UserSettingLayoutContainer";
6-
import { ipcSyncByApp, ipcAsyncByApp } from "../../../utils/ipc";
5+
import { observer } from "mobx-react-lite";
76
import { useTranslation } from "react-i18next";
87
import { AppearancePicker, FlatPrefersColorScheme } from "flat-components";
8+
9+
import { UserSettingLayoutContainer } from "../UserSettingLayoutContainer";
10+
import { ipcSyncByApp, ipcAsyncByApp } from "../../../utils/ipc";
911
import { ConfigStoreContext, GlobalStoreContext } from "../../../components/StoreProvider";
1012
import { useSafePromise } from "../../../utils/hooks/lifecycle";
1113
import { loginCheck, rename } from "../../../api-middleware/flatServer";
@@ -19,7 +21,7 @@ enum SelectLanguage {
1921
English,
2022
}
2123

22-
export const GeneralSettingPage = (): React.ReactElement => {
24+
export const GeneralSettingPage = observer(function GeneralSettingPage() {
2325
const sp = useSafePromise();
2426
const { t, i18n } = useTranslation();
2527
const [openAtLogin, setOpenAtLogin] = useState(false);
@@ -99,7 +101,11 @@ export const GeneralSettingPage = (): React.ReactElement => {
99101
<ConfirmButtons onConfirm={changeUserName} />
100102
</div>
101103
<div className="general-setting-binding-methods">
102-
<BindingWeChat isBind={bindings.wechat} onRefresh={refreshBindings} />
104+
<BindingWeChat
105+
globalStore={globalStore}
106+
isBind={bindings.wechat}
107+
onRefresh={refreshBindings}
108+
/>
103109
</div>
104110
</div>
105111
<div className="general-setting-checkbox">
@@ -137,4 +143,4 @@ export const GeneralSettingPage = (): React.ReactElement => {
137143
</div>
138144
</UserSettingLayoutContainer>
139145
);
140-
};
146+
});

desktop/renderer-app/src/stores/global-store.ts

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ export class GlobalStore {
5858
}
5959
};
6060

61+
public updateUserToken = (token: string): void => {
62+
if (this.userInfo) {
63+
this.userInfo.token = token;
64+
}
65+
};
66+
6167
public updateLastLoginCheck = (val: number | null): void => {
6268
this.lastLoginCheck = val;
6369
};

packages/flat-i18n/locales/en.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -463,5 +463,7 @@
463463
"bind-wechat-failed": "Failed to bind WeChat",
464464
"bind-wechat-not-support-unbind": "Not support unbind WeChat",
465465
"is-bind": "is bind",
466-
"not-bind": "not bind"
466+
"not-bind": "not bind",
467+
"unbind-confirm": "Are you sure to unbind?",
468+
"unbind-success": "Unbind success"
467469
}

packages/flat-i18n/locales/zh-CN.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -463,5 +463,7 @@
463463
"bind-wechat-failed": "绑定失败",
464464
"bind-wechat-not-support-unbind": "暂未支持解绑",
465465
"is-bind": "已绑定",
466-
"not-bind": "未绑定"
466+
"not-bind": "未绑定",
467+
"unbind-confirm": "确定解除绑定吗?",
468+
"unbind-success": "解绑成功"
467469
}

web/flat-web/src/api-middleware/flatServer/index.ts

+23
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,26 @@ export async function bindingProcess(authUUID: string): Promise<BindingProcessRe
658658
};
659659
}
660660
}
661+
662+
export enum LoginPlatform {
663+
WeChat = "WeChat",
664+
Github = "Github",
665+
Apple = "Apple",
666+
Agora = "Agora",
667+
Google = "Google",
668+
Phone = "Phone",
669+
}
670+
671+
export interface RemoveBindingPayload {
672+
target: LoginPlatform;
673+
}
674+
675+
export interface RemoveBindingResult {
676+
token: string;
677+
}
678+
679+
export async function removeBinding(target: LoginPlatform): Promise<RemoveBindingResult> {
680+
return await post<RemoveBindingPayload, RemoveBindingResult>("user/binding/remove", {
681+
target,
682+
});
683+
}

web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/binding/WeChat.tsx

+23-3
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ import { message, Modal } from "antd";
66
import { WechatFilled } from "@ant-design/icons";
77

88
import { getQRCodeURL } from "../../../LoginPage/WeChatLogin";
9-
import { setBindingAuthUUID, bindingProcess } from "../../../../api-middleware/flatServer";
9+
import {
10+
setBindingAuthUUID,
11+
bindingProcess,
12+
removeBinding,
13+
LoginPlatform,
14+
} from "../../../../api-middleware/flatServer";
1015
import { FLAT_SERVER_USER_BINDING } from "../../../../api-middleware/flatServer/constants";
1116
import { useSafePromise } from "../../../../utils/hooks/lifecycle";
17+
import { errorTips } from "../../../../components/Tips/ErrorTips";
18+
import { GlobalStore } from "../../../../stores/GlobalStore";
1219

1320
export interface BindingWeChatProps {
1421
isBind: boolean;
1522
onRefresh: () => void;
23+
globalStore: GlobalStore;
1624
}
1725

18-
export const BindingWeChat: React.FC<BindingWeChatProps> = ({ isBind, onRefresh }) => {
26+
export const BindingWeChat: React.FC<BindingWeChatProps> = ({ isBind, onRefresh, globalStore }) => {
1927
const sp = useSafePromise();
2028
const { t } = useTranslation();
2129
const [authUUID, setAuthUUID] = useState("");
@@ -57,7 +65,19 @@ export const BindingWeChat: React.FC<BindingWeChatProps> = ({ isBind, onRefresh
5765
};
5866

5967
const unbind = (): void => {
60-
message.info(t("bind-wechat-not-support-unbind"));
68+
Modal.confirm({
69+
content: t("unbind-confirm"),
70+
onOk: async () => {
71+
try {
72+
const { token } = await sp(removeBinding(LoginPlatform.WeChat));
73+
globalStore.updateUserToken(token);
74+
onRefresh();
75+
message.info(t("unbind-success"));
76+
} catch (err) {
77+
errorTips(err);
78+
}
79+
},
80+
});
6181
};
6282

6383
return (

web/flat-web/src/pages/UserSettingPage/GeneralSettingPage/index.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ import type { CheckboxChangeEvent } from "antd/lib/checkbox";
33
import "./index.less";
44

55
import React, { useContext, useState } from "react";
6+
import { observer } from "mobx-react-lite";
67
import { Checkbox, Input, message, Radio } from "antd";
78
import { FlatPrefersColorScheme, AppearancePicker } from "flat-components";
89
import { UserSettingLayoutContainer } from "../UserSettingLayoutContainer";
910
import { useTranslation } from "react-i18next";
11+
1012
import { ConfigStoreContext, GlobalStoreContext } from "../../../components/StoreProvider";
1113
import { useSafePromise } from "../../../utils/hooks/lifecycle";
1214
import { loginCheck, rename } from "../../../api-middleware/flatServer";
@@ -20,7 +22,7 @@ enum SelectLanguage {
2022
English,
2123
}
2224

23-
export const GeneralSettingPage = (): React.ReactElement => {
25+
export const GeneralSettingPage = observer(function GeneralSettingPage() {
2426
const globalStore = useContext(GlobalStoreContext);
2527
const configStore = useContext(ConfigStoreContext);
2628

@@ -83,7 +85,11 @@ export const GeneralSettingPage = (): React.ReactElement => {
8385
<ConfirmButtons onConfirm={changeUserName} />
8486
</div>
8587
<div className="general-setting-binding-methods">
86-
<BindingWeChat isBind={bindings.wechat} onRefresh={refreshBindings} />
88+
<BindingWeChat
89+
globalStore={globalStore}
90+
isBind={bindings.wechat}
91+
onRefresh={refreshBindings}
92+
/>
8793
</div>
8894
</div>
8995
<div className="general-setting-select-language">
@@ -125,6 +131,6 @@ export const GeneralSettingPage = (): React.ReactElement => {
125131
</div>
126132
</UserSettingLayoutContainer>
127133
);
128-
};
134+
});
129135

130136
export default GeneralSettingPage;

web/flat-web/src/stores/GlobalStore.ts

+6
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ export class GlobalStore {
5959
}
6060
};
6161

62+
public updateUserToken = (token: string): void => {
63+
if (this.userInfo) {
64+
this.userInfo.token = token;
65+
}
66+
};
67+
6268
public updateLastLoginCheck = (val: number | null): void => {
6369
this.lastLoginCheck = val;
6470
};

0 commit comments

Comments
 (0)