1
0

index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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. storeId: '',
  20. page: 1,
  21. size: 3,
  22. list: [],
  23. openAttr: false,
  24. isMapShow: '', //第三方商户是否启用地图显示
  25. },
  26. onLoad: function(options) {
  27. let that = this;
  28. wx.setStorageSync("navUrl", "/pages/index/index");
  29. // options.scene = 38;
  30. // options.scene = 65;
  31. // options.scene = 28;
  32. // options.scene = 8;
  33. if (options.scene) {
  34. console.log("have scene");
  35. var scene = decodeURIComponent(options.scene);
  36. console.log("scene is ", scene);
  37. that.setData({
  38. storeId: scene
  39. });
  40. wx.setStorageSync('storeId', scene);
  41. //扫码进入的小程序则清除是否分享缓存
  42. wx.removeStorageSync('isShare');
  43. } else {
  44. //是否分享过商品,有数据则分享过;从首页进入页面则没有分享过,从详情页进入则分享过
  45. if (wx.getStorageSync('isShare') == 'true') {
  46. console.log('是分享进入首页门店id:' + wx.getStorageSync('storeId') + ',从商品页获取的缓存')
  47. console.log('是分享进入首页:' + wx.getStorageSync('isShare'))
  48. } else {
  49. //小程序入口进入的主页,则清除storeId,获取当前地址最近的门店
  50. wx.removeStorageSync('storeId');
  51. console.log('不是分享进入首页门店id:【' + wx.getStorageSync('storeId') + '】,获取最近门店')
  52. console.log('不是分享进入首页:' + wx.getStorageSync('isShare'))
  53. }
  54. }
  55. },
  56. onReady: function() {
  57. // 页面渲染完成
  58. },
  59. onShow: function() {
  60. // 页面显示
  61. let that = this;
  62. wx.setStorageSync("navUrl", "/pages/index/index");
  63. if (wx.getStorageSync('userInfo') || wx.getStorageSync('token')) {
  64. that.syncStore();
  65. } else {
  66. wx.navigateTo({
  67. url: '/pages/auth/btnAuth/btnAuth',
  68. })
  69. }
  70. },
  71. onHide: function() {
  72. // 页面隐藏
  73. },
  74. onUnload: function() {
  75. // 页面关闭
  76. },
  77. showCouponPop() {
  78. let that = this;
  79. this.setData({
  80. showPop: false
  81. });
  82. // wx.showToast({
  83. // title: '恭喜获取优惠券一张' + that.data.couponVo.name,
  84. // duration: 2000
  85. // });
  86. wx.showModal({
  87. title: '获取优惠券一张',
  88. showCancel: false,
  89. content: that.data.couponVo.name
  90. })
  91. },
  92. onShareAppMessage: function() {
  93. return {
  94. title: '商业版',
  95. desc: '新人好礼送券',
  96. path: '/pages/index/index'
  97. }
  98. },
  99. getIndexData: function() {
  100. setTimeout(function() {}, 350)
  101. wx.showLoading({
  102. title: '加载中...',
  103. })
  104. let that = this;
  105. util.request(api.IndexUrl).then(function(res) {
  106. if (res.errno === 0) {
  107. // console.log(res.data.banner);
  108. that.setData({
  109. // newGoods: res.data.newGoodsList,
  110. // hotGoods: res.data.hotGoodsList,
  111. // topics: res.data.topicList,
  112. // brand: res.data.brandList,
  113. // floorGoods: res.data.categoryList,
  114. banner: res.data.banner,
  115. // groupBanner: res.data.groupBanner,
  116. channel: res.data.channel
  117. });
  118. // console.log(that.data.hotGoods.length)
  119. // console.log(wx.getStorageSync('storeId'))
  120. // console.log(wx.getStorageSync('mapIndexStoreId'))
  121. // if (that.data.hotGoods.length == 0) {
  122. // that.getGoodsList();
  123. // }
  124. console.log("isShare:" + wx.getStorageSync('isShare'))
  125. console.log("isLoadGoods:" + wx.getStorageSync('isLoadGoods'))
  126. //当切换了门店地图(mapIndexStoreId),或是由用户分享进入的商品详情切换进入的首页(isShare:true)
  127. if (wx.getStorageSync('storeId') != wx.getStorageSync('mapIndexStoreId') || wx.getStorageSync('isShare') == 'true' || that.data.hotGoods.length == 0 || wx.getStorageSync('isLoadGoods') == '1') {
  128. that.setData({
  129. hotGoods: [],
  130. page: 1
  131. });
  132. that.getGoodsList();
  133. if (wx.getStorageSync('isLoadGoods') == '1') {
  134. util.request(api.UpdateStoreLoadGoods, {
  135. storeId: wx.getStorageSync('storeId')
  136. }).then(function(res) {});
  137. }
  138. wx.setStorageSync('isShare', 'false');
  139. }
  140. }
  141. wx.hideLoading();
  142. });
  143. },
  144. getGoodsList: function() {
  145. let that = this;
  146. util.request(api.GoodsHot).then(function(res) {
  147. if (res.errno == 0) {
  148. util.request(api.HotGoodsList, {
  149. isHot: 1,
  150. page: that.data.page,
  151. size: that.data.size,
  152. categoryId: 0
  153. })
  154. .then(function(res) {
  155. if (res.errno == 0) {
  156. let goodsList = that.data.hotGoods.concat(res.data.goodsList);
  157. that.setData({
  158. hotGoods: goodsList,
  159. list: res.data.goodsList
  160. });
  161. }
  162. });
  163. wx.hideLoading();
  164. }
  165. });
  166. },
  167. getGroupData: function() {
  168. let that = this;
  169. util.request(api.GroupList).then(function(res) {
  170. if (res.errno === 0) {
  171. that.setData({
  172. groupGoods: res.data.data,
  173. });
  174. }
  175. });
  176. },
  177. handleStore() {
  178. wx.navigateTo({
  179. url: '../map/map',
  180. })
  181. },
  182. goSearch() {
  183. wx.navigateTo({
  184. url: '../search/search',
  185. })
  186. },
  187. goCatalog: function(e) {
  188. let url = '';
  189. // console.log('dataset.goodsBizType:' + e.currentTarget.dataset.goodsBizType);
  190. app.globalData.appGoodsBizType = e.currentTarget.dataset.goodsBizType;
  191. // console.log('appgoodsBizType1:' + app.globalData.appGoodsBizType);
  192. wx.setStorageSync('isSwitchCatalog', 'true'); //主页跳转至分类页加载商品,
  193. wx.switchTab({
  194. url: '/pages/catalog/catalog',
  195. });
  196. },
  197. onReachBottom: function() {
  198. if (this.data.bottomLoadDone === true || this.data.bottomLoading === true) {
  199. return false;
  200. }
  201. this.setData({
  202. bottomLoading: true
  203. });
  204. // this.getFloorCategory();
  205. },
  206. reLoad: function() {
  207. let that = this;
  208. // console.log(wx.getStorageSync('userId'));
  209. // console.log(wx.getStorageSync('storeId'));
  210. // console.log(wx.getStorageSync('merchSn'));
  211. if (wx.getStorageSync('storeId')) {
  212. if (wx.getStorageSync('userId')) {
  213. wx.request({
  214. url: api.updateLoginUser,
  215. data: {
  216. userId: wx.getStorageSync('userId'),
  217. storeId: wx.getStorageSync('storeId'),
  218. merchSn: wx.getStorageSync('merchSn')
  219. },
  220. method: 'POST',
  221. header: {
  222. 'Content-Type': 'application/json'
  223. },
  224. success: function(wxRes) {
  225. if (wxRes.data.errno === 0) {
  226. that.setData({
  227. isMapShow: wxRes.data.data.isMapShow
  228. });
  229. wx.setStorageSync('thirdPartyMerchCode', wxRes.data.data.thirdPartyMerchCode);
  230. // console.log(that.data.isMapShow)
  231. // console.log("用户信息更新成功");
  232. }
  233. },
  234. fail: function(err) {
  235. console.log("failed");
  236. }
  237. });
  238. }
  239. that.getIndexData();
  240. that.enableActivity();
  241. that.getGroupData();
  242. }
  243. },
  244. // 同步门店
  245. syncStore: function() {
  246. let that = this;
  247. // console.log(wx.getStorageSync('merchSn'));
  248. //获取附件门店信息
  249. util.getLocation((lng, lat) => {
  250. wx.setStorageSync('location', JSON.stringify({
  251. lng,
  252. lat
  253. }));
  254. let storeId = wx.getStorageSync('storeId');
  255. console.log('查询门店id:' + storeId)
  256. util.request(api.NearbyList, {
  257. longitude: lng,
  258. latitude: lat,
  259. storeId: storeId,
  260. thirdPartyMerchCode: ''
  261. }).then((res) => {
  262. let nlist = res.data;
  263. // wx.removeStorageSync('nearStoreList');
  264. // wx.removeStorageSync('storeId');
  265. // wx.removeStorageSync('storeVo');
  266. // wx.setStorageSync('storeId', storeId);
  267. if (!wx.getStorageSync('currentCategory')) {
  268. wx.removeStorageSync('currentCategory');
  269. }
  270. if (!nlist.length) {
  271. wx.removeStorageSync('nearStoreList');
  272. wx.removeStorageSync('mapIndexStoreId');
  273. } else {
  274. wx.setStorageSync('nearStoreList', JSON.stringify(nlist));
  275. wx.setStorageSync('mapIndexStoreId', nlist[0].id);
  276. wx.setStorageSync('isLoadGoods', nlist[0].isLoadGoods);
  277. }
  278. if (!nlist.length) {
  279. // wx.removeStorageSync('storeId');
  280. wx.removeStorageSync('storeVo');
  281. that.setData({
  282. storeName: '附近暂无门店'
  283. })
  284. } else {
  285. that.setData({
  286. storeName: nlist[0].storeName,
  287. storeId: nlist[0].id
  288. })
  289. that.chooseStore(nlist[0].id, nlist[0].merchSn);
  290. wx.setStorageSync('storeVo', JSON.stringify(nlist[0]));
  291. }
  292. })
  293. });
  294. },
  295. // 更新门店Id
  296. chooseStore: function(storeId, merchSn) {
  297. let that = this;
  298. util.request(api.ChooseStoreId, {
  299. storeId: storeId,
  300. merchSn: merchSn
  301. }, 'POST').then(function(res) {
  302. if (res.errno === 0) {
  303. wx.setStorageSync('storeId', storeId);
  304. wx.setStorageSync('merchSn', merchSn);
  305. that.reLoad();
  306. }
  307. });
  308. },
  309. //购物车减少
  310. cutNumber: function(e) {
  311. let that = this;
  312. var goodsId = e.currentTarget.dataset.goodsId;
  313. var productId = e.currentTarget.dataset.productId;
  314. var hotGoods = that.data.hotGoods;
  315. // hotGoods.forEach(function (val, index, arr) {
  316. // if (val.product_id == productId) {
  317. // val.cart_num = val.cart_num - 1;
  318. // if (val.cart_num >= 0) {
  319. // hotGoods[index] = val;
  320. // }
  321. // }
  322. // });
  323. // that.setData({ hotGoods: hotGoods });
  324. util.request(api.CartMinus, {
  325. goodsId: goodsId,
  326. productId: productId,
  327. number: 1
  328. }, 'POST').then(function(res) {
  329. if (res.errno === 0 && null != res.data) {
  330. var hotGoods = that.data.hotGoods;
  331. hotGoods.forEach(function(val, index, arr) {
  332. if (val.product_id == productId) {
  333. val.cart_num = res.data;
  334. hotGoods[index] = val;
  335. that.setData({
  336. hotGoods: hotGoods
  337. });
  338. }
  339. }, that);
  340. }
  341. });
  342. },
  343. //购物车增加
  344. addNumber: function(e) {
  345. let that = this;
  346. var goodsId = e.currentTarget.dataset.goodsId;
  347. var productId = e.currentTarget.dataset.productId;
  348. var hotGoods = that.data.hotGoods;
  349. // hotGoods.forEach(function (val, index, arr) {
  350. // if (val.product_id == productId) {
  351. // val.cart_num = val.cart_num + 1;
  352. // hotGoods[index] = val;
  353. // }
  354. // });
  355. // that.setData({ hotGoods: hotGoods });
  356. util.request(api.CartAdd, {
  357. goodsId: goodsId,
  358. productId: productId,
  359. number: 1
  360. }, 'POST').then(function(res) {
  361. if (res.errno === 0 && null != res.data) {
  362. hotGoods.forEach(function(val, index, arr) {
  363. res.data.cartList.forEach(function(cartVal, cartIndex, cartArr) {
  364. if (val.product_id == cartVal.product_id) {
  365. val.cart_num = cartVal.number;
  366. hotGoods[index] = val;
  367. }
  368. });
  369. that.setData({
  370. hotGoods: hotGoods
  371. });
  372. }, that);
  373. } else {
  374. wx.showToast({
  375. title: res.errmsg,
  376. icon: 'none'
  377. })
  378. }
  379. });
  380. },
  381. // 查询是否有活动
  382. enableActivity: function() {
  383. let that = this;
  384. let couponIds = wx.getStorageSync('couponIds');
  385. if (!couponIds) {
  386. couponIds = new Array();
  387. }
  388. // util.request(api.EnableActivity, { couponIds: couponIds }).then(function (res) {
  389. // // if (res.errno === 0 && null != res.data.showCoupon) {
  390. // // if (couponIds.contains(res.data.showCoupon.id)) {
  391. // // return;
  392. // // }
  393. // // couponIds.push(res.data.showCoupon.id);
  394. // // wx.setStorageSync('couponIds', couponIds);
  395. // // that.setData({
  396. // // couponVo: res.data.showCoupon,
  397. // // showPop: true
  398. // // });
  399. // // } else
  400. // if (res.errno === 0 && null != res.data.takeCoupon && null != res.data.takeCoupon.id) {
  401. // that.setData({
  402. // couponVo: res.data.takeCoupon,
  403. // showPop: true
  404. // });
  405. // }
  406. // });
  407. },
  408. // 商品扫码
  409. scanGoodsCode: function(e) {
  410. var that = this;
  411. var code;
  412. var value;
  413. var substrValue;
  414. var scanType;
  415. // 调起客户端扫码界面进行扫码
  416. wx.scanCode({
  417. // 是否只能从相机扫码
  418. onlyFromCamera: true,
  419. // 扫码类型, barCode:一维码, qrCode:二维码
  420. scanType: ['barCode', 'qrCode'],
  421. success: function(res) {
  422. that.code = "结果:" + res.result + ",路径:" + res.path + ",编码:" + res.rawData;
  423. that.value = res.result;
  424. that.scanType = res.scanType;
  425. that.setData({
  426. goodsCode: that.code
  427. });
  428. if (that.scanType == 'QR_CODE') { //二维码
  429. that.substrValue = that.value.substring(0, 5);
  430. that.value = that.value.substring(5, that.value.length);
  431. // var goodId = that.value.substring(18, that.value.length);
  432. var scanArray = that.value.split('&');
  433. // console.log(scanArray.length);
  434. if (scanArray.length < 2) {
  435. wx.showModal({
  436. title: '',
  437. content: '您所扫描的商品无效',
  438. showCancel: false
  439. });
  440. return;
  441. }
  442. var goodId = scanArray[0].substring(18, that.value.length);
  443. var storeId = scanArray[1].substring(8, that.value.length);
  444. // console.log("storeId:" + storeId);
  445. // console.log("goodId:" + goodId);
  446. if (that.substrValue != 'emato') { //../goods/goods?id=&merchSn=
  447. wx.showModal({
  448. title: '',
  449. content: '您所扫描的商品无效',
  450. showCancel: false,
  451. success: function(res) {
  452. if (res.confirm) {
  453. console.log('用户点击确定')
  454. } else if (res.cancel) {
  455. console.log('用户点击取消')
  456. }
  457. }
  458. });
  459. return;
  460. }
  461. if (storeId != wx.getStorageSync('storeId')) {
  462. wx.showModal({
  463. title: '扫描结果',
  464. content: '该商品不属于当前门店',
  465. showCancel: false
  466. });
  467. return;
  468. }
  469. util.request(api.GoodsDetail, {
  470. id: goodId,
  471. storeId: storeId,
  472. referrer: ''
  473. }).then(function(res) {
  474. if (res.errno === 0) {
  475. // console.log(res);
  476. // 跳转页面
  477. setTimeout(function() {
  478. wx.navigateTo({
  479. url: that.value,
  480. success: function(e) {
  481. console.log('跳转成功');
  482. },
  483. fail: function(e) {
  484. console.log('跳转失败');
  485. }
  486. })
  487. }, 350)
  488. } else {
  489. wx.showModal({
  490. title: '扫描结果',
  491. content: '商品不存在',
  492. showCancel: false
  493. });
  494. }
  495. });
  496. } else { //其他码
  497. //弹框显示结果
  498. wx.showModal({
  499. title: '扫描结果',
  500. content: that.value,
  501. showCancel: false
  502. });
  503. }
  504. },
  505. fail: function() {
  506. // 显示提示框
  507. wx.showToast({
  508. title: '扫码失败',
  509. icon: 'none',
  510. // 提示的延迟时间
  511. duration: 3000
  512. })
  513. }
  514. })
  515. },
  516. imgOnLoad: function(e) {
  517. let that = this;
  518. // console.log('图片加载完成');
  519. // var realthumb = e.target.dataset.thumb;
  520. // let list = that.data.list
  521. // for (var i = 0; i < list.length; i++) {
  522. // if (list[i].thumb == realthumb) {
  523. // list[i].loaded = true
  524. // }
  525. // that.setData({
  526. // list: list
  527. // })
  528. // }
  529. },
  530. switchAttrPop: function() {
  531. this.setData({
  532. openAttr: !this.data.openAttr
  533. })
  534. },
  535. hideSwitchAttrPop: function() {
  536. this.setData({
  537. openAttr: false
  538. })
  539. },
  540. //购物车增加
  541. addCart: function(e) {
  542. let that = this;
  543. that.setData({
  544. number: 1
  545. });
  546. var goodsId = e.currentTarget.dataset.goodsId;
  547. var retailPrice = e.currentTarget.dataset.retailPrice;
  548. util.request(api.GoodsSku, {
  549. goodsId: goodsId
  550. }).then(function(res) {
  551. if (res.errno === 0 && null != res.data) {
  552. that.setData({
  553. goodsVo: res.data.goodsVo,
  554. specificationList: res.data.specificationList,
  555. productList: res.data.productList,
  556. openAttr: !that.data.openAttr,
  557. retailPrice: retailPrice,
  558. stockNum: res.data.stockNum,
  559. cartNumber: res.data.cartNumber,
  560. checkedSpecText: res.data.specificationList[0].valueList[0].value
  561. });
  562. //
  563. let _specificationList = res.data.specificationList;
  564. for (let i = 0; i < _specificationList.length; i++) {
  565. if (_specificationList[i].valueList.length == 1) {
  566. //如果已经选中,则反选
  567. _specificationList[i].valueList[0].checked = true;
  568. }
  569. }
  570. that.setData({
  571. 'specificationList': _specificationList
  572. });
  573. }
  574. });
  575. },
  576. shows: function(e) {
  577. // wx.redirectTo({
  578. // url: '/pages/images/images'
  579. // })
  580. wx.navigateTo({
  581. url: '../ucenter/campMinus/campMinus?campMinusId=' + 9 + '&&storeId=' + 8 + '&&campName=' + '满200减50' + "&&isShare=1"
  582. })
  583. },
  584. onReachBottom() {
  585. var that = this;
  586. if (that.data.list.length > 0) {
  587. console.log(that.data.list)
  588. wx.showLoading({
  589. title: '加载中...',
  590. })
  591. }
  592. that.setData({
  593. page: that.data.page + 1
  594. });
  595. that.getGoodsList();
  596. }
  597. })