1
0

index.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. console.log(res.data.banner);
  47. that.setData({
  48. // newGoods: res.data.newGoodsList,
  49. hotGoods: res.data.hotGoodsList,
  50. // topics: res.data.topicList,
  51. // brand: res.data.brandList,
  52. // floorGoods: res.data.categoryList,
  53. banner: res.data.banner,
  54. groupBanner: res.data.groupBanner,
  55. channel: res.data.channel
  56. });
  57. }
  58. });
  59. },
  60. getGroupData: function () {
  61. let that = this;
  62. util.request(api.GroupList).then(function (res) {
  63. if (res.errno === 0) {
  64. that.setData({
  65. groupGoods: res.data.data,
  66. });
  67. }
  68. });
  69. },
  70. onLoad: function (options) {
  71. let that = this;
  72. wx.setStorageSync("navUrl", "/pages/index/index")
  73. that.syncStore();
  74. },
  75. onReady: function () {
  76. // 页面渲染完成
  77. },
  78. onShow: function () {
  79. // 页面显示
  80. },
  81. onHide: function () {
  82. // 页面隐藏
  83. },
  84. onUnload: function () {
  85. // 页面关闭
  86. },
  87. handleStore() {
  88. wx.navigateTo({
  89. url: '../store/store',
  90. })
  91. },
  92. goSearch() {
  93. wx.navigateTo({
  94. url: '../search/search',
  95. })
  96. },
  97. onReachBottom: function () {
  98. if (this.data.bottomLoadDone === true || this.data.bottomLoading === true) {
  99. return false;
  100. }
  101. this.setData({
  102. bottomLoading: true
  103. });
  104. // this.getFloorCategory();
  105. },
  106. reLoad: function () {
  107. let that = this;
  108. if (wx.getStorageSync('storeId')) {
  109. that.getIndexData();
  110. that.enableActivity();
  111. that.getGroupData();
  112. }
  113. },
  114. // 同步门店
  115. syncStore: function () {
  116. let that = this;
  117. if (!wx.getStorageSync('storeId')) {
  118. util.getLocation((lng, lat) => {
  119. wx.setStorageSync('location', JSON.stringify({ lng, lat }));
  120. util.request(api.NearbyList, { longitude: lng, latitude: lat }).then((res) => {
  121. let nlist = res.data;
  122. if (!nlist.length) {
  123. wx.removeStorageSync('nearStoreList');
  124. wx.removeStorageSync('storeId');
  125. wx.removeStorageSync('storeVo');
  126. that.setData({
  127. storeName: '附近暂无门店'
  128. })
  129. } else {
  130. that.setData({
  131. storeName: nlist[0].storeName
  132. })
  133. that.chooseStore(nlist[0].id)
  134. wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
  135. wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
  136. }
  137. that.reLoad();
  138. })
  139. })
  140. } else if (wx.getStorageSync('storeVo')
  141. && wx.getStorageSync('storeVo').length > 0) {
  142. var storeVo = JSON.parse(wx.getStorageSync('storeVo'));
  143. that.chooseStore(storeVo.id);
  144. that.setData({
  145. storeName: storeVo.storeName
  146. })
  147. that.reLoad();
  148. }
  149. },
  150. // 更新门店Id
  151. chooseStore: function (storeId) {
  152. let that = this;
  153. util.request(api.ChooseStoreId, { storeId: storeId }, 'POST').then(function (res) {
  154. if (res.errno === 0) {
  155. wx.setStorageSync('storeId', storeId);
  156. }
  157. });
  158. },
  159. //购物车减少
  160. cutNumber: function (e) {
  161. let that = this;
  162. var goodsId = e.currentTarget.dataset.goodsId;
  163. var productId = e.currentTarget.dataset.productId;
  164. var hotGoods = that.data.hotGoods;
  165. // hotGoods.forEach(function (val, index, arr) {
  166. // if (val.product_id == productId) {
  167. // val.cart_num = val.cart_num - 1;
  168. // if (val.cart_num >= 0) {
  169. // hotGoods[index] = val;
  170. // }
  171. // }
  172. // });
  173. // that.setData({ hotGoods: hotGoods });
  174. util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  175. if (res.errno === 0 && null != res.data) {
  176. var hotGoods = that.data.hotGoods;
  177. hotGoods.forEach(function (val, index, arr) {
  178. if (val.product_id == productId) {
  179. val.cart_num = res.data;
  180. hotGoods[index] = val;
  181. that.setData({ hotGoods: hotGoods });
  182. }
  183. }, that);
  184. }
  185. });
  186. },
  187. //购物车增加
  188. addNumber: function (e) {
  189. let that = this;
  190. var goodsId = e.currentTarget.dataset.goodsId;
  191. var productId = e.currentTarget.dataset.productId;
  192. var hotGoods = that.data.hotGoods;
  193. // hotGoods.forEach(function (val, index, arr) {
  194. // if (val.product_id == productId) {
  195. // val.cart_num = val.cart_num + 1;
  196. // hotGoods[index] = val;
  197. // }
  198. // });
  199. // that.setData({ hotGoods: hotGoods });
  200. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  201. if (res.errno === 0 && null != res.data) {
  202. hotGoods.forEach(function (val, index, arr) {
  203. res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
  204. if (val.product_id == cartVal.product_id) {
  205. val.cart_num = cartVal.number;
  206. hotGoods[index] = val;
  207. }
  208. });
  209. that.setData({ hotGoods: hotGoods });
  210. }, that);
  211. }
  212. });
  213. },
  214. // 查询是否有活动
  215. enableActivity: function () {
  216. let that = this;
  217. let couponIds = wx.getStorageSync('couponIds');
  218. if (!couponIds) {
  219. couponIds = new Array();
  220. }
  221. util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
  222. // if (res.errno === 0 && null != res.data.showCoupon) {
  223. // if (couponIds.contains(res.data.showCoupon.id)) {
  224. // return;
  225. // }
  226. // couponIds.push(res.data.showCoupon.id);
  227. // wx.setStorageSync('couponIds', couponIds);
  228. // that.setData({
  229. // couponVo: res.data.showCoupon,
  230. // showPop: true
  231. // });
  232. // } else
  233. if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
  234. that.setData({
  235. couponVo: res.data.takeCoupon,
  236. showPop: true
  237. });
  238. }
  239. });
  240. },
  241. // 商品扫码
  242. scanGoodsCode: function (e) {
  243. var that = this;
  244. var code;
  245. // 调起客户端扫码界面进行扫码
  246. wx.scanCode({
  247. // 是否只能从相机扫码
  248. onlyFromCamera: true,
  249. // 扫码类型, barCode:一维码, qrCode:二维码
  250. scanType: ['barCode', 'qrCode'],
  251. success: function (res) {
  252. that.code = "结果:" + res.result + ",路径:" + res.path;
  253. that.setData({
  254. goodsCode: that.code
  255. })
  256. // 跳转页面
  257. wx.navigateTo({
  258. url: '../details/details?id=111',
  259. success: function (e) {
  260. console.log('跳转成功')
  261. },
  262. fail: function (e) {
  263. console.log('跳转失败')
  264. }
  265. })
  266. },
  267. fail: function () {
  268. // 显示提示框
  269. wx.showToast({
  270. title: '扫码失败',
  271. icon: 'none',
  272. // 提示的延迟时间
  273. duration: 3000
  274. })
  275. }
  276. })
  277. }
  278. })