index.js 17 KB

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