diff --git a/src/pages/plan/key/UseKeyAddEdit.jsx b/src/pages/plan/key/UseKeyAddEdit.jsx index 16e22ae1..056f1958 100644 --- a/src/pages/plan/key/UseKeyAddEdit.jsx +++ b/src/pages/plan/key/UseKeyAddEdit.jsx @@ -26,6 +26,13 @@ import Grid from "@/components/gird/main.js"; import UseCouponAddEdit from "@/components/UseCouponAddEdit"; // 优惠券 import UseExchangeAddEdit from "@/components/UseExchangeAddEdit"; // 兑换码 +import { + handelResponse, + getReseller, + addKeysBatchInfo, + keyEditApproval, +} from "@/assets/api.js"; + const formRules = { bind_object: [{ type: "required", message: "请选择绑定类型" }], }; @@ -40,9 +47,9 @@ const UseKeyAddEdit = () => { recall_visible: false, audit_visible: false, key_status: 1, - payType: 1, - stock: 0, + payType: 3, keyType: 1, + rowIndex: -1, distdata: [], couponData: [], rowCouponData: "", @@ -156,33 +163,39 @@ const UseKeyAddEdit = () => { timer: 0, draw_title: "", drawerVisible: false, - direct_reseller_id: "", + direct_reseller_id: 0, }); const keyFormEl = useRef(null); const bindObjEl = useRef(null); + const addEditExchangeEl = useRef(null); + const addEditCouponEl = useRef(null); const [model, setModel] = useSetState({ plan_title: "", batch_name: "", style: 1, allow_repetition: 1, allow_loss: 1, + quantity: "", + stock: "", }); useEffect(() => { init(); }, []); const init = () => { - let planItem = sessionStorage.getItem("plan_item"); - if (planItem) { - setModel({ - plan_title: JSON.parse(planItem).title, - }); - } // KeyPcType 1编辑 2复制 3新增 if (state.KeyPcType === "3") { setState({ pageTitle: "新增key", }); + let planItem = sessionStorage.getItem("plan_item"); + if (planItem) { + let obj = JSON.parse(planItem); + setModel({ + plan_title: obj.title, + }); + getResellerFun(obj.reseller_id); + } } else if (state.KeyPcType === "1") { setState({ pageTitle: "编辑key", @@ -193,11 +206,47 @@ const UseKeyAddEdit = () => { }); } }; + // 获取分销商id + const getResellerFun = (id) => { + getReseller(id).then((res) => { + handelResponse( + res, + (req, msg) => { + setState({ direct_reseller_id: req.direct_reseller_id }); + }, + (err) => { + Notify.error(err); + } + ); + }); + }; const onStyleChange = () => {}; const initUpload = () => {}; - const planAddExchangeCoupon = () => {}; + const planAddExchangeCoupon = (type) => { + if (type === 0) { + setState({ draw_title: "新建兑换码" }); + setTimeout(() => { + addEditExchangeEl.current.clearExchangeForm(); + }, 300); + } else { + setState({ draw_title: "新建优惠券" }); + setTimeout(() => { + addEditCouponEl.current.clearCouponForm(); + }, 300); + } + setState({ rowIndex: -1 }); + setState({ drawerVisible: true }); + }; const pageChange = () => {}; - const onBindNum = () => {}; + const onBindNum = (e, rowData, rowIndex) => { + if (state.keyType === 1) { + state.distdata[rowIndex].restrict = e.target.value; + setState({ distdata: state.distdata }); + } else { + state.couponData[rowIndex].restrict = e.target.value; + setState({ couponData: state.couponData }); + } + }; // 优惠券范围 const coupon_range = (product) => { let arr_legal = product.legal.map((item) => item.product_name); @@ -209,14 +258,226 @@ const UseKeyAddEdit = () => { const rowItemClick = () => {}; const onRepetitionChange = () => {}; const onLossChange = () => {}; - const onSubmit = () => {}; - const Cancel = () => {}; + const Cancel = () => { + Sweetalert.confirm({ + type: "warning", + closeBtn: true, + title: "确认操作", + content:

是否取消本次操作?

