feat: 登录状态
This commit is contained in:
parent
c0d9192030
commit
a298594cf8
|
@ -21,7 +21,7 @@ export default function InviteCode() {
|
||||||
|
|
||||||
const defaultColumns = [
|
const defaultColumns = [
|
||||||
{
|
{
|
||||||
title: "字符",
|
title: "神秘代码",
|
||||||
dataIndex: "code",
|
dataIndex: "code",
|
||||||
key: "code",
|
key: "code",
|
||||||
filters: [],
|
filters: [],
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { Body, Controller, Inject, Post } from '@midwayjs/core';
|
import { Body, Controller, Get, Inject, Post } from '@midwayjs/core';
|
||||||
import { Context } from '@midwayjs/koa';
|
import { Context } from '@midwayjs/koa';
|
||||||
import { BizCode } from '../biz/code';
|
import { BizCode } from '../biz/code';
|
||||||
import {
|
import {
|
||||||
|
@ -10,7 +10,7 @@ import {
|
||||||
import { UserAdminAuthDTO, UserWebAuthDTO } from '../dto/user.dto';
|
import { UserAdminAuthDTO, UserWebAuthDTO } from '../dto/user.dto';
|
||||||
import { XCodeService } from '../service/xcode.service';
|
import { XCodeService } from '../service/xcode.service';
|
||||||
import { UserService } from '../service/user.service';
|
import { UserService } from '../service/user.service';
|
||||||
import { createToken, md5 } from '../util/encrypt';
|
import { createToken, decodeToken, md5 } from '../util/encrypt';
|
||||||
|
|
||||||
@Controller('/user')
|
@Controller('/user')
|
||||||
export class UserController {
|
export class UserController {
|
||||||
|
@ -94,4 +94,16 @@ export class UserController {
|
||||||
return { code: BizCode.ERROR, msg: '用户名密码错误' };
|
return { code: BizCode.ERROR, msg: '用户名密码错误' };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Get('/web/state')
|
||||||
|
async() {
|
||||||
|
try {
|
||||||
|
const token = this.ctx.cookies.get(webSign);
|
||||||
|
const user = decodeToken(token);
|
||||||
|
return { code: BizCode.OK, data: user };
|
||||||
|
} catch (error) {
|
||||||
|
this.ctx.logger.error(error);
|
||||||
|
return { code: BizCode.ERROR, msg: '[error] /web/state error' };
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,3 +8,5 @@ export const getChapterGuideById = (course_id: string) =>
|
||||||
|
|
||||||
export const userLogin = (p: ILoginRequest) =>
|
export const userLogin = (p: ILoginRequest) =>
|
||||||
R.post("/api/user/web/auth", { ...p });
|
R.post("/api/user/web/auth", { ...p });
|
||||||
|
|
||||||
|
export const userState = () => R.get("/api/user/web/state");
|
||||||
|
|
|
@ -2,10 +2,12 @@ import "./index.less";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
import { menuRouters } from "../../router";
|
import { menuRouters } from "../../router";
|
||||||
import { Input } from "@arco-design/web-react";
|
import { Input } from "@arco-design/web-react";
|
||||||
const InputSearch = Input.Search;
|
import { useUserStore } from "../../store/user.store";
|
||||||
|
|
||||||
function Nav() {
|
function Nav() {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const user = useUserStore((s: any) => s.user);
|
||||||
|
const exit = useUserStore((s: any) => s.userExit);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<header>
|
<header>
|
||||||
|
@ -31,10 +33,16 @@ function Nav() {
|
||||||
</span>
|
</span>
|
||||||
)
|
)
|
||||||
)}
|
)}
|
||||||
<InputSearch allowClear placeholder="搜索" style={{ width: 150 }} />
|
|
||||||
</div>
|
</div>
|
||||||
<div className="end">
|
<div className="end">
|
||||||
|
{!user ? (
|
||||||
<span onClick={() => navigate("/login")}>登录</span>
|
<span onClick={() => navigate("/login")}>登录</span>
|
||||||
|
) : (
|
||||||
|
<div>
|
||||||
|
<span>{user.user_login}</span>
|
||||||
|
<span onClick={() => exit()}>退出</span>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</nav>
|
</nav>
|
||||||
</header>
|
</header>
|
||||||
|
|
|
@ -2,6 +2,7 @@ import { Button, Result } from "@arco-design/web-react";
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { useLocation, useNavigate } from "react-router-dom";
|
import { useLocation, useNavigate } from "react-router-dom";
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
|
import { useUserStore } from "../store/user.store";
|
||||||
|
|
||||||
interface IGuardProps {
|
interface IGuardProps {
|
||||||
children: JSX.Element;
|
children: JSX.Element;
|
||||||
|
@ -11,6 +12,8 @@ const needAuthList = ["course/detail"];
|
||||||
|
|
||||||
export const Guard = (props: IGuardProps) => {
|
export const Guard = (props: IGuardProps) => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const user = useUserStore((s: any) => s.user);
|
||||||
|
const fetchUser = useUserStore((s: any) => s.fetchUser);
|
||||||
|
|
||||||
const Result403 = (
|
const Result403 = (
|
||||||
<div style={{ paddingTop: 100 }}>
|
<div style={{ paddingTop: 100 }}>
|
||||||
|
@ -31,7 +34,9 @@ export const Guard = (props: IGuardProps) => {
|
||||||
const needAuth = needAuthList.some((p) => location.pathname.indexOf(p) > -1);
|
const needAuth = needAuthList.some((p) => location.pathname.indexOf(p) > -1);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
console.log(user);
|
||||||
console.log("location.pathname changed 拦截", location.pathname);
|
console.log("location.pathname changed 拦截", location.pathname);
|
||||||
|
if (!user) fetchUser();
|
||||||
}, [location.pathname]);
|
}, [location.pathname]);
|
||||||
|
|
||||||
if (!sign && needAuth) return Result403;
|
if (!sign && needAuth) return Result403;
|
||||||
|
|
25
apps/web/src/store/user.store.ts
Normal file
25
apps/web/src/store/user.store.ts
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
import { create } from "zustand";
|
||||||
|
import Cookie from "js-cookie";
|
||||||
|
import { userState } from "../api";
|
||||||
|
import { Message } from "@arco-design/web-react";
|
||||||
|
|
||||||
|
export const useUserStore = create((set) => {
|
||||||
|
return {
|
||||||
|
user: null,
|
||||||
|
setUser: (user: any) => set({ user }),
|
||||||
|
fetchUser: async () => {
|
||||||
|
const sign = Cookie.get("_sign_web");
|
||||||
|
if (!sign) return set({ user: null });
|
||||||
|
userState().then((res: any) => {
|
||||||
|
const { code, data } = res;
|
||||||
|
if (code === 10000) set({ user: data });
|
||||||
|
});
|
||||||
|
},
|
||||||
|
userExit: () => {
|
||||||
|
set({ user: null });
|
||||||
|
Cookie.remove("_sign_web");
|
||||||
|
Cookie.remove("_sign_web.sig");
|
||||||
|
Message.success("拜拜~");
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
|
@ -11,10 +11,11 @@ interface IProps {
|
||||||
|
|
||||||
function Player(props: IProps) {
|
function Player(props: IProps) {
|
||||||
const playerRef = useRef<any>();
|
const playerRef = useRef<any>();
|
||||||
|
const instanceRef = useRef<any>();
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (props.video) {
|
if (props.video) {
|
||||||
new DPlayer({
|
instanceRef.current = new DPlayer({
|
||||||
container: playerRef.current,
|
container: playerRef.current,
|
||||||
video: { ...props.video, type: "hls" },
|
video: { ...props.video, type: "hls" },
|
||||||
preload: "auto",
|
preload: "auto",
|
||||||
|
@ -25,6 +26,9 @@ function Player(props: IProps) {
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
return () => {
|
||||||
|
instanceRef.current.destroy();
|
||||||
|
};
|
||||||
}, [props.video]);
|
}, [props.video]);
|
||||||
|
|
||||||
return <div id="vs" ref={playerRef} style={{ height: "100%" }}></div>;
|
return <div id="vs" ref={playerRef} style={{ height: "100%" }}></div>;
|
||||||
|
|
|
@ -3,6 +3,7 @@ import { userLogin } from "../../api";
|
||||||
import { Message, Button } from "@arco-design/web-react";
|
import { Message, Button } from "@arco-design/web-react";
|
||||||
import "./index.less";
|
import "./index.less";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { useUserStore } from "../../store/user.store";
|
||||||
|
|
||||||
const defaultForm = {
|
const defaultForm = {
|
||||||
user_login: "",
|
user_login: "",
|
||||||
|
@ -17,6 +18,7 @@ export function Login() {
|
||||||
let [countdown, setCountdown] = useState(DURATION);
|
let [countdown, setCountdown] = useState(DURATION);
|
||||||
const timer = useRef<any>();
|
const timer = useRef<any>();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
const setUser = useUserStore((s: any) => s.setUser);
|
||||||
|
|
||||||
const onClickSmsBtn = () => {
|
const onClickSmsBtn = () => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
|
@ -31,7 +33,7 @@ export function Login() {
|
||||||
if (code === 10000) {
|
if (code === 10000) {
|
||||||
Message.success(msg);
|
Message.success(msg);
|
||||||
navigate(-1);
|
navigate(-1);
|
||||||
console.log(data);
|
setUser(data);
|
||||||
}
|
}
|
||||||
if (code === 20000) Message.error(msg);
|
if (code === 20000) Message.error(msg);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue
Block a user