shoppingCart.html 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639
  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 src="/assets/index/js/jquery-1.12.0.js"></script>
  194. <script src="/assets/index/js/flexible.js"></script>
  195. <script src="/assets/index/js/public.js?t=11"></script>
  196. <script src="/assets/index/js/swiper.js?t=6"></script>
  197. <script src="/assets/index/js/jquery.lazyload.js"></script>
  198. <script>
  199. $(document).ready(function () {
  200. var addressId = {$addressId};
  201. var payByPoints = "{$payByPoints}";
  202. //console.log(payByPoints, payByPoints)
  203. //默认地址
  204. const defaultAddressInfo = {
  205. userName: "{$name}",
  206. lastName: "{$lastName}",
  207. phoneNumber: "{$phone}",
  208. areaSelect: "{$regionId}",
  209. address: "{$detail}",
  210. mailbox: "{$email}",
  211. postalCode: "{$zipCode}"
  212. }
  213. // 是否已填写过地址
  214. if (addressId) {
  215. var isAddress = true
  216. } else {
  217. var isAddress = false
  218. }
  219. //初始化地址模块标签显示隐藏
  220. function initAddressLabel() {
  221. //是否已填写过地址、显示默认地址,隐藏填写地址表单
  222. if (isAddress) {
  223. $("#addressInfo").show(0)
  224. $("#addressContainer").hide(0)
  225. } else {
  226. $("#addressInfo").hide(0)
  227. $("#addressContainer").show(0)
  228. $("#addressButModel").show(0)
  229. }
  230. }
  231. //初始化表单
  232. function initAddressForm() {
  233. const {userName, lastName, phoneNumber, areaSelect, address, mailbox, postalCode} = defaultAddressInfo || {}
  234. userName && $("#userName").val(userName)
  235. lastName && $("#lastName").val(lastName)
  236. phoneNumber && $("#phoneNumber").val(phoneNumber)
  237. areaSelect && $("#areaSelect").val(areaSelect)
  238. address && $("#address").val(address)
  239. mailbox && $("#mailbox").val(mailbox)
  240. postalCode && $("#postalCode").val(postalCode)
  241. }
  242. initAddressLabel()
  243. initAddressForm()
  244. //更改地址,需显示填写地址表单、取消保存按钮
  245. $("#changeAddress").on(tap, async function () {
  246. //获取地址列表
  247. /* $.ajax({
  248. url: "/index/address/list",
  249. method: 'GET',
  250. headers: {
  251. 'Content-Type': 'application/json',
  252. 'storeId': '10001',
  253. 'platform': 'H5',
  254. },
  255. dataType: 'json',
  256. success: function (obj) {
  257. if (obj.status === 200 || obj.status === '200') {
  258. console.log(obj.data)
  259. return false;
  260. showToast("Successful")
  261. window.location.href = '../cart/shoppingCart';
  262. } else if (obj.status === 401 || obj.status === '401') {
  263. window.location.replace('../passport/logIn.html')
  264. } else {
  265. showToast(obj.message)
  266. }
  267. },
  268. error: function (xhr, status, error) {
  269. reject(error);
  270. }
  271. });*/
  272. $("#addressContainer").show(0)
  273. $("#addressButModel").show(0)
  274. });
  275. //取消更改
  276. $("#addCancel").on(tap, async function () {
  277. $("#addressContainer").hide(0)
  278. });
  279. // 添加地址
  280. function addAddress() {
  281. const userNameEle = $("#userName")
  282. const lastNameEle = $("#lastName")
  283. const phoneNumberEle = $("#phoneNumber")
  284. const areaSelectEle = $("#areaSelect")
  285. const addressEle = $("#address")
  286. const mailboxEle = $("#mailbox")
  287. const postalCodeEle = $("#postalCode")
  288. // 名字
  289. const userName = userNameEle.val()
  290. const lastName = lastNameEle.val()
  291. // 手机号码
  292. const phoneNumber = phoneNumberEle.val()
  293. // 地区
  294. const areaSelect = areaSelectEle.val()
  295. // 详情地址
  296. const address = addressEle.val()
  297. // 邮箱
  298. const mailbox = mailboxEle.val()
  299. // 邮政编码
  300. const postalCode = postalCodeEle.val()
  301. if (!userName || userName === "") {
  302. userNameEle.focus()
  303. return
  304. }
  305. if (!phoneNumber || phoneNumber === "") {
  306. phoneNumberEle.focus()
  307. return
  308. }
  309. if (!areaSelect || areaSelect === "") {
  310. areaSelectEle.focus()
  311. return
  312. }
  313. if (!address || address === "") {
  314. addressEle.focus()
  315. return
  316. }
  317. const params = {
  318. form: {
  319. name: userName,
  320. last_name: lastName,
  321. phone: phoneNumber,
  322. email: mailbox,
  323. region: [{label: '', value: 1}, {label: '', value: 2}, {label: areaSelect, value: areaSelect}],
  324. //mailbox,
  325. detail: address,
  326. zip_code: postalCode
  327. }
  328. }
  329. return new Promise(function (resolve, reject) {
  330. $.ajax({
  331. url: "/index/address/add",
  332. method: 'POST',
  333. data: JSON.stringify(params),
  334. headers: {
  335. 'Content-Type': 'application/json',
  336. 'storeId': '10001',
  337. 'platform': 'H5',
  338. },
  339. dataType: 'json',
  340. success: function (response) {
  341. resolve(response);
  342. },
  343. error: function (xhr, status, error) {
  344. reject(error);
  345. }
  346. });
  347. });
  348. }
  349. //保存更改
  350. $("#addSave").on(tap, async function () {
  351. const {data, status} = await addAddress()
  352. if (status === 200 || status === "200") {
  353. showToast('Success')
  354. setTimeout(function () {
  355. $("#addressInfo").show(0)
  356. addressId = data.address_id
  357. $("#addressText").text(data.full)
  358. $("#fullNameText").text(data.name + " " + data.last_name)
  359. $("#contactNumberText").text(data.phone)
  360. $("#emailText").text(data.email)
  361. $("#addressContainer").hide(0)
  362. }, 2000)
  363. } else {
  364. showToast('Something wrong.Try again later.')
  365. }
  366. });
  367. function showToast(message) {
  368. var toastElement = $('#toastContainer');
  369. toastElement.text(message);
  370. toastElement.stop(true, true).fadeIn(200).delay(2000).fadeOut(200);
  371. }
  372. //支付方法
  373. const orderPayment = (params) => {
  374. return new Promise(function (resolve, reject) {
  375. $.ajax({
  376. url: "/index/checkout/submit?payType=30&mode=cart&address_id=" + addressId,
  377. method: "POST",
  378. data: JSON.stringify(params),
  379. headers: {
  380. "Content-Type": "application/json",
  381. "storeId": "10001",
  382. "platform": "H5",
  383. // "Access-Token": vapesToken
  384. },
  385. dataType: "json",
  386. success: function (response) {
  387. resolve(response);
  388. },
  389. error: function (xhr, status, error) {
  390. reject(error);
  391. }
  392. });
  393. });
  394. }
  395. const orderPaymentPoints = (params) => {
  396. return new Promise(function (resolve, reject) {
  397. $.ajax({
  398. url: "/index/checkout/submit?payType=50&mode=cart&address_id=" + addressId,
  399. method: "POST",
  400. data: JSON.stringify(params),
  401. headers: {
  402. "Content-Type": "application/json",
  403. "storeId": "10001",
  404. "platform": "H5",
  405. // "Access-Token": vapesToken
  406. },
  407. dataType: "json",
  408. success: function (response) {
  409. resolve(response);
  410. },
  411. error: function (xhr, status, error) {
  412. reject(error);
  413. }
  414. });
  415. });
  416. }
  417. /**
  418. * 商品操作方法
  419. * @param _this $(this)
  420. * @param type 类型:add || reduce || delete
  421. * @returns
  422. */
  423. function productOperate(_this, type) {
  424. //商品id
  425. const productId = _this.closest(".productItem").attr("data-id");
  426. const goodsId = _this.closest(".productItem").attr("goods-id");
  427. const goodsSkuId = _this.closest(".productItem").attr("goods-sku-id");
  428. let goodsNum = parseInt(_this.closest(".productItem").attr("data-goods-num"));
  429. //console.log(productId, goodsId, goodsSkuId)
  430. //商品增加
  431. if (type === "add" || type === "reduce") {
  432. if (type === "add") {
  433. goodsNum += 1
  434. } else {
  435. goodsNum -= 1
  436. }
  437. $.ajax({
  438. url: "/index/cart/update?goodsId=" + goodsId + "&goodsSkuId=" + goodsSkuId + "&goodsNum=" + goodsNum,
  439. method: "POST",
  440. headers: {
  441. "Content-Type": "application/json",
  442. "storeId": "10001",
  443. "platform": "H5",
  444. "Accept": "*/*"
  445. // "Access-Token": vapesToken
  446. },
  447. dataType: "json",
  448. success: function (obj) {
  449. if (obj.status === 200 || obj.status === '200') {
  450. showToast("Successful")
  451. window.location.href = '../cart/shoppingCart';
  452. } else if (obj.status === 401 || obj.status === '401') {
  453. window.location.replace('../passport/logIn.html')
  454. } else {
  455. showToast(obj.message)
  456. }
  457. //window.location.replace( '/index/cart/shoppingCart.html')
  458. },
  459. error: function (xhr, status, error) {
  460. showToast("Please enter the quantity of products")
  461. //reject(error);
  462. }
  463. });
  464. return
  465. }
  466. //商品减少
  467. //商品删除
  468. if (type === "delete") {
  469. var params = {"cartIds": [productId]}
  470. console.log("商品删除")
  471. $.ajax({
  472. url: "/index/cart/clear",
  473. method: "POST",
  474. headers: {
  475. "Content-Type": "application/json",
  476. "storeId": "10001",
  477. "platform": "H5",
  478. // "Access-Token": vapesToken
  479. },
  480. data: JSON.stringify(params),
  481. dataType: "json",
  482. success: function (obj) {
  483. if (obj.status === 200 || obj.status === '200') {
  484. //showToast("Successful")
  485. window.location.href = '../cart/shoppingCart';
  486. } else if (obj.status === 401 || obj.status === '401') {
  487. window.location.replace('../passport/logIn.html')
  488. } else {
  489. showToast(obj.message)
  490. }
  491. },
  492. error: function (xhr, status, error) {
  493. reject(error);
  494. }
  495. });
  496. }
  497. }
  498. //支付
  499. $(".payPalButton").on(tap, async function () {
  500. console.log("paypaing")
  501. $("#loadingModel").show(0)
  502. // 支付之前先判断是否有默认地址
  503. if (!addressId) {
  504. showToast('No address')
  505. return
  506. // 先添加地址
  507. // const res = await addAddress()
  508. // addressId = res
  509. }
  510. const params = {
  511. //"cartIds": "10002",
  512. "delivery": 10,
  513. "address_id": addressId
  514. }
  515. const res = await orderPayment(params)
  516. const {status, message, data} = res || {}
  517. if (status === 200 || status === "200") {
  518. window.open(data.payment.approval_link, "_blank");
  519. var interId = setInterval(function () {
  520. fetchOrder(data.orderId, interId);
  521. }, 3000)
  522. } else if (status === 401 || status === "401") {
  523. window.location.replace('../passport/logIn.html')
  524. } else {
  525. showToast(message)
  526. }
  527. });
  528. function fetchOrder(orderId, interId) {
  529. $.get('/index/order/detail?orderId=' + orderId, function (res) {
  530. console.log(res.data.order.pay_status);
  531. if (res.status == 200 && res.data.order.pay_status == 20) {
  532. clearInterval(interId);
  533. $("#loadingModel").hide(0);
  534. window.location.replace('/index/user/orderDetails.html?orderId=' + orderId)
  535. }
  536. })
  537. }
  538. //支付
  539. $(".checkoutButton").on(tap, async function () {
  540. if (payByPoints !== '1') {
  541. showToast('Insufficient points!')
  542. return;
  543. }
  544. console.log("points")
  545. const params = {
  546. "delivery": 10,
  547. "address_id": addressId
  548. }
  549. $("#loadingModel").show(0)
  550. const res = await orderPaymentPoints(params)
  551. const {status, message, data} = res || {}
  552. if (status === 200 || status === "200") {
  553. $("#loadingModel").hide(0)
  554. showToast(message)
  555. setTimeout(function () {
  556. window.location.replace('/index/user/order.html')
  557. }, 3000)
  558. } else if (status === 401 || status === "401") {
  559. showToast('Login first!')
  560. setTimeout(function () {
  561. window.location.replace('../passport/logIn.html')
  562. }, 3000)
  563. //window.location.replace('../passport/logIn.html')
  564. } else {
  565. showToast(message)
  566. }
  567. });
  568. //商品减少
  569. $(".numberReduce").on(tap, function () {
  570. productOperate($(this), "reduce")
  571. })
  572. //商品增加
  573. $(".numberAdd").on(tap, function () {
  574. productOperate($(this), "add")
  575. })
  576. //商品删除
  577. $(".productDelete").on(tap, function () {
  578. productOperate($(this), "delete")
  579. })
  580. //前往购物车
  581. $(".shoppingCartIcon").on(tap, function () {
  582. window.location.href = "./shoppingCart.html"
  583. });
  584. /* $(".selectOption").on(tap, function () {
  585. console.log('so!!!')
  586. var selectedOptionInput = document.getElementById('selectedOption');
  587. selectedOptionInput.value = optionValue;
  588. // 如果你希望在选择后执行其他操作,可以在这里添加代码
  589. // 例如提交表单、显示消息等
  590. })
  591. function deleteOption(optionId) {
  592. var optionElement = document.getElementById(optionId);
  593. optionElement.parentNode.remove();
  594. // 如果需要在删除选项后执行其他操作,可以在这里添加代码
  595. }*/
  596. })
  597. </script>
  598. {/block}