| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616 |
- "use client";
- import { fetchApi } from "@/app/_modules/func";
- import {
- DeleteOutlined,
- ExclamationCircleFilled,
- PlusOutlined,
- ReloadOutlined,
- } from "@ant-design/icons";
- import type {
- ActionType,
- ProColumns,
- ProFormInstance,
- } from "@ant-design/pro-components";
- import {
- ModalForm,
- PageContainer,
- ProForm,
- ProFormRadio,
- ProFormSelect,
- ProFormText,
- ProTable,
- } from "@ant-design/pro-components";
- import { Button, message, Modal, Space, Tag } from "antd";
- import { useRouter } from "next/navigation";
- import {
- faCheck,
- faPenToSquare,
- faToggleOff,
- faToggleOn,
- faXmark,
- } from "@fortawesome/free-solid-svg-icons";
- import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
- import { useRef, useState } from "react";
- import dynamic from "next/dynamic";
- import "react-quill/dist/quill.snow.css";
- const ReactQuill = dynamic(() => import("react-quill"), { ssr: false });
- //查询表格数据API
- const queryAPI = "/api/system/notice/list";
- //新建数据API
- const newAPI = "/api/system/notice";
- //修改数据API
- const modifyAPI = "/api/system/notice";
- //查询详情数据API
- const queryDetailAPI = "/api/system/notice";
- //删除API
- const deleteAPI = "/api/system/notice";
- export default function Notice() {
- const { push } = useRouter();
- //表格列定义
- const columns: ProColumns[] = [
- {
- title: "公告编号",
- dataIndex: "noticeId",
- search: false,
- },
- {
- title: "公告标题",
- fieldProps: {
- placeholder: "请输入公告标题",
- },
- dataIndex: "noticeTitle",
- ellipsis: true,
- sorter: true,
- order: 3,
- },
- {
- title: "公告类型",
- fieldProps: {
- placeholder: "请选择公告类型",
- },
- dataIndex: "noticeType",
- valueType: "select",
- render: (_, record) => {
- return (
- <Space>
- <Tag color={record.noticeType === "2" ? "green" : "orange"}>
- {_}
- </Tag>
- </Space>
- );
- },
- valueEnum: {
- 2: {
- text: "公告",
- status: "2",
- },
- 1: {
- text: "通知",
- status: "1",
- },
- },
- order: 1,
- },
- {
- title: "状态",
- dataIndex: "status",
- valueType: "select",
- search: false,
- render: (_, record) => {
- return (
- <Space>
- <Tag
- color={record.status === "0" ? "green" : "red"}
- icon={
- record.status === "0" ? (
- <FontAwesomeIcon icon={faCheck} />
- ) : (
- <FontAwesomeIcon icon={faXmark} />
- )
- }
- >
- {_}
- </Tag>
- </Space>
- );
- },
- valueEnum: {
- 0: {
- text: "正常",
- status: "0",
- },
- 1: {
- text: "关闭",
- status: "1",
- },
- },
- },
- {
- title: "创建者",
- fieldProps: {
- placeholder: "请输入创建者",
- },
- dataIndex: "createBy",
- order: 2,
- },
- {
- title: "创建时间",
- dataIndex: "createTime",
- valueType: "dateTime",
- search: false,
- },
- {
- title: "操作",
- key: "option",
- search: false,
- render: (_, record) => [
- <Button
- key="modifyBtn"
- type="link"
- icon={<FontAwesomeIcon icon={faPenToSquare} />}
- onClick={() => onClickShowRowModifyModal(record)}
- >
- 修改
- </Button>,
- <Button
- key="deleteBtn"
- type="link"
- danger
- icon={<DeleteOutlined />}
- onClick={() => onClickDeleteRow(record)}
- >
- 删除
- </Button>,
- ],
- },
- ];
- //0.查询表格数据
- const queryTableData = async (params: any, sorter: any, filter: any) => {
- const searchParams = {
- pageNum: params.current,
- ...params,
- };
- delete searchParams.current;
- const queryParams = new URLSearchParams(searchParams);
- Object.keys(sorter).forEach((key) => {
- queryParams.append("orderByColumn", key);
- if (sorter[key] === "ascend") {
- queryParams.append("isAsc", "ascending");
- } else {
- queryParams.append("isAsc", "descending");
- }
- });
- const body = await fetchApi(`${queryAPI}?${queryParams}`, push);
- return body;
- };
- //1.新建
- //确定新建数据
- const executeAddData = async (values: any) => {
- const body = await fetchApi(newAPI, push, {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(values),
- });
- if (body != undefined) {
- if (body.code == 200) {
- message.success(body.msg);
- if (actionTableRef.current) {
- actionTableRef.current.reload();
- }
- return true;
- }
- message.error(body.msg);
- return false;
- }
- return false;
- };
- //2.修改
- //是否展示修改对话框
- const [isShowModifyDataModal, setIsShowModifyDataModal] = useState(false);
- //展示修改对话框
- const onClickShowRowModifyModal = (record?: any) => {
- queryRowData(record);
- setIsShowModifyDataModal(true);
- };
- //修改数据表单引用
- const modifyFormRef = useRef<ProFormInstance>();
- //操作当前数据的附加数据
- const [operatRowData, setOperateRowData] = useState<{
- [key: string]: any;
- }>({});
- //查询并加载待修改数据的详细信息
- const queryRowData = async (record?: any) => {
- const noticeId =
- record !== undefined ? record.noticeId : selectedRow.noticeId;
- operatRowData["noticeId"] = noticeId;
- setOperateRowData(operatRowData);
- if (noticeId !== undefined) {
- const body = await fetchApi(`${queryDetailAPI}/${noticeId}`, push);
- if (body !== undefined) {
- if (body.code == 200) {
- modifyFormRef?.current?.setFieldsValue({
- //需要加载到修改表单中的数据
- noticeTitle: body.data.noticeTitle,
- noticeType: body.data.noticeType,
- status: body.data.status,
- noticeContent: body.data.noticeContent,
- });
- }
- }
- }
- };
- //确认修改数据
- const executeModifyData = async (values: any) => {
- values["noticeId"] = operatRowData["noticeId"];
- const body = await fetchApi(modifyAPI, push, {
- method: "PUT",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify(values),
- });
- if (body !== undefined) {
- if (body.code == 200) {
- message.success(body.msg);
- //刷新列表
- if (actionTableRef.current) {
- actionTableRef.current.reload();
- }
- setIsShowModifyDataModal(false);
- return true;
- }
- message.error(body.msg);
- return false;
- }
- };
- //3.删除
- //点击删除按钮,展示删除确认框
- const onClickDeleteRow = (record?: any) => {
- const noticeId =
- record != undefined ? record.noticeId : selectedRowKeys.join(",");
- Modal.confirm({
- title: "系统提示",
- icon: <ExclamationCircleFilled />,
- content: `是否确认删除公告编号为“${noticeId}”的数据项?`,
- onOk() {
- executeDeleteRow(noticeId);
- },
- onCancel() {},
- });
- };
- //确定删除选中的数据
- const executeDeleteRow = async (dictId: any) => {
- const body = await fetchApi(`${deleteAPI}/${dictId}`, push, {
- method: "DELETE",
- });
- if (body !== undefined) {
- if (body.code == 200) {
- message.success("删除成功");
- //修改按钮变回不可点击
- setRowCanModify(false);
- //删除按钮变回不可点击
- setRowCanDelete(false);
- //选中行数据重置为空
- setSelectedRowKeys([]);
- //刷新列表
- if (actionTableRef.current) {
- actionTableRef.current.reload();
- }
- } else {
- message.error(body.msg);
- }
- }
- };
- //4.导出
- //5.选择行
- //选中行操作
- const [selectedRowKeys, setSelectedRowKeys] = useState<React.Key[]>([]);
- const [selectedRow, setSelectedRow] = useState(undefined as any);
- //修改按钮是否可用,选中行时才可用
- const [rowCanModify, setRowCanModify] = useState(false);
- //删除按钮是否可用,选中行时才可用
- const [rowCanDelete, setRowCanDelete] = useState(false);
- //ProTable rowSelection
- const rowSelection = {
- onChange: (newSelectedRowKeys: React.Key[], selectedRows: any[]) => {
- setSelectedRowKeys(newSelectedRowKeys);
- setRowCanDelete(newSelectedRowKeys && newSelectedRowKeys.length > 0);
- if (newSelectedRowKeys && newSelectedRowKeys.length == 1) {
- setSelectedRow(selectedRows[0]);
- setRowCanModify(true);
- } else {
- setRowCanModify(false);
- setSelectedRow(undefined);
- }
- },
- //复选框的额外禁用判断
- // getCheckboxProps: (record) => ({
- // disabled: record.userId == 1,
- // }),
- };
- //搜索栏显示状态
- const [showSearch, setShowSearch] = useState(true);
- //action对象引用
- const actionTableRef = useRef<ActionType>();
- //搜索表单对象引用
- const searchTableFormRef = useRef<ProFormInstance>();
- //当前页数和每页条数
- const [page, setPage] = useState(1);
- const defaultPageSize = 10;
- const [pageSize, setPageSize] = useState(defaultPageSize);
- const pageChange = (page: number, pageSize: number) => {
- setPage(page);
- setPageSize(pageSize);
- };
- return (
- <PageContainer title={false}>
- <ProTable
- formRef={searchTableFormRef}
- rowKey="noticeId"
- rowSelection={{
- selectedRowKeys,
- ...rowSelection,
- }}
- columns={columns}
- request={async (params: any, sorter: any, filter: any) => {
- // 表单搜索项会从 params 传入,传递给后端接口。
- const data = await queryTableData(params, sorter, filter);
- if (data !== undefined) {
- return Promise.resolve({
- data: data.rows,
- success: true,
- total: data.total,
- });
- }
- return Promise.resolve({
- data: [],
- success: true,
- });
- }}
- pagination={{
- defaultPageSize: defaultPageSize,
- showQuickJumper: true,
- showSizeChanger: true,
- onChange: pageChange,
- }}
- search={
- showSearch
- ? {
- defaultCollapsed: false,
- searchText: "搜索",
- }
- : false
- }
- dateFormatter="string"
- actionRef={actionTableRef}
- toolbar={{
- actions: [
- <ModalForm
- key="addmodal"
- title="添加公告"
- trigger={
- <Button icon={<PlusOutlined />} type="primary">
- 新建
- </Button>
- }
- autoFocusFirstInput
- modalProps={{
- destroyOnHidden: true,
- }}
- submitTimeout={2000}
- onFinish={executeAddData}
- >
- <ProForm.Group>
- <ProFormText
- width="md"
- name="noticeTitle"
- label="公告标题"
- placeholder="请输入公告标题"
- rules={[{ required: true, message: "请输入公告标题" }]}
- />
- <ProFormSelect
- width="md"
- name="noticeType"
- label="公告类型"
- valueEnum={{
- "2": "公告",
- "1": "通知",
- }}
- placeholder="请选择公告类型"
- rules={[{ required: true, message: "请选择公告类型" }]}
- />
- </ProForm.Group>
- <ProForm.Group>
- <ProFormRadio.Group
- name="status"
- width="sm"
- label="状态"
- initialValue="0"
- options={[
- {
- label: "正常",
- value: "0",
- },
- {
- label: "关闭",
- value: "1",
- },
- ]}
- />
- </ProForm.Group>
- <ProForm.Group>
- <ProForm.Item
- name="noticeContent"
- style={{ width: 688, height: 240 }}
- label="内容"
- >
- <ReactQuill
- theme="snow"
- placeholder="请输入内容"
- style={{ height: 160 }}
- />
- </ProForm.Item>
- </ProForm.Group>
- </ModalForm>,
- <ModalForm
- key="modifymodal"
- title="修改公告"
- formRef={modifyFormRef}
- trigger={
- <Button
- icon={<FontAwesomeIcon icon={faPenToSquare} />}
- disabled={!rowCanModify}
- onClick={() => onClickShowRowModifyModal()}
- >
- 修改
- </Button>
- }
- open={isShowModifyDataModal}
- autoFocusFirstInput
- modalProps={{
- destroyOnHidden: true,
- onCancel: () => {
- setIsShowModifyDataModal(false);
- },
- }}
- submitTimeout={2000}
- onFinish={executeModifyData}
- >
- <ProForm.Group>
- <ProFormText
- width="md"
- name="noticeTitle"
- label="公告标题"
- placeholder="请输入公告标题"
- rules={[{ required: true, message: "请输入公告标题" }]}
- />
- <ProFormSelect
- width="md"
- name="noticeType"
- label="公告类型"
- valueEnum={{
- "2": "公告",
- "1": "通知",
- }}
- placeholder="请选择公告类型"
- rules={[{ required: true, message: "请选择公告类型" }]}
- />
- </ProForm.Group>
- <ProForm.Group>
- <ProFormRadio.Group
- name="status"
- width="sm"
- label="状态"
- initialValue="0"
- options={[
- {
- label: "正常",
- value: "0",
- },
- {
- label: "关闭",
- value: "1",
- },
- ]}
- />
- </ProForm.Group>
- <ProForm.Group>
- <ProForm.Item
- name="noticeContent"
- style={{ width: 688, height: 240 }}
- label="内容"
- >
- <ReactQuill
- theme="snow"
- placeholder="请输入内容"
- style={{ height: 160 }}
- />
- </ProForm.Item>
- </ProForm.Group>
- </ModalForm>,
- <Button
- key="danger"
- danger
- icon={<DeleteOutlined />}
- disabled={!rowCanDelete}
- onClick={() => onClickDeleteRow()}
- >
- 删除
- </Button>,
- ],
- settings: [
- {
- key: "switch",
- icon: showSearch ? (
- <FontAwesomeIcon icon={faToggleOn} />
- ) : (
- <FontAwesomeIcon icon={faToggleOff} />
- ),
- tooltip: showSearch ? "隐藏搜索栏" : "显示搜索栏",
- onClick: (key: string | undefined) => {
- setShowSearch(!showSearch);
- },
- },
- {
- key: "refresh",
- tooltip: "刷新",
- icon: <ReloadOutlined />,
- onClick: (key: string | undefined) => {
- if (actionTableRef.current) {
- actionTableRef.current.reload();
- }
- },
- },
- ],
- }}
- />
- </PageContainer>
- );
- }
|