一、商品提交修改
0.ajax请求src/ajax.js
import axios from 'axios'
import {message} from 'antd'
export default function ajax(url, data={}, type='GET') {
return new Promise((resolve, reject) => {
let promise
if(type==='GET') {
promise = axios.get(url, {
params: data
})
} else {
promise = axios.post(url, data)
}
promise.then(response => {
resolve(response.data)
}).catch(error => {
message.error('请求出错了: ' + error.message)
})
})
}
1.添加修改商品请求api接口api/index.js
export const reqAddOrUpdateProduct = (product) => ajax(BASE + '/manage/product/' + ( product._id?'update':'add'), product, 'POST')
2.点提交后运行的函数add-update.jsx
submit = () => {
this.props.form.validateFields(async (error, values) => {
if (!error) {
const {name, desc, price, categoryIds} = values
let pCategoryId, categoryId
if (categoryIds.length===1) {
pCategoryId = '0'
categoryId = categoryIds[0]
} else {
pCategoryId = categoryIds[0]
categoryId = categoryIds[1]
}
const imgs = this.pw.current.getImgs()
const detail = this.editor.current.getDetail()
const product = {name, desc, price, imgs, detail, pCategoryId, categoryId}
if(this.isUpdate) {
product._id = this.product._id
}
const result = await reqAddOrUpdateProduct(product)
if (result.status===0) {
message.success(`${this.isUpdate ? '更新' : '添加'}商品成功!`)
this.props.history.goBack()
} else {
message.error(`${this.isUpdate ? '更新' : '添加'}商品失败!`)
}
}
})
}
3.效果点提交后自动判断是添加还是修改商品,做出正确动作