1
0

cart.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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. retailPrice: '',
  35. stockNum: 0,
  36. cartNumber: 0,
  37. checkCart: app.globalData.appCheckCart,
  38. total00: 0,
  39. total11: 0,
  40. page: 1,
  41. size: 4,
  42. openTicketAttr: false,
  43. ticketDiscountList: [],
  44. ticketDiscountTotal: 0
  45. },
  46. onLoad: function(options) {
  47. // 页面初始化 options为页面跳转所带来的参数
  48. let that = this;
  49. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  50. that.setData({
  51. isLogin: true
  52. });
  53. } else {
  54. that.setData({
  55. isLogin: false
  56. });
  57. }
  58. },
  59. onReady: function() {
  60. // 页面渲染完成
  61. },
  62. onShow: function () {
  63. let that = this;
  64. that.setData({
  65. checkCart: app.globalData.appCheckCart,
  66. });
  67. // 页面显示
  68. if (wx.getStorageSync('userInfo') && wx.getStorageSync('token')) {
  69. if (wx.getStorageSync('storeId')) {
  70. util.request(api.ChooseStoreId, {
  71. storeId: wx.getStorageSync('storeId'),
  72. merchSn: wx.getStorageSync('merchSn'),
  73. isRefusedLogin: wx.getStorageSync('isRefusedLogin')
  74. }, 'POST').then(function (res) {
  75. if (res.errno === 0) {
  76. wx.setStorageSync('storeId', wx.getStorageSync('storeId'));
  77. wx.setStorageSync('merchSn', wx.getStorageSync('merchSn'));
  78. that.reLoad();
  79. }
  80. });
  81. that.setData({
  82. footprintList: [],
  83. page: 1,
  84. isLogin: true
  85. });
  86. that.getCartList();
  87. that.getTicketDiscountList();
  88. that.getFootprintList();
  89. }
  90. } else {
  91. that.setData({
  92. isLogin: false
  93. });
  94. // wx.navigateTo({
  95. // url: '/pages/auth/btnAuth/btnAuth',
  96. // })
  97. if (wx.getStorageSync('isLocationIndex') != 'true') {//购物车是否已跳转登录页,为false未跳转则跳转
  98. wx.navigateTo({
  99. url: '/pages/ucenter/userLogin/userLogin?view=cart'
  100. })
  101. // wx.switchTab({
  102. // url: '/pages/index/index'
  103. // });
  104. }
  105. }
  106. },
  107. openSortFilter: function(event) {
  108. let that = this;
  109. let currentId = event.currentTarget.id;
  110. switch (currentId) {
  111. case 'defaultActivity':
  112. that.setData({
  113. 'checkCart': '00',
  114. footprintList: [],
  115. page: 1
  116. });
  117. app.globalData.appCheckCart = '00';
  118. this.getCartList();
  119. this.getFootprintList();
  120. break;
  121. case 'ordActivity':
  122. that.setData({
  123. 'checkCart': '11',
  124. footprintList: [],
  125. page: 1
  126. });
  127. app.globalData.appCheckCart = '11';
  128. this.getCartList();
  129. this.getFootprintList();
  130. break;
  131. default:
  132. //综合排序
  133. that.setData({
  134. 'checkCart': '00',
  135. footprintList: [],
  136. page: 1
  137. });
  138. app.globalData.appCheckCart = '00';
  139. this.getCartList();
  140. this.getFootprintList();
  141. }
  142. },
  143. onHide: function() {
  144. // 页面隐藏
  145. },
  146. onUnload: function() {
  147. // 页面关闭
  148. },
  149. reLoad: function () {
  150. let that = this;
  151. if (wx.getStorageSync('storeId')) {
  152. if (wx.getStorageSync('userId')) {
  153. wx.request({
  154. url: api.updateLoginUser,
  155. data: {
  156. userId: wx.getStorageSync('userId'), storeId: wx.getStorageSync('storeId'), merchSn: wx.getStorageSync('merchSn')
  157. },
  158. method: 'POST',
  159. header: {
  160. 'Content-Type': 'application/json'
  161. },
  162. success: function (wxRes) {
  163. if (wxRes.data.errno === 0) {
  164. // console.log("用户信息更新成功");
  165. }
  166. },
  167. fail: function (err) {
  168. console.log("failed");
  169. }
  170. });
  171. }
  172. }
  173. },
  174. setCommonData(res) {
  175. let that = this;
  176. that.setData({
  177. cartGoods: res.data.cartList,
  178. cartGoods00: res.data.cart00List,
  179. cartGoods02: res.data.cart02List,
  180. cartGoods10: res.data.cart10List,
  181. cartGoods11: res.data.cart11List,
  182. validCartList: res.data.validCartList,
  183. cartTotal: res.data.cartTotal,
  184. couponInfoList: res.data.couponInfoList,
  185. total00: res.data.total00,
  186. total11: res.data.total11,
  187. ticketDiscountTotal: res.data.ticketDiscountTotal
  188. });
  189. },
  190. setCheckedData() {
  191. let that = this;
  192. that.setData({
  193. checkedAllStatus: that.isCheckedAll(),
  194. checkedTypeStatus00: that.isCheckedTypeStatus00(),
  195. checkedTypeStatus02: that.isCheckedTypeStatus02(),
  196. checkedTypeStatus10: that.isCheckedTypeStatus10(),
  197. checkedTypeStatus11: that.isCheckedTypeStatus11()
  198. });
  199. },
  200. setCheckedAllData() {
  201. let that = this;
  202. that.setData({
  203. checkedAllStatus: that.isCheckedAll(),
  204. checkedTypeStatus00: that.isCheckedAll(),
  205. checkedTypeStatus02: that.isCheckedAll(),
  206. checkedTypeStatus10: that.isCheckedAll(),
  207. checkedTypeStatus11: that.isCheckedAll()
  208. });
  209. },
  210. getCartList: function() { //获取购物车数据
  211. let that = this;
  212. wx.showLoading({
  213. title: '加载中...',
  214. });
  215. console.log('分享的storeId' + wx.getStorageSync('storeId'))
  216. util.request(api.CartList, {
  217. checkCart: that.data.checkCart,
  218. storeId: wx.getStorageSync('storeId')
  219. }).then(function(res) {
  220. if (res.errno === 0) {
  221. that.setCommonData(res);
  222. }
  223. wx.hideLoading();
  224. //数据渲染选中
  225. that.setCheckedData();
  226. });
  227. },
  228. isCheckedAll: function() {
  229. //判断购物车所有商品是否已全选
  230. return this.data.cartGoods.every(function(element, index, array) {
  231. if (element.checked == true) {
  232. return true;
  233. } else {
  234. return false;
  235. }
  236. });
  237. },
  238. isCheckedTypeStatus00: function() {
  239. //判断该业务类型的购物车商品是否已全选
  240. return this.data.cartGoods00.every(function(element, index, array) {
  241. if (element.checked == true) {
  242. return true;
  243. } else {
  244. return false;
  245. }
  246. });
  247. },
  248. isCheckedTypeStatus02: function() {
  249. //判断该业务类型的购物车商品是否已全选
  250. return this.data.cartGoods02.every(function(element, index, array) {
  251. if (element.checked == true) {
  252. return true;
  253. } else {
  254. return false;
  255. }
  256. });
  257. },
  258. isCheckedTypeStatus10: function() {
  259. //判断该业务类型的购物车商品是否已全选
  260. return this.data.cartGoods10.every(function(element, index, array) {
  261. if (element.checked == true) {
  262. return true;
  263. } else {
  264. return false;
  265. }
  266. });
  267. },
  268. isCheckedTypeStatus11: function() {
  269. //判断该业务类型的购物车商品是否已全选
  270. return this.data.cartGoods11.every(function(element, index, array) {
  271. if (element.checked == true) {
  272. return true;
  273. } else {
  274. return false;
  275. }
  276. });
  277. },
  278. toIndexPage: function() {
  279. wx.switchTab({
  280. url: "/pages/index/index"
  281. });
  282. },
  283. checkedItem: function(event) {
  284. let itemIndex = event.target.dataset.itemIndex;
  285. let goodsBizType = event.target.dataset.goodsBizType;
  286. let goodsId = event.target.dataset.goodsId;
  287. let checked = event.target.dataset.checked;
  288. let that = this;
  289. // console.log(goodsBizType);
  290. // console.log(goodsId);
  291. // console.log(that.data.cartGoods[itemIndex].checked);
  292. util.request(api.CartChecked, {
  293. goodsIds: goodsId,
  294. isChecked: checked ? 0 : 1,
  295. checkCart: that.data.checkCart
  296. }, 'POST').then(function(res) {
  297. if (res.errno === 0) {
  298. that.setCommonData(res);
  299. }
  300. that.setCheckedData();
  301. });
  302. },
  303. getCheckedGoodsCount: function() {
  304. let checkedGoodsCount = 0;
  305. this.data.cartGoods.forEach(function(v) {
  306. if (v.checked === true) {
  307. checkedGoodsCount += v.number;
  308. }
  309. });
  310. return checkedGoodsCount;
  311. },
  312. checkedAll: function() {
  313. let that = this;
  314. var goodsIds = this.data.cartGoods.map(function(v) {
  315. return v.goodsIds;
  316. });
  317. util.request(api.CartChecked, {
  318. goodsIds: goodsIds.join(','),
  319. isChecked: that.isCheckedAll() ? 0 : 1
  320. }, 'POST').then(function(res) {
  321. if (res.errno === 0) {
  322. that.setCommonData(res);
  323. }
  324. that.setCheckedAllData();
  325. });
  326. },
  327. checkedAllGoodType: function(e) {
  328. let that = this;
  329. let goodsBizType = e.target.dataset.goodsBizType;
  330. let isCheckedTypeStatu;
  331. let checkCart;
  332. if (goodsBizType == '00') {
  333. isCheckedTypeStatu = that.isCheckedTypeStatus00();
  334. checkCart = '00';
  335. }
  336. if (goodsBizType == '02') {
  337. isCheckedTypeStatu = that.isCheckedTypeStatus02();
  338. checkCart = '00';
  339. }
  340. if (goodsBizType == '10') {
  341. isCheckedTypeStatu = that.isCheckedTypeStatus10();
  342. checkCart = '00';
  343. }
  344. if (goodsBizType == '11') {
  345. isCheckedTypeStatu = that.isCheckedTypeStatus11();
  346. checkCart = '11';
  347. }
  348. util.request(api.CartChecked, {
  349. isChecked: isCheckedTypeStatu ? 0 : 1,
  350. goodsBizType: goodsBizType,
  351. checkCart: checkCart
  352. }, 'POST').then(function(res) {
  353. if (res.errno === 0) {
  354. // that.setCommonData(res);
  355. that.setData({
  356. validCartList: res.data.validCartList,
  357. couponInfoList: res.data.couponInfoList,
  358. ticketDiscountTotal: res.data.ticketDiscountTotal
  359. });
  360. if (goodsBizType == '11') {
  361. that.setData({
  362. cartGoods: res.data.cartList,
  363. cartTotal: res.data.cartTotal,
  364. cartGoods11: res.data.cart11List,
  365. total11: res.data.total11
  366. });
  367. } else {
  368. that.setData({
  369. cartGoods: res.data.cartList,
  370. cartTotal: res.data.cartTotal,
  371. cartGoods00: res.data.cart00List,
  372. cartGoods02: res.data.cart02List,
  373. cartGoods10: res.data.cart10List,
  374. total00: res.data.total00
  375. });
  376. }
  377. }
  378. if (goodsBizType == '00') {
  379. that.setData({
  380. checkedTypeStatus00: that.isCheckedTypeStatus00()
  381. });
  382. }
  383. if (goodsBizType == '02') {
  384. that.setData({
  385. checkedTypeStatus02: that.isCheckedTypeStatus02()
  386. });
  387. }
  388. if (goodsBizType == '10') {
  389. that.setData({
  390. checkedTypeStatus10: that.isCheckedTypeStatus10()
  391. });
  392. }
  393. if (goodsBizType == '11') {
  394. that.setData({
  395. checkedTypeStatus11: that.isCheckedTypeStatus11()
  396. });
  397. }
  398. that.setData({
  399. checkedAllStatus: that.isCheckedAll()
  400. });
  401. });
  402. },
  403. updateCart: function(productId, goodsId, number, beforeNumber, id, itemIndex) {
  404. let that = this;
  405. util.request(api.CartUpdate, {
  406. productId: productId,
  407. goodsId: goodsId,
  408. number: number,
  409. id: id,
  410. checkCart: that.data.checkCart
  411. }, 'POST').then(function(res) {
  412. if (res.errno === 0) {
  413. // console.log(res.data);
  414. that.setCommonData(res);
  415. } else {
  416. // util.showErrorToast(res.errmsg);
  417. wx.showModal({
  418. title: '提示信息',
  419. content: res.errmsg,
  420. showCancel: false
  421. });
  422. let cartItem = that.data.cartGoods[itemIndex];
  423. cartItem.number = beforeNumber;
  424. that.setData({
  425. cartGoods: that.data.cartGoods
  426. });
  427. that.setCommonData(res);
  428. }
  429. that.setCheckedData();
  430. });
  431. },
  432. cutNumber: function (event) {
  433. let goodsBizType = event.target.dataset.goodsBizType;
  434. let cartItem = {};
  435. let itemIndex = event.target.dataset.itemIndex;
  436. if (goodsBizType == '00') {
  437. cartItem = this.data.cartGoods00[itemIndex];
  438. }
  439. if (goodsBizType == '02') {
  440. cartItem = this.data.cartGoods02[itemIndex];
  441. }
  442. if (goodsBizType == '10') {
  443. cartItem = this.data.cartGoods10[itemIndex];
  444. }
  445. if (goodsBizType == '11') {
  446. cartItem = this.data.cartGoods11[itemIndex];
  447. }
  448. let beforeNumber = cartItem.number;
  449. let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
  450. cartItem.number = number;
  451. if (goodsBizType == '00') {
  452. this.setData({
  453. cartGoods00: this.data.cartGoods00
  454. });
  455. }
  456. if (goodsBizType == '02') {
  457. this.setData({
  458. cartGoods02: this.data.cartGoods02
  459. });
  460. }
  461. if (goodsBizType == '10') {
  462. this.setData({
  463. cartGoods10: this.data.cartGoods10
  464. });
  465. }
  466. if (goodsBizType == '11') {
  467. this.setData({
  468. cartGoods11: this.data.cartGoods11
  469. });
  470. }
  471. this.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  472. },
  473. addNumber: function (event) {
  474. let that = this;
  475. let goodsBizType = event.target.dataset.goodsBizType;
  476. let cartItem = {};
  477. let itemIndex = event.target.dataset.itemIndex;
  478. if (goodsBizType == '00') {
  479. cartItem = that.data.cartGoods00[itemIndex];
  480. }
  481. if (goodsBizType == '02') {
  482. cartItem = that.data.cartGoods02[itemIndex];
  483. }
  484. if (goodsBizType == '10') {
  485. cartItem = that.data.cartGoods10[itemIndex];
  486. }
  487. if (goodsBizType == '11') {
  488. cartItem = that.data.cartGoods11[itemIndex];
  489. }
  490. let beforeNumber = cartItem.number;
  491. let number = cartItem.number + 1;
  492. cartItem.number = number;
  493. if (goodsBizType == '00') {
  494. that.setData({
  495. cartGoods00: that.data.cartGoods00
  496. });
  497. }
  498. if (goodsBizType == '02') {
  499. that.setData({
  500. cartGoods02: that.data.cartGoods02
  501. });
  502. }
  503. if (goodsBizType == '10') {
  504. that.setData({
  505. cartGoods10: that.data.cartGoods10
  506. });
  507. }
  508. if (goodsBizType == '11') {
  509. that.setData({
  510. cartGoods11: that.data.cartGoods11
  511. });
  512. }
  513. that.updateCart(cartItem.product_id, cartItem.goods_id, number, beforeNumber, cartItem.id, itemIndex);
  514. },
  515. checkoutOrder: function() {
  516. //获取已选择的商品
  517. let that = this;
  518. util.request(api.getCurUser, {
  519. userInfo: app.globalData.userInfo
  520. }, 'POST').then(function(res) {
  521. if (res.errno === 0) {
  522. // console.log('that.data.mobile:' + res.data.mobile);
  523. if (res.data.mobile == '' || res.data.mobile == null) {
  524. wx.showModal({
  525. title: '',
  526. confirmColor: '#b4282d',
  527. showCancel: false,
  528. content: '您的手机号码未绑定,请先绑定手机号再进行购买',
  529. success: function(res) {
  530. if (res.confirm) {
  531. wx.navigateTo({
  532. url: '../../pages/auth/newuser/newuser'
  533. });
  534. }
  535. }
  536. });
  537. } else {
  538. var checkedGoods = that.data.cartGoods.filter(function(element, index, array) {
  539. if (element.checked == true) {
  540. return true;
  541. } else {
  542. return false;
  543. }
  544. });
  545. if (checkedGoods.length <= 0) {
  546. wx.showToast({
  547. title: '请选择要购买的商品',
  548. icon: 'none'
  549. })
  550. return false;
  551. }
  552. wx.navigateTo({
  553. url: '../shopping/checkout/checkout?checkCart=' + that.data.checkCart
  554. })
  555. }
  556. }
  557. });
  558. },
  559. deleteCart: function(event) {
  560. //获取已选择的商品
  561. let that = this;
  562. let cartId = event.target.dataset.cartId;
  563. let goodsName = event.target.dataset.goodsName;
  564. wx.showModal({
  565. title: '',
  566. content: '确定要删除' + goodsName + '?',
  567. success: function(res) {
  568. if (res.confirm) {
  569. util.request(api.CartDelete, {
  570. cartId: cartId,
  571. checkCart: that.data.checkCart
  572. }, 'POST').then(function(res) {
  573. if (res.errno === 0) {
  574. that.setCommonData(res);
  575. }
  576. that.setCheckedData();
  577. });
  578. console.log('用户点击确定')
  579. }
  580. }
  581. });
  582. },
  583. switchAttrPop: function() {
  584. this.setData({
  585. openAttr: !this.data.openAttr,
  586. openTicketAttr: false
  587. })
  588. console.log(this.data.openTicketAttr);
  589. },
  590. hideSwitchAttrPop: function () {
  591. this.setData({
  592. openAttr: false,
  593. openTicketAttr: false
  594. })
  595. },
  596. clickSkuValue: function(event) {
  597. let that = this;
  598. let specValueId = event.currentTarget.dataset.valueId;
  599. let index = event.currentTarget.dataset.index;
  600. let _specificationList = this.data.specificationList;
  601. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  602. if (_specificationList[index].valueList[j].id == specValueId) {
  603. //如果已经选中,则反选
  604. if (_specificationList[index].valueList[j].checked) {
  605. _specificationList[index].valueList[j].checked = false;
  606. } else {
  607. _specificationList[index].valueList[j].checked = true;
  608. }
  609. } else {
  610. _specificationList[index].valueList[j].checked = false;
  611. }
  612. }
  613. this.setData({
  614. 'specificationList': _specificationList
  615. });
  616. //重新计算spec改变后的信息
  617. goodsUtil.changeSpecInfo(that);
  618. },
  619. cutNumber2: function() {
  620. this.setData({
  621. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  622. });
  623. },
  624. addNumber2: function() {
  625. this.setData({
  626. number: this.data.number + 1
  627. });
  628. },
  629. //购物车增加
  630. addCart: function(e) {
  631. let that = this;
  632. that.setData({
  633. number: 1
  634. });
  635. var goodsId = e.currentTarget.dataset.goodsId;
  636. util.request(api.GoodsSku, {
  637. goodsId: goodsId
  638. }).then(function(res) {
  639. if (res.errno === 0 && null != res.data) {
  640. that.setData({
  641. goodsVo: res.data.goodsVo,
  642. specificationList: res.data.specificationList,
  643. productList: res.data.productList,
  644. openAttr: !that.data.openAttr,
  645. stockNum: res.data.stockNum,
  646. cartNumber: res.data.cartNumber,
  647. checkedSpecText: res.data.specificationList[0].valueList[0].value,
  648. openTicketAttr: false
  649. });
  650. //
  651. let _specificationList = res.data.specificationList;
  652. for (let i = 0; i < _specificationList.length; i++) {
  653. if (_specificationList[i].valueList.length == 1) {
  654. //如果已经选中,则反选
  655. _specificationList[i].valueList[0].checked = true;
  656. }
  657. }
  658. that.setData({
  659. 'specificationList': _specificationList
  660. });
  661. }
  662. });
  663. },
  664. //购物车增加
  665. addToCart: function() {
  666. let that = this;
  667. var goodsId = that.data.goodsVo.id;
  668. //提示选择完整规格
  669. if (!that.data.productList || !that.data.productList.length) {
  670. util.showErrorToast('当前门店没有库存');
  671. return false;
  672. }
  673. //提示选择完整规格
  674. if (!goodsUtil.isCheckedAllSpec(that)) {
  675. return false;
  676. }
  677. //根据选中的规格,判断是否有对应的sku信息
  678. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  679. if (!checkedProduct || checkedProduct.length <= 0) {
  680. //找不到对应的product信息,提示没有库存
  681. return false;
  682. }
  683. //验证库存
  684. if (checkedProduct.stock_num < this.data.number) {
  685. //找不到对应的product信息,提示没有库存
  686. return false;
  687. }
  688. util.request(api.CartAdd, {
  689. goodsId: goodsId,
  690. productId: checkedProduct[0].id,
  691. number: this.data.number,
  692. checkCart: that.data.checkCart
  693. }, 'POST').then(function(res) {
  694. if (res.errno === 0 && null != res.data) {
  695. wx.showToast({
  696. title: '添加成功',
  697. icon: 'success',
  698. mask: true
  699. });
  700. that.setData({
  701. openAttr: !that.data.openAttr,
  702. footprintList:[],
  703. page:1
  704. })
  705. // 页面显示
  706. that.getCartList();
  707. that.getFootprintList();
  708. } else {
  709. wx.showToast({
  710. title: res.errmsg,
  711. icon: 'none'
  712. });
  713. that.hideSwitchAttrPop();
  714. }
  715. });
  716. },
  717. deleteValidCart: function() { //获取购物车数据
  718. let that = this;
  719. wx.showModal({
  720. title: '',
  721. content: '确定要清空所有失效商品?',
  722. success: function(res) {
  723. if (res.confirm) {
  724. util.request(api.deleteValidCart, {
  725. checkCart: that.data.checkCart
  726. }).then(function(res) {
  727. if (res.errno === 0) {
  728. that.setCommonData(res);
  729. } else {
  730. util.showErrorToast(res.errmsg);
  731. }
  732. });
  733. }
  734. }
  735. });
  736. },
  737. getFootprintList() {
  738. let that = this;
  739. util.request(api.GuessFootprintList, {
  740. storeId: wx.getStorageSync('storeId'),
  741. checkCart: that.data.checkCart, page: that.data.page, size: that.data.size
  742. }).then(function (res) {
  743. if (res.errno === 0) {
  744. let goodsList = that.data.footprintList.concat(res.data.list);
  745. that.setData({
  746. footprintList: goodsList
  747. });
  748. wx.hideLoading();
  749. }
  750. });
  751. },
  752. onReachBottom() {
  753. var that = this;
  754. if(!that.data.footprintList){
  755. // wx.showLoading({
  756. // title: '加载中...',
  757. // })
  758. }
  759. that.setData({
  760. page: that.data.page + 1
  761. });
  762. that.getFootprintList();
  763. },
  764. switchTicketPop: function () {
  765. this.setData({
  766. openTicketAttr: !this.data.openTicketAttr,
  767. openAttr: false
  768. })
  769. this.getTicketDiscountList();
  770. },
  771. hideSwitchTicketPop: function () {
  772. this.setData({
  773. openTicketAttr: false,
  774. openAttr: false
  775. })
  776. },
  777. hideTicketPop: function () {
  778. this.setData({
  779. openTicketAttr: false
  780. })
  781. },
  782. /**
  783. * 领取优惠券
  784. */
  785. getUserCoupon(event) {
  786. var that = this;
  787. let tickDiscId = event.currentTarget.dataset.couponId;
  788. let storeTopicId = event.currentTarget.dataset.storeTopicId;
  789. let storeId = that.data.storeId;
  790. util.request(api.getUserCoupon, {
  791. tickDiscId: tickDiscId,
  792. storeTopicId: storeTopicId,
  793. storeId: storeId
  794. }, 'POST').then(function (res) {
  795. if (res.errno === 0) {
  796. wx.showToast({
  797. title: '领取成功'
  798. });
  799. that.setData({
  800. discStatus: 0
  801. });
  802. that.getTicketDiscountList();
  803. } else {
  804. wx.showToast({
  805. title: res.errmsg,
  806. icon: 'none'
  807. })
  808. }
  809. });
  810. },
  811. /**
  812. * 查看优惠券列表
  813. */
  814. getTicketDiscountList() {
  815. let that = this;
  816. util.request(api.TicketDiscountList, {
  817. storeId: wx.getStorageSync('storeId')
  818. }).then(function (res) {
  819. if (res.errno === 0) {
  820. that.setData({
  821. ticketDiscountList: res.data
  822. });
  823. }
  824. });
  825. },
  826. })