index.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. var app = getApp();
  2. var status = require('../../utils/index.js');
  3. var util = require('../../utils/util.js');
  4. Component({
  5. externalClasses: ["i-class"],
  6. properties: {
  7. updateCart: {
  8. type: Number,
  9. value: 0,
  10. observer: function (val) {
  11. if (val>0) {
  12. this.updateCartNum();
  13. }
  14. }
  15. },
  16. likeTitle: {
  17. type: String,
  18. value: '大家常买'
  19. },
  20. controller: {
  21. type: 'String',
  22. value: 'index.load_gps_goodslist'
  23. },
  24. gid: {
  25. type: 'Number',
  26. value: 0
  27. }
  28. },
  29. data: {
  30. disabled: false,
  31. list: [],
  32. show_goods_guess_like: 1,
  33. _lock: false
  34. },
  35. attached: function() {
  36. console.log('guess like attached');
  37. this.setData({_lock: true },()=>{
  38. this.initFn();
  39. })
  40. },
  41. pageLifetimes: {
  42. show: function () {
  43. this.data._lock || this.initFn();
  44. console.log('guess like show');
  45. }
  46. },
  47. methods: {
  48. initFn: function(){
  49. let that = this;
  50. this.setData({list: []}, ()=>{
  51. that.getData();
  52. that.updateCartNum();
  53. })
  54. },
  55. getData: function () {
  56. var token = wx.getStorageSync('token');
  57. var that = this;
  58. var cur_community = wx.getStorageSync('community');
  59. let { controller, gid }= this.data;
  60. app.util.request({
  61. 'url': 'entry/wxapp/index',
  62. 'data': {
  63. controller,
  64. token: token,
  65. pageNum: 1,
  66. is_random: 1,
  67. head_id: cur_community.communityId || '',
  68. id: gid
  69. },
  70. dataType: 'json',
  71. success: function (res) {
  72. if (res.data.code == 0) {
  73. let oldList = that.data.list;
  74. let list = res.data.list || [];
  75. list = oldList.concat(list);
  76. let show_goods_guess_like = 1;
  77. if (gid) show_goods_guess_like = res.data.show_goods_guess_like || 0;
  78. that.setData({ list, show_goods_guess_like, _lock: false })
  79. } else {
  80. that.setData({ noMore: true, _lock: false })
  81. }
  82. }
  83. })
  84. },
  85. openSku: function (e) {
  86. let idx = e.currentTarget.dataset.idx;
  87. this.setData({ disabled: false })
  88. let spuItem = this.data.list[idx];
  89. if (spuItem.skuList.length === void 0) {
  90. this.triggerEvent("openSku", {
  91. actId: spuItem.actId,
  92. skuList: spuItem.skuList,
  93. promotionDTO: spuItem.promotionDTO || '',
  94. allData: {
  95. spuName: spuItem.spuName,
  96. skuImage: spuItem.skuImage,
  97. actPrice: spuItem.actPrice,
  98. canBuyNum: spuItem.spuCanBuyNum,
  99. stock: spuItem.spuCanBuyNum,
  100. marketPrice: spuItem.marketPrice,
  101. oneday_limit_count: spuItem.oneday_limit_count,
  102. total_limit_count: spuItem.total_limit_count,
  103. one_limit_count: spuItem.one_limit_count,
  104. goods_start_count: spuItem.goods_start_count
  105. }
  106. })
  107. } else {
  108. this.addCart({ value: 1, type: "plus", idx });
  109. }
  110. },
  111. changeNumber: function (t) {
  112. var e = t.detail;
  113. e && this.addCart(e);
  114. },
  115. outOfMax: function (t) {
  116. var e = t.detail, idx = t.idx, list = this.data.list, spuItem = list[idx], canBuyNum = spuItem.spuCanBuyNum;
  117. if (list[idx].car_count >= canBuyNum) {
  118. wx.showToast({
  119. title: "不能购买更多啦",
  120. icon: "none"
  121. })
  122. }
  123. },
  124. addCart: function (t) {
  125. var token = wx.getStorageSync('token');
  126. var community = wx.getStorageSync('community');
  127. let idx = t.idx;
  128. let list = this.data.list;
  129. let spuItem = list[idx];
  130. var goods_id = spuItem.actId;
  131. var community_id = community.communityId;
  132. let goods_start_count = spuItem.goods_start_count;
  133. let number = spuItem.car_count || 0;
  134. let quantity = 1;
  135. let that = this;
  136. if (t.type == 'plus') {
  137. if(number<goods_start_count) {
  138. quantity = goods_start_count-number;
  139. }
  140. let data = {
  141. goods_id,
  142. community_id,
  143. quantity,
  144. sku_str: '',
  145. buy_type: 'dan',
  146. pin_id: 0,
  147. is_just_addcar: 1
  148. }
  149. util.addCart(data).then(res=>{
  150. if(res.showVipModal==1) {
  151. let { pop_vipmember_buyimage } = res.data;
  152. that.triggerEvent("vipModal", { pop_vipmember_buyimage, showVipModal: true, visible: false });
  153. } else {
  154. if (res.data.code == 3 || res.data.code == 7) {
  155. wx.showToast({
  156. title: res.data.msg,
  157. icon: 'none',
  158. duration: 2000
  159. })
  160. } else if (res.data.code == 6) {
  161. let max_quantity = res.data.max_quantity || '';
  162. list[idx].car_count = res.data.max_quantity || 0;
  163. (max_quantity > 0) && that.setData({ list })
  164. var msg = res.data.msg;
  165. wx.showToast({
  166. title: msg,
  167. icon: 'none',
  168. duration: 2000
  169. })
  170. } else {
  171. that.triggerEvent("changeCartNum", res.data.total);
  172. list[idx].car_count = res.data.cur_count || 0;
  173. that.setData({ list })
  174. wx.showToast({
  175. title: "已加入购物车",
  176. image: "../../images/addShopCart.png"
  177. })
  178. status.indexListCarCount(goods_id, res.data.cur_count);
  179. }
  180. }
  181. })
  182. } else {
  183. if(number<=goods_start_count) {
  184. quantity = number;
  185. }
  186. app.util.request({
  187. url: 'entry/wxapp/user',
  188. data: {
  189. controller: 'car.reduce_car_goods',
  190. token: token,
  191. goods_id: goods_id,
  192. community_id: community_id,
  193. quantity,
  194. sku_str: '',
  195. buy_type: 'dan',
  196. pin_id: 0,
  197. is_just_addcar: 1
  198. },
  199. dataType: 'json',
  200. method: 'POST',
  201. success: function (res) {
  202. if (res.data.code == 3) {
  203. wx.showToast({
  204. title: res.data.msg,
  205. icon: 'none',
  206. duration: 2000
  207. })
  208. } else {
  209. that.triggerEvent("changeCartNum", res.data.total);
  210. list[idx].car_count = res.data.cur_count || 0;
  211. that.setData({ list })
  212. status.indexListCarCount(goods_id, res.data.cur_count);
  213. }
  214. }
  215. })
  216. }
  217. },
  218. updateCartNum: function () {
  219. let goodsListCarCount = app.globalData.goodsListCarCount; //[{ actId: 84, num: 2}]
  220. let list = this.data.list;
  221. if (goodsListCarCount.length > 0 && list.length > 0) {
  222. goodsListCarCount.forEach(function (item) {
  223. let k = list.findIndex((n) => n.actId == item.actId);
  224. if (k != -1 && list[k].skuList.length === 0) {
  225. let newNum = item.num * 1;
  226. list[k].car_count = newNum >= 0 ? newNum : 0;
  227. }
  228. })
  229. this.setData({ list })
  230. }
  231. }
  232. }
  233. })