, + onConfirm: () => { + window.history.back(); + }, + onCancel: () => {}, + className: "questModal", + parentComponent: this, + }); + }; const onReCall = () => {}; - const onConfirm = () => {}; + + /** + * + * @param {0新增addKeysBatchInfo,1编辑keyEditApproval} apiType + * @param {*} data + */ + const couponExchangeSubmitApi = (apiType, data) => { + let keybatch_id = sessionStorage.getItem("keybatch_id"); + if (apiType === 0) { + let create_param = { + id: state.id, + batch_name: state.model.batch_name, + style: state.model.style, + quantity: state.model.quantity, + bind_object: [state.keyType], + allow_repetition: state.model.allow_repetition, + allow_loss: state.model.allow_loss, + merge_stock: state.model.merge_stock, + mobile_excel: state.mobile_excel, + mobile_repeat: state.mobile_repeat, + reseller_id: state.reseller.id, + reseller_name: state.reseller.name, + company_name: state.reseller.company_name, + receive_email: state.reseller.contact_email[0], + payment_direction: state.payment_direction[state.payType - 1], + }; + // 参数 是否是优惠券还是兑换码 + if (state.keyType === 1) { + create_param.code_batch = data; + } else { + create_param.coupon = data; + } + addKeysBatchInfo(state.plan_id, create_param).then((res) => { + handelResponse( + res, + (req, msg) => { + Notify.success("成功发起审批"); + setTimeout(() => { + window.history.back(); + }, 1000); + }, + (err) => { + Notify.error(err); + } + ); + }); + } else { + let edit_param = { + reseller_id: state.reseller.id, + reseller_name: state.reseller.name, + company_name: state.reseller.company_name, + receive_email: state.reseller.contact_email[0], + payment_direction: state.payment_direction[state.payType - 1], + }; + // 参数 是否是优惠券还是兑换码 + if (state.keyType === 1) { + edit_param.code_batch = data; + } else { + edit_param.coupon = data; + } + keyEditApproval(keybatch_id, edit_param).then((res) => { + handelResponse( + res, + (req, msg) => { + Notify.success("成功发起审批"); + setTimeout(() => { + window.history.back(); + }, 1000); + }, + (err) => { + Notify.error(err); + } + ); + }); + } + }; + + // 优惠券 + const couponSubmit = () => { + // 第一步:整理数据 编辑优惠券数据 + let editCouponData = state.couponData.filter( + (item) => + item.checked === true && + (item.status === -1 || item.status === 7 || item.id === "" || !item.id) + ); + // 第二步:是否是 创建中或者驳回 + if (state.key_status === 1) { + // 第三步:是否有原数据 + let couponArr = state.couponData.filter( + (item) => item.id && item.id !== "" + ); + if (couponArr.length > 0) { + // 编辑接口 + couponExchangeSubmitApi(1, editCouponData); + } else { + // 新增接口 + let addCouponData = state.couponData.filter( + (item) => item.checked === true && !item.id + ); + couponExchangeSubmitApi(0, addCouponData); + } + } else { + // 其它状态 + couponExchangeSubmitApi(1, editCouponData); + } + }; + + // 兑换码 + const exchangeSubmit = () => { + // 第一步:整理数据 编辑兑换码数据 + let editExchangeData = state.distdata.filter( + (item) => + item.checked === true && + (item.status === -1 || item.status === 7 || item.id === "" || !item.id) + ); + + // 第二步:是否是 创建中或者驳回 + if (state.key_status === 1) { + // 第三步:是否有原数据 + let arr = editExchangeData.filter((item) => item.id && item.id !== ""); + if (arr.length > 0) { + // 编辑接口 + couponExchangeSubmitApi(1, editExchangeData); + } else { + // 新增接口 + let addArr = state.distdata.filter( + (item) => item.checked === true && !item.id + ); + couponExchangeSubmitApi(0, addArr); + } + } else { + // 其它状态 + couponExchangeSubmitApi(1, editExchangeData); + } + }; + + const onConfirm = () => { + // 判断类型 keyType 1兑换码 2优惠券 + if (state.keyType === 1) { + exchangeSubmit(); + } else { + couponSubmit(); + } + }; const onPayTypeChange = () => {}; const onCheckChange = () => {}; - const codeSubmit = () => {}; - const closeDraw = () => {}; + const codeSubmit = () => { + // 优惠券数据 + if (state.keyType === 2) { + let validator = addEditCouponEl.current.submit(); + if (validator) { + let param = addEditCouponEl.getModel(); + let tempData = state.couponData; + // 优惠券编辑 + if (state.rowIndex > -1) { + tempData[state.rowIndex] = param; + setState({ couponData: tempData }); + } else { + // 优惠券新增 + tempData.push(param); + setState({ couponData: tempData }); + } + setState({ + drawerVisible: false, + }); + } + return; + } + + // 兑换码数据 + if (state.keyType === 1) { + let validator = addEditExchangeEl.current.submit(); + if (validator) { + let param = addEditExchangeEl.current.getModel(); + let tempData = state.distdata; + // 优惠券编辑 + if (state.rowIndex > -1) { + tempData[state.rowIndex] = param; + setState({ distdata: tempData }); + } else { + // 优惠券新增 + tempData.push(param); + setState({ distdata: tempData }); + } + setState({ + drawerVisible: false, + }); + } + return; + } + }; + const closeDraw = () => { + if (state.keyType === 2) { + setTimeout(() => { + addEditCouponEl.current.clearCouponForm(); + }, 300); + } + + if (state.keyType === 1) { + setTimeout(() => { + addEditExchangeEl.current.clearExchangeForm(); + }, 300); + } + setState({ drawerVisible: false }); + }; return (
{
{state.isCancel ? null : ( - )} @@ -465,14 +733,14 @@ const UseKeyAddEdit = () => { ) : ( )}