shoppingCart.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. {extend name="base" /}
  2. {block name="title"}Cart{/block}
  3. {block name="styles"}
  4. <link rel="stylesheet" href="/assets/index/css/shoppingCart.css?t=5">
  5. {/block}
  6. {block name="mainArea"}
  7. <section class="pageContainer">
  8. <h1 class="pageTitle">Shopping Cart</h1>
  9. <div class="pageDataBox">
  10. <!-- 商品模块 -->
  11. <section class="productConatiner">
  12. <h3 class="productModelTitle">Product</h3>
  13. <div class="productList">
  14. {foreach $list as $cart}
  15. <div class="productItem" data-id="{$cart['id']}" goods-id="{$cart['goods']['goods_id']}"
  16. goods-sku-id="{$cart['goods']['skuInfo']['goods_sku_id']}"
  17. data-goods-num="{$cart['goods_num']}">
  18. <div class="productImgBox">
  19. <img class="lazy" src="/assets/index/asstes/icon/transparent.png"
  20. data-original="{$cart['goods']['goods_image']}" alt="提示信息seo"
  21. title="提示信息seo">
  22. </div>
  23. <div class="productInfo">
  24. <div class="lfBox">
  25. <h2 class="productTitle">{$cart['goods']['goods_name']}</h2>
  26. <p class="specs">specs</p>
  27. </div>
  28. <div class="nunberBox">
  29. <p class="numberReduce">-</p>
  30. <p class="numberInput">{$cart['goods_num']}</p>
  31. <p class="numberAdd">+</p>
  32. </div>
  33. <p class="productPrice">${$cart['goods']['goods_price_min']}</p>
  34. <div class="productDelete">
  35. <img src="/assets/index/asstes/icon/productDelete.png" alt="">
  36. </div>
  37. </div>
  38. </div>
  39. {/foreach}
  40. </div>
  41. </section>
  42. <!-- 结算模块 -->
  43. <section class="windContainer">
  44. <div class="windTBox">
  45. <p class="lt">CART TOTALS</p>
  46. <p class="cartTotals"></p>
  47. </div>
  48. <div class="linkItem">
  49. <div class="windInfoBox">
  50. <p class="key">Subtotal</p>
  51. <p class="val">${$cartMoney}</p>
  52. </div>
  53. </div>
  54. <div class="mbItem">
  55. <div class="windInfoBox">
  56. <p class="key">Free Shipping</p>
  57. <p class="val">$0.00</p>
  58. </div>
  59. <!-- <div class="windInfoBox">-->
  60. <!-- <p class="key">Free Shipping</p>-->
  61. <!-- </div>-->
  62. </div>
  63. <div class="mbItem" id="addressInfo">
  64. <div class="windInfoBox">
  65. <div class="key">
  66. <p class="keyItem" id="fullNameText">{$fullName}</p>
  67. <p class="keyItem" id="contactNumberText">{$phone}</p>
  68. <p class="keyItem" id="emailText">{$email}</p>
  69. <p class="keyItem" id="addressText">{$full}</p>
  70. </div>
  71. </div>
  72. <div class="windInfoBox">
  73. <p class="key" id="changeAddress">Change address</p> Or
  74. <p class="key" id="selectAddress">Select address</p>
  75. </div>
  76. </div>
  77. <!-- 选择地址模块 -->
  78. <!-- <div id="addressChooseContainer">
  79. <div class="container">
  80. <h4>请选择一个选项:</h4>
  81. <div class="radio-item">
  82. <input type="radio" id="option1" name="options" value="选项1" class="selectOption">
  83. <label for="option1">选项1</label>
  84. <span class="delete-button" onclick="deleteOption('option1')">Delete</span>
  85. <span class="delete-button" onclick="deleteOption('option1')">设置默认地址</span>
  86. </div>
  87. <div class="radio-item">
  88. <input type="radio" id="option2" name="options" value="选项2" class="selectOption">
  89. <label for="option2">选项2</label>
  90. <span class="delete-button" onclick="deleteOption('option2')">删除</span>
  91. </div>
  92. <div class="radio-item">
  93. <input type="radio" id="option3" name="options" value="选项3" class="selectOption">
  94. <label for="option3">选项3</label>
  95. <span class="delete-button" onclick="deleteOption('option3')">删除</span>
  96. </div>
  97. <input type="hidden" id="selectedOption" name="selectedOption" value="">
  98. </div>
  99. </div>-->
  100. <!-- 填写地址模块 -->
  101. <div id="addressContainer">
  102. <!-- 名字 -->
  103. <div class="addressItem">
  104. <p class="addItmeTitle">First Name:</p>
  105. <input class="addInput" id="userName" type="text" placeholder="First Name">
  106. </div>
  107. <div class="addressItem">
  108. <p class="addItmeTitle">Last Name:</p>
  109. <input class="addInput" id="lastName" type="text" placeholder="Last Name">
  110. </div>
  111. <!-- 手机号码 -->
  112. <div class="addressItem">
  113. <p class="addItmeTitle">Contact Number:</p>
  114. <input class="addInput" id="phoneNumber" type="text" placeholder="Contact Number">
  115. </div>
  116. <!-- 地区 -->
  117. <div class="addressItem">
  118. <p class="addItmeTitle">State:</p>
  119. <select id="areaSelect">
  120. <option value="0" selected>--Choose A state--</option>
  121. {foreach $states50 as $state}
  122. <option value="{$state['id']}">{$state['name']}</option>
  123. {/foreach}
  124. </select>
  125. </div>
  126. <!-- 详情地址 -->
  127. <div class="addressItem">
  128. <p class="addItmeTitle">Address:</p>
  129. <input class="addInput" id="address" type="text" placeholder="Address">
  130. </div>
  131. <!-- 邮箱 -->
  132. <div class="addressItem">
  133. <p class="addItmeTitle">Mailbox:</p>
  134. <input type="text" id="mailbox" class="addInput" placeholder="Mailbox">
  135. </div>
  136. <!-- 邮政编码 -->
  137. <div class="addressItem">
  138. <p class="addItmeTitle">Zip Code:</p>
  139. <input class="addInput" id="postalCode" type="text" placeholder="Zip Code">
  140. </div>
  141. <div id="addressButModel">
  142. <div class="addressButtonBox">
  143. <div id="addCancel">Cancel</div>
  144. <div id="addSave">Save</div>
  145. </div>
  146. </div>
  147. </div>
  148. <!-- <div class="mbItem">
  149. <div class="windInfoBox freeshipping">
  150. <p class="key">Free shipping</p>
  151. </div>
  152. <div class="windInfoBox">
  153. <p class="key">$199.99</p>
  154. </div>
  155. </div>-->
  156. <div class="totalBox">
  157. <p class="totalT">Total</p>
  158. <p class="totalVal">${$cartMoney}</p>
  159. </div>
  160. <div class="mbItem">
  161. <div class="windInfoBox freeshipping">
  162. {if $payByPoints}
  163. <p class="key">You can redeem this order for free using your points! REDEEM NOW.</p>
  164. {/if}
  165. </div>
  166. <div class="windInfoBox">
  167. <p class="key">Your Points:{$points}</p>
  168. </div>
  169. </div>
  170. <div class="payPalButton">
  171. <span>PayPal</span>
  172. </div>
  173. <div class="orBox">
  174. <div class="link"></div>
  175. <p class="or">OR</p>
  176. <div class="link"></div>
  177. </div>
  178. <div class="checkoutButton">
  179. <span>Redemption using Points</span>
  180. </div>
  181. </section>
  182. </div>
  183. </section>
  184. {/block}
  185. {block name="components"}
  186. <div id="loadingModel">
  187. <div class="loadingBox">
  188. <img src="/assets/index/asstes/icon/loading.gif" alt="Jumping to paypal.">
  189. </div>
  190. </div>
  191. {/block}
  192. {block name="scriptSrc"}
  193. <script>
  194. $(document).ready(function () {
  195. //触屏事件 || 点击事件
  196. const tap = "ontouchstart" in window ? "touchstart" : "click";
  197. var addressId = {$addressId};
  198. var payByPoints = "{$payByPoints}";
  199. //console.log(payByPoints, payByPoints)
  200. //默认地址
  201. const defaultAddressInfo = {
  202. userName: "{$name}",
  203. lastName: "{$lastName}",
  204. phoneNumber: "{$phone}",
  205. areaSelect: "{$regionId}",
  206. address: "{$detail}",
  207. mailbox: "{$email}",
  208. postalCode: "{$zipCode}"
  209. }
  210. // 是否已填写过地址
  211. if (addressId) {
  212. var isAddress = true
  213. } else {
  214. var isAddress = false
  215. }
  216. //初始化地址模块标签显示隐藏
  217. function initAddressLabel() {
  218. //是否已填写过地址、显示默认地址,隐藏填写地址表单
  219. if (isAddress) {
  220. $("#addressInfo").show(0)
  221. $("#addressContainer").hide(0)
  222. } else {
  223. $("#addressInfo").hide(0)
  224. $("#addressContainer").show(0)
  225. $("#addressButModel").show(0)
  226. }
  227. }
  228. //初始化表单
  229. function initAddressForm() {
  230. const {userName, lastName, phoneNumber, areaSelect, address, mailbox, postalCode} = defaultAddressInfo || {}
  231. userName && $("#userName").val(userName)
  232. lastName && $("#lastName").val(lastName)
  233. phoneNumber && $("#phoneNumber").val(phoneNumber)
  234. areaSelect && $("#areaSelect").val(areaSelect)
  235. address && $("#address").val(address)
  236. mailbox && $("#mailbox").val(mailbox)
  237. postalCode && $("#postalCode").val(postalCode)
  238. }
  239. initAddressLabel()
  240. initAddressForm()
  241. //更改地址,需显示填写地址表单、取消保存按钮
  242. $("#changeAddress").on(tap, async function () {
  243. //获取地址列表
  244. /* $.ajax({
  245. url: "/index/address/list",
  246. method: 'GET',
  247. headers: {
  248. 'Content-Type': 'application/json',
  249. 'storeId': '10001',
  250. 'platform': 'H5',
  251. },
  252. dataType: 'json',
  253. success: function (obj) {
  254. if (obj.status === 200 || obj.status === '200') {
  255. console.log(obj.data)
  256. return false;
  257. showToast("Successful")
  258. window.location.href = '../cart/shoppingCart';
  259. } else if (obj.status === 401 || obj.status === '401') {
  260. window.location.replace('../passport/logIn.html')
  261. } else {
  262. showToast(obj.message)
  263. }
  264. },
  265. error: function (xhr, status, error) {
  266. reject(error);
  267. }
  268. });*/
  269. $("#addressContainer").show(0)
  270. $("#addressButModel").show(0)
  271. });
  272. //取消更改
  273. $("#addCancel").on(tap, async function () {
  274. $("#addressContainer").hide(0)
  275. });
  276. // 添加地址
  277. function addAddress() {
  278. const userNameEle = $("#userName")
  279. const lastNameEle = $("#lastName")
  280. const phoneNumberEle = $("#phoneNumber")
  281. const areaSelectEle = $("#areaSelect")
  282. const addressEle = $("#address")
  283. const mailboxEle = $("#mailbox")
  284. const postalCodeEle = $("#postalCode")
  285. // 名字
  286. const userName = userNameEle.val()
  287. const lastName = lastNameEle.val()
  288. // 手机号码
  289. const phoneNumber = phoneNumberEle.val()
  290. // 地区
  291. const areaSelect = areaSelectEle.val()
  292. // 详情地址
  293. const address = addressEle.val()
  294. // 邮箱
  295. const mailbox = mailboxEle.val()
  296. // 邮政编码
  297. const postalCode = postalCodeEle.val()
  298. if (!userName || userName === "") {
  299. userNameEle.focus()
  300. return
  301. }
  302. if (!phoneNumber || phoneNumber === "") {
  303. phoneNumberEle.focus()
  304. return
  305. }
  306. if (!areaSelect || areaSelect === "") {
  307. areaSelectEle.focus()
  308. return
  309. }
  310. if (!address || address === "") {
  311. addressEle.focus()
  312. return
  313. }
  314. const params = {
  315. form: {
  316. name: userName,
  317. last_name: lastName,
  318. phone: phoneNumber,
  319. email: mailbox,
  320. region: [{label: '', value: 1}, {label: '', value: 2}, {label: areaSelect, value: areaSelect}],
  321. //mailbox,
  322. detail: address,
  323. zip_code: postalCode
  324. }
  325. }
  326. return new Promise(function (resolve, reject) {
  327. $.ajax({
  328. url: "/index/address/add",
  329. method: 'POST',
  330. data: JSON.stringify(params),
  331. headers: {
  332. 'Content-Type': 'application/json',
  333. 'storeId': '10001',
  334. 'platform': 'H5',
  335. },
  336. dataType: 'json',
  337. success: function (response) {
  338. resolve(response);
  339. },
  340. error: function (xhr, status, error) {
  341. reject(error);
  342. }
  343. });
  344. });
  345. }
  346. //保存更改
  347. $("#addSave").on(tap, async function () {
  348. const {data, status} = await addAddress()
  349. if (status === 200 || status === "200") {
  350. showToast('Success')
  351. setTimeout(function () {
  352. $("#addressInfo").show(0)
  353. addressId = data.address_id
  354. $("#addressText").text(data.full)
  355. $("#fullNameText").text(data.name + " " + data.last_name)
  356. $("#contactNumberText").text(data.phone)
  357. $("#emailText").text(data.email)
  358. $("#addressContainer").hide(0)
  359. }, 2000)
  360. } else {
  361. showToast('Something wrong.Try again later.')
  362. }
  363. });
  364. function showToast(message) {
  365. var toastElement = $('#toastContainer');
  366. toastElement.text(message);
  367. toastElement.stop(true, true).fadeIn(200).delay(2000).fadeOut(200);
  368. }
  369. //支付方法
  370. const orderPayment = (params) => {
  371. return new Promise(function (resolve, reject) {
  372. $.ajax({
  373. url: "/index/checkout/submit?payType=30&mode=cart&address_id=" + addressId,
  374. method: "POST",
  375. data: JSON.stringify(params),
  376. headers: {
  377. "Content-Type": "application/json",
  378. "storeId": "10001",
  379. "platform": "H5",
  380. // "Access-Token": vapesToken
  381. },
  382. dataType: "json",
  383. success: function (response) {
  384. resolve(response);
  385. },
  386. error: function (xhr, status, error) {
  387. reject(error);
  388. }
  389. });
  390. });
  391. }
  392. const orderPaymentPoints = (params) => {
  393. return new Promise(function (resolve, reject) {
  394. $.ajax({
  395. url: "/index/checkout/submit?payType=50&mode=cart&address_id=" + addressId,
  396. method: "POST",
  397. data: JSON.stringify(params),
  398. headers: {
  399. "Content-Type": "application/json",
  400. "storeId": "10001",
  401. "platform": "H5",
  402. // "Access-Token": vapesToken
  403. },
  404. dataType: "json",
  405. success: function (response) {
  406. resolve(response);
  407. },
  408. error: function (xhr, status, error) {
  409. reject(error);
  410. }
  411. });
  412. });
  413. }
  414. /**
  415. * 商品操作方法
  416. * @param _this $(this)
  417. * @param type 类型:add || reduce || delete
  418. * @returns
  419. */
  420. function productOperate(_this, type) {
  421. //商品id
  422. const productId = _this.closest(".productItem").attr("data-id");
  423. const goodsId = _this.closest(".productItem").attr("goods-id");
  424. const goodsSkuId = _this.closest(".productItem").attr("goods-sku-id");
  425. let goodsNum = parseInt(_this.closest(".productItem").attr("data-goods-num"));
  426. //console.log(productId, goodsId, goodsSkuId)
  427. //商品增加
  428. if (type === "add" || type === "reduce") {
  429. if (type === "add") {
  430. goodsNum += 1
  431. } else {
  432. goodsNum -= 1
  433. }
  434. $.ajax({
  435. url: "/index/cart/update?goodsId=" + goodsId + "&goodsSkuId=" + goodsSkuId + "&goodsNum=" + goodsNum,
  436. method: "POST",
  437. headers: {
  438. "Content-Type": "application/json",
  439. "storeId": "10001",
  440. "platform": "H5",
  441. "Accept": "*/*"
  442. // "Access-Token": vapesToken
  443. },
  444. dataType: "json",
  445. success: function (obj) {
  446. if (obj.status === 200 || obj.status === '200') {
  447. showToast("Successful")
  448. window.location.href = '../cart/shoppingCart';
  449. } else if (obj.status === 401 || obj.status === '401') {
  450. window.location.replace('../passport/logIn.html')
  451. } else {
  452. showToast(obj.message)
  453. }
  454. //window.location.replace( '/index/cart/shoppingCart.html')
  455. },
  456. error: function (xhr, status, error) {
  457. showToast("Please enter the quantity of products")
  458. //reject(error);
  459. }
  460. });
  461. return
  462. }
  463. //商品减少
  464. //商品删除
  465. if (type === "delete") {
  466. var params = {"cartIds": [productId]}
  467. console.log("商品删除")
  468. $.ajax({
  469. url: "/index/cart/clear",
  470. method: "POST",
  471. headers: {
  472. "Content-Type": "application/json",
  473. "storeId": "10001",
  474. "platform": "H5",
  475. // "Access-Token": vapesToken
  476. },
  477. data: JSON.stringify(params),
  478. dataType: "json",
  479. success: function (obj) {
  480. if (obj.status === 200 || obj.status === '200') {
  481. //showToast("Successful")
  482. window.location.href = '../cart/shoppingCart';
  483. } else if (obj.status === 401 || obj.status === '401') {
  484. window.location.replace('../passport/logIn.html')
  485. } else {
  486. showToast(obj.message)
  487. }
  488. },
  489. error: function (xhr, status, error) {
  490. reject(error);
  491. }
  492. });
  493. }
  494. }
  495. //支付
  496. $(".payPalButton").on(tap, async function () {
  497. console.log("paypaing")
  498. $("#loadingModel").show(0)
  499. // 支付之前先判断是否有默认地址
  500. if (!addressId) {
  501. showToast('No address')
  502. return
  503. // 先添加地址
  504. // const res = await addAddress()
  505. // addressId = res
  506. }
  507. const params = {
  508. //"cartIds": "10002",
  509. "delivery": 10,
  510. "address_id": addressId
  511. }
  512. const res = await orderPayment(params)
  513. const {status, message, data} = res || {}
  514. if (status === 200 || status === "200") {
  515. window.open(data.payment.approval_link, "_blank");
  516. var interId = setInterval(function () {
  517. fetchOrder(data.orderId, interId);
  518. }, 3000)
  519. } else if (status === 401 || status === "401") {
  520. window.location.replace('../passport/logIn.html')
  521. } else {
  522. showToast(message)
  523. }
  524. });
  525. function fetchOrder(orderId, interId) {
  526. $.get('/index/order/detail?orderId=' + orderId, function (res) {
  527. console.log(res.data.order.pay_status);
  528. if (res.status == 200 && res.data.order.pay_status == 20) {
  529. clearInterval(interId);
  530. $("#loadingModel").hide(0);
  531. window.location.replace('/index/user/orderDetails.html?orderId=' + orderId)
  532. }
  533. })
  534. }
  535. //支付
  536. $(".checkoutButton").on(tap, async function () {
  537. if (payByPoints !== '1') {
  538. showToast('Insufficient points!')
  539. return;
  540. }
  541. console.log("points")
  542. const params = {
  543. "delivery": 10,
  544. "address_id": addressId
  545. }
  546. $("#loadingModel").show(0)
  547. const res = await orderPaymentPoints(params)
  548. const {status, message, data} = res || {}
  549. if (status === 200 || status === "200") {
  550. $("#loadingModel").hide(0)
  551. showToast(message)
  552. setTimeout(function () {
  553. window.location.replace('/index/user/order.html')
  554. }, 3000)
  555. } else if (status === 401 || status === "401") {
  556. showToast('Login first!')
  557. setTimeout(function () {
  558. window.location.replace('../passport/logIn.html')
  559. }, 3000)
  560. //window.location.replace('../passport/logIn.html')
  561. } else {
  562. showToast(message)
  563. }
  564. });
  565. //商品减少
  566. $(".numberReduce").on(tap, function () {
  567. productOperate($(this), "reduce")
  568. })
  569. //商品增加
  570. $(".numberAdd").on(tap, function () {
  571. productOperate($(this), "add")
  572. })
  573. //商品删除
  574. $(".productDelete").on(tap, function () {
  575. productOperate($(this), "delete")
  576. })
  577. //前往购物车
  578. $(".shoppingCartIcon").on(tap, function () {
  579. window.location.href = "./shoppingCart.html"
  580. });
  581. /* $(".selectOption").on(tap, function () {
  582. console.log('so!!!')
  583. var selectedOptionInput = document.getElementById('selectedOption');
  584. selectedOptionInput.value = optionValue;
  585. // 如果你希望在选择后执行其他操作,可以在这里添加代码
  586. // 例如提交表单、显示消息等
  587. })
  588. function deleteOption(optionId) {
  589. var optionElement = document.getElementById(optionId);
  590. optionElement.parentNode.remove();
  591. // 如果需要在删除选项后执行其他操作,可以在这里添加代码
  592. }*/
  593. })
  594. </script>
  595. {/block}