Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(project): support unbind wechat #1573

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions desktop/renderer-app/src/api-middleware/flatServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,26 @@ export async function bindingProcess(authUUID: string): Promise<BindingProcessRe
};
}
}

export enum LoginPlatform {
WeChat = "WeChat",
Github = "Github",
Apple = "Apple",
Agora = "Agora",
Google = "Google",
Phone = "Phone",
}

export interface RemoveBindingPayload {
target: LoginPlatform;
}

export interface RemoveBindingResult {
token: string;
}

export async function removeBinding(target: LoginPlatform): Promise<RemoveBindingResult> {
return await post<RemoveBindingPayload, RemoveBindingResult>("user/binding/remove", {
target,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ import { message, Modal } from "antd";
import { WechatFilled } from "@ant-design/icons";

import { getQRCodeURL } from "../../../LoginPage/WeChatLogin";
import { setBindingAuthUUID, bindingProcess } from "../../../../api-middleware/flatServer";
import {
setBindingAuthUUID,
bindingProcess,
removeBinding,
LoginPlatform,
} from "../../../../api-middleware/flatServer";
import { FLAT_SERVER_USER_BINDING } from "../../../../api-middleware/flatServer/constants";
import { useSafePromise } from "../../../../utils/hooks/lifecycle";
import { GlobalStore } from "../../../../stores/global-store";
import { errorTips } from "../../../../components/Tips/ErrorTips";

export interface BindingWeChatProps {
isBind: boolean;
onRefresh: () => void;
globalStore: GlobalStore;
}

export const BindingWeChat: React.FC<BindingWeChatProps> = ({ isBind, onRefresh }) => {
export const BindingWeChat: React.FC<BindingWeChatProps> = ({ isBind, onRefresh, globalStore }) => {
const sp = useSafePromise();
const { t } = useTranslation();
const [authUUID, setAuthUUID] = useState("");
Expand Down Expand Up @@ -57,7 +65,19 @@ export const BindingWeChat: React.FC<BindingWeChatProps> = ({ isBind, onRefresh
};

const unbind = (): void => {
message.info(t("bind-wechat-not-support-unbind"));
Modal.confirm({
content: t("unbind-confirm"),
onOk: async () => {
try {
const { token } = await sp(removeBinding(LoginPlatform.WeChat));
globalStore.updateUserToken(token);
onRefresh();
message.info(t("unbind-success"));
} catch (err) {
errorTips(err);
}
},
});
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import "./style.less";

import React, { useContext, useEffect, useState } from "react";
import { Checkbox, Input, message, Radio, RadioChangeEvent } from "antd";
import { UserSettingLayoutContainer } from "../UserSettingLayoutContainer";
import { ipcSyncByApp, ipcAsyncByApp } from "../../../utils/ipc";
import { observer } from "mobx-react-lite";
import { useTranslation } from "react-i18next";
import { AppearancePicker, FlatPrefersColorScheme } from "flat-components";

import { UserSettingLayoutContainer } from "../UserSettingLayoutContainer";
import { ipcSyncByApp, ipcAsyncByApp } from "../../../utils/ipc";
import { ConfigStoreContext, GlobalStoreContext } from "../../../components/StoreProvider";
import { useSafePromise } from "../../../utils/hooks/lifecycle";
import { loginCheck, rename } from "../../../api-middleware/flatServer";
Expand All @@ -19,7 +21,7 @@ enum SelectLanguage {
English,
}

export const GeneralSettingPage = (): React.ReactElement => {
export const GeneralSettingPage = observer(function GeneralSettingPage() {
const sp = useSafePromise();
const { t, i18n } = useTranslation();
const [openAtLogin, setOpenAtLogin] = useState(false);
Expand Down Expand Up @@ -99,7 +101,11 @@ export const GeneralSettingPage = (): React.ReactElement => {
<ConfirmButtons onConfirm={changeUserName} />
</div>
<div className="general-setting-binding-methods">
<BindingWeChat isBind={bindings.wechat} onRefresh={refreshBindings} />
<BindingWeChat
globalStore={globalStore}
isBind={bindings.wechat}
onRefresh={refreshBindings}
/>
</div>
</div>
<div className="general-setting-checkbox">
Expand Down Expand Up @@ -137,4 +143,4 @@ export const GeneralSettingPage = (): React.ReactElement => {
</div>
</UserSettingLayoutContainer>
);
};
});
6 changes: 6 additions & 0 deletions desktop/renderer-app/src/stores/global-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ export class GlobalStore {
}
};

public updateUserToken = (token: string): void => {
if (this.userInfo) {
this.userInfo.token = token;
}
};

public updateLastLoginCheck = (val: number | null): void => {
this.lastLoginCheck = val;
};
Expand Down
4 changes: 3 additions & 1 deletion packages/flat-i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -463,5 +463,7 @@
"bind-wechat-failed": "Failed to bind WeChat",
"bind-wechat-not-support-unbind": "Not support unbind WeChat",
"is-bind": "is bind",
"not-bind": "not bind"
"not-bind": "not bind",
"unbind-confirm": "Are you sure to unbind?",
"unbind-success": "Unbind success"
}
4 changes: 3 additions & 1 deletion packages/flat-i18n/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -463,5 +463,7 @@
"bind-wechat-failed": "绑定失败",
"bind-wechat-not-support-unbind": "暂未支持解绑",
"is-bind": "已绑定",
"not-bind": "未绑定"
"not-bind": "未绑定",
"unbind-confirm": "确定解除绑定吗?",
"unbind-success": "解绑成功"
}
23 changes: 23 additions & 0 deletions web/flat-web/src/api-middleware/flatServer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -658,3 +658,26 @@ export async function bindingProcess(authUUID: string): Promise<BindingProcessRe
};
}
}

export enum LoginPlatform {
WeChat = "WeChat",
Github = "Github",
Apple = "Apple",
Agora = "Agora",
Google = "Google",
Phone = "Phone",
}

export interface RemoveBindingPayload {
target: LoginPlatform;
}

export interface RemoveBindingResult {
token: string;
}

export async function removeBinding(target: LoginPlatform): Promise<RemoveBindingResult> {
return await post<RemoveBindingPayload, RemoveBindingResult>("user/binding/remove", {
target,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,24 @@ import { message, Modal } from "antd";
import { WechatFilled } from "@ant-design/icons";

import { getQRCodeURL } from "../../../LoginPage/WeChatLogin";
import { setBindingAuthUUID, bindingProcess } from "../../../../api-middleware/flatServer";
import {
setBindingAuthUUID,
bindingProcess,
removeBinding,
LoginPlatform,
} from "../../../../api-middleware/flatServer";
import { FLAT_SERVER_USER_BINDING } from "../../../../api-middleware/flatServer/constants";
import { useSafePromise } from "../../../../utils/hooks/lifecycle";
import { errorTips } from "../../../../components/Tips/ErrorTips";
import { GlobalStore } from "../../../../stores/GlobalStore";

export interface BindingWeChatProps {
isBind: boolean;
onRefresh: () => void;
globalStore: GlobalStore;
}

export const BindingWeChat: React.FC<BindingWeChatProps> = ({ isBind, onRefresh }) => {
export const BindingWeChat: React.FC<BindingWeChatProps> = ({ isBind, onRefresh, globalStore }) => {
const sp = useSafePromise();
const { t } = useTranslation();
const [authUUID, setAuthUUID] = useState("");
Expand Down Expand Up @@ -57,7 +65,19 @@ export const BindingWeChat: React.FC<BindingWeChatProps> = ({ isBind, onRefresh
};

const unbind = (): void => {
message.info(t("bind-wechat-not-support-unbind"));
Modal.confirm({
content: t("unbind-confirm"),
onOk: async () => {
try {
const { token } = await sp(removeBinding(LoginPlatform.WeChat));
globalStore.updateUserToken(token);
onRefresh();
message.info(t("unbind-success"));
} catch (err) {
errorTips(err);
}
},
});
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ import type { CheckboxChangeEvent } from "antd/lib/checkbox";
import "./index.less";

import React, { useContext, useState } from "react";
import { observer } from "mobx-react-lite";
import { Checkbox, Input, message, Radio } from "antd";
import { FlatPrefersColorScheme, AppearancePicker } from "flat-components";
import { UserSettingLayoutContainer } from "../UserSettingLayoutContainer";
import { useTranslation } from "react-i18next";

import { ConfigStoreContext, GlobalStoreContext } from "../../../components/StoreProvider";
import { useSafePromise } from "../../../utils/hooks/lifecycle";
import { loginCheck, rename } from "../../../api-middleware/flatServer";
Expand All @@ -20,7 +22,7 @@ enum SelectLanguage {
English,
}

export const GeneralSettingPage = (): React.ReactElement => {
export const GeneralSettingPage = observer(function GeneralSettingPage() {
const globalStore = useContext(GlobalStoreContext);
const configStore = useContext(ConfigStoreContext);

Expand Down Expand Up @@ -83,7 +85,11 @@ export const GeneralSettingPage = (): React.ReactElement => {
<ConfirmButtons onConfirm={changeUserName} />
</div>
<div className="general-setting-binding-methods">
<BindingWeChat isBind={bindings.wechat} onRefresh={refreshBindings} />
<BindingWeChat
globalStore={globalStore}
isBind={bindings.wechat}
onRefresh={refreshBindings}
/>
</div>
</div>
<div className="general-setting-select-language">
Expand Down Expand Up @@ -125,6 +131,6 @@ export const GeneralSettingPage = (): React.ReactElement => {
</div>
</UserSettingLayoutContainer>
);
};
});

export default GeneralSettingPage;
6 changes: 6 additions & 0 deletions web/flat-web/src/stores/GlobalStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ export class GlobalStore {
}
};

public updateUserToken = (token: string): void => {
if (this.userInfo) {
this.userInfo.token = token;
}
};

public updateLastLoginCheck = (val: number | null): void => {
this.lastLoginCheck = val;
};
Expand Down