index.jsx 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. 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. let total = {
  152. cashback: 0,
  153. actualPrice: 0,
  154. standardPrice: 0,
  155. };
  156. list.map((x) => {
  157. total.cashback += x.cashback * (x.amount || 1);
  158. total.actualPrice += (x.actualPrice || x.unitPrice) * (x.amount || 1);
  159. total.standardPrice += x.standardPrice * (x.amount || 1);
  160. });
  161. setTotalPrice(total);
  162. }, [list]);
  163. return (
  164. !person.phone ? <AuthPage /> :
  165. <view class='page-index container'>
  166. <view className='index-navbar'>
  167. <CustomNav title='订单' />
  168. </view>
  169. <Popup show={showDialog} maskClosable={false}>
  170. <OrderMolded item={detail} />
  171. <InputNumber value={BuyNumber} onChange={x => setBuyNumber(x)} style={{ marginTop: '40rpx' }} />
  172. <view className='buy-button-box'>
  173. <button className='button-Cancel' onClick={ButtonCancel}>
  174. 取消
  175. </button>
  176. <button className='button-OK' onClick={ButtonOK}>
  177. 确定
  178. </button>
  179. </view>
  180. </Popup>
  181. <view class='coupon-list-box'>
  182. {(list || []).map((item, index) => {
  183. return (
  184. <Card key={index} item={item} packageId={packageId} onMoldeOn={ShowMoldeOn} />
  185. );
  186. })}
  187. </view>
  188. <view className='view-button'>
  189. {/* 协议条款 */}
  190. <view className='Card-number-box'>
  191. <view className='Card-number'>
  192. <text>
  193. 手机号码:
  194. </text>
  195. <text style='right:0;position: absolute;'>
  196. {person.phone}
  197. </text>
  198. </view>
  199. <view className='Card-user'>
  200. <Radio
  201. className='Radio-text'
  202. value='agreement'
  203. checked={agreement}
  204. onClick={() => {
  205. setAgreement(!agreement);
  206. }}
  207. >
  208. 我已阅读知晓并同意
  209. </Radio>
  210. <text className='ptxy' onClick={goRules}>
  211. 《平台用户服务协议》
  212. </text>
  213. </view>
  214. <view className='ul-li-text'>
  215. <view className='ul-li-view'></view>
  216. <text className='ul-li-textname'>返现金将以退款形式到账</text>
  217. </view>
  218. </view>
  219. {/* 支付按钮 */}
  220. <Button className='payorder' onClick={() => onShowPay()}>
  221. <View className='paytext'>¥{formatPrice(totalPrice?.actualPrice || 0)}元</View>
  222. <View className='paycontent'>
  223. <View className='paybuttontop'>返现¥{formatPrice(totalPrice?.cashback || 0)}</View>
  224. <View className='paybuttonbottom'>门店市面价:{formatPrice(totalPrice?.standardPrice || 0)}元</View>
  225. </View>
  226. <View className='paytext'>支付订单</View>
  227. </Button>
  228. </view>
  229. </view>
  230. );
  231. });