basket.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. // pages/basket/basket.js
  2. var http = require("../../utils/http.js");
  3. var util = require("../../utils/util.js");
  4. const Big = require("../../utils/big.min.js");
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. // picDomain: config.picDomain,
  11. shopCartOrders: [], //所有店铺的数据
  12. shopCartItemDiscounts: [],
  13. finalMoney: 0,
  14. totalMoney: 0,
  15. subtractMoney: 0,
  16. allChecked: true,
  17. hideModal: true, //模态框的状态 true-隐藏 false-显示
  18. hidePriModal: true, //金额明细模态框的状态 true-隐藏 false-显示
  19. animationData: {},
  20. prodDiscounts: [],
  21. discountid: -1,
  22. basketId: 0,
  23. toPayArray: []
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad: function (options) {
  29. },
  30. /**
  31. * 生命周期函数--监听页面初次渲染完成
  32. */
  33. onReady: function () {
  34. },
  35. /**
  36. * 生命周期函数--监听页面显示
  37. */
  38. onShow: function () {
  39. //加载购物车
  40. this.loadBasketData(null)
  41. },
  42. /**
  43. * 生命周期函数--监听页面隐藏
  44. */
  45. onHide: function () {
  46. this.setData({
  47. shopCartOrders: [],
  48. })
  49. },
  50. /**
  51. * 加载订单数据
  52. */
  53. loadBasketData(discountData) {
  54. var reqData = [];
  55. var shopCartIds = [];
  56. if (discountData) {
  57. reqData.push(discountData);
  58. this.data.shopCartOrders.forEach(shopCart => {
  59. shopCart.shopCartItemDiscounts.forEach(cItems => {
  60. cItems.shopCartItems.forEach(pItem => {
  61. if (pItem.checked) {
  62. shopCartIds.push(pItem.basketId);
  63. }
  64. })
  65. })
  66. })
  67. }
  68. //加载购物车
  69. var params = {
  70. url: "/p/shopCart/info/1",
  71. method: "POST",
  72. data: reqData,
  73. dontTrunLogin: true,
  74. callBack: res => {
  75. let img=''
  76. res.forEach(item=>{
  77. item.shopCartItemDiscounts.forEach(cart=>{
  78. cart.shopCartItems.map(e=>{
  79. img=e.pic.split(',')
  80. e.pic=img[0]
  81. })
  82. })
  83. })
  84. if (res.length > 0) {
  85. var shopCartOrders = res;
  86. if (shopCartIds.length == 0) { // 初始加载
  87. this.setData({
  88. shopCartOrders: shopCartOrders,
  89. });
  90. } else { // 修改购物车
  91. var checkedLog = [] // 之前勾选的basketId数组
  92. this.data.shopCartOrders.forEach(shopCart => {
  93. shopCart.shopCartItemDiscounts.forEach(shopCartItemDiscount => {
  94. shopCartItemDiscount.shopCartItems.forEach(shopCartItem => {
  95. if (shopCartItem.checked) {
  96. checkedLog.push(shopCartItem.basketId)
  97. }
  98. })
  99. })
  100. })
  101. shopCartOrders.forEach(newShopCart => {
  102. newShopCart.shopCartItemDiscounts.forEach(newShopCartItemDiscount => {
  103. newShopCartItemDiscount.shopCartItems.forEach(shopCartItem => {
  104. if (checkedLog.includes(shopCartItem.basketId)) {
  105. shopCartItem.checked = true
  106. }
  107. })
  108. });
  109. })
  110. this.setData({
  111. shopCartOrders
  112. })
  113. }
  114. } else {
  115. this.setData({
  116. shopCartOrders: [],
  117. });
  118. }
  119. this.checkAllSelected()
  120. this.calTotalPrice(); //计算总价
  121. },
  122. };
  123. http.request(params);
  124. http.getCartCount(); //重新计算购物车总数量
  125. },
  126. /**
  127. * 去结算
  128. */
  129. toFirmOrder: function () {
  130. var shopCartOrders = this.data.shopCartOrders;
  131. var basketIds = [];
  132. let arr =[]
  133. shopCartOrders.forEach(shopCart => {
  134. var shopCartItemDiscounts = shopCart.shopCartItemDiscounts;
  135. shopCartItemDiscounts.forEach(shopCartItemDiscount => {
  136. shopCartItemDiscount.shopCartItems.forEach(shopCartItem => {
  137. if (shopCartItem.checked) {
  138. arr.push({...shopCartItem})
  139. basketIds.push(shopCartItem.basketId)
  140. }
  141. })
  142. })
  143. })
  144. if (!basketIds.length) {
  145. wx.showToast({
  146. title: '请选择商品',
  147. icon: "none"
  148. })
  149. return
  150. }
  151. const result =arr.every(item => item.shopId === arr[0].shopId);
  152. console.log(result);
  153. if (result==false) {
  154. wx.showToast({
  155. title: '多个门店不可同时结算~',
  156. icon: "none"
  157. })
  158. return
  159. }
  160. wx.setStorageSync("basketIds", JSON.stringify(basketIds));
  161. wx.navigateTo({
  162. url: '/pages/submit-order/submit-order?orderEntry=0&shopId='+arr[0].shopId,
  163. })
  164. },
  165. /**
  166. * 全选
  167. */
  168. onSelAll: function () {
  169. var allChecked = this.data.allChecked;
  170. allChecked = !allChecked; //改变状态
  171. var shopCartOrders = this.data.shopCartOrders;
  172. if (allChecked) {
  173. this.setData({
  174. toPayArray: shopCartOrders
  175. })
  176. } else {
  177. this.setData({
  178. toPayArray: []
  179. })
  180. }
  181. shopCartOrders.forEach(shopCart => {
  182. shopCart.checked = allChecked;
  183. var shopCartItemDiscounts = shopCart.shopCartItemDiscounts;
  184. for (var i = 0; i < shopCartItemDiscounts.length; i++) {
  185. var cItems = shopCartItemDiscounts[i].shopCartItems;
  186. for (var j = 0; j < cItems.length; j++) {
  187. if(cItems[j].isDelete == 0){
  188. cItems[j].checked = allChecked;
  189. }
  190. }
  191. }
  192. })
  193. this.setData({
  194. allChecked: allChecked,
  195. shopCartOrders: shopCartOrders
  196. });
  197. this.calTotalPrice(); //计算总价
  198. },
  199. /**
  200. * 每一项的选择事件
  201. */
  202. onSelectedItem: function (e) {
  203. var index = e.currentTarget.dataset.index; // 获取data- 传进来的index
  204. var scindex = e.currentTarget.dataset.scindex;
  205. var topindex = e.currentTarget.dataset.topindex;
  206. var shopCartOrders = this.data.shopCartOrders;
  207. var shopCartItemDiscounts = shopCartOrders[topindex].shopCartItemDiscounts; // 获取购物车列表
  208. var checked = shopCartItemDiscounts[scindex].shopCartItems[index].checked; // 获取当前商品的选中状态
  209. shopCartItemDiscounts[scindex].shopCartItems[index].checked = !checked; // 改变状态
  210. this.setData({
  211. shopCartOrders: shopCartOrders
  212. });
  213. this.checkAllSelected(); //检查全选状态
  214. this.calTotalPrice(); //计算总价
  215. },
  216. /**
  217. * 每一个店铺的选择
  218. */
  219. onSelectedShopItem: function (e) {
  220. let arr=[]
  221. var topindex = e.currentTarget.dataset.topindex;
  222. var shopCartOrders = this.data.shopCartOrders;
  223. var checked = shopCartOrders[topindex].checked; // 获取当前商品的选中状态
  224. shopCartOrders[topindex].checked = !checked; // 改变状态
  225. if (shopCartOrders[topindex].checked) {
  226. arr.push({...e})
  227. this.setData({
  228. toPayArray:arr
  229. })
  230. }else{
  231. this.setData({
  232. toPayArray:[]
  233. })
  234. }
  235. shopCartOrders[topindex].shopCartItemDiscounts.forEach(shopCartItem => {
  236. var cItems = shopCartItem.shopCartItems;
  237. for (var j = 0; j < cItems.length; j++) {
  238. cItems[j].checked = !checked;
  239. }
  240. })
  241. this.setData({
  242. shopCartOrders: shopCartOrders
  243. });
  244. this.checkAllSelected(); //检查全选状态
  245. this.calTotalPrice(); //计算总价
  246. },
  247. /**
  248. * 检查全选状态
  249. */
  250. checkAllSelected: function () {
  251. var allChecked = true;
  252. var shopCartOrders = this.data.shopCartOrders;
  253. var flag = false;
  254. shopCartOrders.forEach(shopCart => {
  255. var shopChecked = true;
  256. var shopCartItemDiscounts = shopCart.shopCartItemDiscounts;
  257. for (var i = 0; i < shopCartItemDiscounts.length; i++) {
  258. var cItems = shopCartItemDiscounts[i].shopCartItems;
  259. for (var j = 0; j < cItems.length; j++) {
  260. if (!cItems[j].checked) {
  261. shopChecked = false;
  262. allChecked = false;
  263. flag = true;
  264. break;
  265. }
  266. }
  267. if (flag) {
  268. break;
  269. }
  270. }
  271. shopCart.checked = shopChecked;
  272. })
  273. this.setData({
  274. allChecked: allChecked,
  275. shopCartOrders: shopCartOrders
  276. });
  277. },
  278. /**
  279. * 计算购物车总额
  280. */
  281. calTotalPrice: function () {
  282. var shopCartOrders = this.data.shopCartOrders;
  283. var shopCartIds = [];
  284. shopCartOrders.forEach(shopCart => {
  285. var shopCartItemDiscounts = shopCart.shopCartItemDiscounts;
  286. for (var i = 0; i < shopCartItemDiscounts.length; i++) {
  287. var cItems = shopCartItemDiscounts[i].shopCartItems;
  288. for (var j = 0; j < cItems.length; j++) {
  289. if (cItems[j].checked) {
  290. shopCartIds.push(cItems[j].basketId);
  291. }
  292. }
  293. }
  294. });
  295. var ths = this;
  296. wx.showLoading();
  297. var params = {
  298. url: "/p/shopCart/totalPay",
  299. method: "POST",
  300. data: shopCartIds,
  301. callBack: function (res) {
  302. ths.setData({
  303. finalMoney: res.finalMoney,
  304. totalMoney: res.totalMoney,
  305. subtractMoney: res.subtractMoney
  306. });
  307. wx.hideLoading();
  308. }
  309. };
  310. http.request(params);
  311. },
  312. /**
  313. * 减少数量
  314. */
  315. onCountMinus: function (e) {
  316. var index = e.currentTarget.dataset.index;
  317. var scindex = e.currentTarget.dataset.scindex;
  318. var topindex = e.currentTarget.dataset.topindex;
  319. var shopCartOrders = this.data.shopCartOrders;
  320. var shopCartItemDiscounts = shopCartOrders[topindex].shopCartItemDiscounts;
  321. var prodCount = shopCartItemDiscounts[scindex].shopCartItems[index].prodCount;
  322. // if (prodCount > 1) {
  323. this.updateCount(shopCartOrders, topindex, scindex, index, -1);
  324. // }
  325. },
  326. /**
  327. * 增加数量
  328. */
  329. onCountPlus: function (e) {
  330. var index = e.currentTarget.dataset.index;
  331. var scindex = e.currentTarget.dataset.scindex;
  332. var topindex = e.currentTarget.dataset.topindex;
  333. var shopCartOrders = this.data.shopCartOrders;
  334. this.updateCount(shopCartOrders, topindex, scindex, index, 1);
  335. },
  336. /**
  337. * 改变购物车数量接口
  338. */
  339. updateCount: function (shopCartOrders, topindex, scindex, index, prodCount) {
  340. var shopCartItemDiscounts = shopCartOrders[topindex].shopCartItemDiscounts;
  341. var ths = this;
  342. wx.showLoading({
  343. mask: true
  344. });
  345. var params = {
  346. url: "/p/shopCart/changeItem",
  347. method: "POST",
  348. data: {
  349. count: prodCount,
  350. prodId: shopCartItemDiscounts[scindex].shopCartItems[index].prodId,
  351. skuId: shopCartItemDiscounts[scindex].shopCartItems[index].skuId,
  352. shopId: shopCartItemDiscounts[scindex].shopCartItems[index].shopId,
  353. platform:1
  354. },
  355. callBack: function (res) {
  356. if(res.code == 500){
  357. return wx.showToast({
  358. title:res.msg,
  359. icon:'none'
  360. })
  361. }
  362. shopCartItemDiscounts[scindex].shopCartItems[index].prodCount += prodCount;
  363. ths.setData({
  364. shopCartOrders: shopCartOrders
  365. });
  366. var discountData = {
  367. basketId: shopCartItemDiscounts[scindex].shopCartItems[index].basketId,
  368. discountId: shopCartItemDiscounts[scindex].shopCartItems[index].discountId
  369. }
  370. ths.loadBasketData(discountData)
  371. wx.hideLoading();
  372. }
  373. };
  374. http.request(params);
  375. },
  376. /**
  377. * 删除购物车单个商品
  378. */
  379. onDelProd: function (e) {
  380. var ths = this;
  381. let basketId = e.currentTarget.dataset.basketid
  382. wx.showModal({
  383. title: '',
  384. content: '确认要删除选中的商品吗?',
  385. confirmColor: "#eb2444",
  386. success(res) {
  387. if (res.confirm) {
  388. wx.showLoading({
  389. mask: true
  390. });
  391. var params = {
  392. url: "/p/shopCart/deleteItem",
  393. method: "DELETE",
  394. data: [basketId],
  395. callBack: function (res) {
  396. wx.hideLoading();
  397. ths.onShow();
  398. }
  399. };
  400. http.request(params);
  401. }
  402. }
  403. })
  404. },
  405. /**
  406. * 删除购物车商品
  407. */
  408. onDelBasket: function () {
  409. var ths = this;
  410. var shopCartOrders = this.data.shopCartOrders;
  411. var basketIds = [];
  412. shopCartOrders.forEach(shopCart => {
  413. var shopCartItemDiscounts = shopCart.shopCartItemDiscounts;
  414. for (var i = 0; i < shopCartItemDiscounts.length; i++) {
  415. var cItems = shopCartItemDiscounts[i].shopCartItems;
  416. for (var j = 0; j < cItems.length; j++) {
  417. if (cItems[j].checked) {
  418. basketIds.push(cItems[j].basketId);
  419. }
  420. }
  421. }
  422. })
  423. if (basketIds.length == 0) {
  424. wx.showToast({
  425. title: '请选择商品',
  426. icon: "none"
  427. })
  428. } else {
  429. wx.showModal({
  430. title: '',
  431. content: '确认要删除选中的商品吗?',
  432. confirmColor: "#eb2444",
  433. success(res) {
  434. if (res.confirm) {
  435. wx.showLoading({
  436. mask: true
  437. });
  438. var params = {
  439. url: "/p/shopCart/deleteItem",
  440. method: "DELETE",
  441. data: basketIds,
  442. callBack: function (res) {
  443. wx.hideLoading();
  444. ths.onShow();
  445. }
  446. };
  447. http.request(params);
  448. }
  449. }
  450. })
  451. }
  452. },
  453. /**
  454. * 点击满减活动 弹窗
  455. */
  456. onChooseDiscount: function (e) {
  457. var that = this;
  458. var basketId = e.currentTarget.dataset.basketid
  459. var discounts = e.currentTarget.dataset.discounts
  460. var discountid = e.currentTarget.dataset.discountid
  461. this.setData({
  462. prodDiscounts: discounts,
  463. hideModal: false,
  464. discountid: discountid,
  465. basketId: basketId
  466. });
  467. var animation = wx.createAnimation({
  468. duration: 500,
  469. timingFunction: 'ease',
  470. })
  471. this.animation = animation
  472. setTimeout(function () {
  473. that.fadeIn();
  474. }, 100)
  475. },
  476. radioChange(e) {
  477. this.loadBasketData({
  478. basketId: this.data.basketId,
  479. discountId: e.detail.value
  480. });
  481. this.setData({
  482. hideModal: true
  483. });
  484. },
  485. // 隐藏遮罩层
  486. hideModal: function () {
  487. var that = this;
  488. var animation = wx.createAnimation({
  489. duration: 600,
  490. timingFunction: 'ease',
  491. })
  492. this.animation = animation
  493. that.fadeDown();
  494. setTimeout(function () {
  495. that.setData({
  496. hideModal: true
  497. })
  498. }, 500)
  499. },
  500. //动画集
  501. fadeIn: function () {
  502. this.animation.translateY(0).step()
  503. this.setData({
  504. animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
  505. })
  506. },
  507. fadeDown: function () {
  508. this.animation.translateY(300).step()
  509. this.setData({
  510. animationData: this.animation.export(),
  511. })
  512. },
  513. /**
  514. * 跳转到商品详情`
  515. */
  516. toProdPage(e) {
  517. wx.navigateTo({
  518. url: '/pages/prod/prod?prodid=' + e.currentTarget.dataset.prodid,
  519. })
  520. },
  521. /**
  522. * 金额明细弹窗
  523. */
  524. showPriDet() {
  525. var that = this;
  526. if (this.data.hidePriModal == true) {
  527. this.setData({
  528. hidePriModal: false
  529. })
  530. var animation = wx.createAnimation({
  531. duration: 500,
  532. timingFunction: 'ease',
  533. })
  534. this.animation = animation
  535. setTimeout(function () {
  536. that.fadeIn();
  537. }, 100)
  538. } else if (this.data.hidePriModal == false) {
  539. this.hidePriModal()
  540. }
  541. },
  542. hidePriModal() {
  543. var that = this;
  544. var animation = wx.createAnimation({
  545. duration: 800,
  546. timingFunction: 'ease',
  547. })
  548. this.animation = animation
  549. that.fadeDown();
  550. setTimeout(function () {
  551. that.setData({
  552. hidePriModal: true
  553. })
  554. }, 680)
  555. },
  556. goShopping(){
  557. wx.switchTab({
  558. url: '/pages/index/index',
  559. })
  560. }
  561. })