feat: Oauth update

This commit is contained in:
landaiqing
2024-05-27 17:54:33 +08:00
parent 015d7b256f
commit 90a1c1f3ce
10 changed files with 78 additions and 72 deletions

View File

@@ -14,7 +14,7 @@ VITE_API_BASE_URL='http://127.0.0.1:3000'
VITE_TITLE_NAME='五味子云存储' VITE_TITLE_NAME='五味子云存储'
# token key # token key
VITE_APP_TOKEN_KEY='token' VITE_APP_TOKEN_KEY='schisandra'
# the upload url # the upload url
VITE_UPLOAD_URL='http://127.0.0.1:3000' VITE_UPLOAD_URL='http://127.0.0.1:3000'

View File

@@ -21,12 +21,4 @@ export const oauthLogin = (type: string) => {
method: "get", method: "get",
}); });
}; };
/**
* 获取用户信息
*/
export const geOauthtUserInfo = () => {
return web.request({
url: "/oauth/userInfo/",
method: "get",
});
};

View File

@@ -1,27 +1,24 @@
/** @format */ /** @format */
import React, { useEffect } from "react"; import { useEffect } from "react";
import "./index.less"; import "./index.less";
import { geOauthtUserInfo } from "@/api/user"; import { useNavigate, useSearchParams } from "react-router-dom";
import localforage from "localforage"; import useStore from "@/utils/store/useStore.tsx";
import { observer } from "mobx-react";
const LoadingPage: React.FC = () => {
async function getUserInfo() {
const res: any = await geOauthtUserInfo();
localforage.setItem("token", res.data.token).then();
localforage.setItem("userId", res.data.userId).then();
}
const LoadingPage = () => {
const [search] = useSearchParams();
const navigate = useNavigate();
const token: any = search.get("token");
const userId: any = search.get("userId");
const store = useStore("user");
store.setToken(token);
store.setUserId(userId);
useEffect(() => { useEffect(() => {
document.body.classList.add("loading-body"); document.body.classList.add("loading-body");
getUserInfo().then(() => { if (store.getToken() !== null && store.getUserId() !== null) {
if ( navigate("/main");
localforage.getItem("token").then() !== null && }
localforage.getItem("userId").then() !== null
) {
window.location.href = "/main";
}
});
return () => { return () => {
document.body.classList.remove("loading-body"); document.body.classList.remove("loading-body");
}; };
@@ -70,4 +67,4 @@ const LoadingPage: React.FC = () => {
</> </>
); );
}; };
export default React.memo(LoadingPage); export default observer(LoadingPage);

View File

@@ -33,7 +33,7 @@ const routes: RouteObject[] = [
}, },
{ {
path: "/loading", path: "/loading",
Component: (props) => ComponentLoading(Loading, props), Component: Loading,
}, },
]; ];

View File

@@ -1,9 +1,11 @@
import { lazy } from 'react' /** @format */
import { lazy } from "react";
const main = lazy( const main = lazy(
() => () =>
new Promise((resolve: any) => { new Promise((resolve: any) => {
setTimeout(() => resolve(import('@/views/Main')), 1000) setTimeout(() => resolve(import("@/views/Main")), 1000);
}), }),
) );
export default main export default main;

View File

