mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-11 19:03:43 +08:00
feat: add new theme berry (#860)
* feat: add theme berry * docs: add development notes * fix: fix blank page * chore: update implementation * fix: fix package.json * chore: update ui copy --------- Co-authored-by: JustSong <songquanpeng@foxmail.com>
This commit is contained in:
13
web/berry/src/hooks/useAuth.js
Normal file
13
web/berry/src/hooks/useAuth.js
Normal file
@@ -0,0 +1,13 @@
|
||||
import { isAdmin } from 'utils/common';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
const navigate = useNavigate();
|
||||
|
||||
const useAuth = () => {
|
||||
const userIsAdmin = isAdmin();
|
||||
|
||||
if (!userIsAdmin) {
|
||||
navigate('/panel/404');
|
||||
}
|
||||
};
|
||||
|
||||
export default useAuth;
|
||||
78
web/berry/src/hooks/useLogin.js
Normal file
78
web/berry/src/hooks/useLogin.js
Normal file
@@ -0,0 +1,78 @@
|
||||
import { API } from 'utils/api';
|
||||
import { useDispatch } from 'react-redux';
|
||||
import { LOGIN } from 'store/actions';
|
||||
import { useNavigate } from 'react-router';
|
||||
import { showSuccess } from 'utils/common';
|
||||
|
||||
const useLogin = () => {
|
||||
const dispatch = useDispatch();
|
||||
const navigate = useNavigate();
|
||||
const login = async (username, password) => {
|
||||
try {
|
||||
const res = await API.post(`/api/user/login`, {
|
||||
username,
|
||||
password
|
||||
});
|
||||
const { success, message, data } = res.data;
|
||||
if (success) {
|
||||
localStorage.setItem('user', JSON.stringify(data));
|
||||
dispatch({ type: LOGIN, payload: data });
|
||||
navigate('/panel');
|
||||
}
|
||||
return { success, message };
|
||||
} catch (err) {
|
||||
// 请求失败,设置错误信息
|
||||
return { success: false, message: '' };
|
||||
}
|
||||
};
|
||||
|
||||
const githubLogin = async (code, state) => {
|
||||
try {
|
||||
const res = await API.get(`/api/oauth/github?code=${code}&state=${state}`);
|
||||
const { success, message, data } = res.data;
|
||||
if (success) {
|
||||
if (message === 'bind') {
|
||||
showSuccess('绑定成功!');
|
||||
navigate('/panel');
|
||||
} else {
|
||||
dispatch({ type: LOGIN, payload: data });
|
||||
localStorage.setItem('user', JSON.stringify(data));
|
||||
showSuccess('登录成功!');
|
||||
navigate('/panel');
|
||||
}
|
||||
}
|
||||
return { success, message };
|
||||
} catch (err) {
|
||||
// 请求失败,设置错误信息
|
||||
return { success: false, message: '' };
|
||||
}
|
||||
};
|
||||
|
||||
const wechatLogin = async (code) => {
|
||||
try {
|
||||
const res = await API.get(`/api/oauth/wechat?code=${code}`);
|
||||
const { success, message, data } = res.data;
|
||||
if (success) {
|
||||
dispatch({ type: LOGIN, payload: data });
|
||||
localStorage.setItem('user', JSON.stringify(data));
|
||||
showSuccess('登录成功!');
|
||||
navigate('/panel');
|
||||
}
|
||||
return { success, message };
|
||||
} catch (err) {
|
||||
// 请求失败,设置错误信息
|
||||
return { success: false, message: '' };
|
||||
}
|
||||
};
|
||||
|
||||
const logout = async () => {
|
||||
await API.get('/api/user/logout');
|
||||
localStorage.removeItem('user');
|
||||
dispatch({ type: LOGIN, payload: null });
|
||||
navigate('/');
|
||||
};
|
||||
|
||||
return { login, logout, githubLogin, wechatLogin };
|
||||
};
|
||||
|
||||
export default useLogin;
|
||||
39
web/berry/src/hooks/useRegister.js
Normal file
39
web/berry/src/hooks/useRegister.js
Normal file
@@ -0,0 +1,39 @@
|
||||
import { API } from 'utils/api';
|
||||
import { useNavigate } from 'react-router';
|
||||
import { showSuccess } from 'utils/common';
|
||||
|
||||
const useRegister = () => {
|
||||
const navigate = useNavigate();
|
||||
const register = async (input, turnstile) => {
|
||||
try {
|
||||
const res = await API.post(`/api/user/register?turnstile=${turnstile}`, input);
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
showSuccess('注册成功!');
|
||||
navigate('/login');
|
||||
}
|
||||
return { success, message };
|
||||
} catch (err) {
|
||||
// 请求失败,设置错误信息
|
||||
return { success: false, message: '' };
|
||||
}
|
||||
};
|
||||
|
||||
const sendVerificationCode = async (email, turnstile) => {
|
||||
try {
|
||||
const res = await API.get(`/api/verification?email=${email}&turnstile=${turnstile}`);
|
||||
const { success, message } = res.data;
|
||||
if (success) {
|
||||
showSuccess('验证码发送成功,请检查你的邮箱!');
|
||||
}
|
||||
return { success, message };
|
||||
} catch (err) {
|
||||
// 请求失败,设置错误信息
|
||||
return { success: false, message: '' };
|
||||
}
|
||||
};
|
||||
|
||||
return { register, sendVerificationCode };
|
||||
};
|
||||
|
||||
export default useRegister;
|
||||
18
web/berry/src/hooks/useScriptRef.js
Normal file
18
web/berry/src/hooks/useScriptRef.js
Normal file
@@ -0,0 +1,18 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
|
||||
// ==============================|| ELEMENT REFERENCE HOOKS ||============================== //
|
||||
|
||||
const useScriptRef = () => {
|
||||
const scripted = useRef(true);
|
||||
|
||||
useEffect(
|
||||
() => () => {
|
||||
scripted.current = true;
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
return scripted;
|
||||
};
|
||||
|
||||
export default useScriptRef;
|
||||
Reference in New Issue
Block a user