添加弹出对话框,添加里面有一个上传图片,我们上传的图片有一个单独的添加接口,所以我们使用action属性绑定我们要上传的路径,headres获取token,使用onChange获取图片上传的路径,然后在我们点击提交form表单数据时把图片临时地址添加成功
import { DeleteOutlined, EditOutlined } from '@ant-design/icons';
import { Button, Card, Form, Input, message, Modal, Pagination, Table, Upload } from 'antd';
import React, { useEffect, useRef, useState } from 'react';
import axios from '../utils/request';
import './goods.scss';
const { Search } = Input;
const props = {
name: 'file',
action: 'https://www.mocky.io/v2/5cc8019d300000980a055e76',
headers: {
authorization: 'authorization-text',
}
}
export default function Goods() {
let formRef = useRef()
let addFormRef = useRef(null)
const [userData, setUserData] = useState([])
// 添加弹窗状态
const [addIsModalVisible, setAddIsModalVisible] = useState(false);
// 存图片
let [img,setimg] = useState("");
// 搜索
let [search, setSearch] = useState("")
// 总条数
let [total, setTotal] = useState(0)
// 当前页
let [page, setPage] = useState(1)
// 修改弹出框
const [isModalVisible, setIsModalVisible] = useState(false);
// 每页条数
let [pageSize, setPageSize] = useState(2)
const [goodid, setgoodid] = useState(0)
const showModal = () => {
setAddIsModalVisible(true);
};
// 修改商品
const onFinish = async (values) => {
let { meta } = await axios.put(`goods/${goodid}`, {
goods_name: values.goods_name,
goods_number: values.goods_number,
goods_price: values.goods_price,
goods_weight: values.goods_weight
})
// console.log(data);
if (meta.status === 200) {
message.success(meta.msg)
setIsModalVisible(false);
getUserData()
} else {
message.info(meta.msg)
setIsModalVisible(false);
}
};
const onFinishFailed = (errorInfo) => {
console.log('Failed:', errorInfo);
};
// 修改弹出框,数据回填
const showedit = async (e) => {
setIsModalVisible(true);
setgoodid(e)
let { data } = await axios.get(`goods/${e}`)
console.log(data);
formRef.current.setFieldsValue({
goods_name: data.goods_name,
goods_price: data.goods_price,
goods_number: data.goods_number,
goods_weight: data.goods_weight
})
};
// 初始化数据
useEffect(() => {
getUserData()
setPage(1)
}, [search, page, pageSize])
// 添加
const layout = { labelCol: { span: 4 }, wrapperCol: { span: 20 } };
// 添加取消
const addHandleCancel = () => {
setAddIsModalVisible(false);
};
// 修改取消按钮
const editHandleCancel = () => {
setIsModalVisible(false)
}
// 删除
const delGoods = async (e) => {
console.log(e);
let { meta } = await axios.delete(`goods/${e}`)
switch (meta.status) {
case 200:
message.success(meta.msg)
break;
default:
message.warning("删除失败")
break;
}
getUserData()
}
// 初始化请求数据 用户
const getUserData = async () => {
console.log(pageSize);
console.log(search);
console.log(page);
let { data } = await axios.get(`goods?pagenum=${page}&query=${search}&pagesize=${pageSize}`)
console.log(data.goods);
if (data) {
setTotal(data.total);
setUserData(data.goods);
}
}
const columns = [
{
title: '商品名称',
dataIndex: 'goods_name',
key: 'username',
},
{
title: '商品价格',
dataIndex: 'goods_price',
key: 'email',
},
{
title: '商品重量',
dataIndex: 'goods_weight',
key: 'mobile',
},
{
title: '创建时间',
dataIndex: 'goods_state',
key: 'role_name',
},
{
title: '操作',
key: 'action',
render: (_, record) => {
return (
<>
{ showedit(record.goods_id) }}>
{ delGoods(record.goods_id) }} >
>
)
}
}
];
// 搜索
const onSearch = (val) => {
setSearch(val)
}
// 分页
const onChange = (page, pageSize) => {
setPage(page)
setPageSize(pageSize)
}
let token = sessionStorage.getItem("token");
// 添加商品
const onAdd = async (value)=>{
console.log(value);
let pics = [{pic:img}];
let data= await axios.post("goods",{...value,pics})
setAddIsModalVisible(false);
getUserData()
}
const success = (info)=>{
if(info.file.status==="done"){
console.log(info.file.response.data.tmp_path);
setimg(info.file.response.data.tmp_path)
}
}
return (