1
0

index.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. goCatalog: function (e) {
  98. let url = '';
  99. // console.log('dataset.goodsBizType:' + e.currentTarget.dataset.goodsBizType);
  100. app.globalData.appGoodsBizType = e.currentTarget.dataset.goodsBizType;
  101. // console.log('appgoodsBizType1:' + app.globalData.appGoodsBizType);
  102. wx.switchTab({
  103. url: '/pages/catalog/catalog',
  104. });
  105. },
  106. onReachBottom: function () {
  107. if (this.data.bottomLoadDone === true || this.data.bottomLoading === true) {
  108. return false;
  109. }
  110. this.setData({
  111. bottomLoading: true
  112. });
  113. // this.getFloorCategory();
  114. },
  115. reLoad: function () {
  116. let that = this;
  117. if (wx.getStorageSync('storeId')) {
  118. that.getIndexData();
  119. that.enableActivity();
  120. that.getGroupData();
  121. }
  122. },
  123. // 同步门店
  124. syncStore: function () {
  125. let that = this;
  126. if (!wx.getStorageSync('storeId')) {
  127. util.getLocation((lng, lat) => {
  128. wx.setStorageSync('location', JSON.stringify({ lng, lat }));
  129. util.request(api.NearbyList, { longitude: lng, latitude: lat }).then((res) => {
  130. let nlist = res.data;
  131. if (!nlist.length) {
  132. wx.removeStorageSync('nearStoreList');
  133. wx.removeStorageSync('storeId');
  134. wx.removeStorageSync('storeVo');
  135. that.setData({
  136. storeName: '附近暂无门店'
  137. })
  138. } else {
  139. that.setData({
  140. storeName: nlist[0].storeName
  141. })
  142. that.chooseStore(nlist[0].id)
  143. wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
  144. wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
  145. }
  146. that.reLoad();
  147. })
  148. })
  149. } else if (wx.getStorageSync('storeVo')
  150. && wx.getStorageSync('storeVo').length > 0) {
  151. var storeVo = JSON.parse(wx.getStorageSync('storeVo'));
  152. that.chooseStore(storeVo.id);
  153. that.setData({
  154. storeName: storeVo.storeName
  155. })
  156. that.reLoad();
  157. }
  158. },
  159. // 更新门店Id
  160. chooseStore: function (storeId) {
  161. let that = this;
  162. util.request(api.ChooseStoreId, { storeId: storeId }, 'POST').then(function (res) {
  163. if (res.errno === 0) {
  164. wx.setStorageSync('storeId', storeId);
  165. }
  166. });
  167. },
  168. //购物车减少
  169. cutNumber: function (e) {
  170. let that = this;
  171. var goodsId = e.currentTarget.dataset.goodsId;
  172. var productId = e.currentTarget.dataset.productId;
  173. var hotGoods = that.data.hotGoods;
  174. // hotGoods.forEach(function (val, index, arr) {
  175. // if (val.product_id == productId) {
  176. // val.cart_num = val.cart_num - 1;
  177. // if (val.cart_num >= 0) {
  178. // hotGoods[index] = val;
  179. // }
  180. // }
  181. // });
  182. // that.setData({ hotGoods: hotGoods });
  183. util.request(api.CartMinus, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  184. if (res.errno === 0 && null != res.data) {
  185. var hotGoods = that.data.hotGoods;
  186. hotGoods.forEach(function (val, index, arr) {
  187. if (val.product_id == productId) {
  188. val.cart_num = res.data;
  189. hotGoods[index] = val;
  190. that.setData({ hotGoods: hotGoods });
  191. }
  192. }, that);
  193. }
  194. });
  195. },
  196. //购物车增加
  197. addNumber: function (e) {
  198. let that = this;
  199. var goodsId = e.currentTarget.dataset.goodsId;
  200. var productId = e.currentTarget.dataset.productId;
  201. var hotGoods = that.data.hotGoods;
  202. // hotGoods.forEach(function (val, index, arr) {
  203. // if (val.product_id == productId) {
  204. // val.cart_num = val.cart_num + 1;
  205. // hotGoods[index] = val;
  206. // }
  207. // });
  208. // that.setData({ hotGoods: hotGoods });
  209. util.request(api.CartAdd, { goodsId: goodsId, productId: productId, number: 1 }, 'POST').then(function (res) {
  210. if (res.errno === 0 && null != res.data) {
  211. hotGoods.forEach(function (val, index, arr) {
  212. res.data.cartList.forEach(function (cartVal, cartIndex, cartArr) {
  213. if (val.product_id == cartVal.product_id) {
  214. val.cart_num = cartVal.number;
  215. hotGoods[index] = val;
  216. }
  217. });
  218. that.setData({ hotGoods: hotGoods });
  219. }, that);
  220. }
  221. });
  222. },
  223. // 查询是否有活动
  224. enableActivity: function () {
  225. let that = this;
  226. let couponIds = wx.getStorageSync('couponIds');
  227. if (!couponIds) {
  228. couponIds = new Array();
  229. }
  230. util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
  231. // if (res.errno === 0 && null != res.data.showCoupon) {
  232. // if (couponIds.contains(res.data.showCoupon.id)) {
  233. // return;
  234. // }
  235. // couponIds.push(res.data.showCoupon.id);
  236. // wx.setStorageSync('couponIds', couponIds);
  237. // that.setData({
  238. // couponVo: res.data.showCoupon,
  239. // showPop: true
  240. // });
  241. // } else
  242. if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
  243. that.setData({
  244. couponVo: res.data.takeCoupon,
  245. showPop: true
  246. });
  247. }
  248. });
  249. },
  250. // 商品扫码
  251. scanGoodsCode: function (e) {
  252. var that = this;
  253. var code;
  254. var value;
  255. var substrValue;
  256. var scanType;
  257. // 调起客户端扫码界面进行扫码
  258. wx.scanCode({
  259. // 是否只能从相机扫码
  260. onlyFromCamera: true,
  261. // 扫码类型, barCode:一维码, qrCode:二维码
  262. scanType: ['barCode', 'qrCode'],
  263. success: function (res) {
  264. that.code = "结果:" + res.result + ",路径:" + res.path + ",编码:" + res.rawData;
  265. that.value = res.result;
  266. that.scanType = res.scanType;
  267. that.setData({
  268. goodsCode: that.code
  269. });
  270. if (that.scanType == 'QR_CODE') {//二维码
  271. that.substrValue = that.value.substring(0, 5);
  272. that.value = that.value.substring(5, that.value.length);
  273. if (that.substrValue != 'emato') {//../goods/goods?id=
  274. wx.showModal({
  275. title: '',
  276. content: '您所扫描的商品无效',
  277. showCancel: false,
  278. success: function (res) {
  279. if (res.confirm) {
  280. console.log('用户点击确定')
  281. } else if (res.cancel) {
  282. console.log('用户点击取消')
  283. }
  284. }
  285. });
  286. } else {
  287. // 跳转页面
  288. wx.navigateTo({
  289. url: that.value,
  290. success: function (e) {
  291. console.log('跳转成功');
  292. },
  293. fail: function (e) {
  294. console.log('跳转失败');
  295. }
  296. })
  297. }
  298. }else{//其他码
  299. //弹框显示结果
  300. wx.showModal({
  301. title: '扫描结果',
  302. content: that.value,
  303. showCancel: false
  304. });
  305. }
  306. },
  307. fail: function () {
  308. // 显示提示框
  309. wx.showToast({
  310. title: '扫码失败',
  311. icon: 'none',
  312. // 提示的延迟时间
  313. duration: 3000
  314. })
  315. }
  316. })
  317. }
  318. })