1
0

cart.js 14 KB

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