@@ -1,26 +1,28 @@
/** @format */ /** @format */
import { action, makeAutoObservable } from "mobx"; import { action, makeObservable, observable } from "mobx";
import { makePersistable, isHydrated } from "mobx-persist-store"; import { isHydrated, makePersistable } from "mobx-persist-store";
import { handleLocalforage } from "@/utils/localforage"; import { handleLocalforage } from "@/utils/localforage";
export class useUserStore { export class useUserStore {
token: any = ""; token: string = "";
userId: string = "";
constructor() { constructor() {
makeAutoObservable( // makeAutoObservable(this);
this, makeObservable(this, {
{ token: observable,
setToken: action, userId: observable,
}, setToken: action,
{ autoBind: true }, setUserId: action,
); getToken: action,
// eslint-disable-next-line @typescript-eslint/ban-ts-comment getUserId: action,
// @ts-ignore isHydrated: action,
});
makePersistable(this, { makePersistable(this, {
// 在构造函数内使用 makePersistable // 在构造函数内使用 makePersistable
name: "token", // 保存的name用于在storage中的名称标识只要不和storage中其他名称重复就可以 name: "userInfo", // 保存的name用于在storage中的名称标识只要不和storage中其他名称重复就可以
properties: ["token"], // 要保存的字段这些字段会被保存在name对应的storage中注意不写在这里面的字段将不会被保存刷新页面也将丢失get字段例外。get数据会在数据返回后再自动计算 properties: ["token", "userId"], // 要保存的字段这些字段会被保存在name对应的storage中注意不写在这里面的字段将不会被保存刷新页面也将丢失get字段例外。get数据会在数据返回后再自动计算
storage: handleLocalforage, // 保存的位置看自己的业务情况选择可以是localStoragesessionstorage storage: handleLocalforage, // 保存的位置看自己的业务情况选择可以是localStoragesessionstorage
// 。。还有一些其他配置参数例如数据过期时间等等可以康康文档像storage这种字段可以配置在全局配置里详见文档 // 。。还有一些其他配置参数例如数据过期时间等等可以康康文档像storage这种字段可以配置在全局配置里详见文档
}).then( }).then(
@@ -31,15 +33,23 @@ export class useUserStore {
); );
} }
get getToken() { getToken() {
return this.token ? this.token : null; return this.token ? this.token : null;
} }
get isHydrated() { getUserId() {
return this.userId ? this.userId : null;
}
isHydrated() {
return isHydrated(this); return isHydrated(this);
} }
setToken(token: string) { setToken(token: string) {
this.token = token; this.token = token;
} }
setUserId(userId: string) {
this.userId = userId;
}
} }

View File

@@ -3,13 +3,11 @@
import Request from "./request"; import Request from "./request";
import { handleLocalforage } from "@/utils/localforage"; import { handleLocalforage } from "@/utils/localforage";
const token = String(handleLocalforage.getItem("token")); const token = handleLocalforage.getItem("token").then();
const web: Request = new Request({ const web: Request = new Request({
baseURL: import.meta.env.VITE_APP_BASE_API, baseURL: import.meta.env.VITE_APP_BASE_API,
headers: { headers: {
// 'Content-Type': 'application/json;charset=utf-8', token: import.meta.env.VITE_APP_TOKEN_KEY + " " + token,
// Accept: 'application/json',
Authorization: token,
}, },
}); });

View File

@@ -4,26 +4,28 @@ import CryptoJS from "crypto-js";
const key = CryptoJS.enc.Hex.parse("d86d7bab3d6ac01ad9dc6a897652f2d2"); const key = CryptoJS.enc.Hex.parse("d86d7bab3d6ac01ad9dc6a897652f2d2");
// const iv = CryptoJS.enc.Latin1.parse("d86d7bab3d6ac01ad9dc6a897652f2d2"); // const iv = CryptoJS.enc.Latin1.parse("d86d7bab3d6ac01ad9dc6a897652f2d2");
// 加密 /**
* 加密
* @param data
*/
function EncryptData(data: any) { function EncryptData(data: any) {
const srcs = CryptoJS.enc.Utf8.parse(data); const src = CryptoJS.enc.Utf8.parse(data);
const encrypted = CryptoJS.AES.encrypt(srcs, key, { const encrypted = CryptoJS.AES.encrypt(src, key, {
mode: CryptoJS.mode.ECB, mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7, padding: CryptoJS.pad.Pkcs7,
}); });
return encrypted.toString(); return encrypted.toString();
} }
// 解密 /**
* 解密
* @param data
*/
function DecryptData(data: any) { function DecryptData(data: any) {
// const stime = new Date().getTime();
const decrypt = CryptoJS.AES.decrypt(data, key, { const decrypt = CryptoJS.AES.decrypt(data, key, {
mode: CryptoJS.mode.ECB, mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7, padding: CryptoJS.pad.Pkcs7,
}); });
const result = JSON.parse(CryptoJS.enc.Utf8.stringify(decrypt).toString()); return JSON.parse(CryptoJS.enc.Utf8.stringify(decrypt).toString());
// const etime = new Date().getTime();
// console.log("DecryptData Time:" + (etime - stime));
return result;
} }
export { EncryptData, DecryptData }; export { EncryptData, DecryptData };

View File

@@ -1,11 +1,14 @@
/** @format */ /** @format */
import DefaultLayOut from "@/layout/default"; import DefaultLayOut from "@/layout/default";
import { useEffect } from "react";
import { observer } from "mobx-react";
export default () => { export default observer(() => {
useEffect(() => {}, []);
return ( return (
<> <>
<DefaultLayOut /> <DefaultLayOut />
</> </>
); );
}; });

View File

@@ -10,9 +10,8 @@ import {
WechatOutlined, WechatOutlined,
} from "@ant-design/icons"; } from "@ant-design/icons";
import { ProFormCaptcha, ProFormCheckbox, ProFormText } from "@ant-design/pro-components"; import { ProFormCaptcha, ProFormCheckbox, ProFormText } from "@ant-design/pro-components";
import { Divider, Space, Tabs, message, Image, Alert, Form, Button } from "antd"; import { Alert, Button, Divider, Form, Image, message, Space, Tabs } from "antd";
import { CSSProperties, useRef } from "react"; import { CSSProperties, useRef, useState } from "react";
import { useState } from "react";
import logo from "@/assets/icons/schisandra.svg"; import logo from "@/assets/icons/schisandra.svg";
import qrCode from "@/assets/images/login_qrcode-landaiqing.jpg"; import qrCode from "@/assets/images/login_qrcode-landaiqing.jpg";
import styles from "./index.module.less"; import styles from "./index.module.less";
@@ -351,6 +350,9 @@ export default observer(() => {
borderRadius: "50%", borderRadius: "50%",
}}> }}>
<GithubOutlined <GithubOutlined
onClick={() => {
oAuthLogin("github").then();
}}
style={{ ...iconStyles, color: "#333333" }} style={{ ...iconStyles, color: "#333333" }}
/> />
</div> </div>
@@ -367,7 +369,7 @@ export default observer(() => {
}}> }}>
<GitlabOutlined <GitlabOutlined
onClick={() => { onClick={() => {
oAuthLogin("GITEE").then(); oAuthLogin("gitee").then();
}} }}
style={{ ...iconStyles, color: "#FF6A10" }} style={{ ...iconStyles, color: "#FF6A10" }}
/> />