cart.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. var util = require('../../../utils/util.js');
  2. var mall = require('../../../api/mall.js');
  3. var user = require('../../../utils/user.js');
  4. var app = getApp();
  5. Page({
  6. data: {
  7. cartGoods: [],
  8. cartTotal: {
  9. "goodsCount": 0,
  10. "goodsAmount": 0.00,
  11. "checkedGoodsCount": 0,
  12. "checkedGoodsAmount": 0.00
  13. },
  14. isEditCart: false,
  15. checkedAllStatus: true,
  16. editCartList: [],
  17. hasLogin: false
  18. },
  19. onLoad: function (options) {
  20. // 页面初始化 options为页面跳转所带来的参数
  21. },
  22. onReady: function () {
  23. // 页面渲染完成
  24. },
  25. onPullDownRefresh() {
  26. wx.showNavigationBarLoading() //在标题栏中显示加载
  27. this.getCartList();
  28. wx.hideNavigationBarLoading() //完成停止加载
  29. wx.stopPullDownRefresh() //停止下拉刷新
  30. },
  31. goIndex: function () {
  32. wx.switchTab({
  33. url: '/pages/index/index',
  34. })
  35. },
  36. onShow: function () {
  37. console.info(app.globalData.hasLogin)
  38. let that = this;
  39. that.getCartList();
  40. // if (app.globalData.hasLogin) {
  41. // that.setData({
  42. // hasLogin: app.globalData.hasLogin
  43. // });
  44. // that.getCartList();
  45. // } else {
  46. // app.checkLoginReadyCallback = res => {
  47. // this.setData({
  48. // hasLogin: true
  49. // });
  50. // that.getCartList();
  51. // }
  52. // }
  53. },
  54. onHide: function () {
  55. // 页面隐藏
  56. },
  57. onUnload: function () {
  58. // 页面关闭
  59. },
  60. getPhoneNumber: function (e) {
  61. let that = this;
  62. var ivObj = e.detail.iv
  63. var telObj = e.detail.encryptedData
  64. let r_userid = wx.getStorageSync('r_userid');
  65. if (r_userid == null || r_userid == "") r_userid = 0;
  66. //------执行Login---------
  67. wx.login({
  68. success: res => {
  69. util.request(mall.AuthRegister, {
  70. code: res.code,
  71. encryptedData: telObj,
  72. iv: ivObj,
  73. r_userid: r_userid
  74. }).then(function (res) {
  75. if (res.errno === 0) {
  76. that.setData({
  77. hasLogin: true
  78. });
  79. //存储用户信息
  80. app.globalData.hasLogin = true;
  81. wx.setStorageSync('userInfo', res.data.userInfo);
  82. wx.setStorageSync('token', res.data.token);
  83. wx.showToast({
  84. title: '授权注册成功',
  85. icon: 'success',
  86. duration: 2000
  87. })
  88. }
  89. });
  90. }
  91. });
  92. //-----------------是否授权,授权通过进入主页面,授权拒绝则停留在登陆界面
  93. if (e.detail.errMsg == 'getPhoneNumber:fail user deny') { //用户点击拒绝
  94. wx.showModal({
  95. title: '警告',
  96. content: '您点击了拒绝授权,部分功能无法使用!!!',
  97. showCancel: false,
  98. confirmText: '确定',
  99. success: function (res) {
  100. // 用户没有授权成功,不需要改变 isHide 的值
  101. if (res.confirm) {
  102. wx.switchTab({
  103. url: '/pages/index/index',
  104. })
  105. };
  106. }
  107. })
  108. }
  109. },
  110. getCartList: function () {
  111. let that = this;
  112. util.request(mall.CartList).then(function (res) {
  113. if (res.errno === 0) {
  114. that.setData({
  115. cartGoods: res.data.cartList,
  116. cartTotal: res.data.cartTotal
  117. });
  118. that.setData({
  119. checkedAllStatus: that.isCheckedAll()
  120. });
  121. //显示购物车角标
  122. if (res.data.cartTotal != null) {
  123. wx.setStorageSync('cartcount', res.data.cartTotal.goodsCount);
  124. // user.cartshow();
  125. }
  126. }
  127. });
  128. },
  129. isCheckedAll: function () {
  130. //判断购物车商品已全选
  131. return this.data.cartGoods.every(function (element, index, array) {
  132. if (element.checked == true) {
  133. return true;
  134. } else {
  135. return false;
  136. }
  137. });
  138. },
  139. doCheckedAll: function () {
  140. let checkedAll = this.isCheckedAll()
  141. this.setData({
  142. checkedAllStatus: this.isCheckedAll()
  143. });
  144. },
  145. checkedItem: function (event) {
  146. let itemIndex = event.target.dataset.itemIndex;
  147. let that = this;
  148. let productIds = [];
  149. productIds.push(that.data.cartGoods[itemIndex].commoditySkuId);
  150. if (!this.data.isEditCart) {
  151. util.request(mall.CartChecked, {
  152. productIds: productIds,
  153. isChecked: that.data.cartGoods[itemIndex].checked ? 0 : 1
  154. }, 'POST').then(function (res) {
  155. if (res.errno === 0) {
  156. that.setData({
  157. cartGoods: res.data.cartList,
  158. cartTotal: res.data.cartTotal
  159. });
  160. }
  161. that.setData({
  162. checkedAllStatus: that.isCheckedAll()
  163. });
  164. });
  165. } else {
  166. //编辑状态
  167. let tmpCartData = this.data.cartGoods.map(function (element, index, array) {
  168. if (index == itemIndex) {
  169. element.checked = !element.checked;
  170. }
  171. return element;
  172. });
  173. that.setData({
  174. cartGoods: tmpCartData,
  175. checkedAllStatus: that.isCheckedAll(),
  176. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  177. });
  178. }
  179. },
  180. getCheckedGoodsCount: function () {
  181. let checkedGoodsCount = 0;
  182. this.data.cartGoods.forEach(function (v) {
  183. if (v.checked === true) {
  184. checkedGoodsCount += v.number;
  185. }
  186. });
  187. console.log(checkedGoodsCount);
  188. return checkedGoodsCount;
  189. },
  190. getCheckedGoodsAmount: function () {
  191. let checkedGoodsAmount = 0;
  192. this.data.cartGoods.forEach(function (v) {
  193. if (v.checked === true) {
  194. checkedGoodsAmount += v.number * v.price;
  195. }
  196. });
  197. return checkedGoodsAmount.toFixed(2);
  198. },
  199. checkedAll: function () {
  200. let that = this;
  201. if (!this.data.isEditCart) {
  202. var productIds = this.data.cartGoods.map(function (v) {
  203. return v.commoditySkuId;
  204. });
  205. util.request(mall.CartChecked, {
  206. productIds: productIds,
  207. isChecked: that.isCheckedAll() ? 0 : 1
  208. }, 'POST').then(function (res) {
  209. if (res.errno === 0) {
  210. console.log(res.data);
  211. that.setData({
  212. cartGoods: res.data.cartList,
  213. cartTotal: res.data.cartTotal
  214. });
  215. }
  216. that.setData({
  217. checkedAllStatus: that.isCheckedAll()
  218. });
  219. });
  220. } else {
  221. //编辑状态
  222. let checkedAllStatus = that.isCheckedAll();
  223. let tmpCartData = this.data.cartGoods.map(function (v) {
  224. v.checked = !checkedAllStatus;
  225. return v;
  226. });
  227. that.setData({
  228. cartGoods: tmpCartData,
  229. checkedAllStatus: that.isCheckedAll(),
  230. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  231. });
  232. }
  233. },
  234. editCart: function () {
  235. var that = this;
  236. if (this.data.isEditCart) {
  237. this.getCartList();
  238. this.setData({
  239. isEditCart: !this.data.isEditCart
  240. });
  241. } else {
  242. //编辑状态
  243. let tmpCartList = this.data.cartGoods.map(function (v) {
  244. v.checked = false;
  245. return v;
  246. });
  247. this.setData({
  248. editCartList: this.data.cartGoods,
  249. cartGoods: tmpCartList,
  250. isEditCart: !this.data.isEditCart,
  251. checkedAllStatus: that.isCheckedAll(),
  252. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  253. });
  254. }
  255. },
  256. updateCart: function (cartItem,index,number,count) {
  257. let that = this;
  258. util.request(mall.CartUpdate, {
  259. id:cartItem.id,
  260. commodityId:cartItem.commodityId,
  261. commoditySkuId:cartItem.commoditySkuId,
  262. number:number
  263. }, 'POST').then(res=> {
  264. console.log(res);
  265. if(res.errno==0){
  266. // cartItem.number = number;
  267. let cartItem = "cartGoods[" + index + "].number";
  268. this.setData({
  269. [cartItem]:number,
  270. });
  271. console.log(this.cartGoods);
  272. this.setData({
  273. 'cartTotal.checkedGoodsAmount': that.getCheckedGoodsAmount(),
  274. 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  275. });
  276. user.cartadd(count);
  277. that.setData({
  278. checkedAllStatus: that.isCheckedAll()
  279. });
  280. }else{
  281. util.showErrorToast(res.errmsg);
  282. }
  283. }).catch(err=>{
  284. console.log(err);
  285. util.showErrorToast(err.errno);
  286. });
  287. },
  288. cutNumber: function (event) {
  289. let that = this;
  290. let itemIndex = event.target.dataset.itemIndex;
  291. let cartItem = this.data.cartGoods[itemIndex];
  292. //删除操作
  293. if (cartItem.number == 1) {
  294. wx.showModal({
  295. title: "提醒",
  296. content: "确定从购物车删除?",
  297. success(res) {
  298. if (res.confirm)
  299. that.deleteCartGoods(cartItem);
  300. }
  301. })
  302. } else {
  303. let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
  304. // cartItem.number = number;
  305. // this.setData({
  306. // cartGoods: this.data.cartGoods,
  307. // 'cartTotal.checkedGoodsAmount': that.getCheckedGoodsAmount(),
  308. // 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  309. // });
  310. this.updateCart(cartItem,itemIndex,number,-1);
  311. //显示tab购物车
  312. // user.cartadd(-1);
  313. }
  314. },
  315. addNumber: function (event) {
  316. let that = this;
  317. let itemIndex = event.target.dataset.itemIndex;
  318. let cartItem = this.data.cartGoods[itemIndex];
  319. console.log(cartItem);
  320. if(cartItem.goodsSn=='1036016'){
  321. wx.showToast({
  322. title: '此款商品只能购买一个',
  323. icon: 'none',
  324. duration: 2000
  325. });
  326. var number=1;
  327. }else{
  328. var number = cartItem.number + 1;
  329. //显示tab购物车
  330. // user.cartadd(1);
  331. }
  332. // cartItem.number = number;
  333. // this.setData({
  334. // cartGoods: this.data.cartGoods,
  335. // 'cartTotal.checkedGoodsAmount': that.getCheckedGoodsAmount(),
  336. // 'cartTotal.checkedGoodsCount': that.getCheckedGoodsCount()
  337. // });
  338. this.updateCart(cartItem,itemIndex,number,1);
  339. },
  340. checkoutOrder: function () { //下单
  341. //获取已选择的商品
  342. let that = this;
  343. var checkedGoods = this.data.cartGoods.filter(function (element, index, array) {
  344. if (element.checked == true) {
  345. return true;
  346. } else {
  347. return false;
  348. }
  349. });
  350. if (checkedGoods.length <= 0) {
  351. util.showErrorToast('请先选择商品')
  352. return false;
  353. }
  354. // storage中设置了cartId,则是购物车购买
  355. try {
  356. wx.setStorageSync('cartId', 0);
  357. wx.navigateTo({
  358. url: '/pages/mall/checkout/checkout'
  359. })
  360. } catch (e) {}
  361. },
  362. deleteCart: function () {
  363. //获取已选择的商品
  364. let that = this;
  365. let productIds = this.data.cartGoods.filter(function (element, index, array) {
  366. if (element.checked == true) {
  367. return true;
  368. } else {
  369. return false;
  370. }
  371. });
  372. if (productIds.length <= 0) {
  373. return false;
  374. }
  375. productIds = productIds.map(function (element, index, array) {
  376. if (element.checked == true) {
  377. return element.productId;
  378. }
  379. });
  380. let arr=[];
  381. arr.push(productIds);
  382. console.log(arr);
  383. util.request(mall.CartDelete, {
  384. productIds: arr
  385. }, 'POST').then(function (res) {
  386. console.log(res);
  387. if (res.errno === 0) {
  388. let cartList = res.data.cartList.map(v => {
  389. v.checked = false;
  390. return v;
  391. });
  392. that.setData({
  393. cartGoods: cartList,
  394. cartTotal: res.data.cartTotal
  395. });
  396. }
  397. that.setData({
  398. checkedAllStatus: that.isCheckedAll()
  399. });
  400. });
  401. },
  402. deleteCartGoods: function (cartItem) {
  403. //获取已选择的商品
  404. let that = this;
  405. console.log(cartItem);
  406. util.request(mall.CartDelete, {
  407. productIds:[cartItem.commoditySkuId],
  408. }, 'POST').then(function (res) {
  409. console.log(res.data);
  410. if (res.errno === 0) {
  411. let cartList = res.data.cartList.map(v => {
  412. v.checked = false;
  413. return v;
  414. });
  415. that.setData({
  416. cartGoods: cartList,
  417. cartTotal: res.data.cartTotal
  418. });
  419. user.cartadd(-1);
  420. user.cartshow();
  421. }
  422. that.setData({
  423. checkedAllStatus: that.isCheckedAll()
  424. });
  425. });
  426. }
  427. })