index.js 17 KB

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