shoppingCart.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. const app = getApp()
  2. const api = require('../../../request/api');
  3. const http = require('../../../request/http');
  4. const utils = require('../../../utils/util.js');
  5. const storeKeys = require('../../../utils/storageKeys.js');
  6. Page({
  7. data: {
  8. isLoading: true, //是否处于加载
  9. isLogOn: true, //是否已登录
  10. statusBarHeight: app.globalData.statusBarHeight,
  11. isAllSelect: false, //是否全选
  12. cartList: [], //购物车总数据
  13. validData: [], //购物车有效数据
  14. rulesSkuList: [], //弹出规格选择数组
  15. rulesSkuObj: {}, //弹出规则选中数据
  16. rulesIndex: 0, //当前正在选规则数据的索引
  17. showRulesPopup: false, //是否显示规则选择
  18. isShowD: false,
  19. showDetailsPopup:false, //是否显示查看明细Popup
  20. selectedCartList:[], //已勾选的数据
  21. userCouponItem:null, //可用优惠卷
  22. isEdit:false, //是否处于编辑状态
  23. isAllDelete:false, //是否全选删除
  24. showDeleteModal: false, //是否显示确认删除弹窗
  25. isShowDetails: false, //是否显示查看明细入口
  26. totalprice: "0.00", //合计价格
  27. activityPrice: 0, //活动优惠金额
  28. totalReduce:0, //共减金额
  29. finalPrice:0, //最终合计金额
  30. showStockUnModal: false, // 是否线上库存不足弹窗
  31. },
  32. onShow() {
  33. if (!utils.getStorageSync(storeKeys.TOKEN)) {
  34. this.setData({
  35. isLogOn: false
  36. })
  37. app.changeTabBarBadge();
  38. } else {
  39. this.setData({
  40. isLogOn: true,
  41. isEdit:false,
  42. isAllDelete:false
  43. })
  44. this.onPullDownRefresh();
  45. }
  46. },
  47. onPullDownRefresh: function () {
  48. wx.stopPullDownRefresh();
  49. this.getCartData();
  50. },
  51. // 获取购物车数据
  52. getCartData() {
  53. // wx.showLoading({
  54. // title: "加载中"
  55. // });
  56. http.request({
  57. url: api.URL + '/api/cart/list',
  58. method: 'GET',
  59. token: utils.getStorageSync(storeKeys.TOKEN),
  60. success: (res) => {
  61. let data = res.data.data;
  62. wx.hideLoading();
  63. //可用优惠卷
  64. let userCouponItem = null;
  65. //最终价格
  66. let finalPrice = data.sum_total_price;
  67. //活动优惠金额
  68. let activityPrice = data.activity_discount_total_price || 0;
  69. //共减金额
  70. let totalReduce = activityPrice;
  71. if(finalPrice > 0){
  72. finalPrice = parseFloat(parseFloat(finalPrice)-parseFloat(activityPrice)).toFixed(2)
  73. if(data.user_coupon && data.user_coupon.length > 0){
  74. //第一条数据为最优优惠卷
  75. userCouponItem = data.user_coupon[0];
  76. finalPrice = parseFloat(parseFloat(finalPrice)-parseFloat(userCouponItem.reduced_price)).toFixed(2)
  77. totalReduce = parseFloat(parseFloat(totalReduce)+parseFloat(userCouponItem.reduced_price)).toFixed(2)
  78. }
  79. }
  80. let isShowDetails = false;
  81. if((activityPrice && activityPrice > 0) || userCouponItem){
  82. isShowDetails = true;
  83. }
  84. this.setData({
  85. cartList: data.list,
  86. totalprice: data.sum_total_price,
  87. finalPrice,
  88. userCouponItem,
  89. isShowDetails,
  90. totalReduce,
  91. activityPrice,
  92. isLoading: false
  93. })
  94. //有效数据
  95. let validData = [];
  96. let selectedCartList = [];
  97. data.list && data.list.forEach((item, index) => {
  98. this.selectComponent("#action" + index).closeButtonGroup();
  99. //商品状态err_status 0正常 1库存不足 2商品失效
  100. if (item.goods.status == 10 && item.err_status == 0) {
  101. validData.push(item)
  102. }
  103. })
  104. //处理全选
  105. let isAllSelect = validData.length > 0 ? true : false;
  106. validData.forEach((item, index) => {
  107. this.selectComponent("#action" + index).closeButtonGroup();
  108. if (!item.selected) {
  109. isAllSelect = false;
  110. }else{
  111. selectedCartList.push(item)
  112. }
  113. })
  114. this.setData({
  115. isAllSelect,
  116. validData,
  117. selectedCartList,
  118. isAllDelete:false
  119. })
  120. app.changeTabBarBadge(data.cartTotal);
  121. },
  122. error: (res) => {
  123. wx.hideLoading();
  124. utils.toast(res.data.message)
  125. },
  126. })
  127. },
  128. navigateTo(e) {
  129. if (!this.data.isShowD) {
  130. app.navigateTo(e);
  131. }
  132. },
  133. changeEdit(){
  134. this.setData({
  135. isEdit: !this.data.isEdit
  136. })
  137. },
  138. // 回到首页
  139. goHome() {
  140. wx.switchTab({
  141. url: '/pages/tabBar/index/index'
  142. })
  143. },
  144. // 全部商品
  145. goAllGoods() {
  146. wx.switchTab({
  147. url: '/pages/tabBar/allGoods/allGoods'
  148. })
  149. },
  150. //商品勾选/取消勾选 -- 结算
  151. changeCartSelect(e) {
  152. let {
  153. index
  154. } = e.currentTarget.dataset;
  155. let item = this.data.cartList[index];
  156. //已下架
  157. if (item.goods.status == 20 || item.err_status == 1 || item.err_status == 2) {
  158. return;
  159. }
  160. let cartIds = [];
  161. let selected = item.selected == 1 ? 0 : 1
  162. cartIds.push(item.id)
  163. this.goodsSelect(cartIds, selected);
  164. },
  165. //商品勾选/取消勾选 -- 删除
  166. changeCartDelete(e){
  167. let { index } = e.currentTarget.dataset;
  168. let item = this.data.cartList[index];
  169. this.setData({
  170. [`cartList[${index}].isDelete`]: !item.isDelete
  171. })
  172. this.changeAllDelete();
  173. },
  174. //全选 -- 删除
  175. allDelete(){
  176. let isAllDelete = !this.data.isAllDelete;
  177. let cartList = this.data.cartList;
  178. for(let i = 0; i<cartList.length; i++){
  179. cartList[i].isDelete = isAllDelete
  180. }
  181. this.setData({
  182. cartList,
  183. isAllDelete
  184. })
  185. },
  186. //处理全选
  187. changeAllDelete(){
  188. let isAllDelete = true;
  189. this.data.cartList.forEach((item) => {
  190. if (!item.isDelete) {
  191. isAllDelete = false;
  192. }
  193. })
  194. this.setData({isAllDelete})
  195. },
  196. //批量删除
  197. deleteCart: utils.throttle(function (e) {
  198. let cartIds = [];
  199. this.data.cartList && this.data.cartList.forEach((item) => {
  200. if (item.isDelete) {
  201. cartIds.push(item.id)
  202. }
  203. })
  204. if (cartIds.length <= 0) {
  205. utils.toast("请勾选需要删除的商品")
  206. return;
  207. }
  208. this.setData({
  209. showDeleteModal:true
  210. })
  211. }),
  212. //确定批量删除
  213. confirmDelete: utils.throttle(function (e) {
  214. let cartIds = [];
  215. this.data.cartList && this.data.cartList.forEach((item)=>{
  216. if(item.isDelete){
  217. cartIds.push(item.id)
  218. }
  219. })
  220. let data = {
  221. cartIds
  222. }
  223. wx.showLoading({
  224. title: "加载中"
  225. });
  226. http.request({
  227. url: api.URL + '/api/cart/delete',
  228. method: 'POST',
  229. data,
  230. token: utils.getStorageSync(storeKeys.TOKEN),
  231. success: (res) => {
  232. this.getCartData();
  233. this.hideDeleteModal()
  234. },
  235. error: (res) => {
  236. wx.hideLoading();
  237. utils.toast(res.data.message)
  238. },
  239. })
  240. }),
  241. //取消批量删除
  242. hideDeleteModal(){
  243. this.setData({
  244. showDeleteModal:false
  245. })
  246. },
  247. //执行商品勾选/取消勾选
  248. goodsSelect(cartIds, selected) {
  249. let data = {
  250. cartIds, //购物车ID集
  251. selected //勾选状态 0不勾选 1勾选
  252. }
  253. // wx.showLoading({
  254. // title: "加载中"
  255. // });
  256. http.request({
  257. url: api.URL + '/api/cart/goods_select',
  258. method: 'POST',
  259. data,
  260. token: utils.getStorageSync(storeKeys.TOKEN),
  261. success: (res) => {
  262. this.getCartData();
  263. },
  264. error: (res) => {
  265. wx.hideLoading();
  266. utils.toast(res.data.message)
  267. },
  268. })
  269. },
  270. //增加/删减商品数量
  271. changeCartNumber: utils.throttle(function (e) {
  272. let {
  273. index,
  274. state
  275. } = e.currentTarget.dataset;
  276. let cartList = this.data.cartList;
  277. let item = cartList[index];
  278. let goodsNum = item.goods_num;
  279. // state:1增加 0删减
  280. if (state == 1) {
  281. if (goodsNum >= item.goods.skuInfo.stock_num) {
  282. utils.toast("库存不足")
  283. return;
  284. }
  285. goodsNum++
  286. } else {
  287. if (goodsNum <= 1) {
  288. utils.toast("不能再减了~");
  289. return;
  290. }
  291. goodsNum--;
  292. }
  293. let data = {
  294. "goodsId": item.goods_id, //商品ID
  295. "goodsSkuId": item.goods_sku_id, //商品规格ID
  296. "goodsNum": goodsNum //修改后的商品规格数量,不能小于1
  297. }
  298. // wx.showLoading({
  299. // title: "加载中"
  300. // });
  301. http.request({
  302. url: api.URL + '/api/cart/upd',
  303. method: 'POST',
  304. data,
  305. token: utils.getStorageSync(storeKeys.TOKEN),
  306. success: (res) => {
  307. this.getCartData();
  308. },
  309. error: (res) => {
  310. wx.hideLoading();
  311. utils.toast(res.data.message)
  312. },
  313. })
  314. }, 800),
  315. //删除购物车数据
  316. cartDelete(e) {
  317. let index = e.currentTarget.dataset.index;
  318. let item = this.data.cartList[index];
  319. let data = {
  320. cartIds: [item.id]
  321. }
  322. // wx.showLoading({
  323. // title: "加载中"
  324. // });
  325. http.request({
  326. url: api.URL + '/api/cart/delete',
  327. method: 'POST',
  328. data,
  329. token: utils.getStorageSync(storeKeys.TOKEN),
  330. success: (res) => {
  331. this.getCartData();
  332. },
  333. error: (res) => {
  334. wx.hideLoading();
  335. utils.toast(res.data.message)
  336. },
  337. })
  338. },
  339. //全选
  340. allSelect: utils.throttle(function (e) {
  341. let cartIds = [];
  342. let selected = this.data.isAllSelect ? 0 : 1;
  343. this.data.cartList.forEach((item) => {
  344. if (item.goods.status == 10 && item.err_status == 0) {
  345. cartIds.push(item.id)
  346. }
  347. })
  348. if (cartIds.length <= 0) {
  349. return;
  350. }
  351. this.goodsSelect(cartIds, selected);
  352. }, 800),
  353. //结算
  354. settlement() {
  355. let cartIds = "";
  356. this.data.cartList.forEach((item, index) => {
  357. if (item.selected == 1) {
  358. cartIds += index < this.data.cartList.length - 1 ? item.id + ',' : item.id;
  359. }
  360. })
  361. if (!cartIds) {
  362. utils.toast("您还没有选择哦~")
  363. return;
  364. }
  365. let data = {
  366. mode: "cart",
  367. cartIds
  368. };
  369. let url = "/pages/cart/pages/confirmOrder/confirmOrder?data=" + JSON.stringify(data);
  370. app.navigateToUrl(url);
  371. },
  372. //底部显示规则选择
  373. showRulesPopup(e) {
  374. let index = e.currentTarget.dataset.index;
  375. let item = this.data.cartList[index];
  376. if (item.err_status == 1) {
  377. return;
  378. }
  379. this.data.rulesIndex = index;
  380. wx.showLoading({
  381. title: "加载中"
  382. });
  383. http.request({
  384. url: api.URL + '/api/goods/' + item.goods.goods_id,
  385. method: 'GET',
  386. token: utils.getStorageSync(storeKeys.TOKEN),
  387. success: (res) => {
  388. wx.hideLoading();
  389. let skuList = res.data.data.skuList;
  390. //目前选中的规则
  391. let skuId = item.goods.skuInfo.goods_sku_id;
  392. let rulesSkuObj = {};
  393. if (skuList) {
  394. for (let i = 0; i < skuList.length; i++) {
  395. if (skuList[i].stock_num < 1 || !skuList[i].stock_num) {
  396. skuList[i].lack = true;
  397. }
  398. if (skuId == skuList[i].goods_sku_id) {
  399. rulesSkuObj = skuList[i];
  400. }
  401. }
  402. }
  403. this.setData({
  404. showRulesPopup: true,
  405. rulesSkuList: skuList,
  406. rulesSkuObj
  407. })
  408. },
  409. error: (res) => {
  410. wx.hideLoading();
  411. utils.toast(res.data.message)
  412. },
  413. })
  414. },
  415. //规格切换
  416. changeSpec(e) {
  417. let index = e.currentTarget.dataset.index;
  418. let item = this.data.rulesSkuList[index];
  419. if (item.lack) {
  420. utils.toast("库存不足")
  421. return;
  422. }
  423. this.setData({
  424. rulesSkuObj: item
  425. })
  426. },
  427. //确认更改规则
  428. confirmBuyPopup() {
  429. let cartItem = this.data.cartList[this.data.rulesIndex];
  430. let cartId = cartItem.id;
  431. let goodsId = cartItem.goods.goods_id;
  432. let goodsSkuId = this.data.rulesSkuObj.goods_sku_id;
  433. let data = {
  434. cartId, //购物车ID
  435. goodsId, //商品ID
  436. goodsSkuId //商品规格ID
  437. }
  438. // wx.showLoading({
  439. // title: "加载中"
  440. // });
  441. http.request({
  442. url: api.URL + '/api/cart/updSku',
  443. method: 'POST',
  444. data,
  445. token: utils.getStorageSync(storeKeys.TOKEN),
  446. success: (res) => {
  447. this.getCartData();
  448. this.hideRulesPopup()
  449. },
  450. error: (res) => {
  451. wx.hideLoading();
  452. utils.toast(res.data.message)
  453. },
  454. })
  455. },
  456. //底部隐藏规则选择
  457. hideRulesPopup() {
  458. this.setData({
  459. showRulesPopup: false
  460. })
  461. },
  462. changeTap(e) {
  463. this.data.isShowD = e.detail
  464. },
  465. //显示查看明细
  466. showDetailsPopup(){
  467. let showDetailsPopup = !this.data.showDetailsPopup;
  468. this.setData({
  469. showDetailsPopup
  470. })
  471. },
  472. //隐藏查看明细
  473. hideDetailsPopup(){
  474. this.setData({
  475. showDetailsPopup: false
  476. })
  477. },
  478. onHide(){
  479. this.hideDetailsPopup()
  480. }
  481. })