index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. const util = require('../../utils/util.js');
  2. const api = require('../../config/api.js');
  3. const user = require('../../services/user.js');
  4. //获取应用实例
  5. const app = getApp()
  6. Page({
  7. data: {
  8. groupGoods: [],
  9. hotGoods: [],
  10. topics: [],
  11. brands: [],
  12. floorGoods: [],
  13. banner: [],
  14. channel: [],
  15. groupBanner: {},
  16. storeName: '',
  17. showPop: false,//活动弹窗
  18. couponVo: {}
  19. },
  20. showCouponPop() {
  21. let that = this;
  22. this.setData({
  23. showPop: false
  24. });
  25. // wx.showToast({
  26. // title: '恭喜获取优惠券一张' + that.data.couponVo.name,
  27. // duration: 2000
  28. // });
  29. wx.showModal({
  30. title: '获取优惠券一张',
  31. showCancel: false,
  32. content: that.data.couponVo.name
  33. })
  34. },
  35. onShareAppMessage: function () {
  36. return {
  37. title: '商业版',
  38. desc: '新人好礼送券',
  39. path: '/pages/index/index'
  40. }
  41. },
  42. getIndexData: function () {
  43. let that = this;
  44. util.request(api.IndexUrl).then(function (res) {
  45. if (res.errno === 0) {
  46. that.setData({
  47. // newGoods: res.data.newGoodsList,
  48. hotGoods: res.data.hotGoodsList,
  49. // topics: res.data.topicList,
  50. // brand: res.data.brandList,
  51. // floorGoods: res.data.categoryList,
  52. banner: res.data.banner,
  53. groupBanner: res.data.groupBanner,
  54. channel: res.data.channel
  55. });
  56. }
  57. });
  58. },
  59. getGroupData: function () {
  60. let that = this;
  61. util.request(api.GroupList).then(function (res) {
  62. if (res.errno === 0) {
  63. that.setData({
  64. groupGoods: res.data.data,
  65. });
  66. }
  67. });
  68. },
  69. onLoad: function (options) {
  70. let that = this;
  71. wx.setStorageSync("navUrl", "/pages/index/index")
  72. that.syncStore();
  73. },
  74. onReady: function () {
  75. // 页面渲染完成
  76. },
  77. onShow: function () {
  78. // 页面显示
  79. },
  80. onHide: function () {
  81. // 页面隐藏
  82. },
  83. onUnload: function () {
  84. // 页面关闭
  85. },
  86. handleStore() {
  87. wx.navigateTo({
  88. url: '../store/store',
  89. })
  90. },
  91. goSearch() {
  92. wx.navigateTo({
  93. url: '../search/search',
  94. })
  95. },
  96. onReachBottom: function () {
  97. if (this.data.bottomLoadDone === true || this.data.bottomLoading === true) {
  98. return false;
  99. }
  100. this.setData({
  101. bottomLoading: true
  102. });
  103. // this.getFloorCategory();
  104. },
  105. reLoad: function () {
  106. let that = this;
  107. if (wx.getStorageSync('storeId')) {
  108. that.getIndexData();
  109. that.enableActivity();
  110. that.getGroupData();
  111. }
  112. },
  113. // 同步门店
  114. syncStore: function () {
  115. let that = this;
  116. if (!wx.getStorageSync('storeId')) {
  117. util.getLocation((lng, lat) => {
  118. wx.setStorageSync('location', JSON.stringify({ lng, lat }));
  119. util.request(api.NearbyList, { longitude: lng, latitude: lat }).then((res) => {
  120. let nlist = res.data;
  121. if (!nlist.length) {
  122. wx.removeStorageSync('nearStoreList');
  123. wx.removeStorageSync('storeId');
  124. wx.removeStorageSync('storeVo');
  125. that.setData({
  126. storeName: '附近暂无门店'
  127. })
  128. } else {
  129. that.setData({
  130. storeName: nlist[0].storeName
  131. })
  132. that.chooseStore(nlist[0].id)
  133. wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
  134. wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
  135. }
  136. that.reLoad();
  137. })
  138. })
  139. } else if (wx.getStorageSync('storeVo')
  140. && wx.getStorageSync('storeVo').length > 0) {
  141. var storeVo = JSON.parse(wx.getStorageSync('storeVo'));
  142. that.chooseStore(storeVo.id);
  143. that.setData({
  144. storeName: storeVo.storeName
  145. })
  146. that.reLoad();
  147. }
  148. },
  149. // 更新门店Id
  150. chooseStore: function (storeId) {
  151. let that = this;
  152. util.request(api.ChooseStoreId, { storeId: storeId }, 'POST').then(function (res) {
  153. if (res.errno === 0) {
  154. wx.setStorageSync('storeId', storeId);
  155. }
  156. });
  157. },
  158. //购物车减少
  159. cutNumber: function (e) {
  160. let that = this;
  161. var goodsId = e.currentTarget.dataset.goodsId;
  162. var productId = e.currentTarget.dataset.productId;
  163. var hotGoods = that.data.hotGoods;
  164. // hotGoods.forEach(function (val, index, arr) {
  165. // if (val.product_id == productId) {
  166. // val.cart_num = val.cart_num - 1;
  167. // if (val.cart_num >= 0) {
  168. // hotGoods[index] = val;
  169. // }
  170. // }
  171. // });
  172. // that.setData({ hotGoods: hotGoods });
  173. util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  174. if (res.errno === 0 && null != res.data) {
  175. var hotGoods = that.data.hotGoods;
  176. hotGoods.forEach(function (val, index, arr) {
  177. if (val.product_id == productId) {
  178. val.cart_num = res.data;
  179. hotGoods[index] = val;
  180. that.setData({ hotGoods: hotGoods });
  181. }
  182. }, that);
  183. }
  184. });
  185. },
  186. //购物车增加
  187. addNumber: function (e) {
  188. let that = this;
  189. var goodsId = e.currentTarget.dataset.goodsId;
  190. var productId = e.currentTarget.dataset.productId;
  191. var hotGoods = that.data.hotGoods;
  192. // hotGoods.forEach(function (val, index, arr) {
  193. // if (val.product_id == productId) {
  194. // val.cart_num = val.cart_num + 1;
  195. // hotGoods[index] = val;
  196. // }
  197. // });
  198. // that.setData({ hotGoods: hotGoods });
  199. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  200. if (res.errno === 0 && null != res.data) {
  201. hotGoods.forEach(function (val, index, arr) {
  202. res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
  203. if (val.product_id == cartVal.product_id) {
  204. val.cart_num = cartVal.number;
  205. hotGoods[index] = val;
  206. }
  207. });
  208. that.setData({ hotGoods: hotGoods });
  209. }, that);
  210. }
  211. });
  212. },
  213. // 查询是否有活动
  214. enableActivity: function () {
  215. let that = this;
  216. let couponIds = wx.getStorageSync('couponIds');
  217. if (!couponIds) {
  218. couponIds = new Array();
  219. }
  220. util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
  221. // if (res.errno === 0 && null != res.data.showCoupon) {
  222. // if (couponIds.contains(res.data.showCoupon.id)) {
  223. // return;
  224. // }
  225. // couponIds.push(res.data.showCoupon.id);
  226. // wx.setStorageSync('couponIds', couponIds);
  227. // that.setData({
  228. // couponVo: res.data.showCoupon,
  229. // showPop: true
  230. // });
  231. // } else
  232. if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
  233. that.setData({
  234. couponVo: res.data.takeCoupon,
  235. showPop: true
  236. });
  237. }
  238. });
  239. }
  240. })