1
0

index.js 16 KB

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