Initial commit

This commit is contained in:
南宁网宿科技
2023-06-03 23:57:39 +08:00
commit 13bed2bafb
1017 changed files with 175796 additions and 0 deletions
@@ -0,0 +1,467 @@
<!-- 用户编辑弹窗 -->
<template>
<ele-modal
width="980px"
:visible="visible"
:confirm-loading="loading"
:maxable="maxAble"
:title="isUpdate ? '编辑文章' : '添加文章'"
:body-style="{ paddingBottom: '8px' }"
@update:visible="updateVisible"
:maskClosable="false"
@ok="save"
>
<a-form
ref="formRef"
:model="form"
:rules="rules"
:label-col="{ md: { span: 8 }, sm: { span: 24 } }"
:wrapper-col="{ md: { span: 24 }, sm: { span: 24 } }"
layout="vertical"
>
<a-form-item label="文章分类" name="categoryId">
<category-select
:data="categoryList"
placeholder="请选择文章分类"
v-model:value="form.categoryId"
/>
</a-form-item>
<a-form-item label="文章标题" name="title">
<a-input
allow-clear
:maxlength="20"
placeholder="请输入文章标题"
v-model:value="form.title"
/>
</a-form-item>
<a-form-item label="封面图" name="images">
<ele-image-upload
v-model:value="images"
:limit="1"
:drag="true"
:upload-handler="uploadHandler"
@upload="onUpload"
/>
</a-form-item>
<a-form-item label="文章内容" name="content">
<!-- 编辑器 -->
<div class="content">
<tinymce-editor
v-model:value="content"
:disabled="disabled"
:init="config"
placeholder="图片直接粘贴自动上传"
@change="onChange"
@paste="onPaste"
/>
</div>
</a-form-item>
<a-form-item label="文章来源" name="source">
<source-select
v-model:value="form.source"
/>
</a-form-item>
<a-form-item label="展现方式" name="showType">
<a-radio-group v-model:value="form.showType">
<a-radio :value="10">小图模式(300 * 188)</a-radio>
<a-radio :value="20">大图模式(750 * 455)</a-radio>
</a-radio-group>
</a-form-item>
<a-form-item label="排序" name="sortNumber">
<a-input-number
:min="0"
:max="99999"
class="ele-fluid"
placeholder="请输入排序号"
v-model:value="form.sortNumber"
/>
</a-form-item>
</a-form>
</ele-modal>
</template>
<script lang="ts" setup>
import { ref, reactive, watch, computed } from "vue";
import { Form, message, Modal } from "ant-design-vue";
import { useUserStore } from "@/store/modules/user";
import type { Article } from "@/api/cms/article/model";
import { addArticle, updateArticle } from "@/api/cms/article";
import { FILE_SERVER, FILE_THUMBNAIL } from "@/config/setting";
import { ItemType } from "ele-admin-pro/es/ele-image-upload/types";
import { uploadFile } from "@/api/system/file";
import TinymceEditor from "@/components/TinymceEditor/index.vue";
import { FormInstance, Rule, RuleObject } from "ant-design-vue/es/form";
import CategorySelect from "./category-select.vue";
import { ArticleCategory } from "@/api/cms/category/model";
import SourceSelect from "../dictionary/source-select.vue";
import { Goods } from "@/api/goods/model";
import useFormData from "@/utils/use-form-data";
const useForm = Form.useForm;
const props = defineProps<{
// 弹窗是否打开
visible: boolean;
categoryId?: number;
// 修改回显的数据
data?: Article | null;
categoryList?: ArticleCategory[];
}>();
const emit = defineEmits<{
(e: "done"): void;
(e: "update:visible", visible: boolean): void;
}>();
// 是否是修改
const isUpdate = ref(false);
const disabled = ref(false);
// 是否显示最大化切换按钮
const maxAble = ref(true);
// 选项卡位置
const activeKey = ref("1");
// 已上传数据
const images = ref<ItemType[]>([]);
// 当前用户信息
const userStore = useUserStore();
const loginUser = computed(() => userStore.info ?? {});
// 编辑器内容,双向绑定
const content = ref<any>('');
const uploadImgContent = ref<string>('');
const formRef = ref<FormInstance | null>(null);
// 已上传数据, 可赋初始值用于回显
const avatar = ref(<any>[]);
// 提交状态
const loading = ref(false);
// 表单数据
const { form, resetFields, assignFields } = useFormData<Article>({
articleId: undefined,
// 封面图
image: "",
// 文章标题
title: "",
// 展现方式
showType: 10,
// 文章来源
source: undefined,
// 文章类型
categoryId: undefined,
// 文章内容
content: "",
// 虚拟阅读量
virtualViews: "",
// 实际阅读量
actualViews: "",
// 用户ID
userId: "",
// 所属门店ID
shopId: "",
// 排序
sortNumber: 100,
// 备注
comments: "",
// 状态
status: 0,
// 创建时间
createTime: "",
// 更新时间
updateTime: ""
});
/* 更新visible */
const updateVisible = (value: boolean) => {
emit("update:visible", value);
};
// 表单验证规则
const rules = reactive<Record<string, Rule[]>>({
title: [
{
required: true,
type: "string",
message: "请输入文章标题",
trigger: "blur"
}
],
content: [
{
required: true,
type: "string",
message: "请输入文章内容",
trigger: "blur",
validator: async (_rule: RuleObject, value: string) => {
if (content.value == "") {
return Promise.reject("请输入文字内容");
}
return Promise.resolve();
}
}
],
categoryId: [
{
required: true,
type: "number",
message: "请选择文章分类",
trigger: "blur"
}
],
// images: [
// {
// required: true,
// type: "string",
// message: "请上传文章封面图",
// trigger: "blur",
// validator: async (_rule: RuleObject, value: string) => {
// if (images.value.length == 0) {
// return Promise.reject('请上传封面图');
// }
// return Promise.resolve();
// }
// }
// ],
sortNumber: [
{
required: true,
type: "number",
message: "请输入排序",
trigger: "blur"
}
],
source: [
{
required: false,
type: "string",
message: "请输入文章来源",
trigger: "blur"
}
]
});
/* 上传事件 */
const uploadHandler = (file: File) => {
const item: ItemType = {
file,
uid: (file as any).uid,
name: file.name
};
if (!file.type.startsWith('image')) {
message.error('只能选择图片');
return;
}
if (file.size / 1024 / 1024 > 2) {
message.error('大小不能超过 2MB');
return;
}
onUpload(item);
};
// 上传文件
const onUpload = (item) => {
const { file } = item;
uploadFile(file)
.then((data) => {
images.value.push({
uid: data.id,
url: FILE_THUMBNAIL + data.path,
status: 'done'
});
form.image = data.path;
message.success('上传成功');
})
.catch((e) => {
message.error(e.message);
});
};
const editorRef = ref<InstanceType<typeof TinymceEditor> | null>(null);
const config = ref({
height: 500,
images_upload_handler: (blobInfo, success, error) => {
const file = blobInfo.blob();
// 使用 axios 上传,实际开发这段建议写在 api 中再调用 api
const formData = new FormData();
formData.append('file', file, file.name);
uploadFile(<File>file)
.then((result) => {
if (result.length) {
console.log(file.size / 1024);
if (file.size / 1024 / 1024 > 2) {
error('图片大小不能超过 2MB');
}
success(FILE_SERVER + result.path);
} else {
error('上传失败');
}
})
.catch((e) => {
message.error(e.message);
});
},
// 自定义文件上传(这里使用把选择的文件转成 blob 演示)
// file_picker_callback: (callback: any, _value: any, meta: any) => {
// const input = document.createElement("input");
// input.setAttribute("type", "file");
// // 设定文件可选类型
// if (meta.filetype === "image") {
// input.setAttribute("accept", "image/*");
// } else if (meta.filetype === "media") {
// input.setAttribute("accept", "video/*");
// }
// input.onchange = () => {
// const file = input.files?.[0];
// console.log(file);
// if (!file) {
// return;
// }
// if (meta.filetype === "media") {
// if (!file.type.startsWith("video/")) {
// editorRef.value?.alert({ content: "只能选择视频文件" });
// return;
// }
// }
// if (file.size / 1024 / 1024 > 20) {
// editorRef.value?.alert({ content: "大小不能超过 20MB" });
// return;
// }
// const reader = new FileReader();
// reader.onload = (e) => {
// if (e.target?.result != null) {
// const blob = new Blob([e.target.result], { type: file.type });
// callback(URL.createObjectURL(blob));
// }
// };
// reader.readAsArrayBuffer(file);
// };
// input.click();
// }
});
const onChange = (e) => {
uploadImgContent.value = e.originalEvent.value.content;
}
/* 粘贴图片上传服务器并插入编辑器 */
const onPaste = (e) => {
const items = (e.clipboardData || e.originalEvent.clipboardData).items;
console.log(items.length);
let hasFile = false;
for (let i = 0; i < items.length; i++) {
if (items[i].type.indexOf('image') !== -1) {
let file = items[i].getAsFile();
const item: ItemType = {
file,
uid: (file as any).lastModified,
name: file.name
};
console.log(item.file);
console.log(uploadImgContent.value);
uploadFile(<File>item.file)
.then((result) => {
console.log(content.value);
console.log(file);
const addPath = `<img class="content-img" src="${FILE_SERVER + result.path}">\n\r`;
content.value = content.value.replace(uploadImgContent.value,addPath)
})
.catch((e) => {
message.error(e.message);
});
hasFile = true;
}
}
if (hasFile) {
e.preventDefault();
}
}
/* 保存编辑 */
const save = () => {
if (!formRef.value) {
return;
}
formRef.value
.validate()
.then(() => {
loading.value = true;
const formData = {
...form,
content: content.value
};
const saveOrUpdate = isUpdate.value ? updateArticle : addArticle;
saveOrUpdate(formData)
.then((msg) => {
loading.value = false;
message.success(msg);
updateVisible(false);
emit("done");
})
.catch((e) => {
loading.value = false;
message.error(e.message);
});
})
.catch(() => {
});
};
watch(
() => props.visible,
(visible) => {
if (visible) {
form.categoryId = props.categoryId;
if (props.data) {
loading.value = false;
assignFields({
...props.data
});
images.value = [];
if(props.data.image){
images.value.push({
uid: 1,
url: FILE_SERVER + props.data.image,
status: 'done'
});
}
content.value = props.data.content;
isUpdate.value = true;
} else {
isUpdate.value = false;
content.value = '';
}
} else {
resetFields();
formRef.value?.clearValidate();
}
}
);
</script>
<style lang="less">
.tab-pane {
min-height: 300px;
}
.form-item-help {
font-size: 12px;
// line-height: 2;
line-height: 1.5;
padding-top: 4px;
min-height: 22px;
// margin-top: -2px;
transition: color 0.3s cubic-bezier(0.215, 0.61, 0.355, 1);
a {
margin: 0 3px;
}
.extra,
small {
color: rgba(0, 0, 0, 0.45);
font-size: 12.5px !important;
}
.extra {
margin-bottom: 4px !important;
}
}
</style>
@@ -0,0 +1,173 @@
<!-- 用户编辑弹窗 -->
<template>
<a-drawer
width="60%"
:visible="visible"
:confirm-loading="loading"
:title="'文章预览'"
:body-style="{ paddingBottom: '8px' }"
@update:visible="updateVisible"
:footer="null"
>
<a-form
:label-col="{ md: { span: 4 }, sm: { span: 24 } }"
:wrapper-col="{ md: { span: 19 }, sm: { span: 24 } }"
>
<div class="base-form" style="margin-bottom: 20px">
<div class="card-head">
<h2>{{ record.title }}</h2>
</div>
<div class="card-desc">
<a-descriptions :column="4" :labelStyle="{ color: '#999999' }">
<a-descriptions-item label="作者">
<a-tooltip :title="`${record.nickname}`">
<a-avatar :src="record.userAvatar" size="small" />
{{ record.nickname }}
</a-tooltip>
</a-descriptions-item>
<a-descriptions-item label="文章ID">
<span>{{ record.articleId }}</span>
</a-descriptions-item>
<a-descriptions-item label="来源">
<span>{{ record.source }}</span>
</a-descriptions-item>
<a-descriptions-item label="发布时间">
<span>{{ timeAgo(record.createTime) }}</span>
</a-descriptions-item>
</a-descriptions>
</div>
<div class="content">
<!-- 编辑器 -->
<tinymce-editor
v-model:value="record.content"
:disabled="disabled"
:init="config"
/>
</div>
</div>
</a-form>
</a-drawer>
</template>
<script lang="ts" setup>
import { timeAgo } from 'ele-admin-pro';
import { ref, reactive, watch } from 'vue';
import { Form } from 'ant-design-vue';
import { assignObject } from 'ele-admin-pro';
import type { Article } from '@/api/cms/article/model';
import { FILE_SERVER } from '@/config/setting';
import TinymceEditor from '@/components/TinymceEditor/index.vue';
import { getArticle } from "@/api/cms/article";
const useForm = Form.useForm;
const props = defineProps<{
// 弹窗是否打开
visible: boolean;
// 修改回显的数据
data?: Article | null;
}>();
const emit = defineEmits<{
(e: 'update:visible', visible: boolean): void;
}>();
// 文章信息
const record = reactive<Article>({
// 文章id
articleId: 0,
// 文章标题
title: '',
// 文章类型
categoryId: 0,
// 封面图
avatar: '',
// 文章内容
content: '',
// 虚拟阅读量
virtualViews: '',
// 实际阅读量
actualViews: '',
// 文章来源
source: '',
// 用户ID
userId: '',
nickname: '',
userAvatar: '',
// 所属门店ID
shopId: '',
// 排序
sortNumber: 100,
// 备注
comments: '',
// 状态
status: 0,
// 创建时间
createTime: '',
// 更新时间
updateTime: ''
});
// 请求状态
const loading = ref(true);
const { resetFields } = useForm(record);
const disabled = ref(true);
/* 更新visible */
const updateVisible = (value: boolean) => {
emit('update:visible', value);
};
/* 打开外部链接 */
const openUrl = (record) => {
window.open(record.panel);
};
const config = ref({
toolbar: false,
menubar: false,
height: 620,
darkTheme: true,
inline: true
// quickbars_insert_toolbar: false
});
watch(
() => props.visible,
(visible) => {
if (visible) {
if (props.data) {
loading.value = false;
getArticle(Number(props.data.articleId)).then((data) => {
assignObject(record, data);
});
}
} else {
resetFields();
}
}
);
</script>
<style lang="less">
.tab-pane {
min-height: 100px;
}
.card-head {
display: flex;
height: 40px;
align-items: center;
justify-content: center;
margin-bottom: 30px;
}
.base-form {
.card-desc {
color: #e3e3e3;
}
.content {
margin-top: 20px;
line-height: 2em;
font-size: 17px;
color: #333333;
}
}
</style>
@@ -0,0 +1,106 @@
<!-- 搜索表单 -->
<template>
<a-space :size="10" style="flex-wrap: wrap">
<a-button
type="primary"
class="ele-btn-icon"
@click="add"
v-permission="'cms:article:save'"
>
<template #icon>
<plus-outlined />
</template>
<span>新建</span>
</a-button>
<a-button
danger
type="primary"
class="ele-btn-icon"
v-permission="'cms:article:remove'"
@click="removeBatch"
:disabled="props.selection.length === 0"
>
<template #icon>
<delete-outlined />
</template>
<span>批量删除</span>
</a-button>
<a-input-search
allow-clear
v-model:value="searchText"
placeholder="请输入关键词"
@search="search"
@pressEnter="search"
>
<template #addonBefore>
<a-select v-model:value="type" style="width: 100px; margin: -5px -12px">
<a-select-option value="title">文章标题</a-select-option>
<a-select-option value="articleId">文章ID</a-select-option>
<a-select-option value="userId">用户ID</a-select-option>
</a-select>
</template>
</a-input-search>
</a-space>
</template>
<script lang="ts" setup>
import {
PlusOutlined,
SearchOutlined,
DeleteOutlined
} from '@ant-design/icons-vue';
import useSearch from '@/utils/use-search';
import type { ArticleParam } from '@/api/cms/article/model';
import { ref, watch } from 'vue';
import { assignObject } from 'ele-admin-pro';
const emit = defineEmits<{
(e: 'search', where?: ArticleParam): void;
(e: 'add'): void;
(e: 'remove'): void;
}>();
const props = defineProps<{
// 勾选的项目
selection?: [];
}>();
// 表单数据
const { where } = useSearch<ArticleParam>({
title: ''
});
// 搜索内容
const searchText = ref('');
const type = ref('title');
/* 搜索 */
const search = () => {
assignObject(where, {});
if (type.value == 'title') {
where.title = searchText.value;
}
if (type.value == 'articleId') {
where.articleId = Number(searchText.value);
}
if (type.value == 'userId') {
where.userId = Number(searchText.value);
}
emit('search', where);
};
/* 添加 */
const add = () => {
emit('add');
};
// 批量删除
const removeBatch = () => {
emit('remove');
};
// 监听变化
watch(
() => props.selection,
() => {}
);
</script>
@@ -0,0 +1,186 @@
<!-- 分类编辑弹窗 -->
<template>
<ele-modal
:width="620"
:visible="visible"
:confirm-loading="loading"
:title="isUpdate ? '修改分类' : '添加分类'"
:body-style="{ paddingBottom: '8px' }"
@update:visible="updateVisible"
@ok="save"
>
<a-form
:label-col="{ md: { span: 7 }, sm: { span: 24 } }"
:wrapper-col="{ md: { span: 17 }, sm: { span: 24 } }"
>
<a-row :gutter="16">
<a-col :md="12" :sm="24" :xs="24">
<a-form-item label="上级分类" v-bind="validateInfos.parentId">
<CategorySelect
:data="categoryList"
placeholder="请选择上级分类"
v-model:value="form.parentId"
/>
</a-form-item>
<a-form-item label="分类名称" v-bind="validateInfos.title">
<a-input
allow-clear
:maxlength="20"
placeholder="请输入分类名称"
v-model:value="form.title"
@pressEnter="save"
/>
</a-form-item>
<a-form-item label="排序号" v-bind="validateInfos.sortNumber">
<a-input-number
:min="0"
:max="99999"
class="ele-fluid"
placeholder="请输入排序号"
v-model:value="form.sortNumber"
/>
</a-form-item>
</a-col>
<a-col :md="12" :sm="24" :xs="24">
<a-form-item label="备注">
<a-textarea
:rows="4"
:maxlength="200"
placeholder="请输入备注"
v-model:value="form.comments"
/>
</a-form-item>
</a-col>
</a-row>
</a-form>
</ele-modal>
</template>
<script lang="ts" setup>
import { ref, reactive, watch } from 'vue';
import { Form, message } from 'ant-design-vue';
import { assignObject } from 'ele-admin-pro';
import CategorySelect from './category-select.vue';
import {
addArticleCategory,
updateArticleCategory
} from '@/api/cms/category';
import type { ArticleCategory } from '@/api/cms/category/model';
const useForm = Form.useForm;
const emit = defineEmits<{
(e: 'done'): void;
(e: 'update:visible', visible: boolean): void;
}>();
const props = defineProps<{
// 弹窗是否打开
visible: boolean;
// 修改回显的数据
data?: ArticleCategory | null;
// 分类id
categoryId?: number;
// 全部分类
categoryList: ArticleCategory[];
}>();
// 是否是修改
const isUpdate = ref(false);
// 提交状态
const loading = ref(false);
// 表单数据
const form = reactive<ArticleCategory>({
// 文章分类id
categoryId: 0,
// 文章分类
title: '',
// 上级分类
parentId: 0,
// 封面图
avatar: '',
// 用户ID
userId: '',
// 所属门店ID
shopId: '',
// 排序
sortNumber: 100,
// 备注
comments: '',
// 状态
status: ''
});
// 表单验证规则
const rules = reactive({
title: [
{
required: true,
message: '请输入分类名称',
type: 'string',
trigger: 'blur'
}
],
sortNumber: [
{
required: true,
message: '请输入排序号',
type: 'number',
trigger: 'blur'
}
]
});
const { resetFields, validate, validateInfos } = useForm(form, rules);
/* 保存编辑 */
const save = () => {
validate()
.then(() => {
loading.value = true;
const categoryData = {
...form,
parentId: form.parentId || 0
};
const saveOrUpdate = isUpdate.value
? updateArticleCategory
: addArticleCategory;
saveOrUpdate(categoryData)
.then((msg) => {
loading.value = false;
message.success(msg);
updateVisible(false);
emit('done');
})
.catch((e) => {
loading.value = false;
message.error(e.message);
});
})
.catch(() => {});
};
/* 更新visible */
const updateVisible = (value: boolean) => {
emit('update:visible', value);
};
watch(
() => props.visible,
(visible) => {
if (visible) {
if (props.data) {
assignObject(form, props.data);
isUpdate.value = true;
} else {
form.parentId = props.categoryId;
isUpdate.value = false;
}
} else {
resetFields();
}
}
);
</script>
@@ -0,0 +1,39 @@
<!-- 目录选择下拉框 -->
<template>
<a-tree-select
allow-clear
tree-default-expand-all
:placeholder="placeholder"
:value="value || undefined"
:tree-data="data"
:dropdown-style="{ maxHeight: '360px', overflow: 'auto' }"
@update:value="updateValue"
/>
</template>
<script lang="ts" setup>
import type { Article } from '@/api/cms/article/model';
const emit = defineEmits<{
(e: 'update:value', value?: number): void;
}>();
withDefaults(
defineProps<{
// 选中的数据(v-modal)
value?: number;
// 提示信息
placeholder?: string;
// 文章分类
data: Article[];
}>(),
{
placeholder: '请选择上级分类'
}
);
/* 更新选中数据 */
const updateValue = (value?: number) => {
emit('update:value', value);
};
</script>
@@ -0,0 +1,51 @@
<!-- 文章来源选择下拉框 -->
<template>
<a-select
optionFilterProp="label"
:options="data"
allow-clear
:value="value"
:placeholder="placeholder"
@update:value="updateValue"
@blur="onBlur"
@change="onChange"
/>
</template>
<script lang="ts" setup>
import { getDictionaryOptions } from '@/utils/common';
const emit = defineEmits<{
(e: 'update:value', value: string): void;
(e: 'blur'): void;
(e: 'change'): void;
}>();
withDefaults(
defineProps<{
value?: string;
placeholder?: string;
}>(),
{
placeholder: '请选择文章来源'
}
);
// 字典数据
const data = getDictionaryOptions('articleSource');
/* 更新选中数据 */
const updateValue = (value: string) => {
emit('update:value', value);
};
/* 失去焦点 */
const onBlur = () => {
emit('blur');
};
/* 选择事件 */
const onChange = (e) => {
emit('change', e);
};
</script>
+217
View File
@@ -0,0 +1,217 @@
<template>
<div class="ele-body">
<a-card :bordered="false" :body-style="{ padding: '16px' }">
<ele-split-layout
width="266px"
allow-collapse
:right-style="{ overflow: 'hidden' }"
:style="{ minHeight: 'calc(100vh - 152px)' }"
>
<div>
<ele-toolbar theme="default">
<a-space :size="10">
<span>文章分类</span>
<a-button
v-permission="'cms:article:save'"
type="primary"
:size="`small`"
class="ele-btn-icon"
@click="openEdit()"
>
<template #icon>
<plus-outlined />
</template>
</a-button>
<a-button
v-permission="'cms:article:update'"
type="primary"
:size="`small`"
:disabled="!current"
class="ele-btn-icon"
@click="openEdit(current)"
>
<template #icon>
<edit-outlined />
</template>
</a-button>
<a-button
v-permission="'cms:article:remove'"
danger
type="primary"
:size="`small`"
:disabled="!current"
class="ele-btn-icon"
@click="remove"
>
<template #icon>
<delete-outlined />
</template>
</a-button>
</a-space>
</ele-toolbar>
<div class="ele-border-split sys-category-list">
<a-tree
:tree-data="data"
v-model:expanded-keys="expandedRowKeys"
v-model:selected-keys="selectedRowKeys"
@select="onTreeSelect"
/>
</div>
</div>
<template #content>
<list
v-if="current"
:category-list="data"
:data="current"
:category-id="current.categoryId"
/>
</template>
</ele-split-layout>
</a-card>
<!-- 编辑弹窗 -->
<category-edit
v-model:visible="showEdit"
:data="editData"
:category-list="data"
:category-id="current?.categoryId"
@done="query"
/>
</div>
</template>
<script lang="ts" setup>
import { createVNode, ref } from 'vue';
import { message, Modal } from 'ant-design-vue';
import {
PlusOutlined,
EditOutlined,
DeleteOutlined,
ExclamationCircleOutlined
} from '@ant-design/icons-vue';
import { toTreeData, eachTreeData } from 'ele-admin-pro';
import List from './list.vue';
import CategoryEdit from './components/category-edit.vue';
import {
listArticleCategory,
removeArticleCategory
} from '@/api/cms/category';
import { ArticleCategory } from '@/api/cms/category/model';
// 加载状态
const loading = ref(true);
// 树形数据
const data = ref<ArticleCategory[]>([]);
// 树展开的key
const expandedRowKeys = ref<number[]>([]);
// 树选中的key
const selectedRowKeys = ref<number[]>([]);
// 选中数据
const current = ref<ArticleCategory | null>(null);
// 是否显示表单弹窗
const showEdit = ref(false);
// 编辑回显数据
const editData = ref<ArticleCategory | null>(null);
/* 查询 */
const query = () => {
loading.value = true;
listArticleCategory()
.then((list) => {
loading.value = false;
const eks: number[] = [];
list.forEach((d) => {
d.key = d.categoryId;
d.value = d.categoryId;
if (typeof d.categoryId === 'number') {
eks.push(d.categoryId);
}
});
expandedRowKeys.value = eks;
data.value = toTreeData({
data: list,
idField: 'categoryId',
parentIdField: 'parentId'
});
if (list.length) {
if (typeof list[0].categoryId === 'number') {
selectedRowKeys.value = [list[0].categoryId];
}
current.value = list[0];
} else {
selectedRowKeys.value = [];
current.value = null;
}
})
.catch((e) => {
loading.value = false;
message.error(e.message);
});
};
/* 选择数据 */
const onTreeSelect = () => {
current.value = {};
eachTreeData(data.value, (d) => {
if (
typeof d.categoryId === 'number' &&
selectedRowKeys.value.includes(d.categoryId)
) {
current.value = d;
return false;
}
});
};
/* 打开编辑弹窗 */
const openEdit = (item?: ArticleCategory | null) => {
editData.value = item ?? null;
showEdit.value = true;
};
/* 删除 */
const remove = () => {
Modal.confirm({
title: '提示',
content: '确定要删除选中的分类吗?',
icon: createVNode(ExclamationCircleOutlined),
maskClosable: true,
onOk: () => {
const hide = message.loading('请求中..', 0);
removeArticleCategory(current.value?.categoryId)
.then((msg) => {
hide();
message.success(msg);
query();
})
.catch((e) => {
hide();
message.error(e.message);
});
}
});
};
query();
</script>
<script lang="ts">
export default {
name: 'ArticleIndex'
};
</script>
<style lang="less" scoped>
.sys-category-list {
padding: 12px 6px;
height: calc(100vh - 242px);
border-width: 1px;
border-style: solid;
overflow: auto;
}
</style>
+347
View File
@@ -0,0 +1,347 @@
<template>
<!-- 表格 -->
<ele-pro-table
ref="tableRef"
row-key="articleId"
:columns="columns"
:datasource="datasource"
v-model:selection="selection"
:customRow="customRow"
height="calc(100vh - 290px)"
tool-class="ele-toolbar-form"
:scroll="{ x: 800 }"
tools-theme="default"
bordered
class="sys-category-table"
>
<template #toolbar>
<article-search
:selection="selection"
@search="reload"
@add="openEdit()"
@remove="removeBatch"
/>
</template>
<template #bodyCell="{ column, record }">
<template v-if="column.key === 'title'">
<a-tooltip :title="`文章预览 ${record.articleId}`">
<a href="#" @click="openInfo(record)">{{ record.title }}</a>
</a-tooltip>
</template>
<template v-if="column.key === 'image'">
<a-image
:width="45"
v-if="record.image"
:src="FILE_THUMBNAIL + record.image"
:preview="false"
/>
</template>
<template v-if="column.key === 'status'">
<a-tag
v-if="record.status === 0"
color="green"
@click="(checked: boolean) => editStatus(record.status, record)"
>显示</a-tag
>
<a-tag
v-if="record.status === 1"
color="red"
@click="(checked: boolean) => editStatus(record.status, record)"
>隐藏</a-tag
>
</template>
<template v-if="column.key === 'comments'">
<a-tooltip :title="record.comments" placement="topLeft">
{{ record.comments }}
</a-tooltip>
</template>
<template v-if="column.key === 'nickname'">
<a-tooltip
:title="`${record.nickname} ${toDateString(record.createTime)}`"
>
<a-avatar :src="record.userAvatar" size="small" />
{{ timeAgo(record.createTime) }}
</a-tooltip>
</template>
<template v-if="column.key === 'createTime'">
<a-tooltip :title="`${toDateString(record.createTime)}`">
<span>{{ timeAgo(record.createTime) }}</span>
</a-tooltip>
</template>
<template v-else-if="column.key === 'action'">
<a-space>
<a @click="openEdit(record)">修改</a>
<a-divider type="vertical" />
<a-popconfirm title="确定要删除此文章吗?" @confirm="remove(record)">
<a class="ele-text-danger">删除</a>
</a-popconfirm>
</a-space>
</template>
</template>
</ele-pro-table>
<!-- 编辑弹窗 -->
<article-edit
:data="current"
v-model:visible="showEdit"
:category-list="categoryList"
:category-id="categoryId"
@done="reload"
/>
<!-- 文章预览 -->
<article-info v-model:visible="showInfo" :data="current" @done="reload" />
</template>
<script lang="ts" setup>
import { createVNode, ref, watch } from 'vue';
import { message, Modal } from 'ant-design-vue';
import type { EleProTable } from 'ele-admin-pro';
import type {
DatasourceFunction,
ColumnItem
} from 'ele-admin-pro/es/ele-pro-table/types';
import { toDateString } from 'ele-admin-pro';
import ArticleSearch from './components/article-search.vue';
import ArticleEdit from './components/article-edit.vue';
import ArticleInfo from './components/article-info.vue';
import { timeAgo } from 'ele-admin-pro';
import {
pageArticle,
removeArticle,
removeBatchArticle,
updateArticleStatus
} from '@/api/cms/article';
import type { Article, ArticleParam } from '@/api/cms/article/model';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { FILE_THUMBNAIL } from '@/config/setting';
// import router from '@/router';
import { ArticleCategory } from '@/api/cms/category/model';
const props = defineProps<{
// 文章 id
categoryId?: number;
// 全部分类
categoryList: ArticleCategory[];
}>();
// 表格实例
const tableRef = ref<InstanceType<typeof EleProTable> | null>(null);
// 表格选中数据
const selection = ref<Article[]>([]);
// 表格列配置
const columns = ref<ColumnItem[]>([
{
key: 'index',
width: 48,
align: 'center',
fixed: 'left',
hideInSetting: true,
customRender: ({ index }) => index + (tableRef.value?.tableIndex ?? 0)
},
{
title: '文章标题',
dataIndex: 'title',
width: 280,
key: 'title'
},
{
title: '封面图',
dataIndex: 'image',
key: 'image',
align: 'center'
},
{
title: '点击',
dataIndex: 'actualViews',
sorter: true
},
// {
// title: '备注',
// dataIndex: 'comments',
// key: 'comments',
// ellipsis: true
// },
{
title: '排序',
dataIndex: 'sortNumber',
sorter: true
},
{
title: '状态',
key: 'status',
filters: [
{
text: '显示',
value: 0
},
{
text: '隐藏',
value: 1
}
],
filterMultiple: false,
align: 'center'
},
{
title: '创建时间',
dataIndex: 'createTime',
key: 'createTime',
sorter: true,
ellipsis: true,
customRender: ({ text }) => toDateString(text)
},
{
title: '操作',
key: 'action',
width: 100,
align: 'center',
hideInSetting: true
}
]);
// 当前编辑数据
const current = ref<Article | null>(null);
// 是否显示文章详情
const showInfo = ref(false);
// 是否显示编辑弹窗
const showEdit = ref(false);
// 表格数据源
const datasource: DatasourceFunction = ({
page,
limit,
where,
orders,
filters
}) => {
if (props.categoryId) {
where.categoryId = props.categoryId;
}
// 按条件排序
if (filters) {
where.virtualViews = filters.virtualViews;
where.actualViews = filters.actualViews;
where.sortNumber = filters.sortNumber;
where.status = filters.status;
}
return pageArticle({
...where,
...orders,
page,
limit
});
};
/* 搜索 */
const reload = (where?: ArticleParam) => {
selection.value = [];
tableRef?.value?.reload({ where: where });
};
/* 打开用户详情弹窗 */
const openInfo = (row?: Article) => {
console.log(row);
current.value = row ?? null;
showInfo.value = true;
// 跳转文章详情页
// router.push({
// path: '/content/detail',
// query: { articleId: row?.articleId }
// });
};
/* 打开编辑弹窗 */
const openEdit = (row?: Article) => {
current.value = row ?? null;
showEdit.value = true;
};
/* 删除单个 */
const remove = (row: Article) => {
const hide = message.loading('请求中..', 0);
removeArticle(row.articleId)
.then((msg) => {
hide();
message.success(msg);
reload();
})
.catch((e) => {
hide();
message.error(e.message);
});
};
/* 批量删除 */
const removeBatch = () => {
if (!selection.value.length) {
message.error('请至少选择一条数据');
return;
}
Modal.confirm({
title: '提示',
content: '确定要删除选中的记录吗?',
icon: createVNode(ExclamationCircleOutlined),
maskClosable: true,
onOk: () => {
const hide = message.loading('请求中..', 0);
removeBatchArticle(selection.value.map((d) => d.articleId))
.then((msg) => {
hide();
message.success(msg);
reload();
})
.catch((e) => {
hide();
message.error(e.message);
});
}
});
};
/* 修改文章状态 */
const editStatus = (checked: boolean, row: Article) => {
const status = checked ? 0 : 1;
updateArticleStatus(row.articleId, status)
.then((msg) => {
row.status = status;
message.success(msg);
})
.catch((e) => {
message.error(e.message);
});
};
/* 自定义行属性 */
const customRow = (record: Article) => {
return {
// 行点击事件
onClick: () => {
// console.log(record);
},
// 行双击事件
onDblclick: () => {
openEdit(record);
}
};
};
// 监听文章 id 变化
watch(
() => props.categoryId,
() => {
reload();
}
);
</script>
<style lang="less" scoped>
.sys-category-table :deep(.ant-table-body) {
overflow: auto !important;
overflow: overlay !important;
}
.sys-category-table :deep(.ant-table-pagination.ant-pagination) {
padding: 0 4px;
margin-bottom: 0;
}
</style>