Detail.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. var purchaseQuantity = 1
  2. var areaId = 0
  3. function init(skuHistoricalPrice, skuPrice) {
  4. var priceInput = document.getElementById("irpc-input")
  5. priceInput.addEventListener("input", (e) => {
  6. purchaseQuantity = e.target.value
  7. if (purchaseQuantity <= 0) {
  8. purchaseQuantity = 1
  9. }
  10. if (purchaseQuantity > 999) {
  11. purchaseQuantity = 999
  12. }
  13. })
  14. var pchp = document.getElementById("price-calc-historical-price")
  15. var pcp = document.getElementById("price-calc-price")
  16. setInterval(() => {
  17. priceInput.value = purchaseQuantity
  18. //计算价格
  19. if (pchp) {
  20. pchp.innerText = (skuHistoricalPrice * purchaseQuantity).toFixed(2)
  21. }
  22. if (pcp) {
  23. pcp.innerText = (skuPrice * purchaseQuantity).toFixed(2)
  24. }
  25. }, 100)
  26. }
  27. // 购买数量减
  28. function purchaseQuantityAdd() {
  29. if (purchaseQuantity >= 999) {
  30. purchaseQuantity = 999
  31. return
  32. }
  33. purchaseQuantity++;
  34. }
  35. // 购买数量减
  36. function purchaseQuantityMinus() {
  37. if (purchaseQuantity <= 1) {
  38. purchaseQuantity = 1
  39. return
  40. }
  41. purchaseQuantity--;
  42. }
  43. function GetAreaById(areaId) {
  44. let areaEles = document.getElementsByClassName("info-left-select-btn")
  45. for (let i = 0; i < areaEles.length; i++) {
  46. if (areaEles[i].getAttribute("areaId") == areaId) {
  47. areaEles[i].style.backgroundColor = "rgb(199, 199, 199)"
  48. } else {
  49. areaEles[i].style.backgroundColor = ""
  50. }
  51. }
  52. console.log(areaId)
  53. let eles = document.getElementsByClassName("info-left-select-param-btn");
  54. for (let i = 0; i < eles.length; i++) {
  55. if (eles[i].getAttribute("areaId") == areaId) {
  56. eles[i].style = ""
  57. } else {
  58. eles[i].style.display = "none"
  59. }
  60. }
  61. return areaId
  62. }
  63. function RouterJumpId(id) {
  64. window.location.href = "?skuId=" + id
  65. }
  66. //TODO a
  67. function orderSubmit() {
  68. const data = {
  69. skuId: skuData.id,
  70. count: purchaseQuantity,
  71. //优惠券
  72. couponUserId: 0,
  73. //联系信息
  74. contactInformation: ""
  75. };
  76. fetch('/api/order/submit', {
  77. method: 'POST',
  78. headers: {
  79. 'Content-Type': 'application/json'
  80. },
  81. body: JSON.stringify(data)
  82. })
  83. .then(response => response.json())
  84. .then(data => {
  85. console.log('Success:', data);
  86. if (data.code === 200) {
  87. let orderAmount = document.getElementById("order-amount")
  88. let orderPrice = document.getElementById("order-price")
  89. let orderHistoricalPrice = document.getElementById("order-historical-price")
  90. let orderCount = document.getElementById("order-count")
  91. let orderBuyCurtain = document.getElementById("order-buy-curtain")
  92. orderBuyCurtain.style.display = ""
  93. orderCount.innerText = purchaseQuantity
  94. orderPrice.innerText = (skuPrice * purchaseQuantity).toFixed(2)
  95. orderAmount.innerText = (skuPrice * purchaseQuantity).toFixed(2)
  96. orderHistoricalPrice.innerText = (skuHistoricalPrice * purchaseQuantity).toFixed(2)
  97. }
  98. alert(data.msg);
  99. })
  100. .catch((error) => {
  101. console.error('Error:', error);
  102. alert("购买失败");
  103. });
  104. }
  105. let selectIndex = 0;
  106. let nowSku = {Price: 100, SkuImage: "path/to/image", SkuName: "SKU 名称", HistoricalPrices: 120}; // Example data
  107. let count = 1;
  108. let goodsName = "商品名称"; // Example data
  109. function closeView() {
  110. document.querySelector(".order-buy-curtain").style.display = "none";
  111. }
  112. function selectPayMethod(index) {
  113. selectIndex = index;
  114. document.querySelectorAll(".order-buy-pay-item").forEach((item, idx) => {
  115. item.style.backgroundColor = idx === index ? "#cccccc" : "#dddddd";
  116. });
  117. updatePaySelectPosition();
  118. document.getElementById("test-purchase").style.display = index === 2 ? "flex" : "none";
  119. }
  120. function updatePaySelectPosition() {
  121. const select = document.getElementById("pay-select");
  122. const wxColor = "rgba(0, 168, 242, 1)";
  123. const zfbColor = "rgba(1, 183, 0, 1)";
  124. const pyColor = "rgba(234, 98, 0, 1)";
  125. let bc = "";
  126. switch (selectIndex) {
  127. case 0:
  128. bc = wxColor;
  129. break;
  130. case 1:
  131. bc = zfbColor;
  132. break;
  133. case 2:
  134. bc = pyColor;
  135. break;
  136. }
  137. select.style.left = 10 + (selectIndex * (10 + 340 / 3)) + "px";
  138. select.style.borderColor = bc;
  139. }
  140. function orderTest() {
  141. console.log("Test order submitted");
  142. alert("假装购买成功");
  143. }
  144. document.addEventListener("DOMContentLoaded", function () {
  145. selectPayMethod(0); // Set default selection
  146. });