1
0

index.js 9.3 KB

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