123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import withLayout from "@/layouts";
  2. import image from "@/assets/icons/ProCard/8kb.jpg";
  3. import { useState, useEffect } from "react";
  4. import { Button, Checkbox, Input } from "@tarojs/components";
  5. import food from "@/assets/icons/ProCard/food.png";
  6. import ProCard_hot from "@/assets/icons/ProCard/ProCard_hot.png";
  7. import CustomNav from "@/components/CustomNav";
  8. import OrderMolded from "@/components/OrderMolded";
  9. import Popup from "@/components/Popup";
  10. import "./style.less";
  11. import Taro, { useDidShow } from "@tarojs/taro";
  12. import { getShopPackageDetail } from "@/services/home";
  13. import { saveOrder, getOrderSub, payOrder } from "@/services/payOrder";
  14. import formatTime from "@/utils/formatTime";
  15. export default withLayout((props) => {
  16. const { router, person } = props;
  17. const { packageId, orderId } = props.router.params;
  18. const [payInfo,setPayInfo] = useState()
  19. const [showDialog, setShowDialog] = useState(false);
  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 [DisabledBool, setDisabledBool] = 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. setList(
  40. list.map((x) =>
  41. x.packageId == detail.packageId ? { ...x, amount: BuyNumber } : x
  42. )
  43. );
  44. setShowDialog(false);
  45. };
  46. const NumberAdd = () => {
  47. setBuyNumber(BuyNumber + 1);
  48. console.log(BuyNumber);
  49. };
  50. const NumberCut = () => {
  51. if (DisabledBool) return;
  52. setBuyNumber(BuyNumber - 1);
  53. console.log(BuyNumber);
  54. };
  55. const onInput = (e) => {
  56. let values = e.detail.value;
  57. setBuyNumber(values - 0);
  58. };
  59. const requestPayment = (params) =>{
  60. console.log(params,'requestPayment')
  61. Taro.requestPayment({
  62. ...params,
  63. package: params.packageValue,
  64. success: () => {
  65. console.log("success");
  66. setPayInfo()
  67. Taro.showToast({
  68. title: "支付成功",
  69. icon: "none",
  70. duration: 2000,
  71. });
  72. },
  73. fail: (e) => {
  74. console.log(e, "fail");
  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. if (packageId) {
  90. saveOrder(
  91. list.map((x) => {
  92. return {
  93. amount: x.amount || 1,
  94. itemId: x.packageId,
  95. price: x.actualPrice,
  96. };
  97. })
  98. ).then((res) => {
  99. setPayInfo(res)
  100. requestPayment(res)
  101. });
  102. }
  103. if (orderId) {
  104. payOrder(orderId).then((res) => {
  105. setPayInfo(res)
  106. requestPayment(res)
  107. });
  108. }
  109. } else {
  110. Taro.showToast({
  111. title: "请勾选《平台用户服务协议》",
  112. icon: "none",
  113. duration: 2000,
  114. });
  115. }
  116. };
  117. useEffect(() => {
  118. if (packageId) {
  119. getShopPackageDetail(packageId).then((res) => {
  120. setList([res]);
  121. });
  122. }
  123. if (orderId) {
  124. getOrderSub({ pageNum: 1, pageSize: 999, orderId }).then((res) => {
  125. setList(res.records);
  126. });
  127. }
  128. }, [packageId, orderId]);
  129. useEffect(() => {
  130. if (BuyNumber < 2) {
  131. setDisabledBool(true);
  132. } else {
  133. setDisabledBool(false);
  134. }
  135. }, [BuyNumber]);
  136. useEffect(() => {
  137. let total = {
  138. cashback: 0,
  139. actualPrice: 0,
  140. standardPrice: 0,
  141. };
  142. list.map((x) => {
  143. total.cashback += x.cashback * (x.amount || 1);
  144. total.actualPrice += (x.actualPrice || x.unitPrice) * (x.amount || 1);
  145. total.standardPrice += x.standardPrice * (x.amount || 1);
  146. });
  147. setTotalPrice(total);
  148. }, [list]);
  149. return (
  150. <view class="container">
  151. <CustomNav title="订单" />
  152. <Popup show={showDialog} maskClosable={false}>
  153. <OrderMolded item={detail}/>
  154. <view className="item-center-Number">
  155. <view
  156. className="buy-num-minus"
  157. disabled={DisabledBool}
  158. onClick={NumberCut}
  159. >
  160. -
  161. </view>
  162. <Input
  163. className="buy-num-input"
  164. type="number"
  165. min="1"
  166. onInput={onInput}
  167. value={BuyNumber}
  168. />
  169. <view className="buy-num-add" onClick={NumberAdd}>
  170. +
  171. </view>
  172. </view>
  173. <view className="buy-button-box">
  174. <button className="button-Cancel" onClick={ButtonCancel}>
  175. 取消
  176. </button>
  177. <button className="button-OK" onClick={ButtonOK}>
  178. 确定
  179. </button>
  180. </view>
  181. </Popup>
  182. <view class="coupon-list">
  183. {list?.map((item) => {
  184. return (
  185. <view class="wrapper">
  186. <view class="left-complete-one">
  187. <image className="left-image-1" src={ProCard_hot}></image>
  188. <view className="left-viewText">
  189. 返现¥{(item.cashback || 0) / 100}
  190. </view>
  191. <view className="title-image">
  192. <image
  193. className="image-1"
  194. mode="scaleToFill"
  195. src={item.poster}
  196. ></image>
  197. <image className="image-2" src={food}></image>
  198. </view>
  199. <view className="title-content">
  200. <view className="Pro-title">
  201. <view className="title-text">{item.description}</view>
  202. </view>
  203. <text className="title-money">
  204. ¥{(item.actualPrice || item.unitPrice || 0) / 100}元
  205. <text className="title-money-2">
  206. 门市价{(item.standardPrice || 0) / 100}元
  207. </text>
  208. </text>
  209. <view className="title-time">
  210. 有效期:{formatTime(item.startTime, "yyyy/MM/dd")}-
  211. {formatTime(item.endTime, "yyyy/MM/dd")}
  212. </view>
  213. </view>
  214. </view>
  215. <view
  216. class="right-complete-two"
  217. onClick={() => ShowMoldeOn(item)}
  218. >
  219. <view className="right-content">
  220. <view className="right-number">×{item.amount || 1}</view>
  221. <view className="right-title">数量</view>
  222. </view>
  223. </view>
  224. </view>
  225. );
  226. })}
  227. </view>
  228. <view className="view-button">
  229. <view className="Card-number-box">
  230. <view className="Card-number">
  231. <text style="left:29px;position: absolute; padding-top:20px">
  232. 手机号码:
  233. </text>
  234. <text style="right:29px;position: absolute;padding-top:20px">
  235. {person.phone}
  236. </text>
  237. </view>
  238. <view className="Card-user">
  239. <Checkbox
  240. style="padding-left:20px; position: relative; top:24px;"
  241. value="agreement"
  242. checked={agreement}
  243. onClick={() => {
  244. setAgreement(!agreement);
  245. }}
  246. >
  247. 我已阅读知晓并同意
  248. <text style="color:#274190;text-decoration:underline;font-weight:400">
  249. 《平台用户服务协议》
  250. </text>
  251. </Checkbox>
  252. </view>
  253. </view>
  254. <view className="button-box-button-box">
  255. <view className="button-text-image">
  256. <text className="button-text-money-hot">
  257. 返现¥{(totalPrice?.cashback || 0) / 100}
  258. </text>
  259. <image className="button-image" src={ProCard_hot}>
  260. 123123
  261. </image>
  262. <view className="button-text-money">
  263. 门店市面价:{(totalPrice?.standardPrice || 0) / 100}元
  264. </view>
  265. </view>
  266. <Button className="button-box-one" onClick={() => onShowPay()}>
  267. ¥{(totalPrice?.actualPrice || 0) / 100}元
  268. <text className="button-text-one">支付订单</text>
  269. </Button>
  270. </view>
  271. </view>
  272. </view>
  273. );
  274. });