basket.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  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/${wx.getStorageSync('channelId')||3}`,
  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&&cItems[j].stock>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 stock = e.currentTarget.dataset.stock;
  207. if(stock == 0){
  208. return
  209. }
  210. var shopCartOrders = this.data.shopCartOrders;
  211. var shopCartItemDiscounts = shopCartOrders[topindex].shopCartItemDiscounts; // 获取购物车列表
  212. var checked = shopCartItemDiscounts[scindex].shopCartItems[index].checked; // 获取当前商品的选中状态
  213. shopCartItemDiscounts[scindex].shopCartItems[index].checked = !checked; // 改变状态
  214. this.setData({
  215. shopCartOrders: shopCartOrders
  216. });
  217. this.checkAllSelected(); //检查全选状态
  218. this.calTotalPrice(); //计算总价
  219. },
  220. /**
  221. * 每一个店铺的选择
  222. */
  223. onSelectedShopItem: function (e) {
  224. let arr=[]
  225. var topindex = e.currentTarget.dataset.topindex;
  226. var shopCartOrders = this.data.shopCartOrders;
  227. var checked = shopCartOrders[topindex].checked; // 获取当前商品的选中状态
  228. shopCartOrders[topindex].checked = !checked; // 改变状态
  229. if (shopCartOrders[topindex].checked) {
  230. arr.push({...e})
  231. this.setData({
  232. toPayArray:arr
  233. })
  234. }else{
  235. this.setData({
  236. toPayArray:[]
  237. })
  238. }
  239. shopCartOrders[topindex].shopCartItemDiscounts.forEach(shopCartItem => {
  240. var cItems = shopCartItem.shopCartItems;
  241. for (var j = 0; j < cItems.length; j++) {
  242. if(cItems[j].isDelete == 0&&cItems[j].stock>0){
  243. cItems[j].checked = !checked;
  244. }
  245. }
  246. })
  247. this.setData({
  248. shopCartOrders: shopCartOrders
  249. });
  250. this.checkAllSelected(); //检查全选状态
  251. this.calTotalPrice(); //计算总价
  252. },
  253. /**
  254. * 检查全选状态
  255. */
  256. checkAllSelected: function () {
  257. var allChecked = true;
  258. var shopCartOrders = this.data.shopCartOrders;
  259. var flag = false;
  260. shopCartOrders.forEach(shopCart => {
  261. var shopChecked = true;
  262. var shopCartItemDiscounts = shopCart.shopCartItemDiscounts;
  263. for (var i = 0; i < shopCartItemDiscounts.length; i++) {
  264. var cItems = shopCartItemDiscounts[i].shopCartItems;
  265. for (var j = 0; j < cItems.length; j++) {
  266. if (!cItems[j].checked) {
  267. shopChecked = false;
  268. allChecked = false;
  269. flag = true;
  270. break;
  271. }
  272. }
  273. if (flag) {
  274. break;
  275. }
  276. }
  277. shopCart.checked = shopChecked;
  278. })
  279. this.setData({
  280. allChecked: allChecked,
  281. shopCartOrders: shopCartOrders
  282. });
  283. },
  284. /**
  285. * 计算购物车总额
  286. */
  287. calTotalPrice: function () {
  288. var shopCartOrders = this.data.shopCartOrders;
  289. var shopCartIds = [];
  290. shopCartOrders.forEach(shopCart => {
  291. var shopCartItemDiscounts = shopCart.shopCartItemDiscounts;
  292. for (var i = 0; i < shopCartItemDiscounts.length; i++) {
  293. var cItems = shopCartItemDiscounts[i].shopCartItems;
  294. for (var j = 0; j < cItems.length; j++) {
  295. if (cItems[j].checked) {
  296. shopCartIds.push(cItems[j].basketId);
  297. }
  298. }
  299. }
  300. });
  301. var ths = this;
  302. wx.showLoading();
  303. var params = {
  304. url: `/p/shopCart/totalPay?channelId=${wx.getStorageSync('channelId')||3}`,
  305. method: "POST",
  306. data: shopCartIds,
  307. callBack: function (res) {
  308. ths.setData({
  309. finalMoney: res.finalMoney,
  310. totalMoney: res.totalMoney,
  311. subtractMoney: res.subtractMoney
  312. });
  313. wx.hideLoading();
  314. }
  315. };
  316. http.request(params);
  317. },
  318. /**
  319. * 减少数量
  320. */
  321. onCountMinus: function (e) {
  322. var index = e.currentTarget.dataset.index;
  323. var scindex = e.currentTarget.dataset.scindex;
  324. var topindex = e.currentTarget.dataset.topindex;
  325. var shopCartOrders = this.data.shopCartOrders;
  326. var shopCartItemDiscounts = shopCartOrders[topindex].shopCartItemDiscounts;
  327. var prodCount = shopCartItemDiscounts[scindex].shopCartItems[index].prodCount;
  328. // if (prodCount > 1) {
  329. this.updateCount(shopCartOrders, topindex, scindex, index, -1);
  330. // }
  331. },
  332. /**
  333. * 增加数量
  334. */
  335. onCountPlus: function (e) {
  336. var index = e.currentTarget.dataset.index;
  337. var scindex = e.currentTarget.dataset.scindex;
  338. var topindex = e.currentTarget.dataset.topindex;
  339. var shopCartOrders = this.data.shopCartOrders;
  340. this.updateCount(shopCartOrders, topindex, scindex, index, 1);
  341. },
  342. /**
  343. * 改变购物车数量接口
  344. */
  345. updateCount: function (shopCartOrders, topindex, scindex, index, prodCount) {
  346. var shopCartItemDiscounts = shopCartOrders[topindex].shopCartItemDiscounts;
  347. var ths = this;
  348. wx.showLoading({
  349. mask: true
  350. });
  351. var params = {
  352. url: "/p/shopCart/changeItem",
  353. method: "POST",
  354. data: {
  355. count: prodCount,
  356. prodId: shopCartItemDiscounts[scindex].shopCartItems[index].prodId,
  357. skuId: shopCartItemDiscounts[scindex].shopCartItems[index].skuId,
  358. shopId: shopCartItemDiscounts[scindex].shopCartItems[index].shopId,
  359. channelId:wx.getStorageSync('channelId')||3
  360. },
  361. callBack: function (res) {
  362. if(res.code == 500){
  363. return wx.showToast({
  364. title:res.msg,
  365. icon:'none'
  366. })
  367. }
  368. shopCartItemDiscounts[scindex].shopCartItems[index].prodCount += prodCount;
  369. ths.setData({
  370. shopCartOrders: shopCartOrders
  371. });
  372. var discountData = {
  373. basketId: shopCartItemDiscounts[scindex].shopCartItems[index].basketId,
  374. discountId: shopCartItemDiscounts[scindex].shopCartItems[index].discountId
  375. }
  376. ths.loadBasketData(discountData)
  377. wx.hideLoading();
  378. }
  379. };
  380. http.request(params);
  381. },
  382. /**
  383. * 删除购物车单个商品
  384. */
  385. onDelProd: function (e) {
  386. var ths = this;
  387. let basketId = e.currentTarget.dataset.basketid
  388. wx.showModal({
  389. title: '',
  390. content: '确认要删除选中的商品吗?',
  391. confirmColor: "#eb2444",
  392. success(res) {
  393. if (res.confirm) {
  394. wx.showLoading({
  395. mask: true
  396. });
  397. var params = {
  398. url: "/p/shopCart/deleteItem",
  399. method: "DELETE",
  400. data: [basketId],
  401. callBack: function (res) {
  402. wx.hideLoading();
  403. ths.onShow();
  404. }
  405. };
  406. http.request(params);
  407. }
  408. }
  409. })
  410. },
  411. /**
  412. * 删除购物车商品
  413. */
  414. onDelBasket: function () {
  415. var ths = this;
  416. var shopCartOrders = this.data.shopCartOrders;
  417. var basketIds = [];
  418. shopCartOrders.forEach(shopCart => {
  419. var shopCartItemDiscounts = shopCart.shopCartItemDiscounts;
  420. for (var i = 0; i < shopCartItemDiscounts.length; i++) {
  421. var cItems = shopCartItemDiscounts[i].shopCartItems;
  422. for (var j = 0; j < cItems.length; j++) {
  423. if (cItems[j].checked) {
  424. basketIds.push(cItems[j].basketId);
  425. }
  426. }
  427. }
  428. })
  429. if (basketIds.length == 0) {
  430. wx.showToast({
  431. title: '请选择商品',
  432. icon: "none"
  433. })
  434. } else {
  435. wx.showModal({
  436. title: '',
  437. content: '确认要删除选中的商品吗?',
  438. confirmColor: "#eb2444",
  439. success(res) {
  440. if (res.confirm) {
  441. wx.showLoading({
  442. mask: true
  443. });
  444. var params = {
  445. url: "/p/shopCart/deleteItem",
  446. method: "DELETE",
  447. data: basketIds,
  448. callBack: function (res) {
  449. wx.hideLoading();
  450. ths.onShow();
  451. }
  452. };
  453. http.request(params);
  454. }
  455. }
  456. })
  457. }
  458. },
  459. /**
  460. * 点击满减活动 弹窗
  461. */
  462. onChooseDiscount: function (e) {
  463. var that = this;
  464. var basketId = e.currentTarget.dataset.basketid
  465. var discounts = e.currentTarget.dataset.discounts
  466. var discountid = e.currentTarget.dataset.discountid
  467. this.setData({
  468. prodDiscounts: discounts,
  469. hideModal: false,
  470. discountid: discountid,
  471. basketId: basketId
  472. });
  473. var animation = wx.createAnimation({
  474. duration: 500,
  475. timingFunction: 'ease',
  476. })
  477. this.animation = animation
  478. setTimeout(function () {
  479. that.fadeIn();
  480. }, 100)
  481. },
  482. radioChange(e) {
  483. this.loadBasketData({
  484. basketId: this.data.basketId,
  485. discountId: e.detail.value
  486. });
  487. this.setData({
  488. hideModal: true
  489. });
  490. },
  491. // 隐藏遮罩层
  492. hideModal: function () {
  493. var that = this;
  494. var animation = wx.createAnimation({
  495. duration: 600,
  496. timingFunction: 'ease',
  497. })
  498. this.animation = animation
  499. that.fadeDown();
  500. setTimeout(function () {
  501. that.setData({
  502. hideModal: true
  503. })
  504. }, 500)
  505. },
  506. //动画集
  507. fadeIn: function () {
  508. this.animation.translateY(0).step()
  509. this.setData({
  510. animationData: this.animation.export() //动画实例的export方法导出动画数据传递给组件的animation属性
  511. })
  512. },
  513. fadeDown: function () {
  514. this.animation.translateY(300).step()
  515. this.setData({
  516. animationData: this.animation.export(),
  517. })
  518. },
  519. /**
  520. * 跳转到商品详情`
  521. */
  522. toProdPage(e) {
  523. wx.navigateTo({
  524. url: '/pages/prod/prod?prodid=' + e.currentTarget.dataset.prodid,
  525. })
  526. },
  527. /**
  528. * 金额明细弹窗
  529. */
  530. showPriDet() {
  531. var that = this;
  532. if (this.data.hidePriModal == true) {
  533. this.setData({
  534. hidePriModal: false
  535. })
  536. var animation = wx.createAnimation({
  537. duration: 500,
  538. timingFunction: 'ease',
  539. })
  540. this.animation = animation
  541. setTimeout(function () {
  542. that.fadeIn();
  543. }, 100)
  544. } else if (this.data.hidePriModal == false) {
  545. this.hidePriModal()
  546. }
  547. },
  548. hidePriModal() {
  549. var that = this;
  550. var animation = wx.createAnimation({
  551. duration: 800,
  552. timingFunction: 'ease',
  553. })
  554. this.animation = animation
  555. that.fadeDown();
  556. setTimeout(function () {
  557. that.setData({
  558. hidePriModal: true
  559. })
  560. }, 680)
  561. },
  562. goShopping(){
  563. wx.switchTab({
  564. url: '/pages/index/index',
  565. })
  566. }
  567. })