init
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
import { BrowserRouter, Navigate, Route, Routes } from 'react-router-dom'
|
||||
import { ConfigProvider, App as AntdApp } from 'antd'
|
||||
import zhCN from 'antd/locale/zh_CN'
|
||||
import { AuthProvider, useAuth } from './store/auth'
|
||||
import MainLayout from './layouts/MainLayout'
|
||||
import Login from './pages/Login'
|
||||
import Register from './pages/Register'
|
||||
import Dashboard from './pages/Dashboard'
|
||||
import Skins from './pages/Skins'
|
||||
import Orders from './pages/Orders'
|
||||
import Distributors from './pages/Distributors'
|
||||
import type { ReactNode } from 'react'
|
||||
|
||||
function PrivateRoute({ children }: { children: ReactNode }) {
|
||||
const { token } = useAuth()
|
||||
if (!token) return <Navigate to="/login" replace />
|
||||
return <>{children}</>
|
||||
}
|
||||
|
||||
function AdminRoute({ children }: { children: ReactNode }) {
|
||||
const { isAdmin } = useAuth()
|
||||
if (!isAdmin) return <Navigate to="/" replace />
|
||||
return <>{children}</>
|
||||
}
|
||||
|
||||
function AppRoutes() {
|
||||
return (
|
||||
<Routes>
|
||||
<Route path="/login" element={<Login />} />
|
||||
<Route path="/register" element={<Register />} />
|
||||
<Route
|
||||
path="/"
|
||||
element={
|
||||
<PrivateRoute>
|
||||
<MainLayout />
|
||||
</PrivateRoute>
|
||||
}
|
||||
>
|
||||
<Route index element={<Dashboard />} />
|
||||
<Route path="skins" element={<Skins />} />
|
||||
<Route path="orders" element={<Orders />} />
|
||||
<Route
|
||||
path="distributors"
|
||||
element={
|
||||
<AdminRoute>
|
||||
<Distributors />
|
||||
</AdminRoute>
|
||||
}
|
||||
/>
|
||||
</Route>
|
||||
<Route path="*" element={<Navigate to="/" replace />} />
|
||||
</Routes>
|
||||
)
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<ConfigProvider
|
||||
locale={zhCN}
|
||||
theme={{
|
||||
token: {
|
||||
colorPrimary: '#1677ff',
|
||||
borderRadius: 6,
|
||||
},
|
||||
}}
|
||||
>
|
||||
<AntdApp>
|
||||
<AuthProvider>
|
||||
<BrowserRouter>
|
||||
<AppRoutes />
|
||||
</BrowserRouter>
|
||||
</AuthProvider>
|
||||
</AntdApp>
|
||||
</ConfigProvider>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import request from './request'
|
||||
import type {
|
||||
DashboardStats,
|
||||
LoginResult,
|
||||
Order,
|
||||
PageResult,
|
||||
Skin,
|
||||
User,
|
||||
} from '../types'
|
||||
|
||||
export const authApi = {
|
||||
login: async (username: string, password: string) => {
|
||||
const res = await request.post('/auth/login', { username, password })
|
||||
return res.data.data as LoginResult
|
||||
},
|
||||
register: async (data: { username: string; password: string; nickname?: string }) => {
|
||||
const res = await request.post('/auth/register', data)
|
||||
return res.data.data as User
|
||||
},
|
||||
profile: async () => {
|
||||
const res = await request.get('/auth/profile')
|
||||
return res.data.data as User
|
||||
},
|
||||
}
|
||||
|
||||
export const dashboardApi = {
|
||||
stats: () =>
|
||||
request.get('/dashboard').then((r) => r.data.data as DashboardStats),
|
||||
}
|
||||
|
||||
export const skinApi = {
|
||||
list: (params?: Record<string, unknown>) =>
|
||||
request.get('/skins', { params }).then((r) => r.data.data as PageResult<Skin>),
|
||||
get: (id: number) =>
|
||||
request.get(`/skins/${id}`).then((r) => r.data.data as Skin),
|
||||
create: (data: Partial<Skin>) =>
|
||||
request.post('/skins', data).then((r) => r.data.data as Skin),
|
||||
update: (id: number, data: Partial<Skin>) =>
|
||||
request.put(`/skins/${id}`, data).then((r) => r.data.data),
|
||||
remove: (id: number) =>
|
||||
request.delete(`/skins/${id}`).then((r) => r.data.data),
|
||||
}
|
||||
|
||||
export const orderApi = {
|
||||
list: (params?: Record<string, unknown>) =>
|
||||
request.get('/orders', { params }).then((r) => r.data.data as PageResult<Order>),
|
||||
create: (data: { skin_id: number; buyer_name?: string; remark?: string }) =>
|
||||
request.post('/orders', data).then((r) => r.data.data as Order),
|
||||
updateStatus: (id: number, status: string) =>
|
||||
request.patch(`/orders/${id}/status`, { status }).then((r) => r.data.data),
|
||||
}
|
||||
|
||||
export const userApi = {
|
||||
list: (params?: Record<string, unknown>) =>
|
||||
request.get('/users', { params }).then((r) => r.data.data as PageResult<User>),
|
||||
create: (data: { username: string; password: string; nickname?: string; role?: string }) =>
|
||||
request.post('/users', data).then((r) => r.data.data as User),
|
||||
updateStatus: (id: number, status: number) =>
|
||||
request.patch(`/users/${id}/status`, { status }).then((r) => r.data.data),
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import axios from 'axios'
|
||||
import type { ApiResponse } from '../types'
|
||||
|
||||
const request = axios.create({
|
||||
baseURL: '/api',
|
||||
timeout: 15000,
|
||||
})
|
||||
|
||||
request.interceptors.request.use((config) => {
|
||||
const token = localStorage.getItem('token')
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`
|
||||
}
|
||||
return config
|
||||
})
|
||||
|
||||
request.interceptors.response.use(
|
||||
(res) => {
|
||||
const body = res.data as ApiResponse
|
||||
if (body.code !== 0) {
|
||||
return Promise.reject(new Error(body.message || '请求失败'))
|
||||
}
|
||||
return res
|
||||
},
|
||||
(err) => {
|
||||
const msg = err.response?.data?.message || err.message || '网络错误'
|
||||
if (err.response?.status === 401) {
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('user')
|
||||
if (!window.location.pathname.includes('/login')) {
|
||||
window.location.href = '/login'
|
||||
}
|
||||
}
|
||||
return Promise.reject(new Error(msg))
|
||||
},
|
||||
)
|
||||
|
||||
export default request
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 13 KiB |
File diff suppressed because one or more lines are too long
|
After Width: | Height: | Size: 8.5 KiB |
@@ -0,0 +1,17 @@
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
html,
|
||||
body,
|
||||
#root {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
min-height: 100%;
|
||||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue',
|
||||
Arial, 'Noto Sans', sans-serif;
|
||||
}
|
||||
|
||||
a {
|
||||
color: #1677ff;
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
import { useMemo, useState } from 'react'
|
||||
import { Outlet, useLocation, useNavigate } from 'react-router-dom'
|
||||
import {
|
||||
Layout,
|
||||
Menu,
|
||||
theme,
|
||||
Dropdown,
|
||||
Space,
|
||||
Typography,
|
||||
Avatar,
|
||||
} from 'antd'
|
||||
import {
|
||||
DashboardOutlined,
|
||||
SkinOutlined,
|
||||
ShoppingOutlined,
|
||||
TeamOutlined,
|
||||
UserOutlined,
|
||||
LogoutOutlined,
|
||||
MenuFoldOutlined,
|
||||
MenuUnfoldOutlined,
|
||||
} from '@ant-design/icons'
|
||||
import { useAuth } from '../store/auth'
|
||||
import type { MenuProps } from 'antd'
|
||||
|
||||
const { Header, Sider, Content } = Layout
|
||||
|
||||
export default function MainLayout() {
|
||||
const [collapsed, setCollapsed] = useState(false)
|
||||
const { user, logout, isAdmin } = useAuth()
|
||||
const navigate = useNavigate()
|
||||
const location = useLocation()
|
||||
const {
|
||||
token: { colorBgContainer, borderRadiusLG },
|
||||
} = theme.useToken()
|
||||
|
||||
const menuItems: MenuProps['items'] = useMemo(() => {
|
||||
const items: MenuProps['items'] = [
|
||||
{ key: '/', icon: <DashboardOutlined />, label: '数据概览' },
|
||||
{ key: '/skins', icon: <SkinOutlined />, label: '皮肤商品' },
|
||||
{ key: '/orders', icon: <ShoppingOutlined />, label: '订单管理' },
|
||||
]
|
||||
if (isAdmin) {
|
||||
items.push({ key: '/distributors', icon: <TeamOutlined />, label: '分销商' })
|
||||
}
|
||||
return items
|
||||
}, [isAdmin])
|
||||
|
||||
const userMenu: MenuProps['items'] = [
|
||||
{
|
||||
key: 'logout',
|
||||
icon: <LogoutOutlined />,
|
||||
label: '退出登录',
|
||||
onClick: () => {
|
||||
logout()
|
||||
navigate('/login')
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
return (
|
||||
<Layout style={{ minHeight: '100vh' }}>
|
||||
<Sider trigger={null} collapsible collapsed={collapsed} theme="dark">
|
||||
<div
|
||||
style={{
|
||||
height: 64,
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
color: '#fff',
|
||||
fontWeight: 700,
|
||||
fontSize: collapsed ? 14 : 16,
|
||||
letterSpacing: 1,
|
||||
}}
|
||||
>
|
||||
{collapsed ? '皮肤' : '皮肤分销系统'}
|
||||
</div>
|
||||
<Menu
|
||||
theme="dark"
|
||||
mode="inline"
|
||||
selectedKeys={[location.pathname === '/' ? '/' : `/${location.pathname.split('/')[1]}`]}
|
||||
items={menuItems}
|
||||
onClick={({ key }) => navigate(key)}
|
||||
/>
|
||||
</Sider>
|
||||
<Layout>
|
||||
<Header
|
||||
style={{
|
||||
padding: '0 24px',
|
||||
background: colorBgContainer,
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'space-between',
|
||||
}}
|
||||
>
|
||||
<span
|
||||
style={{ fontSize: 18, cursor: 'pointer' }}
|
||||
onClick={() => setCollapsed(!collapsed)}
|
||||
>
|
||||
{collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
|
||||
</span>
|
||||
<Dropdown menu={{ items: userMenu }}>
|
||||
<Space style={{ cursor: 'pointer' }}>
|
||||
<Avatar size="small" icon={<UserOutlined />} />
|
||||
<Typography.Text>
|
||||
{user?.nickname || user?.username}
|
||||
{isAdmin ? '(管理员)' : '(分销商)'}
|
||||
</Typography.Text>
|
||||
</Space>
|
||||
</Dropdown>
|
||||
</Header>
|
||||
<Content style={{ margin: 24 }}>
|
||||
<div
|
||||
style={{
|
||||
padding: 24,
|
||||
minHeight: 360,
|
||||
background: colorBgContainer,
|
||||
borderRadius: borderRadiusLG,
|
||||
}}
|
||||
>
|
||||
<Outlet />
|
||||
</div>
|
||||
</Content>
|
||||
</Layout>
|
||||
</Layout>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { StrictMode } from 'react'
|
||||
import { createRoot } from 'react-dom/client'
|
||||
import App from './App'
|
||||
import './index.css'
|
||||
|
||||
createRoot(document.getElementById('root')!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
)
|
||||
@@ -0,0 +1,90 @@
|
||||
import { useEffect, useState } from 'react'
|
||||
import { Card, Col, Row, Statistic, Typography, Spin, message } from 'antd'
|
||||
import {
|
||||
SkinOutlined,
|
||||
TeamOutlined,
|
||||
ShoppingOutlined,
|
||||
DollarOutlined,
|
||||
PercentageOutlined,
|
||||
ClockCircleOutlined,
|
||||
} from '@ant-design/icons'
|
||||
import { dashboardApi } from '../api'
|
||||
import type { DashboardStats } from '../types'
|
||||
|
||||
export default function Dashboard() {
|
||||
const [stats, setStats] = useState<DashboardStats | null>(null)
|
||||
const [loading, setLoading] = useState(true)
|
||||
|
||||
useEffect(() => {
|
||||
dashboardApi
|
||||
.stats()
|
||||
.then(setStats)
|
||||
.catch((e) => message.error(e.message))
|
||||
.finally(() => setLoading(false))
|
||||
}, [])
|
||||
|
||||
if (loading) {
|
||||
return (
|
||||
<div style={{ textAlign: 'center', padding: 80 }}>
|
||||
<Spin size="large" />
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Typography.Title level={4} style={{ marginTop: 0 }}>
|
||||
数据概览
|
||||
</Typography.Title>
|
||||
<Row gutter={[16, 16]}>
|
||||
<Col xs={24} sm={12} lg={8}>
|
||||
<Card>
|
||||
<Statistic title="皮肤商品" value={stats?.skin_count ?? 0} prefix={<SkinOutlined />} />
|
||||
</Card>
|
||||
</Col>
|
||||
<Col xs={24} sm={12} lg={8}>
|
||||
<Card>
|
||||
<Statistic title="分销商" value={stats?.distributor_count ?? 0} prefix={<TeamOutlined />} />
|
||||
</Card>
|
||||
</Col>
|
||||
<Col xs={24} sm={12} lg={8}>
|
||||
<Card>
|
||||
<Statistic title="订单总数" value={stats?.order_count ?? 0} prefix={<ShoppingOutlined />} />
|
||||
</Card>
|
||||
</Col>
|
||||
<Col xs={24} sm={12} lg={8}>
|
||||
<Card>
|
||||
<Statistic
|
||||
title="成交金额"
|
||||
value={stats?.total_sales ?? 0}
|
||||
precision={2}
|
||||
prefix={<DollarOutlined />}
|
||||
suffix="元"
|
||||
/>
|
||||
</Card>
|
||||
</Col>
|
||||
<Col xs={24} sm={12} lg={8}>
|
||||
<Card>
|
||||
<Statistic
|
||||
title="累计佣金"
|
||||
value={stats?.total_commission ?? 0}
|
||||
precision={2}
|
||||
prefix={<PercentageOutlined />}
|
||||
suffix="元"
|
||||
/>
|
||||
</Card>
|
||||
</Col>
|
||||
<Col xs={24} sm={12} lg={8}>
|
||||
<Card>
|
||||
<Statistic
|
||||
title="待处理订单"
|
||||
value={stats?.pending_order_count ?? 0}
|
||||
prefix={<ClockCircleOutlined />}
|
||||
valueStyle={{ color: (stats?.pending_order_count ?? 0) > 0 ? '#cf1322' : undefined }}
|
||||
/>
|
||||
</Card>
|
||||
</Col>
|
||||
</Row>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,169 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import {
|
||||
Button,
|
||||
Form,
|
||||
Input,
|
||||
Modal,
|
||||
Space,
|
||||
Switch,
|
||||
Table,
|
||||
Tag,
|
||||
Typography,
|
||||
message,
|
||||
} from 'antd'
|
||||
import { PlusOutlined, ReloadOutlined } from '@ant-design/icons'
|
||||
import type { ColumnsType } from 'antd/es/table'
|
||||
import dayjs from 'dayjs'
|
||||
import { userApi } from '../api'
|
||||
import type { User } from '../types'
|
||||
|
||||
export default function Distributors() {
|
||||
const [list, setList] = useState<User[]>([])
|
||||
const [total, setTotal] = useState(0)
|
||||
const [page, setPage] = useState(1)
|
||||
const [size, setSize] = useState(10)
|
||||
const [keyword, setKeyword] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [open, setOpen] = useState(false)
|
||||
const [form] = Form.useForm()
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const data = await userApi.list({
|
||||
page,
|
||||
size,
|
||||
keyword,
|
||||
role: 'distributor',
|
||||
})
|
||||
setList(data.list || [])
|
||||
setTotal(data.total)
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '加载失败')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [page, size, keyword])
|
||||
|
||||
useEffect(() => {
|
||||
load()
|
||||
}, [load])
|
||||
|
||||
const onSubmit = async () => {
|
||||
const values = await form.validateFields()
|
||||
try {
|
||||
await userApi.create({ ...values, role: 'distributor' })
|
||||
message.success('创建成功')
|
||||
setOpen(false)
|
||||
form.resetFields()
|
||||
load()
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '创建失败')
|
||||
}
|
||||
}
|
||||
|
||||
const toggleStatus = async (record: User, checked: boolean) => {
|
||||
try {
|
||||
await userApi.updateStatus(record.id, checked ? 1 : 0)
|
||||
message.success('状态已更新')
|
||||
load()
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '更新失败')
|
||||
}
|
||||
}
|
||||
|
||||
const columns: ColumnsType<User> = [
|
||||
{ title: 'ID', dataIndex: 'id', width: 70 },
|
||||
{ title: '用户名', dataIndex: 'username' },
|
||||
{ title: '昵称', dataIndex: 'nickname' },
|
||||
{
|
||||
title: '邀请码',
|
||||
dataIndex: 'invite_code',
|
||||
render: (v: string) => <Tag color="blue">{v}</Tag>,
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
dataIndex: 'status',
|
||||
width: 120,
|
||||
render: (v: number, record) => (
|
||||
<Switch
|
||||
checkedChildren="启用"
|
||||
unCheckedChildren="禁用"
|
||||
checked={v === 1}
|
||||
onChange={(checked) => toggleStatus(record, checked)}
|
||||
/>
|
||||
),
|
||||
},
|
||||
{
|
||||
title: '注册时间',
|
||||
dataIndex: 'created_at',
|
||||
width: 170,
|
||||
render: (v: string) => dayjs(v).format('YYYY-MM-DD HH:mm'),
|
||||
},
|
||||
]
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Space style={{ marginBottom: 16, width: '100%', justifyContent: 'space-between' }}>
|
||||
<Typography.Title level={4} style={{ margin: 0 }}>
|
||||
分销商管理
|
||||
</Typography.Title>
|
||||
<Space>
|
||||
<Input.Search
|
||||
placeholder="搜索用户名/昵称"
|
||||
allowClear
|
||||
onSearch={(v) => {
|
||||
setPage(1)
|
||||
setKeyword(v)
|
||||
}}
|
||||
style={{ width: 220 }}
|
||||
/>
|
||||
<Button icon={<ReloadOutlined />} onClick={load}>
|
||||
刷新
|
||||
</Button>
|
||||
<Button
|
||||
type="primary"
|
||||
icon={<PlusOutlined />}
|
||||
onClick={() => {
|
||||
form.resetFields()
|
||||
setOpen(true)
|
||||
}}
|
||||
>
|
||||
新增分销商
|
||||
</Button>
|
||||
</Space>
|
||||
</Space>
|
||||
|
||||
<Table
|
||||
rowKey="id"
|
||||
loading={loading}
|
||||
columns={columns}
|
||||
dataSource={list}
|
||||
pagination={{
|
||||
current: page,
|
||||
pageSize: size,
|
||||
total,
|
||||
showSizeChanger: true,
|
||||
onChange: (p, s) => {
|
||||
setPage(p)
|
||||
setSize(s)
|
||||
},
|
||||
}}
|
||||
/>
|
||||
|
||||
<Modal title="新增分销商" open={open} onOk={onSubmit} onCancel={() => setOpen(false)} destroyOnClose>
|
||||
<Form form={form} layout="vertical" style={{ marginTop: 16 }}>
|
||||
<Form.Item name="username" label="用户名" rules={[{ required: true, min: 3 }]}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="nickname" label="昵称">
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="password" label="密码" rules={[{ required: true, min: 6 }]}>
|
||||
<Input.Password />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import { useState } from 'react'
|
||||
import { useNavigate, Link } from 'react-router-dom'
|
||||
import { Card, Form, Input, Button, Typography, message, Space } from 'antd'
|
||||
import { UserOutlined, LockOutlined } from '@ant-design/icons'
|
||||
import { useAuth } from '../store/auth'
|
||||
|
||||
export default function Login() {
|
||||
const { login } = useAuth()
|
||||
const navigate = useNavigate()
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const onFinish = async (values: { username: string; password: string }) => {
|
||||
setLoading(true)
|
||||
try {
|
||||
await login(values.username, values.password)
|
||||
message.success('登录成功')
|
||||
navigate('/')
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '登录失败')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
minHeight: '100vh',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
background: 'linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%)',
|
||||
}}
|
||||
>
|
||||
<Card style={{ width: 400, boxShadow: '0 8px 32px rgba(0,0,0,0.3)' }}>
|
||||
<Space direction="vertical" size="middle" style={{ width: '100%' }}>
|
||||
<div style={{ textAlign: 'center' }}>
|
||||
<Typography.Title level={3} style={{ marginBottom: 4 }}>
|
||||
游戏皮肤分销系统
|
||||
</Typography.Title>
|
||||
<Typography.Text type="secondary">登录后台管理</Typography.Text>
|
||||
</div>
|
||||
<Form layout="vertical" onFinish={onFinish} initialValues={{ username: 'admin', password: 'admin123' }}>
|
||||
<Form.Item name="username" rules={[{ required: true, message: '请输入用户名' }]}>
|
||||
<Input prefix={<UserOutlined />} placeholder="用户名" size="large" />
|
||||
</Form.Item>
|
||||
<Form.Item name="password" rules={[{ required: true, message: '请输入密码' }]}>
|
||||
<Input.Password prefix={<LockOutlined />} placeholder="密码" size="large" />
|
||||
</Form.Item>
|
||||
<Form.Item>
|
||||
<Button type="primary" htmlType="submit" loading={loading} block size="large">
|
||||
登录
|
||||
</Button>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
<div style={{ textAlign: 'center' }}>
|
||||
<Typography.Text type="secondary">
|
||||
还没有账号? <Link to="/register">注册分销商</Link>
|
||||
</Typography.Text>
|
||||
</div>
|
||||
<Typography.Paragraph type="secondary" style={{ fontSize: 12, marginBottom: 0, textAlign: 'center' }}>
|
||||
默认管理员:admin / admin123
|
||||
</Typography.Paragraph>
|
||||
</Space>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import {
|
||||
Button,
|
||||
Select,
|
||||
Space,
|
||||
Table,
|
||||
Tag,
|
||||
Typography,
|
||||
message,
|
||||
} from 'antd'
|
||||
import { ReloadOutlined } from '@ant-design/icons'
|
||||
import type { ColumnsType } from 'antd/es/table'
|
||||
import dayjs from 'dayjs'
|
||||
import { orderApi } from '../api'
|
||||
import { useAuth } from '../store/auth'
|
||||
import type { Order } from '../types'
|
||||
|
||||
const statusMap: Record<string, { color: string; text: string }> = {
|
||||
pending: { color: 'orange', text: '待支付' },
|
||||
paid: { color: 'blue', text: '已支付' },
|
||||
delivered: { color: 'green', text: '已交付' },
|
||||
cancelled: { color: 'default', text: '已取消' },
|
||||
}
|
||||
|
||||
export default function Orders() {
|
||||
const { isAdmin } = useAuth()
|
||||
const [list, setList] = useState<Order[]>([])
|
||||
const [total, setTotal] = useState(0)
|
||||
const [page, setPage] = useState(1)
|
||||
const [size, setSize] = useState(10)
|
||||
const [status, setStatus] = useState<string | undefined>()
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const data = await orderApi.list({ page, size, status })
|
||||
setList(data.list || [])
|
||||
setTotal(data.total)
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '加载失败')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [page, size, status])
|
||||
|
||||
useEffect(() => {
|
||||
load()
|
||||
}, [load])
|
||||
|
||||
const changeStatus = async (id: number, next: string) => {
|
||||
try {
|
||||
await orderApi.updateStatus(id, next)
|
||||
message.success('状态已更新')
|
||||
load()
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '更新失败')
|
||||
}
|
||||
}
|
||||
|
||||
const columns: ColumnsType<Order> = [
|
||||
{ title: '订单号', dataIndex: 'order_no', width: 200 },
|
||||
{
|
||||
title: '皮肤',
|
||||
dataIndex: ['skin', 'name'],
|
||||
render: (_, r) => r.skin?.name || `#${r.skin_id}`,
|
||||
},
|
||||
{
|
||||
title: '分销商',
|
||||
dataIndex: ['distributor', 'nickname'],
|
||||
render: (_, r) => r.distributor?.nickname || r.distributor?.username || `#${r.distributor_id}`,
|
||||
},
|
||||
{ title: '买家', dataIndex: 'buyer_name', width: 100 },
|
||||
{
|
||||
title: '金额',
|
||||
dataIndex: 'amount',
|
||||
width: 100,
|
||||
render: (v: number) => `¥${v.toFixed(2)}`,
|
||||
},
|
||||
{
|
||||
title: '佣金',
|
||||
dataIndex: 'commission_amt',
|
||||
width: 100,
|
||||
render: (v: number) => `¥${v.toFixed(2)}`,
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
dataIndex: 'status',
|
||||
width: 100,
|
||||
render: (v: string) => {
|
||||
const s = statusMap[v] || { color: 'default', text: v }
|
||||
return <Tag color={s.color}>{s.text}</Tag>
|
||||
},
|
||||
},
|
||||
{
|
||||
title: '时间',
|
||||
dataIndex: 'created_at',
|
||||
width: 170,
|
||||
render: (v: string) => dayjs(v).format('YYYY-MM-DD HH:mm'),
|
||||
},
|
||||
]
|
||||
|
||||
if (isAdmin) {
|
||||
columns.push({
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
width: 220,
|
||||
render: (_, record) => (
|
||||
<Space>
|
||||
{record.status === 'pending' && (
|
||||
<>
|
||||
<Button type="link" size="small" onClick={() => changeStatus(record.id, 'paid')}>
|
||||
标记已付
|
||||
</Button>
|
||||
<Button type="link" size="small" danger onClick={() => changeStatus(record.id, 'cancelled')}>
|
||||
取消
|
||||
</Button>
|
||||
</>
|
||||
)}
|
||||
{record.status === 'paid' && (
|
||||
<Button type="link" size="small" onClick={() => changeStatus(record.id, 'delivered')}>
|
||||
标记交付
|
||||
</Button>
|
||||
)}
|
||||
</Space>
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Space style={{ marginBottom: 16, width: '100%', justifyContent: 'space-between' }}>
|
||||
<Typography.Title level={4} style={{ margin: 0 }}>
|
||||
订单管理
|
||||
</Typography.Title>
|
||||
<Space>
|
||||
<Select
|
||||
allowClear
|
||||
placeholder="订单状态"
|
||||
style={{ width: 140 }}
|
||||
value={status}
|
||||
onChange={(v) => {
|
||||
setPage(1)
|
||||
setStatus(v)
|
||||
}}
|
||||
options={[
|
||||
{ value: 'pending', label: '待支付' },
|
||||
{ value: 'paid', label: '已支付' },
|
||||
{ value: 'delivered', label: '已交付' },
|
||||
{ value: 'cancelled', label: '已取消' },
|
||||
]}
|
||||
/>
|
||||
<Button icon={<ReloadOutlined />} onClick={load}>
|
||||
刷新
|
||||
</Button>
|
||||
</Space>
|
||||
</Space>
|
||||
|
||||
<Table
|
||||
rowKey="id"
|
||||
loading={loading}
|
||||
columns={columns}
|
||||
dataSource={list}
|
||||
pagination={{
|
||||
current: page,
|
||||
pageSize: size,
|
||||
total,
|
||||
showSizeChanger: true,
|
||||
onChange: (p, s) => {
|
||||
setPage(p)
|
||||
setSize(s)
|
||||
},
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
import { useState } from 'react'
|
||||
import { useNavigate, Link } from 'react-router-dom'
|
||||
import { Card, Form, Input, Button, Typography, message, Space } from 'antd'
|
||||
import { UserOutlined, LockOutlined } from '@ant-design/icons'
|
||||
import { authApi } from '../api'
|
||||
|
||||
export default function Register() {
|
||||
const navigate = useNavigate()
|
||||
const [loading, setLoading] = useState(false)
|
||||
|
||||
const onFinish = async (values: {
|
||||
username: string
|
||||
password: string
|
||||
nickname?: string
|
||||
}) => {
|
||||
setLoading(true)
|
||||
try {
|
||||
await authApi.register(values)
|
||||
message.success('注册成功,请登录')
|
||||
navigate('/login')
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '注册失败')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<div
|
||||
style={{
|
||||
minHeight: '100vh',
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
background: 'linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%)',
|
||||
}}
|
||||
>
|
||||
<Card style={{ width: 400, boxShadow: '0 8px 32px rgba(0,0,0,0.3)' }}>
|
||||
<Space direction="vertical" size="middle" style={{ width: '100%' }}>
|
||||
<div style={{ textAlign: 'center' }}>
|
||||
<Typography.Title level={3} style={{ marginBottom: 4 }}>
|
||||
注册分销商
|
||||
</Typography.Title>
|
||||
<Typography.Text type="secondary">创建分销账号</Typography.Text>
|
||||
</div>
|
||||
<Form layout="vertical" onFinish={onFinish}>
|
||||
<Form.Item name="username" rules={[{ required: true, min: 3, message: '用户名至少3位' }]}>
|
||||
<Input prefix={<UserOutlined />} placeholder="用户名" size="large" />
|
||||
</Form.Item>
|
||||
<Form.Item name="nickname">
|
||||
<Input prefix={<UserOutlined />} placeholder="昵称(可选)" size="large" />
|
||||
</Form.Item>
|
||||
<Form.Item name="password" rules={[{ required: true, min: 6, message: '密码至少6位' }]}>
|
||||
<Input.Password prefix={<LockOutlined />} placeholder="密码" size="large" />
|
||||
</Form.Item>
|
||||
<Form.Item>
|
||||
<Button type="primary" htmlType="submit" loading={loading} block size="large">
|
||||
注册
|
||||
</Button>
|
||||
</Form.Item>
|
||||
</Form>
|
||||
<div style={{ textAlign: 'center' }}>
|
||||
<Typography.Text type="secondary">
|
||||
已有账号? <Link to="/login">去登录</Link>
|
||||
</Typography.Text>
|
||||
</div>
|
||||
</Space>
|
||||
</Card>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
import { useCallback, useEffect, useState } from 'react'
|
||||
import {
|
||||
Button,
|
||||
Form,
|
||||
Input,
|
||||
InputNumber,
|
||||
Modal,
|
||||
Popconfirm,
|
||||
Select,
|
||||
Space,
|
||||
Table,
|
||||
Tag,
|
||||
Typography,
|
||||
message,
|
||||
} from 'antd'
|
||||
import { PlusOutlined, ReloadOutlined } from '@ant-design/icons'
|
||||
import type { ColumnsType } from 'antd/es/table'
|
||||
import { skinApi, orderApi } from '../api'
|
||||
import { useAuth } from '../store/auth'
|
||||
import type { Skin } from '../types'
|
||||
|
||||
export default function Skins() {
|
||||
const { isAdmin } = useAuth()
|
||||
const [list, setList] = useState<Skin[]>([])
|
||||
const [total, setTotal] = useState(0)
|
||||
const [page, setPage] = useState(1)
|
||||
const [size, setSize] = useState(10)
|
||||
const [keyword, setKeyword] = useState('')
|
||||
const [loading, setLoading] = useState(false)
|
||||
const [open, setOpen] = useState(false)
|
||||
const [editing, setEditing] = useState<Skin | null>(null)
|
||||
const [form] = Form.useForm()
|
||||
|
||||
const load = useCallback(async () => {
|
||||
setLoading(true)
|
||||
try {
|
||||
const data = await skinApi.list({ page, size, keyword })
|
||||
setList(data.list || [])
|
||||
setTotal(data.total)
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '加载失败')
|
||||
} finally {
|
||||
setLoading(false)
|
||||
}
|
||||
}, [page, size, keyword])
|
||||
|
||||
useEffect(() => {
|
||||
load()
|
||||
}, [load])
|
||||
|
||||
const openCreate = () => {
|
||||
setEditing(null)
|
||||
form.resetFields()
|
||||
form.setFieldsValue({ stock: -1, commission: 0.1, status: 1 })
|
||||
setOpen(true)
|
||||
}
|
||||
|
||||
const openEdit = (record: Skin) => {
|
||||
setEditing(record)
|
||||
form.setFieldsValue(record)
|
||||
setOpen(true)
|
||||
}
|
||||
|
||||
const onSubmit = async () => {
|
||||
const values = await form.validateFields()
|
||||
try {
|
||||
if (editing) {
|
||||
await skinApi.update(editing.id, values)
|
||||
message.success('更新成功')
|
||||
} else {
|
||||
await skinApi.create(values)
|
||||
message.success('创建成功')
|
||||
}
|
||||
setOpen(false)
|
||||
load()
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '操作失败')
|
||||
}
|
||||
}
|
||||
|
||||
const onDelete = async (id: number) => {
|
||||
try {
|
||||
await skinApi.remove(id)
|
||||
message.success('已删除')
|
||||
load()
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '删除失败')
|
||||
}
|
||||
}
|
||||
|
||||
const onOrder = async (skin: Skin) => {
|
||||
try {
|
||||
await orderApi.create({ skin_id: skin.id, buyer_name: '演示买家' })
|
||||
message.success('下单成功')
|
||||
} catch (e) {
|
||||
message.error(e instanceof Error ? e.message : '下单失败')
|
||||
}
|
||||
}
|
||||
|
||||
const columns: ColumnsType<Skin> = [
|
||||
{ title: 'ID', dataIndex: 'id', width: 70 },
|
||||
{ title: '名称', dataIndex: 'name' },
|
||||
{ title: '游戏', dataIndex: 'game', width: 120 },
|
||||
{ title: '分类', dataIndex: 'category', width: 100 },
|
||||
{
|
||||
title: '售价',
|
||||
dataIndex: 'price',
|
||||
width: 100,
|
||||
render: (v: number) => `¥${v.toFixed(2)}`,
|
||||
},
|
||||
{
|
||||
title: '佣金比例',
|
||||
dataIndex: 'commission',
|
||||
width: 100,
|
||||
render: (v: number) => `${(v * 100).toFixed(0)}%`,
|
||||
},
|
||||
{
|
||||
title: '库存',
|
||||
dataIndex: 'stock',
|
||||
width: 80,
|
||||
render: (v: number) => (v < 0 ? '无限' : v),
|
||||
},
|
||||
{
|
||||
title: '状态',
|
||||
dataIndex: 'status',
|
||||
width: 90,
|
||||
render: (v: number) =>
|
||||
v === 1 ? <Tag color="green">上架</Tag> : <Tag>下架</Tag>,
|
||||
},
|
||||
{
|
||||
title: '操作',
|
||||
key: 'action',
|
||||
width: isAdmin ? 200 : 100,
|
||||
render: (_, record) => (
|
||||
<Space>
|
||||
{isAdmin ? (
|
||||
<>
|
||||
<Button type="link" size="small" onClick={() => openEdit(record)}>
|
||||
编辑
|
||||
</Button>
|
||||
<Popconfirm title="确认删除?" onConfirm={() => onDelete(record.id)}>
|
||||
<Button type="link" size="small" danger>
|
||||
删除
|
||||
</Button>
|
||||
</Popconfirm>
|
||||
</>
|
||||
) : (
|
||||
<Button type="link" size="small" disabled={record.status !== 1} onClick={() => onOrder(record)}>
|
||||
下单
|
||||
</Button>
|
||||
)}
|
||||
</Space>
|
||||
),
|
||||
},
|
||||
]
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Space style={{ marginBottom: 16, width: '100%', justifyContent: 'space-between' }}>
|
||||
<Typography.Title level={4} style={{ margin: 0 }}>
|
||||
皮肤商品
|
||||
</Typography.Title>
|
||||
<Space>
|
||||
<Input.Search
|
||||
placeholder="搜索名称"
|
||||
allowClear
|
||||
onSearch={(v) => {
|
||||
setPage(1)
|
||||
setKeyword(v)
|
||||
}}
|
||||
style={{ width: 220 }}
|
||||
/>
|
||||
<Button icon={<ReloadOutlined />} onClick={load}>
|
||||
刷新
|
||||
</Button>
|
||||
{isAdmin && (
|
||||
<Button type="primary" icon={<PlusOutlined />} onClick={openCreate}>
|
||||
新增皮肤
|
||||
</Button>
|
||||
)}
|
||||
</Space>
|
||||
</Space>
|
||||
|
||||
<Table
|
||||
rowKey="id"
|
||||
loading={loading}
|
||||
columns={columns}
|
||||
dataSource={list}
|
||||
pagination={{
|
||||
current: page,
|
||||
pageSize: size,
|
||||
total,
|
||||
showSizeChanger: true,
|
||||
onChange: (p, s) => {
|
||||
setPage(p)
|
||||
setSize(s)
|
||||
},
|
||||
}}
|
||||
/>
|
||||
|
||||
<Modal
|
||||
title={editing ? '编辑皮肤' : '新增皮肤'}
|
||||
open={open}
|
||||
onOk={onSubmit}
|
||||
onCancel={() => setOpen(false)}
|
||||
destroyOnClose
|
||||
width={560}
|
||||
>
|
||||
<Form form={form} layout="vertical" style={{ marginTop: 16 }}>
|
||||
<Form.Item name="name" label="名称" rules={[{ required: true }]}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Space style={{ width: '100%' }} size="middle">
|
||||
<Form.Item name="game" label="游戏" style={{ width: 240 }}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
<Form.Item name="category" label="分类" style={{ width: 240 }}>
|
||||
<Input />
|
||||
</Form.Item>
|
||||
</Space>
|
||||
<Space style={{ width: '100%' }} size="middle">
|
||||
<Form.Item name="price" label="售价" rules={[{ required: true }]} style={{ width: 160 }}>
|
||||
<InputNumber min={0} precision={2} style={{ width: '100%' }} />
|
||||
</Form.Item>
|
||||
<Form.Item name="cost_price" label="成本价" style={{ width: 160 }}>
|
||||
<InputNumber min={0} precision={2} style={{ width: '100%' }} />
|
||||
</Form.Item>
|
||||
<Form.Item name="commission" label="佣金比例" style={{ width: 160 }}>
|
||||
<InputNumber min={0} max={1} step={0.01} style={{ width: '100%' }} />
|
||||
</Form.Item>
|
||||
</Space>
|
||||
<Space style={{ width: '100%' }} size="middle">
|
||||
<Form.Item name="stock" label="库存(-1无限)" style={{ width: 240 }}>
|
||||
<InputNumber style={{ width: '100%' }} />
|
||||
</Form.Item>
|
||||
<Form.Item name="status" label="状态" style={{ width: 240 }}>
|
||||
<Select
|
||||
options={[
|
||||
{ value: 1, label: '上架' },
|
||||
{ value: 0, label: '下架' },
|
||||
]}
|
||||
/>
|
||||
</Form.Item>
|
||||
</Space>
|
||||
<Form.Item name="description" label="描述">
|
||||
<Input.TextArea rows={3} />
|
||||
</Form.Item>
|
||||
</Form>
|
||||
</Modal>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
useMemo,
|
||||
useState,
|
||||
type ReactNode,
|
||||
} from 'react'
|
||||
import type { User } from '../types'
|
||||
import { authApi } from '../api'
|
||||
|
||||
interface AuthState {
|
||||
token: string | null
|
||||
user: User | null
|
||||
login: (username: string, password: string) => Promise<void>
|
||||
logout: () => void
|
||||
refreshProfile: () => Promise<void>
|
||||
isAdmin: boolean
|
||||
}
|
||||
|
||||
const AuthContext = createContext<AuthState | null>(null)
|
||||
|
||||
function loadUser(): User | null {
|
||||
try {
|
||||
const raw = localStorage.getItem('user')
|
||||
return raw ? (JSON.parse(raw) as User) : null
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const [token, setToken] = useState<string | null>(() => localStorage.getItem('token'))
|
||||
const [user, setUser] = useState<User | null>(() => loadUser())
|
||||
|
||||
const login = useCallback(async (username: string, password: string) => {
|
||||
const result = await authApi.login(username, password)
|
||||
localStorage.setItem('token', result.token)
|
||||
localStorage.setItem('user', JSON.stringify(result.user))
|
||||
setToken(result.token)
|
||||
setUser(result.user)
|
||||
}, [])
|
||||
|
||||
const logout = useCallback(() => {
|
||||
localStorage.removeItem('token')
|
||||
localStorage.removeItem('user')
|
||||
setToken(null)
|
||||
setUser(null)
|
||||
}, [])
|
||||
|
||||
const refreshProfile = useCallback(async () => {
|
||||
const profile = await authApi.profile()
|
||||
localStorage.setItem('user', JSON.stringify(profile))
|
||||
setUser(profile)
|
||||
}, [])
|
||||
|
||||
const value = useMemo(
|
||||
() => ({
|
||||
token,
|
||||
user,
|
||||
login,
|
||||
logout,
|
||||
refreshProfile,
|
||||
isAdmin: user?.role === 'admin',
|
||||
}),
|
||||
[token, user, login, logout, refreshProfile],
|
||||
)
|
||||
|
||||
return <AuthContext.Provider value={value}>{children}</AuthContext.Provider>
|
||||
}
|
||||
|
||||
export function useAuth() {
|
||||
const ctx = useContext(AuthContext)
|
||||
if (!ctx) throw new Error('useAuth must be used within AuthProvider')
|
||||
return ctx
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
export interface User {
|
||||
id: number
|
||||
username: string
|
||||
nickname: string
|
||||
role: 'admin' | 'distributor'
|
||||
status: number
|
||||
invite_code: string
|
||||
parent_id?: number | null
|
||||
created_at: string
|
||||
}
|
||||
|
||||
export interface Skin {
|
||||
id: number
|
||||
name: string
|
||||
game: string
|
||||
category: string
|
||||
cover_url: string
|
||||
price: number
|
||||
cost_price: number
|
||||
commission: number
|
||||
stock: number
|
||||
status: number
|
||||
description: string
|
||||
created_at: string
|
||||
}
|
||||
|
||||
export interface Order {
|
||||
id: number
|
||||
order_no: string
|
||||
skin_id: number
|
||||
skin?: Skin
|
||||
distributor_id: number
|
||||
distributor?: User
|
||||
buyer_name: string
|
||||
amount: number
|
||||
commission_amt: number
|
||||
status: string
|
||||
remark: string
|
||||
created_at: string
|
||||
}
|
||||
|
||||
export interface DashboardStats {
|
||||
skin_count: number
|
||||
distributor_count: number
|
||||
order_count: number
|
||||
total_sales: number
|
||||
total_commission: number
|
||||
pending_order_count: number
|
||||
}
|
||||
|
||||
export interface PageResult<T> {
|
||||
list: T[]
|
||||
total: number
|
||||
page: number
|
||||
size: number
|
||||
}
|
||||
|
||||
export interface ApiResponse<T = unknown> {
|
||||
code: number
|
||||
message: string
|
||||
data: T
|
||||
}
|
||||
|
||||
export interface LoginResult {
|
||||
token: string
|
||||
user: User
|
||||
}
|
||||
Reference in New Issue
Block a user