index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. const App = getApp();
  2. // 工具类
  3. import Util from '../../utils/util.js';
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. isLogin: false,
  10. // 商品列表
  11. goods_list: [],
  12. // 当前动作
  13. action: 'complete',
  14. // 选择的商品
  15. checkedData: [],
  16. // 是否全选
  17. checkedAll: false,
  18. // 商品总价格
  19. cartTotalPrice: '0.00'
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad(options) {
  25. },
  26. /**
  27. * 生命周期函数--监听页面显示
  28. */
  29. onShow() {
  30. let _this = this;
  31. _this.setData({
  32. isLogin: App.checkIsLogin()
  33. });
  34. if (_this.data.isLogin) {
  35. // 获取购物车列表
  36. _this.getCartList();
  37. }
  38. },
  39. /**
  40. * 获取购物车列表
  41. */
  42. getCartList() {
  43. let _this = this;
  44. App._get('cart/lists', {}, result => {
  45. const data = result.data
  46. // 更新购物车数量及角标
  47. App.setCartTotalNum(data.order_total_num)
  48. App.setCartTabBadge()
  49. // 初始化商品选中状态
  50. _this._initGoodsChecked(data)
  51. });
  52. },
  53. /**
  54. * 初始化商品选中状态
  55. */
  56. _initGoodsChecked(data) {
  57. let _this = this;
  58. let checkedData = _this.getCheckedData();
  59. // 将商品设置选中
  60. data.goods_list.forEach(item => {
  61. item.checked = Util.inArray(`${item.goods_id}_${item.goods_sku_id}`, checkedData);
  62. });
  63. _this.setData({
  64. goods_list: data.goods_list,
  65. order_total_price: data.order_total_price,
  66. action: 'complete',
  67. checkedAll: checkedData.length == data.goods_list.length,
  68. });
  69. // 更新购物车已选商品总价格
  70. _this.updateTotalPrice();
  71. },
  72. /**
  73. * 选择框选中
  74. */
  75. onChecked(e) {
  76. let _this = this,
  77. index = e.currentTarget.dataset.index,
  78. goods = _this.data.goods_list[index],
  79. checked = !goods.checked;
  80. // 选中状态
  81. _this.setData({
  82. ['goods_list[' + index + '].checked']: checked
  83. });
  84. // 更新选中记录
  85. _this.onUpdateChecked();
  86. // 更新购物车已选商品总价格
  87. _this.updateTotalPrice();
  88. // 更新全选状态
  89. _this.setData({
  90. checkedAll: _this.getCheckedData().length == _this.data.goods_list.length
  91. });
  92. },
  93. /**
  94. * 获取选中的商品
  95. */
  96. getCheckedData() {
  97. let checkedData = wx.getStorageSync('checkedData');
  98. return checkedData ? checkedData : [];
  99. },
  100. /**
  101. * 选择框全选
  102. */
  103. onCheckedAll(e) {
  104. let _this = this,
  105. goodsList = _this.data.goods_list;
  106. // 更新商品选中记录
  107. goodsList.forEach(item => {
  108. item.checked = !_this.data.checkedAll;
  109. });
  110. _this.setData({
  111. goods_list: goodsList,
  112. checkedAll: !_this.data.checkedAll
  113. });
  114. // 更新购物车已选商品总价格
  115. _this.updateTotalPrice();
  116. // 更新选中记录
  117. _this.onUpdateChecked();
  118. },
  119. /**
  120. * 更新商品选中记录
  121. */
  122. onUpdateChecked() {
  123. let _this = this,
  124. checkedData = [];
  125. _this.data.goods_list.forEach(item => {
  126. if (item.checked == true) {
  127. checkedData.push(`${item.goods_id}_${item.goods_sku_id}`);
  128. }
  129. });
  130. wx.setStorageSync('checkedData', checkedData);
  131. },
  132. /**
  133. * 切换编辑/完成
  134. */
  135. switchAction(e) {
  136. let _this = this;
  137. _this.setData({
  138. action: e.currentTarget.dataset.action
  139. });
  140. },
  141. /**
  142. * 删除商品
  143. */
  144. onDelete() {
  145. let _this = this,
  146. cartIds = _this.getCheckedIds();
  147. if (!cartIds.length) {
  148. App.showError('您还没有选择商品');
  149. return false;
  150. }
  151. console.log(cartIds);
  152. wx.showModal({
  153. title: "提示",
  154. content: "您确定要移除选择的商品吗?",
  155. success(e) {
  156. e.confirm && App._post_form('cart/delete', {
  157. goods_sku_id: cartIds
  158. }, result => {
  159. // 删除选中的商品
  160. _this.onDeleteEvent(cartIds);
  161. // 获取购物车列表
  162. _this.getCartList();
  163. });
  164. }
  165. });
  166. },
  167. /**
  168. * 商品删除事件
  169. */
  170. onDeleteEvent(cartIds) {
  171. let _this = this;
  172. cartIds.forEach((cartIndex) => {
  173. _this.data.goods_list.forEach((item, goodsIndex) => {
  174. if (cartIndex == `${item.goods_id}_${item.goods_sku_id}`) {
  175. _this.data.goods_list.splice(goodsIndex, 1);
  176. }
  177. });
  178. });
  179. // 更新选中记录
  180. _this.onUpdateChecked();
  181. return true;
  182. },
  183. /**
  184. * 获取已选中的商品
  185. */
  186. getCheckedIds() {
  187. let _this = this;
  188. let arrIds = [];
  189. _this.data.goods_list.forEach(item => {
  190. if (item.checked === true) {
  191. arrIds.push(`${item.goods_id}_${item.goods_sku_id}`);
  192. }
  193. });
  194. return arrIds;
  195. },
  196. /**
  197. * 更新购物车已选商品总价格
  198. */
  199. updateTotalPrice() {
  200. let _this = this;
  201. let cartTotalPrice = 0;
  202. _this.data.goods_list.forEach(item => {
  203. if (item.checked === true) {
  204. cartTotalPrice = _this.mathadd(cartTotalPrice, item.total_price);
  205. }
  206. });
  207. _this.setData({
  208. cartTotalPrice: Number(cartTotalPrice).toFixed(2)
  209. });
  210. },
  211. /**
  212. * 递增指定的商品数量
  213. */
  214. onAddCount(e) {
  215. let _this = this,
  216. index = e.currentTarget.dataset.index,
  217. goodsSkuId = e.currentTarget.dataset.skuId,
  218. goods = _this.data.goods_list[index];
  219. // order_total_price = _this.data.order_total_price;
  220. // 后端同步更新
  221. wx.showLoading({
  222. title: '加载中',
  223. mask: true
  224. });
  225. App._post_form('cart/add', {
  226. goods_id: goods.goods_id,
  227. goods_num: 1,
  228. goods_sku_id: goodsSkuId
  229. }, () => {
  230. // 获取购物车列表
  231. _this.getCartList();
  232. }, null, () => {
  233. wx.hideLoading();
  234. });
  235. },
  236. /**
  237. * 递减指定的商品数量
  238. */
  239. onSubCount(e) {
  240. let _this = this,
  241. index = e.currentTarget.dataset.index,
  242. goodsSkuId = e.currentTarget.dataset.skuId,
  243. goods = _this.data.goods_list[index];
  244. if (goods.total_num > 1) {
  245. // 后端同步更新
  246. wx.showLoading({
  247. title: '加载中',
  248. mask: true
  249. })
  250. App._post_form('cart/sub', {
  251. goods_id: goods.goods_id,
  252. goods_sku_id: goodsSkuId
  253. }, () => {
  254. // 获取购物车列表
  255. _this.getCartList();
  256. }, null, () => {
  257. wx.hideLoading();
  258. });
  259. }
  260. },
  261. /**
  262. * 购物车结算
  263. */
  264. submit() {
  265. let _this = this,
  266. cartIds = _this.getCheckedIds();
  267. if (!cartIds.length) {
  268. App.showError('您还没有选择商品');
  269. return false;
  270. }
  271. wx.navigateTo({
  272. url: '../flow/checkout?order_type=cart&cart_ids=' + cartIds
  273. });
  274. },
  275. /**
  276. * 加法
  277. */
  278. mathadd(arg1, arg2) {
  279. return (Number(arg1) + Number(arg2)).toFixed(2);
  280. },
  281. /**
  282. * 减法
  283. */
  284. mathsub(arg1, arg2) {
  285. return (Number(arg1) - Number(arg2)).toFixed(2);
  286. },
  287. /**
  288. * 去购物
  289. */
  290. goShopping() {
  291. wx.switchTab({
  292. url: '../index/index',
  293. });
  294. },
  295. })