cart.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  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. let that = this;
  517. // console.log(that.data.cartGoods10);
  518. var count = 0;
  519. for (var i = 0; i < that.data.cartGoods10.length; i++){
  520. if (that.data.cartGoods10[i].checked==1){
  521. count = count +1;
  522. }
  523. }
  524. // console.log(count);
  525. if (count > 0) {
  526. wx.requestSubscribeMessage({
  527. tmplIds: ['lFTyxlkg1pkRbJVMn-lDhdhsjK5pZqq_8ZTe9Qx_PUA'],
  528. success(res) {
  529. console.log(res);
  530. that.checkOrder();
  531. },
  532. fail(res) {
  533. console.log(res);
  534. that.checkOrder();
  535. }
  536. });
  537. } else {
  538. that.checkOrder();
  539. }
  540. },
  541. checkOrder: function(){
  542. //获取已选择的商品
  543. let that = this;
  544. util.request(api.getCurUser, {
  545. userInfo: app.globalData.userInfo
  546. }, 'POST').then(function (res) {
  547. if (res.errno === 0) {
  548. // console.log('that.data.mobile:' + res.data.mobile);
  549. if (res.data.mobile == '' || res.data.mobile == null) {
  550. wx.showModal({
  551. title: '',
  552. confirmColor: '#b4282d',
  553. showCancel: false,
  554. content: '您的手机号码未绑定,请先绑定手机号再进行购买',
  555. success: function (res) {
  556. if (res.confirm) {
  557. wx.navigateTo({
  558. url: '../../pages/auth/newuser/newuser'
  559. });
  560. }
  561. }
  562. });
  563. } else {
  564. var checkedGoods = that.data.cartGoods.filter(function (element, index, array) {
  565. if (element.checked == true) {
  566. return true;
  567. } else {
  568. return false;
  569. }
  570. });
  571. if (checkedGoods.length <= 0) {
  572. wx.showToast({
  573. title: '请选择要购买的商品',
  574. icon: 'none'
  575. })
  576. return false;
  577. }
  578. wx.navigateTo({
  579. url: '../shopping/checkout/checkout?checkCart=' + that.data.checkCart
  580. })
  581. /**var userInfo = res.data;
  582. checkedGoods.filter(function (element, index, array) {
  583. /**
  584. * 校验购物车中有现场速递的单,
  585. * 且根据用户id判断上次活体人脸核身时间是否在可调用范围内,
  586. * 可以则需人脸核身,否则直接进入确认下单页
  587. * 暂时不开发
  588. **/
  589. /**if (element.goodsBizType == 10) {
  590. util.request(api.CheckFaceTimeByUserId, {
  591. userId: userInfo.id
  592. }, 'POST').then(function (checkRes) {
  593. console.log(checkRes)
  594. if (checkRes.errno === 0) {
  595. if (checkRes.data === true) {
  596. wx.navigateTo({
  597. url: '../checkoutIdCard/checkoutIdCard'
  598. });
  599. return false;
  600. } else {
  601. wx.navigateTo({
  602. url: '../shopping/checkout/checkout?checkCart=' + that.data.checkCart
  603. })
  604. }
  605. }
  606. });
  607. } else {
  608. wx.navigateTo({
  609. url: '../shopping/checkout/checkout?checkCart=' + that.data.checkCart
  610. })
  611. }
  612. });**/
  613. }
  614. }
  615. });
  616. },
  617. //根据用户id判断上次活体人脸核身时间是否在可调用范围内
  618. checkFaceTimeByUserId(userId){
  619. util.request(api.CheckFaceTimeByUserId, {
  620. userId: userId
  621. }, 'POST').then(function (checkRes) {
  622. if (checkRes.errno === 0) {
  623. if (checkRes.data === true){
  624. return true;
  625. }else{
  626. return false;
  627. }
  628. }
  629. })
  630. },
  631. deleteCart: function(event) {
  632. //获取已选择的商品
  633. let that = this;
  634. let cartId = event.target.dataset.cartId;
  635. let goodsName = event.target.dataset.goodsName;
  636. wx.showModal({
  637. title: '',
  638. content: '确定要删除' + goodsName + '?',
  639. success: function(res) {
  640. if (res.confirm) {
  641. util.request(api.CartDelete, {
  642. cartId: cartId,
  643. checkCart: that.data.checkCart
  644. }, 'POST').then(function(res) {
  645. if (res.errno === 0) {
  646. that.setCommonData(res);
  647. }
  648. that.setCheckedData();
  649. });
  650. console.log('用户点击确定')
  651. }
  652. }
  653. });
  654. },
  655. switchAttrPop: function() {
  656. this.setData({
  657. openAttr: !this.data.openAttr,
  658. openTicketAttr: false
  659. })
  660. console.log(this.data.openTicketAttr);
  661. },
  662. hideSwitchAttrPop: function () {
  663. this.setData({
  664. openAttr: false,
  665. openTicketAttr: false
  666. })
  667. },
  668. clickSkuValue: function(event) {
  669. let that = this;
  670. let specValueId = event.currentTarget.dataset.valueId;
  671. let index = event.currentTarget.dataset.index;
  672. let _specificationList = this.data.specificationList;
  673. for (let j = 0; j < _specificationList[index].valueList.length; j++) {
  674. if (_specificationList[index].valueList[j].id == specValueId) {
  675. //如果已经选中,则反选
  676. if (_specificationList[index].valueList[j].checked) {
  677. _specificationList[index].valueList[j].checked = false;
  678. } else {
  679. _specificationList[index].valueList[j].checked = true;
  680. }
  681. } else {
  682. _specificationList[index].valueList[j].checked = false;
  683. }
  684. }
  685. this.setData({
  686. 'specificationList': _specificationList
  687. });
  688. //重新计算spec改变后的信息
  689. goodsUtil.changeSpecInfo(that);
  690. },
  691. cutNumber2: function() {
  692. this.setData({
  693. number: (this.data.number - 1 > 1) ? this.data.number - 1 : 1
  694. });
  695. },
  696. addNumber2: function() {
  697. this.setData({
  698. number: this.data.number + 1
  699. });
  700. },
  701. //购物车增加
  702. addCart: function(e) {
  703. let that = this;
  704. that.setData({
  705. number: 1
  706. });
  707. var goodsId = e.currentTarget.dataset.goodsId;
  708. util.request(api.GoodsSku, {
  709. goodsId: goodsId
  710. }).then(function(res) {
  711. if (res.errno === 0 && null != res.data) {
  712. that.setData({
  713. goodsVo: res.data.goodsVo,
  714. specificationList: res.data.specificationList,
  715. productList: res.data.productList,
  716. openAttr: !that.data.openAttr,
  717. stockNum: res.data.stockNum,
  718. cartNumber: res.data.cartNumber,
  719. checkedSpecText: res.data.specificationList[0].valueList[0].value,
  720. openTicketAttr: false
  721. });
  722. //
  723. let _specificationList = res.data.specificationList;
  724. for (let i = 0; i < _specificationList.length; i++) {
  725. if (_specificationList[i].valueList.length == 1) {
  726. //如果已经选中,则反选
  727. _specificationList[i].valueList[0].checked = true;
  728. }
  729. }
  730. that.setData({
  731. 'specificationList': _specificationList
  732. });
  733. }
  734. });
  735. },
  736. //购物车增加
  737. addToCart: function() {
  738. let that = this;
  739. var goodsId = that.data.goodsVo.id;
  740. //提示选择完整规格
  741. if (!that.data.productList || !that.data.productList.length) {
  742. util.showErrorToast('当前门店没有库存');
  743. return false;
  744. }
  745. //提示选择完整规格
  746. if (!goodsUtil.isCheckedAllSpec(that)) {
  747. return false;
  748. }
  749. //根据选中的规格,判断是否有对应的sku信息
  750. let checkedProduct = goodsUtil.getCheckedProductItem(goodsUtil.getCheckedSpecKey(that), that);
  751. if (!checkedProduct || checkedProduct.length <= 0) {
  752. //找不到对应的product信息,提示没有库存
  753. return false;
  754. }
  755. //验证库存
  756. if (checkedProduct.stock_num < this.data.number) {
  757. //找不到对应的product信息,提示没有库存
  758. return false;
  759. }
  760. util.request(api.CartAdd, {
  761. goodsId: goodsId,
  762. productId: checkedProduct[0].id,
  763. number: this.data.number,
  764. checkCart: that.data.checkCart
  765. }, 'POST').then(function(res) {
  766. if (res.errno === 0 && null != res.data) {
  767. wx.showToast({
  768. title: '添加成功',
  769. icon: 'success',
  770. mask: true
  771. });
  772. that.setData({
  773. openAttr: !that.data.openAttr,
  774. footprintList:[],
  775. page:1
  776. })
  777. // 页面显示
  778. that.getCartList();
  779. that.getFootprintList();
  780. } else {
  781. wx.showToast({
  782. title: res.errmsg,
  783. icon: 'none'
  784. });
  785. that.hideSwitchAttrPop();
  786. }
  787. });
  788. },
  789. deleteValidCart: function() { //获取购物车数据
  790. let that = this;
  791. wx.showModal({
  792. title: '',
  793. content: '确定要清空所有失效商品?',
  794. success: function(res) {
  795. if (res.confirm) {
  796. util.request(api.deleteValidCart, {
  797. checkCart: that.data.checkCart
  798. }).then(function(res) {
  799. if (res.errno === 0) {
  800. that.setCommonData(res);
  801. } else {
  802. util.showErrorToast(res.errmsg);
  803. }
  804. });
  805. }
  806. }
  807. });
  808. },
  809. getFootprintList() {
  810. let that = this;
  811. util.request(api.GuessFootprintList, {
  812. storeId: wx.getStorageSync('storeId'),
  813. checkCart: that.data.checkCart, page: that.data.page, size: that.data.size
  814. }).then(function (res) {
  815. if (res.errno === 0) {
  816. let goodsList = that.data.footprintList.concat(res.data.list);
  817. that.setData({
  818. footprintList: goodsList
  819. });
  820. wx.hideLoading();
  821. }
  822. });
  823. },
  824. onReachBottom() {
  825. var that = this;
  826. if(!that.data.footprintList){
  827. // wx.showLoading({
  828. // title: '加载中...',
  829. // })
  830. }
  831. that.setData({
  832. page: that.data.page + 1
  833. });
  834. that.getFootprintList();
  835. },
  836. switchTicketPop: function () {
  837. this.setData({
  838. openTicketAttr: !this.data.openTicketAttr,
  839. openAttr: false
  840. })
  841. this.getTicketDiscountList();
  842. },
  843. hideSwitchTicketPop: function () {
  844. this.setData({
  845. openTicketAttr: false,
  846. openAttr: false
  847. })
  848. },
  849. hideTicketPop: function () {
  850. this.setData({
  851. openTicketAttr: false
  852. })
  853. },
  854. /**
  855. * 领取优惠券
  856. */
  857. getUserCoupon(event) {
  858. var that = this;
  859. let tickDiscId = event.currentTarget.dataset.couponId;
  860. let storeTopicId = event.currentTarget.dataset.storeTopicId;
  861. let storeId = that.data.storeId;
  862. util.request(api.getUserCoupon, {
  863. tickDiscId: tickDiscId,
  864. storeTopicId: storeTopicId,
  865. storeId: storeId
  866. }, 'POST').then(function (res) {
  867. if (res.errno === 0) {
  868. wx.showToast({
  869. title: '领取成功'
  870. });
  871. that.setData({
  872. discStatus: 0
  873. });
  874. that.getTicketDiscountList();
  875. } else {
  876. wx.showToast({
  877. title: res.errmsg,
  878. icon: 'none'
  879. })
  880. }
  881. });
  882. },
  883. /**
  884. * 查看优惠券列表
  885. */
  886. getTicketDiscountList() {
  887. let that = this;
  888. util.request(api.TicketDiscountList, {
  889. storeId: wx.getStorageSync('storeId')
  890. }).then(function (res) {
  891. if (res.errno === 0) {
  892. that.setData({
  893. ticketDiscountList: res.data
  894. });
  895. }
  896. });
  897. },
  898. })