123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { useState, useEffect } from "react";
  2. import withLayout from "@/layouts";
  3. import Taro, { useDidShow } from "@tarojs/taro";
  4. import { getPackageDetail } from "@/services/home";
  5. import { saveOrder, getOrderSub, payOrder } from "@/services/payOrder";
  6. import formatPrice from "@/utils/formatPrice";
  7. import usePrevious from "@/utils/hooks/usePrevious";
  8. import { Button, Radio, View } from "@tarojs/components";
  9. import InputNumber from "@/components/InputNumber";
  10. import AuthPage from '@/components/AuthPage'
  11. import CustomNav from "@/components/CustomNav";
  12. import OrderMolded from "@/components/OrderMolded";
  13. import Popup from "@/components/Popup";
  14. import Card from "./Card";
  15. import "./style.less";
  16. export default withLayout((props) => {
  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 ShowMoldeOn = (e) => {
  29. if (packageId) {
  30. setBuyNumber(e.amount || 1);
  31. setDetail(e);
  32. setShowDialog(true);
  33. }
  34. };
  35. const ButtonCancel = () => {
  36. setShowDialog(false);
  37. };
  38. const ButtonOK = (e) => {
  39. if (BuyNumber <= 0) {
  40. Taro.showToast({
  41. title: '最少需要一个套餐哦',
  42. icon: 'none',
  43. duration: 2000
  44. })
  45. } else {
  46. setList(
  47. list.map((x) =>
  48. x.packageId == detail.packageId ? { ...x, amount: BuyNumber } : x
  49. )
  50. );
  51. setShowDialog(false);
  52. }
  53. };
  54. const requestPayment = (params) => {
  55. Taro.hideLoading()
  56. Taro.requestPayment({
  57. ...params,
  58. package: params.packageValue,
  59. success: () => {
  60. setPayInfo();
  61. Taro.redirectTo({
  62. url: "/pages/MineUserAll/AllOrder/index",
  63. });
  64. Taro.showToast({
  65. title: "支付成功",
  66. icon: "none",
  67. duration: 2000,
  68. });
  69. },
  70. fail: (e) => {
  71. Taro.showToast({
  72. title: "支付失败",
  73. icon: "none",
  74. duration: 2000,
  75. });
  76. // 跳转到待支付
  77. Taro.navigateTo({
  78. url: `/pages/MineUserAll/AllOrder/index?tabJump=1`
  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. if (!totalPrice?.actualPrice) {
  94. Taro.showToast({
  95. title: '请选择有效账单',
  96. icon: 'none'
  97. })
  98. return
  99. }
  100. saveOrder(
  101. list.map((x) => {
  102. return {
  103. amount: x.amount || 1,
  104. itemId: x.packageId,
  105. price: x.actualPrice,
  106. };
  107. })
  108. ).then((res) => {
  109. // setPayInfo(res);
  110. requestPayment(res);
  111. }).catch(() => {
  112. Taro.hideLoading()
  113. Taro.showToast({
  114. title: "支付失败",
  115. icon: "none",
  116. duration: 2000,
  117. });
  118. });
  119. }
  120. if (orderId) {
  121. payOrder(orderId).then((res) => {
  122. // setPayInfo(res);
  123. requestPayment(res);
  124. }).catch(() => {
  125. Taro.hideLoading()
  126. Taro.showToast({
  127. title: "支付失败",
  128. icon: "none",
  129. duration: 2000,
  130. });
  131. });
  132. }
  133. } else {
  134. Taro.showToast({
  135. title: "请勾选《平台用户服务协议》",
  136. icon: "none",
  137. duration: 2000,
  138. });
  139. }
  140. };
  141. useEffect(() => {
  142. if (packageId) {
  143. getPackageDetail(packageId).then((res) => {
  144. setList([res]);
  145. });
  146. }
  147. if (orderId) {
  148. getOrderSub({ pageNum: 1, pageSize: 999, orderId }).then((res) => {
  149. setList(res.records);
  150. });
  151. }
  152. }, [packageId, orderId]);
  153. //用户协议
  154. const goRules = () => {
  155. Taro.navigateTo({ url: `/pages/MineUserAll/Rules/index` })
  156. }
  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. });