import React, { Component } from 'react' import { Button, Card, Form, Input, Popconfirm, message as Message } from 'antd' import { isEqual } from 'lodash' import { AntIcon, Auth, Container, ModalForm, QueryTable, QueryTableActions } from 'components' import { api } from 'common/api' import getDictData from 'util/dic' import auth from 'components/authorized/handler' import { toCamelCase } from 'util/format' import FormBody from './form' // 配置页面所需接口函数 const apiAction = { page: api.getAppPage, add: api.sysAppAdd, edit: api.sysAppEdit, delete: api.sysAppDelete, setDefault: api.sysAppSetAsDefault, } // 用于弹窗标题 const name = '应用' export default class index extends Component { state = { codes: { commonStatus: [], }, } // 表格实例 table = React.createRef() // 新增窗口实例 addForm = React.createRef() // 编辑窗口实例 editForm = React.createRef() // 表格字段 columns = [ { title: '应用名称', dataIndex: 'name', width: 300, sorter: true, }, { title: '唯一编码', dataIndex: 'code', width: 300, sorter: true, }, { title: '是否默认', dataIndex: 'active', width: 200, sorter: true, render: (text, record) => ( <> {text ? '是' : '否'} {!record.active && ( this.onSetDefault(record)} > 设为默认 )} ), }, { title: '状态', dataIndex: 'status', width: 100, sorter: true, render: text => <>{this.bindCodeValue(text, 'common_status')}, }, { title: '排序', dataIndex: 'sort', width: 100, sorter: true, defaultSortOrder: 'ascend', }, ] /** * 构造函数,在渲染前动态添加操作字段等 * @param {*} props */ constructor(props) { super(props) const flag = auth({ sysApp: [['edit'], ['delete']] }) if (flag) { this.columns.push({ title: '操作', width: 150, dataIndex: 'actions', render: (text, record) => ( this.onOpen(this.editForm, record.id)}>编辑 this.onDelete(record)} > 删除 ), }) } } /** * 阻止外部组件引发的渲染,提升性能 * 可自行添加渲染条件 * [必要] * @param {*} props * @param {*} state * @returns */ shouldComponentUpdate(props, state) { return !isEqual(this.state, state) } /** * 加载字典数据,之后开始加载表格数据 * 如果必须要加载字典数据,可直接对表格设置autoLoad=true */ componentDidMount() { const { onLoading, onLoadData } = this.table.current onLoading() getDictData('common_status').then(res => { this.setState( { codes: res, }, () => { onLoadData() } ) }) } /** * 调用加载数据接口,可在调用前对query进行处理 * [异步,必要] * @param {*} params * @param {*} query * @returns */ loadData = async (params, query) => { const { data } = await apiAction.page({ ...params, ...query, }) return data } /** * 绑定字典数据 * @param {*} code * @param {*} name * @returns */ bindCodeValue(code, name) { name = toCamelCase(name) const codes = this.state.codes[name] if (codes) { const c = codes.find(p => p.code == code) if (c) { return c.value } } return null } /** * 打开新增/编辑弹窗 * @param {*} modal * @param {*} id */ onOpen(modal, id) { modal.current.open({ id, }) } /** * 对表格上的操作进行统一处理 * [异步] * @param {*} action * @param {*} successMessage */ async onAction(action, successMessage) { const { onLoading, onLoaded, onReloadData } = this.table.current onLoading() try { if (action) { await action } if (successMessage) { Message.success(successMessage) } onReloadData() } catch { onLoaded() } } /** * 删除 * @param {*} record */ onDelete(record) { this.onAction(apiAction.delete(record), '删除成功') } //#region 自定义方法 async onSetDefault(record) { this.onAction(apiAction.setDefault(record), '设置成功') } //#endregion render() { return (
} operator={ } /> this.table.current.onReloadData()} > this.table.current.onReloadData()} >
) } }