index.js 16 KB

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