重构自学模块

This commit is contained in:
2025-06-15 14:40:47 +08:00
parent 70dd3540c9
commit 73b86c644a
36 changed files with 1118 additions and 848 deletions

View File

@@ -0,0 +1,43 @@
<script lang="ts" setup>
import { deleteTutorial } from "~/admin/api"
interface Props {
tutorialID: number
}
const props = defineProps<Props>()
const emit = defineEmits(["deleted"])
const router = useRouter()
const message = useMessage()
function goEdit() {
router.push({
name: "admin tutorial edit",
params: { tutorialID: props.tutorialID },
})
}
async function handleDelete() {
try {
await deleteTutorial(props.tutorialID)
message.success("删除成功")
emit("deleted")
} catch (err: any) {
message.error(err.data)
}
}
</script>
<template>
<n-flex>
<n-button size="small" type="success" secondary @click="goEdit">
编辑
</n-button>
<n-popconfirm @positive-click="handleDelete">
<template #trigger>
<n-button size="small" type="error" secondary>删除</n-button>
</template>
确定删除这个教程吗
</n-popconfirm>
</n-flex>
</template>
<style scoped></style>

View File

@@ -0,0 +1,125 @@
<script lang="ts" setup>
import CodeEditor from "~/shared/components/CodeEditor.vue"
import MarkdownEditor from "~/shared/components/MarkdownEditor.vue"
import { Tutorial } from "~/utils/types"
import { createTutorial, getTutorial, updateTutorial } from "../api"
interface Props {
tutorialID?: string
}
const route = useRoute()
const router = useRouter()
const message = useMessage()
const props = defineProps<Props>()
const tutorial = reactive<Tutorial>({
id: 0,
title: "",
content: "",
code: "",
is_public: false,
order: 0,
type: "python", // 默认选择 Python
})
const typeOptions = [
{ label: "Python", value: "python" },
{ label: "C 语言", value: "c" },
]
async function init() {
if (!props.tutorialID) {
return
}
const id = parseInt(route.params.tutorialID as string)
const data = await getTutorial(id)
tutorial.id = data.id
tutorial.title = data.title
tutorial.content = data.content
tutorial.code = data.code || ""
tutorial.is_public = data.is_public
tutorial.order = data.order
tutorial.type = data.type || "python"
}
async function submit() {
if (!tutorial.title || !tutorial.content) {
message.error("标题和正文必填")
return
}
try {
if (route.name === "admin tutorial create") {
await createTutorial({
title: tutorial.title,
content: tutorial.content,
code: tutorial.code,
is_public: tutorial.is_public,
order: tutorial.order,
type: tutorial.type,
})
message.success("成功新建教程 💐")
} else {
await updateTutorial(tutorial)
message.success("修改已保存")
}
router.push({ name: "admin tutorial list" })
} catch (err: any) {
message.error(err.data)
}
}
onMounted(init)
</script>
<template>
<h2 class="title">
{{ route.name === "admin tutorial create" ? "新建教程" : "编辑教程" }}
</h2>
<n-form inline>
<n-form-item label="标题">
<n-input class="contestTitle" v-model:value="tutorial.title" />
</n-form-item>
<n-form-item label="语言">
<n-select
v-model:value="tutorial.type"
:options="typeOptions"
class="select"
/>
</n-form-item>
<n-form-item label="顺序">
<n-input-number style="width: 100px" v-model:value="tutorial.order" :min="0" />
</n-form-item>
<n-form-item label="可见">
<n-switch v-model:value="tutorial.is_public" />
</n-form-item>
<n-form-item>
<n-button type="primary" @click="submit">保存</n-button>
</n-form-item>
</n-form>
<n-tabs type="line" animated>
<n-tab-pane name="content" tab="教程内容">
<MarkdownEditor v-model:value="tutorial.content" />
</n-tab-pane>
<n-tab-pane name="code" tab="示例代码">
<CodeEditor
v-model:value="tutorial.code"
:language="tutorial.type === 'python' ? 'Python3' : 'C'"
height="400px"
/>
</n-tab-pane>
</n-tabs>
</template>
<style scoped>
.title {
margin-top: 0;
}
.select {
width: 100px;
}
.contestTitle {
width: 400px;
}
</style>

100
src/admin/tutorial/list.vue Normal file
View File

@@ -0,0 +1,100 @@
<script setup lang="ts">
import { NSwitch } from "naive-ui"
import { parseTime } from "~/utils/functions"
import { Tutorial } from "~/utils/types"
import { getTutorialList, setTutorialVisibility } from "../api"
import Actions from "./components/Actions.vue"
const tutorials = ref<{ [key: string]: Tutorial[] }>({
python: [],
c: []
})
const message = useMessage()
const activeTab = ref('python')
const columns: DataTableColumn<Tutorial>[] = [
{ title: "ID", key: "id", width: 60 },
{ title: "标题", key: "title", minWidth: 300 },
{
title: "创建时间",
key: "created_at",
width: 180,
render: (row) => parseTime(row.created_at!, "YYYY-MM-DD HH:mm:ss"),
},
{
title: "顺序",
key: "order",
},
{
title: "作者",
key: "created_by",
render: (row) => row.created_by?.username,
width: 80,
},
{
title: "可见",
key: "is_public",
render: (row) =>
h(NSwitch, {
value: row.is_public,
size: "small",
rubberBand: false,
onUpdateValue: () => toggleVisible(row),
}),
},
{
title: "操作",
key: "actions",
width: 140,
render: (row) =>
h(Actions, { tutorialID: row.id, onDeleted: listTutorials }),
},
]
async function toggleVisible(tutorial: Tutorial) {
tutorial.is_public = !tutorial.is_public
try {
await setTutorialVisibility(tutorial.id, tutorial.is_public)
message.success("更新成功")
} catch (err: any) {
message.error(err.data)
tutorial.is_public = !tutorial.is_public
}
}
async function listTutorials() {
tutorials.value = await getTutorialList()
}
onMounted(listTutorials)
</script>
<template>
<n-flex align="center" class="titleWrapper">
<h2 class="title">教程列表</h2>
<n-button
type="primary"
@click="$router.push({ name: 'admin tutorial create' })"
>
新建
</n-button>
</n-flex>
<n-tabs v-model:value="activeTab" type="line" animated>
<n-tab-pane name="python" tab="Python">
<n-data-table striped :columns="columns" :data="tutorials.python" />
</n-tab-pane>
<n-tab-pane name="c" tab="C 语言">
<n-data-table striped :columns="columns" :data="tutorials.c" />
</n-tab-pane>
</n-tabs>
</template>
<style scoped>
.titleWrapper {
margin-bottom: 16px;
}
.title {
margin: 0;
}
</style>