import withLayout from "@/layouts";
import Taro, { useDidShow } from "@tarojs/taro";
import { getPackageDetail } from "@/services/home";
import { saveOrder, getOrderSub, payOrder } from "@/services/payOrder";
import { useState, useEffect } from "react";
import formatPrice from "@/utils/formatPrice";
import { Button, Radio, View } from "@tarojs/components";
import InputNumber from "@/components/InputNumber";
import AuthPage from '@/components/AuthPage'
import CustomNav from "@/components/CustomNav";
import OrderMolded from "@/components/OrderMolded";
import Popup from "@/components/Popup";
import Card from "./Card";
import "./style.less";
export default withLayout((props) => {
//#region
const { router, person } = props;
const { packageId, orderId } = props.router.params;
const [payInfo, setPayInfo] = useState();
// 是否已阅读协议
const [agreement, setAgreement] = useState(false);
// 总价 totalPrice
const [totalPrice, setTotalPrice] = useState({});
const [list, setList] = useState([]);
const [BuyNumber, setBuyNumber] = useState(1);
const [detail, setDetail] = useState({});
const [showDialog, setShowDialog] = useState(false);
const [DisabledBool, setDisabledBool] = useState(false);
const ShowMoldeOn = (e) => {
if (packageId) {
setBuyNumber(e.amount || 1);
setDetail(e);
setShowDialog(true);
}
};
const ButtonCancel = () => {
setShowDialog(false);
};
const ButtonOK = (e) => {
setList(
list.map((x) =>
x.packageId == detail.packageId ? { ...x, amount: BuyNumber } : x
)
);
setShowDialog(false);
};
const NumberAdd = () => {
setBuyNumber(BuyNumber + 1);
};
const NumberCut = () => {
if (DisabledBool) return;
setBuyNumber(BuyNumber - 1);
};
const onInput = (e) => {
let values = e.detail.value;
setBuyNumber(values - 0);
};
const requestPayment = (params) => {
Taro.hideLoading()
Taro.requestPayment({
...params,
package: params.packageValue,
success: () => {
setPayInfo();
Taro.redirectTo({
url: "/pages/MineUserAll/AllOrder/index",
});
Taro.showToast({
title: "支付成功",
icon: "none",
duration: 2000,
});
},
fail: (e) => {
Taro.showToast({
title: "支付失败",
icon: "none",
duration: 2000,
});
},
});
};
const onShowPay = (e) => {
if (agreement) {
if (payInfo) {
requestPayment(payInfo);
return;
}
Taro.showLoading({
title: '支付中',
})
if (packageId) {
saveOrder(
list.map((x) => {
return {
amount: x.amount || 1,
itemId: x.packageId,
price: x.actualPrice,
};
})
).then((res) => {
setPayInfo(res);
requestPayment(res);
}).catch(() => {
Taro.hideLoading()
Taro.showToast({
title: "支付失败",
icon: "none",
duration: 2000,
});
});
}
if (orderId) {
payOrder(orderId).then((res) => {
setPayInfo(res);
requestPayment(res);
}).catch(() => {
Taro.hideLoading()
Taro.showToast({
title: "支付失败",
icon: "none",
duration: 2000,
});
});
}
} else {
Taro.showToast({
title: "请勾选《平台用户服务协议》",
icon: "none",
duration: 2000,
});
}
};
useEffect(() => {
if (packageId) {
getPackageDetail(packageId).then((res) => {
setList([res]);
});
}
if (orderId) {
getOrderSub({ pageNum: 1, pageSize: 999, orderId }).then((res) => {
setList(res.records);
});
}
}, [packageId, orderId]);
//用户协议
const goRules = () => {
Taro.navigateTo({ url: `/pages/MineUserAll/Rules/index` })
}
useEffect(() => {
if (BuyNumber < 2) {
setDisabledBool(true);
} else {
setDisabledBool(false);
}
}, [BuyNumber]);
useEffect(() => {
let total = {
cashback: 0,
actualPrice: 0,
standardPrice: 0,
};
list.map((x) => {
total.cashback += x.cashback * (x.amount || 1);
total.actualPrice += (x.actualPrice || x.unitPrice) * (x.amount || 1);
total.standardPrice += x.standardPrice * (x.amount || 1);
});
setTotalPrice(total);
}, [list]);
return (
!person.phone ? :
setBuyNumber(x)} style={{ marginTop: '40rpx' }} />
{(list || []).map((item, index) => {
return (
);
})}
{/* 协议条款 */}
手机号码:
{person.phone}
{
setAgreement(!agreement);
}}
>
我已阅读知晓并同意
《平台用户服务协议》
返现金将以退款形式到账
{/* 支付按钮 */}
);
});