item.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. const app = getApp()
  2. var WxParse = require('../../wxParse/wxParse.js');
  3. Page({
  4. data: {
  5. disabled: true,
  6. bottom: false,
  7. item_id: 0,
  8. items: [],
  9. shopping_num: 0, //购物车中的商品数量
  10. buy_num: 1, //购买单个物品数量
  11. },
  12. /**
  13. * 生命周期函数--监听页面显示
  14. */
  15. onLoad: function (options) {
  16. app.setUcode(options); //读取邀请码
  17. if (options.scene) {
  18. var scene = app.util().strToArray(decodeURIComponent(options.scene));
  19. var item_id = parseInt(scene.id);
  20. } else {
  21. var item_id = parseInt(options.id);
  22. }
  23. //获取当前页面点击ID
  24. this.setData({
  25. item_id: item_id
  26. });
  27. },
  28. /**
  29. * 生命周期函数--监听页面显示
  30. */
  31. onShow: function () {
  32. this.getItem();
  33. var shopping_num = wx.getStorageSync('shopping_num') || 0;
  34. this.setData({
  35. shopping_num: shopping_num
  36. });
  37. },
  38. /**
  39. * 获取商品信息
  40. */
  41. getItem: function (e) {
  42. let that = this;
  43. var item_id = that.data.item_id;
  44. app.api().Get('api/v3/fastshop/shop/item', {'id': item_id},function (result) {
  45. if (result.data.types == 1 && result.data.state == 1) {
  46. that.setData({
  47. disabled: false,
  48. });
  49. }
  50. that.setData({
  51. items: result.data,
  52. });
  53. WxParse.wxParse('content', 'html', result.data.content, that, 0);
  54. })
  55. },
  56. //加入购物车
  57. add_cart: function () {
  58. let that = this;
  59. var param = {
  60. buy_num:that.data.buy_num,
  61. item_id:that.data.item_id
  62. }
  63. app.api().Post('api/v3/fastshop/cart/edit',param,function (result) {
  64. let shopping_cart = wx.getStorageSync('shopping_cart') || [];
  65. var cart = new Array();
  66. if (app.util().isNull(shopping_cart)) {
  67. cart = result.data;
  68. } else {
  69. for (let sku in result.data) {
  70. shopping_cart[sku] = result.data[sku];
  71. }
  72. cart = shopping_cart;
  73. }
  74. //循环计算数组的和
  75. var shopping_num = 0;
  76. for (let i in cart) {
  77. shopping_num += cart[i];
  78. }
  79. wx.setStorageSync('shopping_cart',cart);
  80. wx.setStorageSync('shopping_num',shopping_num);
  81. that.setData({
  82. shopping_num:shopping_num
  83. })
  84. app.globalData.cartItemNumber = shopping_num;
  85. })
  86. that.setData({
  87. bottom:false
  88. });
  89. },
  90. //立即购买
  91. buy_now: function () {
  92. this.add_cart();
  93. setTimeout(function () {
  94. wx.switchTab({url: 'cart' })
  95. }, 1000);
  96. },
  97. //购物车层
  98. toggleBottomPopup: function () {
  99. this.setData({
  100. bottom: !this.data.bottom
  101. });
  102. },
  103. //编辑购物数量
  104. buyNumber: function (e) {
  105. this.setData({
  106. buy_num: e.detail
  107. })
  108. },
  109. /**
  110. * 分享按钮
  111. */
  112. onShareAppMessage: function (res) {
  113. let that = this;
  114. var item_id = that.data.item_id, items = that.data.items, ucode = app.globalData.loginuser.ucode;
  115. return {
  116. title: '[¥' + items.item.sell_price + ']' + items.item.name,
  117. desc: items.item.title,
  118. path: '/pages/index/item?id=' + item_id + '&ucode=' + ucode,
  119. imageUrl: that.data.url + items.item.img,
  120. success(e) {
  121. wx.showShareMenu({ withShareTicket: true });
  122. }
  123. }
  124. },
  125. //移除触摸限制
  126. moveTouch: function () {
  127. }
  128. })