category.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. // pages/category/category.js
  2. var http = require("../../utils/http.js");
  3. var util = require('../../utils/util.js');
  4. var config = require("../../utils/config.js");
  5. import eventBus from '../../utils/eventBus';
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. showTabbar:true,
  12. showLoading:false,
  13. rightView:'',
  14. paramsObj:{
  15. sort:0,
  16. orderBy:0
  17. },
  18. type:'bottom',
  19. toViewId:'test1',
  20. topHeight:300,
  21. sort:0,
  22. imgUrl:'',
  23. testShopId:0,//109
  24. totalInfo:{"totalMoney":0,"finalMoney":0,"subtractMoney":0.0,"count":0},
  25. shopId:0,
  26. show:false,
  27. showCar:false,
  28. selIndex: 0,
  29. selCategory:0,
  30. subIndex:0,
  31. thridIndex:-1,
  32. subCategoryId:0,
  33. categoryList: [],
  34. categoryImg: '',
  35. subCategoryList: [],
  36. thridCategoryList:[],
  37. pageTopHeight: wx.getWindowInfo().statusBarHeight,
  38. hotList: [],
  39. topCurrentIndex: 0,
  40. productList:[],
  41. carData:[],//购物车数据
  42. animateInfo:{
  43. startX:99999,
  44. startY:0,
  45. endX:0,
  46. endY:0,
  47. },
  48. move:false
  49. },
  50. /**
  51. * 生命周期函数--监听页面加载
  52. */
  53. onLoad: function (options) {
  54. this.getRecommended()
  55. this.get_neighborShop()
  56. // this.get_categoryInfo()
  57. },
  58. scroll(){
  59. },
  60. scrollTop(){
  61. console.log('top',);
  62. if(this.data.subIndex==0||this.data.subCategoryList.length==0||this.data.productList.length == 0) return
  63. this.setData({
  64. type:'top'
  65. })
  66. let data = {currentTarget:{
  67. dataset:{
  68. index:this.data.subIndex-1
  69. }
  70. }}
  71. this.onleftMenuTab(data)
  72. // this.setData({
  73. // topHeight:0
  74. // })
  75. },
  76. scrollBottom(){
  77. // this.data.productList 新增判断 避免为空自动滚动
  78. if(this.data.subIndex==(this.data.subCategoryList.length-1)||this.data.subCategoryList.length==0||this.data.productList.length == 0) return
  79. this.setData({
  80. type:'bottom'
  81. })
  82. let data = {currentTarget:{
  83. dataset:{
  84. index:this.data.subIndex+1
  85. }
  86. }}
  87. this.setData({
  88. topHeight:15
  89. },() => {
  90. // 3. 延迟恢复样式(避免视觉闪烁)
  91. setTimeout(() => {
  92. this.onleftMenuTab(data)
  93. }, 0);
  94. })
  95. },
  96. handleSale(){
  97. this.setData({
  98. sort:1,
  99. paramsObj:{sort:1,orderBy:0}
  100. })
  101. this.getData(this.data.subCategoryList[this.data.subIndex])
  102. },
  103. handlePrice(){
  104. // 没点击价格
  105. if(this.data.sort !=2&&this.data.sort!=3){
  106. this.setData({
  107. sort:2,
  108. paramsObj:{sort:2,orderBy:0}
  109. })
  110. }else{
  111. let sort = this.data.sort ==2?3:2
  112. this.setData({
  113. sort,
  114. paramsObj:{sort:2,orderBy:this.data.sort ==2?0:1}
  115. })
  116. }
  117. this.getData(this.data.subCategoryList[this.data.subIndex])
  118. },
  119. resetData(){
  120. this.setData({
  121. subIndex:0,
  122. thridIndex:-1,
  123. subCategoryId:0,
  124. })
  125. },
  126. // 点击购物车内加减按钮
  127. handleNum(e){
  128. // 查看是否授权
  129. util.checkAuthInfo(()=>{
  130. let {num,item,type,index} = e.currentTarget.dataset
  131. let data = {
  132. "basketId": item.basketId||0,
  133. "count": num,
  134. "distributionCardNo": item.distributionCardNo||'',
  135. "prodId": item.prodId,
  136. "shopId": item.shopId,
  137. "skuId": item.skuId||item.skuList[0].skuId,
  138. channelId:wx.getStorageSync('channelId')||3
  139. }
  140. this.changeCar(data)
  141. if(type == 'add'){
  142. this.addAnimate(e)
  143. }
  144. })
  145. },
  146. addAnimate(e){
  147. let startX = e.touches[0].clientX-10
  148. let startY = e.touches[0].clientY-10
  149. const query = wx.createSelectorQuery().in(this);
  150. query.select('.bottom-box .left .icon').boundingClientRect(cartRect => {
  151. console.log(222222,cartRect);
  152. if (!cartRect) return;
  153. let animateInfo = {
  154. startX,
  155. startY,
  156. endX:cartRect.left,
  157. endY:cartRect.bottom,
  158. }
  159. this.setData({
  160. move:true,
  161. animateInfo
  162. })
  163. console.log(animateInfo);
  164. }).exec();
  165. },
  166. endAnimate(){
  167. console.log('ddddd');
  168. let animateInfo = {
  169. startX:9999,
  170. startY:0,
  171. endX:0,
  172. endY:0,
  173. }
  174. this.setData({
  175. move:false,
  176. animateInfo
  177. })
  178. },
  179. handleAll(){
  180. this.setData({
  181. show:true
  182. })
  183. },
  184. onClose(){
  185. this.setData({
  186. show:false
  187. })
  188. },
  189. openCar(){
  190. console.log(111111);
  191. this.setData({
  192. showCar:true
  193. })
  194. },
  195. onCarClose(){
  196. this.setData({
  197. showCar:false
  198. })
  199. },
  200. /**
  201. * 获取距离最近店铺id
  202. * @param {*} e
  203. */
  204. get_neighborShop: function () {
  205. var params = {
  206. url: "/shop/neighborShop",
  207. method: "GET",
  208. data: {
  209. lat: wx.getStorageSync('LATITUDE'),
  210. lon: wx.getStorageSync('LONGITUDE'),
  211. channelId:wx.getStorageSync('channelId')||3
  212. },
  213. callBack: (res) => {
  214. // this.get_categoryInfo(res)
  215. wx.setStorageSync('shopInfo', res)
  216. this.setData({
  217. shopId:res.shopId
  218. })
  219. this.get_categoryInfo(res.shopId)
  220. this.getCarData(this.data.shopId)
  221. }
  222. };
  223. http.request(params);
  224. },
  225. get_categoryInfo: function (shopid) {
  226. var ths = this;
  227. let that = this
  228. //加载分类列表
  229. var params = {
  230. // url: "/category/categoryInfo",
  231. url: "/category/listCategoryForUser/"+(this.data.testShopId||shopid),
  232. method: "GET",
  233. callBack: function (res) {
  234. ths.setData({
  235. categoryList: res,
  236. subCategoryList: res[ths.data.selIndex].children
  237. });
  238. let categoryId =wx.getStorageSync('categoryId')
  239. if(categoryId){
  240. that.data.categoryList.map((item,index)=>{
  241. if(item.categoryId == categoryId){
  242. that.setData({
  243. selCategory:categoryId,
  244. selIndex:index,
  245. subCategoryList:res[index].children
  246. })
  247. }
  248. })
  249. wx.removeStorageSync('categoryId')
  250. }
  251. // eventBus.on('categoryId', (data) => {
  252. // eventBus.clear()
  253. // that.data.categoryList.map((item,index)=>{
  254. // if(item.categoryId == data){
  255. // that.setData({
  256. // selCategory:data,
  257. // selIndex:index,
  258. // subCategoryList:res[index].children
  259. // })
  260. // }
  261. // })
  262. // });
  263. ths.getData(ths.data.subCategoryList[0])
  264. }
  265. };
  266. http.request(params);
  267. },
  268. getData(data){
  269. console.log(data);
  270. if(!data){
  271. return this.setData({
  272. thridCategoryList:[],
  273. showLoading:false
  274. })
  275. }
  276. this.setData({
  277. thridCategoryList:[],
  278. showLoading:true
  279. })
  280. // 二级分类有商品
  281. if(data.productBeBound == 1){
  282. this.getAllProList(data.categoryId)
  283. // this.getProdList(data.categoryId)
  284. }else{//二级分类有三级分类
  285. this.getAllProList(data.categoryId)
  286. this.setData({
  287. thridCategoryList:data.children,
  288. subCategoryId:data.categoryId
  289. })
  290. }
  291. },
  292. /**
  293. * 选择定位后更新列表
  294. */
  295. go_update() {
  296. this.get_neighborShop()
  297. },
  298. /**
  299. * 生命周期函数--监听页面初次渲染完成
  300. */
  301. onReady: function () {
  302. },
  303. /**
  304. * 生命周期函数--监听页面显示
  305. */
  306. onShow: function () {
  307. console.log('show');
  308. if(this.data.shopId){
  309. this.getCarData(this.data.shopId)
  310. }
  311. // if (getApp().globalData.categoryId) {
  312. // let categoryId = getApp().globalData.categoryId
  313. // let index = getApp().globalData.index
  314. // let pic = getApp().globalData.pic
  315. // this.setData({
  316. // categoryImg: pic,
  317. // selIndex: index
  318. // });
  319. // }else{
  320. // this.get_neighborShop()
  321. // }
  322. // 之前逻辑 每次重新加载
  323. // this.resetData()
  324. // this.get_neighborShop()
  325. },
  326. /**
  327. * 生命周期函数--监听页面隐藏
  328. */
  329. onHide: function () {},
  330. /**
  331. * 生命周期函数--监听页面卸载
  332. */
  333. onUnload: function () {
  334. },
  335. /**
  336. * 页面相关事件处理函数--监听用户下拉动作
  337. */
  338. onPullDownRefresh: function () {
  339. },
  340. /**
  341. * 页面上拉触底事件的处理函数
  342. */
  343. onReachBottom: function () {
  344. },
  345. /**
  346. * 用户点击右上角分享
  347. */
  348. onShareAppMessage: function () {
  349. },
  350. /**
  351. * 分类点击事件,获取子分类
  352. */
  353. onMenuTab: function (e) {
  354. var id = e.currentTarget.dataset.id;
  355. var index = e.currentTarget.dataset.index;
  356. // if(this.data.categoryList[index].children.length == 0){
  357. // return
  358. // }
  359. this.setData({
  360. show:false,
  361. categoryImg: this.data.categoryList[index].pic,
  362. subCategoryList:this.data.categoryList[index].children,
  363. selIndex: index,
  364. selCategory:id,
  365. productList:[],
  366. subIndex:0,
  367. paramsObj:{sort:0,orderBy:0},
  368. sort:0
  369. });
  370. // this.getProdList(this.data.subCategoryList[0].categoryId)
  371. this.getData(this.data.subCategoryList[0])
  372. getApp().globalData.categoryId = ''
  373. getApp().globalData.index = ''
  374. getApp().globalData.pic = ''
  375. },
  376. /**
  377. * 推荐词,type:1关键词,2热门搜索词,3推荐搜索词
  378. */
  379. getRecommended: function () {
  380. var params = {
  381. url: "/keyword/list",
  382. method: "GET",
  383. data: {
  384. type: 2
  385. },
  386. callBack: (res) => {
  387. let reslut = res.filter(item => item.status !== 2) //过滤下线词
  388. let timeReslut = reslut.filter(e => util.dateToTimestamp(e.effectiveTime) < new Date().getTime()) //过滤未生效词
  389. this.setData({
  390. hotList: timeReslut
  391. })
  392. }
  393. };
  394. http.request(params);
  395. },
  396. /**
  397. * 推荐词搜索
  398. */
  399. topSwiperChange: function (e) {
  400. this.setData({
  401. topCurrentIndex: e.detail.current // 获取当前滚动到的swiper-item的索引并更新到data中
  402. })
  403. },
  404. topHotSearch: function () {
  405. const topname = this.data.hotList[this.data.topCurrentIndex].name
  406. wx.navigateTo({
  407. url: `/pages/search-prod-show/search-prod-show?prodName=${topname}&shopId=${this.data.shopId}`,
  408. })
  409. },
  410. // 跳转搜索页
  411. toSearchPage: function () {
  412. wx.navigateTo({
  413. url: `/pages/search-page/search-page?shopId=${this.data.shopId}`,
  414. })
  415. },
  416. /**
  417. * 跳转到定位页面
  418. */
  419. toLocationPage: function () {
  420. wx.navigateTo({
  421. url: '/pages/locationAdd/locationAdd',
  422. })
  423. },
  424. // 点击二级分类
  425. onleftMenuTab(e){
  426. const {
  427. index,
  428. id
  429. } = e.currentTarget.dataset
  430. if(id){
  431. this.setData({
  432. type:'click'
  433. })
  434. console.log('click');
  435. }
  436. this.setData({
  437. subIndex:index,
  438. paramsObj:{sort:0,orderBy:0},
  439. sort:0
  440. })
  441. // this.getProdList(this.data.subCategoryList[index].categoryId)
  442. this.getData(this.data.subCategoryList[index])
  443. },
  444. // 根据分类id获取商品数据
  445. getProdList(categoryId) {
  446. var params = {
  447. url: "/search/searchProdPage",
  448. method: "GET",
  449. data: {
  450. categoryId,
  451. current: 1,
  452. size: 999999,
  453. orderBy:this.data.paramsObj.orderBy,
  454. sort:this.data.paramsObj.sort,
  455. isAllProdType: true,
  456. channelId:wx.getStorageSync('channelId')||3,
  457. // lat: wx.getStorageSync('LATITUDE'),
  458. // lon: wx.getStorageSync('LONGITUDE'),
  459. // distance: wx.getStorageSync('DISTANCE') || 0
  460. },
  461. callBack: (res) => {
  462. setTimeout(() => {
  463. this.setData({
  464. showLoading:false
  465. })
  466. }, 500);
  467. console.log(res);
  468. let productList = res.records
  469. let img = ''
  470. productList.map(e => {
  471. img = e.pic.split(',')
  472. e.pic = img[0]
  473. })
  474. this.setData({
  475. productList,
  476. topHeight:15
  477. })
  478. if(productList.length == 0){
  479. if(this.data.type == 'top'){
  480. this.scrollTop()
  481. }else{
  482. this.scrollBottom()
  483. }
  484. }
  485. this.addParmasProduct()
  486. }
  487. };
  488. http.request(params);
  489. },
  490. // 根据分类id获取所有三级商品数据
  491. getAllProList(categoryId) {
  492. var params = {
  493. url: "/prod/listProdByCategoryIdAndShopId",
  494. method: "post",
  495. data: {
  496. orderBy:this.data.paramsObj.orderBy,
  497. sort:this.data.paramsObj.sort,
  498. categoryId,
  499. current: 1,
  500. size: 999999,
  501. channelId:wx.getStorageSync('channelId')||3,
  502. shopId:this.data.testShopId||this.data.shopId
  503. },
  504. callBack: (res) => {
  505. setTimeout(() => {
  506. this.setData({
  507. showLoading:false
  508. })
  509. }, 500);
  510. let productList = res.records
  511. let img = ''
  512. productList.map(e => {
  513. img = e.pic.split(',')
  514. e.pic = img[0]
  515. })
  516. this.setData({
  517. productList,
  518. topHeight:15
  519. })
  520. if(productList.length == 0){
  521. if(this.data.type == 'top'){
  522. this.scrollTop()
  523. }else if(this.data.type == 'bottom'){
  524. this.scrollBottom()
  525. }
  526. }
  527. this.addParmasProduct()
  528. }
  529. };
  530. http.request(params);
  531. },
  532. // 点击三级分类
  533. handleThrid(e){
  534. let {
  535. index,
  536. categoryid
  537. } = e.currentTarget.dataset
  538. console.log(33333333333,e.currentTarget.dataset);
  539. this.setData({
  540. thridIndex:index
  541. })
  542. if(index == -1){
  543. categoryid = this.data.subCategoryId
  544. }
  545. this.getAllProList(categoryid)
  546. },
  547. // 除了产品列表数据 给产品列表添加显示和数量 同步购物车数据
  548. addParmasProduct(){
  549. if(this.data.productList.length >0){
  550. let productList = JSON.parse(JSON.stringify( this.data.productList))
  551. productList.map(i=>{
  552. i.show = false
  553. this.data.carData.map(item=>{
  554. if(item.prodId == i.prodId){
  555. i.prodCount = item.prodCount
  556. i.show = true
  557. }
  558. })
  559. })
  560. this.setData({
  561. productList
  562. })
  563. }
  564. },
  565. // 获取购物车数据
  566. getCarData() {
  567. var params = {
  568. url: "/p/shopCart/info/1",
  569. method: "post",
  570. data: [{
  571. "basketId": 0,
  572. "discountId": 0,
  573. "shopId":this.data.testShopId||this.data.shopId
  574. }],
  575. callBack: (res) => {
  576. if(res.length){
  577. let carData = res[0].shopCartItemDiscounts[0].shopCartItems
  578. let img = ''
  579. carData.map(e => {
  580. img = e.pic.split(',')
  581. e.pic = img[0]
  582. })
  583. this.setData({
  584. carData
  585. },()=>{
  586. this.getCarTotal()
  587. })
  588. }else{
  589. this.setData({
  590. carData:[]
  591. },()=>{
  592. this.getCarTotal()
  593. })
  594. }
  595. // 给产品列表添加显示和数量 同步购物车数据
  596. this.addParmasProduct()
  597. }
  598. };
  599. http.request(params);
  600. },
  601. // 获取购物车数量和总价
  602. getCarTotal() {
  603. let basketIds = this.data.carData.map(item=>{
  604. return item.basketId
  605. })
  606. var params = {
  607. url: "/p/shopCart/totalPay?t="+new Date().getTime(),
  608. method: "post",
  609. data: basketIds,
  610. callBack: (res) => {
  611. this.setData({
  612. totalInfo:res
  613. })
  614. }
  615. };
  616. http.request(params);
  617. http.getCartCount(); //重新计算购物车总数量
  618. },
  619. // 添加修改购物车数量
  620. changeCar(data) {
  621. var params = {
  622. url: "/p/shopCart/changeItem?t="+new Date().getTime(),
  623. method: "post",
  624. data,
  625. callBack: (res) => {
  626. if(res.code == 500){
  627. wx.showToast({
  628. title: res.msg,
  629. icon:'none'
  630. })
  631. }else{
  632. this.getCarData()
  633. }
  634. }
  635. };
  636. http.request(params);
  637. },
  638. /**
  639. * 跳转到商品详情页
  640. */
  641. toProdPage: function (e) {
  642. var prodid = e.currentTarget.dataset.prodid;
  643. var shopid = e.currentTarget.dataset.shopid;
  644. if (prodid) {
  645. wx.navigateTo({
  646. url: `/pages/prod/prod?prodid=${prodid}&shopid=${shopid}`,
  647. })
  648. }
  649. },
  650. handleBuy(){
  651. if(this.data.carData.length == 0){
  652. return
  653. }
  654. let basketIds = this.data.carData.map(item=>{
  655. return item.basketId
  656. })
  657. wx.setStorageSync("basketIds", JSON.stringify(basketIds));
  658. wx.navigateTo({
  659. url: '/pages/submit-order/submit-order?orderEntry=0&shopId='+this.data.carData[0].shopId,
  660. })
  661. }
  662. })