1
0

cart.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. var util = require('../../utils/util.js');
  2. var goodsUtil = require('../../utils/goods.js');
  3. var api = require('../../config/api.js');
  4. var app = getApp();
  5. Page({
  6. data: {
  7. cartGoods00: [],//保税仓数据
  8. cartGoods02: [],//保税展示数据
  9. cartGoods10: [],//现场速递数据
  10. cartGoods11: [],//普通商品
  11. cartGoods: [],
  12. validCartList: [],
  13. goodsBizTypeList: ['保税仓','保税展示','现场速递','普通商品'],
  14. goodsBizType: '',//货品业务类型
  15. footprintList: [],
  16. cartTotal: {
  17. "goodsCount": 0,
  18. "goodsAmount": 0.00,
  19. "checkedGoodsCount": 0,
  20. "checkedGoodsAmount": 0.00
  21. },
  22. checkedAllStatus: true,
  23. checkedTypeStatus00: true,
  24. checkedTypeStatus02: true,
  25. checkedTypeStatus10: true,
  26. checkedTypeStatus11: true,
  27. couponInfoList: [],
  28. openAttr: false,
  29. specificationList: {},
  30. checkedSpecText: '请选择规格数量',
  31. number: 1,
  32. isAdd: true,
  33. mobile: ''
  34. },
  35. onLoad: function (options) {
  36. // 页面初始化 options为页面跳转所带来的参数
  37. },
  38. onReady: function () {
  39. // 页面渲染完成
  40. },
  41. onShow: function () {
  42. // 页面显示
  43. this.getCartList();
  44. this.getFootprintList();
  45. },
  46. onHide: function () {
  47. // 页面隐藏
  48. },
  49. onUnload: function () {
  50. // 页面关闭
  51. },
  52. setCommonData(res) {
  53. let that = this;
  54. that.setData({
  55. cartGoods: res.data.cartList,
  56. cartGoods00: res.data.cart00List,
  57. cartGoods02: res.data.cart02List,
  58. cartGoods10: res.data.cart10List,
  59. cartGoods11: res.data.cart11List,
  60. validCartList: res.data.validCartList,
  61. cartTotal: res.data.cartTotal,
  62. couponInfoList: res.data.couponInfoList
  63. });
  64. },
  65. setCheckedData() {
  66. let that = this;
  67. that.setData({
  68. checkedAllStatus: that.isCheckedAll(),
  69. checkedTypeStatus00: that.isCheckedTypeStatus00(),
  70. checkedTypeStatus02: that.isCheckedTypeStatus02(),
  71. checkedTypeStatus10: that.isCheckedTypeStatus10(),
  72. checkedTypeStatus11: that.isCheckedTypeStatus11()
  73. });
  74. },
  75. setCheckedAllData() {
  76. let that = this;
  77. that.setData({
  78. checkedAllStatus: that.isCheckedAll(),
  79. checkedTypeStatus00: that.isCheckedAll(),
  80. checkedTypeStatus02: that.isCheckedAll(),
  81. checkedTypeStatus10: that.isCheckedAll(),
  82. checkedTypeStatus11: that.isCheckedAll()
  83. });
  84. },
  85. getCartList: function () {//获取购物车数据
  86. let that = this;
  87. util.request(api.CartList).then(function (res) {
  88. if (res.errno === 0) {
  89. that.setCommonData(res);
  90. }
  91. //数据渲染选中
  92. that.setCheckedData();
  93. });
  94. },
  95. isCheckedAll: function () {
  96. //判断购物车所有商品是否已全选
  97. return this.data.cartGoods.every(function (element, index, array) {
  98. if (element.checked == true) {
  99. return true;
  100. } else {
  101. return false;
  102. }
  103. });
  104. },
  105. isCheckedTypeStatus00: function () {
  106. //判断该业务类型的购物车商品是否已全选
  107. return this.data.cartGoods00.every(function (element, index, array) {
  108. if (element.checked == true) {
  109. return true;
  110. } else {
  111. return false;
  112. }
  113. });
  114. },
  115. isCheckedTypeStatus02: function () {
  116. //判断该业务类型的购物车商品是否已全选
  117. return this.data.cartGoods02.every(function (element, index, array) {
  118. if (element.checked == true) {
  119. return true;
  120. } else {
  121. return false;
  122. }
  123. });
  124. },
  125. isCheckedTypeStatus10: function () {
  126. //判断该业务类型的购物车商品是否已全选
  127. return this.data.cartGoods10.every(function (element, index, array) {
  128. if (element.checked == true) {
  129. return true;
  130. } else {
  131. return false;
  132. }
  133. });
  134. },
  135. isCheckedTypeStatus11: function () {
  136. //判断该业务类型的购物车商品是否已全选
  137. return this.data.cartGoods11.every(function (element, index, array) {
  138. if (element.checked == true) {
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. });
  144. },
  145. toIndexPage: function () {
  146. wx.switchTab({
  147. url: "/pages/index/index"
  148. });
  149. },
  150. checkedItem: function (event) {
  151. let itemIndex = event.target.dataset.itemIndex;
  152. let that = this;
  153. util.request(api.CartChecked, {
  154. productIds: that.data.cartGoods[itemIndex].product_id,
  155. isChecked: that.data.cartGoods[itemIndex].checked ? 0 : 1
  156. }, 'POST').then(function (res) {
  157. if (res.errno === 0) {
  158. that.setCommonData(res);
  159. }
  160. that.setCheckedData();
  161. });
  162. },
  163. getCheckedGoodsCount: function () {
  164. let checkedGoodsCount = 0;
  165. this.data.cartGoods.forEach(function (v) {
  166. if (v.checked === true) {
  167. checkedGoodsCount += v.number;
  168. }
  169. });
  170. console.log(checkedGoodsCount);
  171. return checkedGoodsCount;
  172. },
  173. checkedAll: function () {
  174. let that = this;
  175. var productIds = this.data.cartGoods.map(function (v) {
  176. return v.product_id;
  177. });
  178. util.request(api.CartChecked, {
  179. productIds: productIds.join(','),
  180. isChecked: that.isCheckedAll() ? 0 : 1
  181. }, 'POST').then(function (res) {
  182. if (res.errno === 0) {
  183. that.setCommonData(res);
  184. }
  185. that.setCheckedAllData();
  186. });
  187. },
  188. checkedAllGoodType: function (e) {
  189. let that = this;
  190. let goodsBizType = e.target.dataset.goodsBizType;
  191. let isCheckedTypeStatu;
  192. if (goodsBizType == '00') {
  193. isCheckedTypeStatu = that.isCheckedTypeStatus00();
  194. }
  195. if (goodsBizType == '02') {
  196. isCheckedTypeStatu = that.isCheckedTypeStatus02();
  197. }
  198. if (goodsBizType == '10') {
  199. isCheckedTypeStatu = that.isCheckedTypeStatus10();
  200. }
  201. if (goodsBizType == '11') {
  202. isCheckedTypeStatu = that.isCheckedTypeStatus11();
  203. }
  204. util.request(api.CartChecked, {
  205. isChecked: isCheckedTypeStatu ? 0 : 1,
  206. goodsBizType: goodsBizType
  207. }, 'POST').then(function (res) {
  208. if (res.errno === 0) {
  209. that.setCommonData(res);
  210. }
  211. if (goodsBizType == '00') {
  212. that.setData({
  213. checkedTypeStatus00: that.isCheckedTypeStatus00()
  214. });
  215. }
  216. if (goodsBizType == '02') {
  217. that.setData({
  218. checkedTypeStatus02: that.isCheckedTypeStatus02()
  219. });
  220. }
  221. if (goodsBizType == '10') {
  222. that.setData({
  223. checkedTypeStatus10: that.isCheckedTypeStatus10()
  224. });
  225. }
  226. if (goodsBizType == '11') {
  227. that.setData({
  228. checkedTypeStatus11: that.isCheckedTypeStatus11()
  229. });
  230. }
  231. that.setData({
  232. checkedAllStatus: that.isCheckedAll()
  233. });
  234. });
  235. },
  236. updateCart: function (productId, goodsId, number, beforeNumber, id, itemIndex) {
  237. let that = this;
  238. util.request(api.CartUpdate, {
  239. productId: productId,
  240. goodsId: goodsId,
  241. number: number,
  242. id: id
  243. }, 'POST').then(function (res) {
  244. if (res.errno === 0) {
  245. console.log(res.data);
  246. that.setCommonData(res);
  247. } else {
  248. // util.showErrorToast(res.errmsg);
  249. wx.showModal({
  250. title: '提示信息',
  251. content: res.errmsg,
  252. showCancel: false
  253. });
  254. let cartItem = that.data.cartGoods[itemIndex];
  255. cartItem.number = beforeNumber;
  256. that.setData({
  257. cartGoods: that.data.cartGoods
  258. });
  259. }
  260. that.setCheckedData();
  261. });
  262. },
  263. cutNumber: function (event) {
  264. let itemIndex = event.target.dataset.itemIndex;
  265. let cartItem = this.data.cartGoods[itemIndex];
  266. let beforeNumber = cartItem.number;
  267. let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
  268. cartItem.number = number;
  269. this.setData({
  270. cartGoods: this.data.cartGoods
  271. });
  272. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  273. },
  274. addNumber: function (event) {
  275. let itemIndex = event.target.dataset.itemIndex;
  276. let cartItem = this.data.cartGoods[itemIndex];
  277. let beforeNumber = cartItem.number;
  278. let number = cartItem.number + 1;
  279. cartItem.number = number;
  280. this.setData({
  281. cartGoods: this.data.cartGoods
  282. });
  283. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  284. },
  285. checkoutOrder: function () {
  286. //获取已选择的商品
  287. let that = this;
  288. util.request(api.getCurUser, {
  289. userInfo: app.globalData.userInfo
  290. }, 'POST').then(function (res) {
  291. if (res.errno === 0) {
  292. console.log('that.data.mobile:' + res.data.mobile);
  293. if (res.data.mobile == '' || res.data.mobile == null) {
  294. wx.showModal({
  295. title: '',
  296. confirmColor: '#b4282d',
  297. showCancel: false,
  298. content: '您的手机号码未绑定,请先绑定手机号再进行购买',
  299. success: function (res) {
  300. if (res.confirm) {
  301. wx.navigateTo({
  302. url: '../../pages/auth/newuser/newuser'
  303. });
  304. }
  305. }
  306. });
  307. } else {
  308. var checkedGoods = that.data.cartGoods.filter(function (element, index, array) {
  309. if (element.checked == true) {
  310. return true;
  311. } else {
  312. return false;
  313. }
  314. });
  315. if (checkedGoods.length <= 0) {
  316. return false;
  317. }
  318. wx.navigateTo({
  319. url: '../shopping/checkout/checkout'
  320. })
  321. }
  322. }
  323. });
  324. },
  325. deleteCart: function (event) {
  326. //获取已选择的商品
  327. let that = this;
  328. let cartId = event.target.dataset.cartId;
  329. let goodsName = event.target.dataset.goodsName;
  330. wx.showModal({
  331. title: '',
  332. content: '确定要删除' + goodsName + '?',
  333. success: function (res) {
  334. if (res.confirm) {
  335. util.request(api.CartDelete, {
  336. cartId: cartId
  337. }, 'POST').then(function (res) {
  338. if (res.errno === 0) {
  339. that.setCommonData(res);
  340. }
  341. that.setCheckedData();
  342. });
  343. console.log('用户点击确定')
  344. }
  345. }
  346. });
  347. },
  348. getFootprintList() {
  349. let that = this;
  350. util.request(api.GuessFootprintList, { storeId: wx.getStorageSync('storeId') },).then(function (res) {
  351. if (res.errno === 0) {
  352. that.setData({
  353. footprintList: res.data.list
  354. });
  355. }
  356. });
  357. },
  358. switchAttrPop: function () {
  359. this.setData({
  360. openAttr: !this.data.openAttr
  361. })
  362. },
  363. clickSkuValue: function (event) {
  364. let that = this;
  365. let specValueId = event.currentTarget.dataset.valueId;
  366. let index = event.currentTarget.dataset.index;
  367. let _specificationList = this.data.specificationList;
  368. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  369. if (_specificationList[index].valueList[j].id == specValueId) {
  370. //如果已经选中,则反选
  371. if (_specificationList[index].valueList[j].checked) {
  372. _specificationList[index].valueList[j].checked = false;
  373. } else {
  374. _specificationList[index].valueList[j].checked = true;
  375. }
  376. } else {
  377. _specificationList[index].valueList[j].checked = false;
  378. }
  379. }
  380. this.setData({
  381. 'specificationList': _specificationList
  382. });
  383. //重新计算spec改变后的信息
  384. goodsUtil.changeSpecInfo(that);
  385. },
  386. cutNumber2: function () {
  387. this.setData({
  388. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  389. });
  390. },
  391. addNumber2: function () {
  392. this.setData({
  393. number: this.data.number + 1
  394. });
  395. },
  396. //购物车增加
  397. addCart: function (e) {
  398. let that = this;
  399. var goodsId = e.currentTarget.dataset.goodsId;
  400. util.request(api.GoodsSku, { goodsId: goodsId }).then(function (res) {
  401. if (res.errno === 0 && null != res.data) {
  402. that.setData({
  403. goodsVo: res.data.goodsVo,
  404. specificationList: res.data.specificationList,
  405. productList: res.data.productList,
  406. openAttr: !that.data.openAttr
  407. });
  408. //
  409. let _specificationList = res.data.specificationList;
  410. for (let i = 0; i < _specificationList.length; i++) {
  411. if (_specificationList[i].valueList.length == 1) {
  412. //如果已经选中,则反选
  413. _specificationList[i].valueList[0].checked = true;
  414. }
  415. }
  416. that.setData({
  417. 'specificationList': _specificationList
  418. });
  419. }
  420. });
  421. },
  422. //购物车增加
  423. addToCart: function () {
  424. let that = this;
  425. var goodsId = that.data.goodsVo.id;
  426. //提示选择完整规格
  427. if (!that.data.productList || !that.data.productList.length) {
  428. util.showErrorToast('当前门店没有库存');
  429. return false;
  430. }
  431. //提示选择完整规格
  432. if (!goodsUtil.isCheckedAllSpec(that)) {
  433. return false;
  434. }
  435. //根据选中的规格,判断是否有对应的sku信息
  436. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  437. if (!checkedProduct || checkedProduct.length <= 0) {
  438. //找不到对应的product信息,提示没有库存
  439. return false;
  440. }
  441. //验证库存
  442. if (checkedProduct.stock_num < this.data.number) {
  443. //找不到对应的product信息,提示没有库存
  444. return false;
  445. }
  446. util.request(api.CartAdd, {
  447. goodsId: goodsId,
  448. productId: checkedProduct[0].id,
  449. number: this.data.number
  450. }, 'POST').then(function (res) {
  451. if (res.errno === 0 && null != res.data) {
  452. wx.showToast({
  453. title: '添加成功',
  454. icon: 'success',
  455. mask: true
  456. });
  457. that.setData({
  458. openAttr: !that.data.openAttr
  459. })
  460. // 页面显示
  461. that.getCartList();
  462. that.getFootprintList();
  463. } else {
  464. util.showErrorToast(res.errmsg)
  465. }
  466. });
  467. },
  468. deleteValidCart: function () {//获取购物车数据
  469. let that = this;
  470. wx.showModal({
  471. title: '',
  472. content: '确定要清空所有失效商品?',
  473. success: function (res) {
  474. if (res.confirm) {
  475. util.request(api.deleteValidCart).then(function (res) {
  476. if (res.errno === 0) {
  477. that.setCommonData(res);
  478. } else {
  479. util.showErrorToast(res.errmsg);
  480. }
  481. });
  482. }
  483. }
  484. });
  485. }
  486. })