index.jsx 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. import withLayout from "@/layouts";
  2. import Taro, { useDidShow } from "@tarojs/taro";
  3. import { getPackageDetail } from "@/services/home";
  4. import { saveOrder, getOrderSub, payOrder } from "@/services/payOrder";
  5. import { useState, useEffect } from "react";
  6. import formatPrice from "@/utils/formatPrice";
  7. import { Button, Radio, View } from "@tarojs/components";
  8. import InputNumber from "@/components/InputNumber";
  9. import AuthPage from '@/components/AuthPage'
  10. import CustomNav from "@/components/CustomNav";
  11. import OrderMolded from "@/components/OrderMolded";
  12. import Popup from "@/components/Popup";
  13. import Card from "./Card";
  14. import "./style.less";
  15. export default withLayout((props) => {
  16. //#region
  17. const { router, person } = props;
  18. const { packageId, orderId } = props.router.params;
  19. const [payInfo, setPayInfo] = useState();
  20. // 是否已阅读协议
  21. const [agreement, setAgreement] = useState(false);
  22. // 总价 totalPrice
  23. const [totalPrice, setTotalPrice] = useState({});
  24. const [list, setList] = useState([]);
  25. const [BuyNumber, setBuyNumber] = useState(1);
  26. const [detail, setDetail] = useState({});
  27. const [showDialog, setShowDialog] = useState(false);
  28. const [DisabledBool, setDisabledBool] = useState(false);
  29. const ShowMoldeOn = (e) => {
  30. if (packageId) {
  31. setBuyNumber(e.amount || 1);
  32. setDetail(e);
  33. setShowDialog(true);
  34. }
  35. };
  36. const ButtonCancel = () => {
  37. setShowDialog(false);
  38. };
  39. const ButtonOK = (e) => {
  40. setList(
  41. list.map((x) =>
  42. x.packageId == detail.packageId ? { ...x, amount: BuyNumber } : x
  43. )
  44. );
  45. setShowDialog(false);
  46. };
  47. const NumberAdd = () => {
  48. setBuyNumber(BuyNumber + 1);
  49. };
  50. const NumberCut = () => {
  51. if (DisabledBool) return;
  52. setBuyNumber(BuyNumber - 1);
  53. };
  54. const onInput = (e) => {
  55. let values = e.detail.value;
  56. setBuyNumber(values - 0);
  57. };
  58. const requestPayment = (params) => {
  59. Taro.hideLoading()
  60. Taro.requestPayment({
  61. ...params,
  62. package: params.packageValue,
  63. success: () => {
  64. setPayInfo();
  65. Taro.redirectTo({
  66. url: "/pages/MineUserAll/AllOrder/index",
  67. });
  68. Taro.showToast({
  69. title: "支付成功",
  70. icon: "none",
  71. duration: 2000,
  72. });
  73. },
  74. fail: (e) => {
  75. Taro.showToast({
  76. title: "支付失败",
  77. icon: "none",
  78. duration: 2000,
  79. });
  80. },
  81. });
  82. };
  83. const onShowPay = (e) => {
  84. if (agreement) {
  85. if (payInfo) {
  86. requestPayment(payInfo);
  87. return;
  88. }
  89. Taro.showLoading({
  90. title: '支付中',
  91. })
  92. if (packageId) {
  93. saveOrder(
  94. list.map((x) => {
  95. return {
  96. amount: x.amount || 1,
  97. itemId: x.packageId,
  98. price: x.actualPrice,
  99. };
  100. })
  101. ).then((res) => {
  102. setPayInfo(res);
  103. requestPayment(res);
  104. }).catch(() => {
  105. Taro.hideLoading()
  106. Taro.showToast({
  107. title: "支付失败",
  108. icon: "none",
  109. duration: 2000,
  110. });
  111. });
  112. }
  113. if (orderId) {
  114. payOrder(orderId).then((res) => {
  115. setPayInfo(res);
  116. requestPayment(res);
  117. }).catch(() => {
  118. Taro.hideLoading()
  119. Taro.showToast({
  120. title: "支付失败",
  121. icon: "none",
  122. duration: 2000,
  123. });
  124. });
  125. }
  126. } else {
  127. Taro.showToast({
  128. title: "请勾选《平台用户服务协议》",
  129. icon: "none",
  130. duration: 2000,
  131. });
  132. }
  133. };
  134. useEffect(() => {
  135. if (packageId) {
  136. getPackageDetail(packageId).then((res) => {
  137. setList([res]);
  138. });
  139. }
  140. if (orderId) {
  141. getOrderSub({ pageNum: 1, pageSize: 999, orderId }).then((res) => {
  142. setList(res.records);
  143. });
  144. }
  145. }, [packageId, orderId]);
  146. //用户协议
  147. const goRules = () => {
  148. Taro.navigateTo({ url: `/pages/MineUserAll/Rules/index` })
  149. }
  150. useEffect(() => {
  151. if (BuyNumber < 2) {
  152. setDisabledBool(true);
  153. } else {
  154. setDisabledBool(false);
  155. }
  156. }, [BuyNumber]);
  157. useEffect(() => {
  158. let total = {
  159. cashback: 0,
  160. actualPrice: 0,
  161. standardPrice: 0,
  162. };
  163. list.map((x) => {
  164. total.cashback += x.cashback * (x.amount || 1);
  165. total.actualPrice += (x.actualPrice || x.unitPrice) * (x.amount || 1);
  166. total.standardPrice += x.standardPrice * (x.amount || 1);
  167. });
  168. setTotalPrice(total);
  169. }, [list]);
  170. return (
  171. !person.phone ? <AuthPage /> :
  172. <view class='page-index container'>
  173. <view className='index-navbar'>
  174. <CustomNav title='订单' />
  175. </view>
  176. <Popup show={showDialog} maskClosable={false}>
  177. <OrderMolded item={detail} />
  178. <InputNumber value={BuyNumber} onChange={x => setBuyNumber(x)} style={{ marginTop: '40rpx' }} />
  179. <view className='buy-button-box'>
  180. <button className='button-Cancel' onClick={ButtonCancel}>
  181. 取消
  182. </button>
  183. <button className='button-OK' onClick={ButtonOK}>
  184. 确定
  185. </button>
  186. </view>
  187. </Popup>
  188. <view class='coupon-list-box'>
  189. {(list || []).map((item, index) => {
  190. return (
  191. <Card key={index} item={item} packageId={packageId} onMoldeOn={ShowMoldeOn} />
  192. );
  193. })}
  194. </view>
  195. <view className='view-button'>
  196. {/* 协议条款 */}
  197. <view className='Card-number-box'>
  198. <view className='Card-number'>
  199. <text>
  200. 手机号码:
  201. </text>
  202. <text style='right:0;position: absolute;'>
  203. {person.phone}
  204. </text>
  205. </view>
  206. <view className='Card-user'>
  207. <Radio
  208. className='Radio-text'
  209. value='agreement'
  210. checked={agreement}
  211. onClick={() => {
  212. setAgreement(!agreement);
  213. }}
  214. >
  215. 我已阅读知晓并同意
  216. </Radio>
  217. <text className='ptxy' onClick={goRules}>
  218. 《平台用户服务协议》
  219. </text>
  220. </view>
  221. <view className='ul-li-text'>
  222. <view className='ul-li-view'></view>
  223. <text className='ul-li-textname'>返现金将以退款形式到账</text>
  224. </view>
  225. </view>
  226. {/* 支付按钮 */}
  227. <Button className='payorder' onClick={() => onShowPay()}>
  228. <View className='paytext'>¥{formatPrice(totalPrice?.actualPrice || 0)}元</View>
  229. <View className='paycontent'>
  230. <View className='paybuttontop'>返现¥{formatPrice(totalPrice?.cashback || 0)}</View>
  231. <View className='paybuttonbottom'>门店市面价:{formatPrice(totalPrice?.standardPrice || 0)}元</View>
  232. </View>
  233. <View className='paytext'>支付订单</View>
  234. </Button>
  235. </view>
  236. </view>
  237. );
  238. });