cart.js 11 KB

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