import React, { useState, useEffect, useCallback } from 'react'; import PropTypes from 'prop-types'; import { Table, Button, Checkbox, Pagination, Dialog, Form, Input, Select } from '@alifd/next'; import styles from './index.module.scss'; import { Item } from '@alifd/next/types/breadcrumb'; import IceContainer from '@icedesign/container'; import lodashFind from 'lodash.find'; const FormItem = Form.Item; const formItemWidth = '500px'; import { ajax } from '@/utils'; import PageHead from '@/components/page-head'; import getCustomHeader from '@/components/token-header'; const dialogDefaultItem = { dialogTitle: '添加菜单', type: 1, // 1 菜单 2 操作 online: true, parentId: 0, name: '', code: '', weight: 0 }; const MenuList = ({ title, treeData }) => { const [data, setData] = useState(treeData); const [dialogVisible, setDialogVisible] = useState(false); const [dialogData, setDialogData] = useState({ ...dialogDefaultItem }); // const getCustomHeader = () => { // const token = document.getElementsByName('_csrf')[0]; // const csrfHeader = document.getElementsByName('_csrf_header')[0]; // let customHeader = {}; // if (csrfHeader && token) { // customHeader[`${csrfHeader.content}`] = token.content; // } // return customHeader; // }; const addOrEditMenu = async values => { console.log(values); const postData = { ...values }; if (dialogData.id) { postData.id = dialogData.id; } try { const apiUrl = dialogData.id !== undefined ? 'editMenu' : 'addMenu'; const responseData = await ajax({ api: apiUrl, method: 'post', type: 'json', data: JSON.stringify(postData), contentType: 'application/json', headers: getCustomHeader() }); setDialogVisible(false); if (responseData.success) { location.reload(); // await setData(responseData.result); } else { Dialog.alert({ title: '操作失败!', content: responseData.message || `系统错误请稍后再试~` }); } } catch (e) { setDialogVisible(false); Dialog.alert({ title: '操作失败!', content: `系统错误请稍后再试~` }); } // async; }; const deleteMenu = async values => { // console.log(values); try { const responseData = await ajax({ api: 'removeMenu', method: 'post', type: 'json', contentType: 'application/json', data: JSON.stringify(values), headers: getCustomHeader() }); if (responseData.success) { location.reload(); } else { Dialog.alert({ title: '操作失败!', content: responseData.message || `系统错误请稍后再试~` }); } } catch (e) { Dialog.alert({ title: '操作失败!', content: `系统错误请稍后再试~` }); } // async; }; const moveTop = id => { console.log('moveTop', id); }; const moveDown = id => { console.log('moveDown', id); }; const switchMenuStatus = async (id, online) => { // console.log(values); try { const responseData = await ajax({ api: 'editMenu', method: 'post', type: 'json', contentType: 'application/json', data: JSON.stringify({ id: id, online: !online }), headers: getCustomHeader() }); if (responseData.success) { location.reload(); } else { Dialog.alert({ title: '操作失败!', content: responseData.message || `系统错误请稍后再试~` }); } } catch (e) { Dialog.alert({ title: '操作失败!', content: `系统错误请稍后再试~` }); } // async; }; const renderAddMenuDialog = () => { let newItemDefaultValue = { ...dialogDefaultItem }; setDialogData(newItemDefaultValue); setDialogVisible(true); }; const renderEditMenuDialog = id => { // console.log('edit', id); const dialogItem = { ...dialogDefaultItem, dialogTitle: '编辑菜单', ...lodashFind(data, { id: id }) }; setDialogData(dialogItem); setDialogVisible(true); }; const renderConfirmDialog = id => { const menuItem = { ...lodashFind(data, { id: id }) }; Dialog.confirm({ title: '确定删除菜单吗?', content: `确定删除菜单【 ${menuItem.name} 】吗?此操作不可逆`, onOk: () => deleteMenu({ id }) }); }; const renderDialogWithData = dialogItem => { return ( setDialogVisible(false)} >
是否启用 提交
); }; const tableDataFormater = data => { let flatData = []; const flatLooper = (dataList, depth) => { dataList.forEach(item => { const newItem = { id: item.id, code: item.code, name: item.name, value: item.id, // for select parentId: item.parentId, url: item.url, online: item.online, depth: depth, type: item.type, weight: item.weight }; flatData.push(newItem); if (item.children && item.children[0]) { const newDepth = depth + 1; flatLooper(item.children, newDepth); } }); return flatData; }; return flatLooper(data, 0); }; useEffect(() => { setData(tableDataFormater(treeData)); }, []); treeData = treeData.map(item => { item.title = item.name; return item; }); const renderTree = (value, index, record) => { if (record.depth > 0) { return ( |-- {value} ); } return value; }; const renderOpts = (value, index, record) => { return (
{/* */}
); }; const renderStatus = (value, index, record) => { return value ? ( 启用 ) : ( 未启用 ); }; const renderType = (value, index, record) => { if (value == 1) return 菜单; if (value == 2) return 操作; return ''; }; return (
{renderDialogWithData(dialogData)} this.setState({ treeData })} > { return {value}; }} />
); }; MenuList.defaultProps = { links: [] }; MenuList.propTypes = { title: PropTypes.string.isRequired }; export default MenuList;