index.js 18 KB

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