1
0

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