123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- import React, { useEffect, useState, useReducer } from 'react';
- import { PageContainer } from '@ant-design/pro-layout';
- import {
- Row,
- Col,
- Space,
- Button,
- Input,
- Table,
- Card,
- Select,
- message,
- Tooltip,
- Image,
- Tag,
- } from 'antd';
- import { getPlate, getPosts, enablePosts } from '@/services/tieba';
- import { useClassify } from '@/hooks/tieba/index';
- import type { initstateType, actionType } from './data';
- import DetailModal from './modal';
- import { Record } from 'immutable';
- const request = (url: string): Promise<any> => {
- return new Promise((resolve, reject) => {
- const xhr = new XMLHttpRequest();
- xhr.open('GET', url, false);
- xhr.onreadystatechange = function () {
- if (xhr.readyState === 4 && xhr.status === 200) {
- resolve(xhr.response);
- } else {
- reject(new Error('置换contenturl出错'));
- }
- };
- xhr.send();
- }).catch((e) => e);
- };
- const sendRequest = async (url: string) => await request(url);
- const initstate: initstateType = {
- curPage: 1,
- barId: '',
- categoryId: '',
- label: '',
- type: 0,
- };
- const reduce: React.Reducer<initstateType, actionType> = (state, action): initstateType => {
- const { type, value } = action;
- switch (type) {
- case 'reload':
- return { ...state, barId: '', categoryId: '', label: '' };
- case 'barId':
- return { ...state, barId: value };
- case 'curPage':
- return { ...state, curPage: value };
- case 'categoryId':
- return { ...state, categoryId: value };
- case 'label':
- return { ...state, label: value };
- case 'type':
- return { ...state, type: value };
- default:
- throw new Error('posts/index.tsx => reduce 参数 action 下, type类型不存在');
- }
- };
- const Posts: React.FC = () => {
- const [plateList, setPlateList] = useState([]);
- const [postsList, setPostsList] = useState([]);
- const classIfy = useClassify();
- const [total, setTotal] = useState(0);
- const [queryParams, dispatch] = useReducer(reduce, initstate);
- const [visible, setVissible] = useState(false);
- const [detailData, setDetailData] = useState({});
- const [loading, setLoading] = useState(false);
- useEffect(() => {
- GetPlate();
- GetPosts();
- }, []);
- useEffect(() => {
- GetPosts();
- }, [queryParams.curPage]);
- // 获取贴吧板块
- const GetPlate = async (): Promise<void> => {
- const { code, data } = await getPlate({ curPage: queryParams.curPage, pageSize: 10 });
- if (code === 0) {
- const { records } = data;
- setPlateList(records);
- }
- };
- // 获取帖子
- const GetPosts = async (): Promise<void> => {
- setLoading(true);
- const { code, data } = await getPosts(queryParams);
- setLoading(false);
- if (code === 0) {
- const { records, total: Total } = data;
- // eslint-disable-next-line no-plusplus
- for (let i = 0; i < records.length; i++) {
- // eslint-disable-next-line no-await-in-loop
- // records[i].content = records[i].contentUrl && await sendRequest(records[i]?.contentUrl)
- }
- console.log(records, 'recordsrecordsrecords');
- setPostsList(records);
- setTotal(Total);
- }
- };
- // 封禁帖子
- const EnablePosts = async (record: Record<string, any>): Promise<void> => {
- const $par = {
- id: record.id,
- enable: record.enable ? 0 : 1,
- type: queryParams.type,
- };
- const { code } = await enablePosts($par);
- if (code === 0) {
- message.success('操作成功');
- }
- GetPosts();
- };
- // 打开弹窗
- const openModal = (record: Record<string, any>): void => {
- setVissible(true);
- setDetailData(record);
- };
- // 关闭弹窗
- const closeModal = (): void => setVissible(false);
- // 选择barId
- const changeBarId = (value: string): void => dispatch({ type: 'barId', value });
- // 填写标题
- const changeLabel = (e: React.ChangeEvent<HTMLInputElement>) =>
- dispatch({ type: 'label', value: e.target.value });
- return (
- <PageContainer title="帖子管理">
- <Card>
- <Row gutter={[16, 16]} justify="space-between">
- <Col>
- <Space>
- <Select
- style={{ width: 200 }}
- placeholder="请选择贴吧板块"
- value={queryParams.barId || undefined}
- onChange={changeBarId}
- allowClear
- >
- {plateList.length &&
- plateList.map((item: Record<string, any>) => (
- <Select.Option key={item.id} value={item.id}>
- {item.label}
- </Select.Option>
- ))}
- </Select>
- <Select
- style={{ width: 200 }}
- placeholder="请选择分类"
- value={queryParams.categoryId || undefined}
- onChange={(value: string) => dispatch({ type: 'categoryId', value })}
- allowClear
- >
- {classIfy.length &&
- classIfy.map((item: Record<string, any>) => (
- <Select.Option key={item.id} value={item.id}>
- {item.label}
- </Select.Option>
- ))}
- </Select>
- <Input onChange={changeLabel} placeholder="请输入帖子标题"></Input>
- <Select
- style={{ width: 200 }}
- placeholder="请选择类型"
- value={queryParams.type}
- onChange={(value: number) => dispatch({ type: 'type', value })}
- allowClear
- >
- <Select.Option value={0}>帖子</Select.Option>
- <Select.Option value={1}>问答</Select.Option>
- </Select>
- </Space>
- </Col>
- <Col>
- <Space>
- <Button onClick={() => dispatch({ type: 'reload' })}>重置</Button>
- <Button type="primary" onClick={GetPosts}>
- 搜索
- </Button>
- </Space>
- </Col>
- </Row>
- <Table
- rowKey={(record) => record.id}
- loading={loading}
- style={{ marginTop: 20 }}
- dataSource={postsList}
- pagination={{ total, onChange: (page) => dispatch({ type: 'curPage', value: page }) }}
- >
- <Table.Column
- key="label"
- dataIndex="label"
- title="帖子标题"
- ellipsis
- width={200}
- render={(text, record) => (
- <Tooltip title={'pagesarticle'} placement="topLeft">
- <span>{text}</span>
- </Tooltip>
- )}
- />
- <Table.Column
- key="content"
- width={320}
- ellipsis
- dataIndex="content"
- title="内容"
- render={(text, record: Record<string, any>) => (
- <Tooltip placement="topLeft" title={record.content}>
- <span>{record.content}</span>
- </Tooltip>
- )}
- ></Table.Column>
- <Table.Column
- key="authorName"
- dataIndex="authorName"
- title="发布人"
- render={(text, record: Record<string, any>) => (
- <Space>
- <Col>
- <Tag color="blue"> {record.authorName} </Tag>
- </Col>
- {/* <Col>{record.createTime}</Col> */}
- </Space>
- )}
- />
- <Table.Column key="replyNum" dataIndex="replyNum" title="回帖数"></Table.Column>
- <Table.Column key="praiseNum" dataIndex="praiseNum" title="点赞数"></Table.Column>
- <Table.Column
- key="path"
- dataIndex="path"
- title="链接地址"
- render={(text, record: Record<string, any>) => (
- <Tooltip
- title={
- record.hasOwnProperty('coverImg')
- ? `/pages/article/index?id=${record.id}&type=POSTS`
- : `/pages/article/index?id=${record.id}&type=ISSUES`
- }
- placement="topLeft"
- >
- <span>/pages/article/index....</span>
- </Tooltip>
- )}
- />
- <Table.Column
- key="action"
- dataIndex="action"
- title="操作"
- render={(text, record: Record<string, any>) => (
- <Space>
- <a onClick={() => openModal(record)}>查看</a>
- <a onClick={() => EnablePosts(record)}>{record.enable ? '封禁' : '解封'}</a>
- </Space>
- )}
- />
- </Table>
- </Card>
- {/* 详情弹窗 */}
- <DetailModal detailData={detailData} visible={visible} closeModal={closeModal} />
- </PageContainer>
- );
- };
- export default Posts;
|