category.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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. url: "/category/listCategoryForUser",
  233. method: "GET",
  234. data:{
  235. shopId:this.data.testShopId||shopid,
  236. channelId:wx.getStorageSync('channelId')||3,
  237. },
  238. callBack: function (res) {
  239. ths.setData({
  240. categoryList: res,
  241. subCategoryList: res[ths.data.selIndex].children
  242. });
  243. let categoryId =wx.getStorageSync('categoryId')
  244. if(categoryId){
  245. that.data.categoryList.map((item,index)=>{
  246. if(item.categoryId == categoryId){
  247. that.setData({
  248. selCategory:categoryId,
  249. selIndex:index,
  250. subCategoryList:res[index].children
  251. })
  252. }
  253. })
  254. wx.removeStorageSync('categoryId')
  255. }
  256. // eventBus.on('categoryId', (data) => {
  257. // eventBus.clear()
  258. // that.data.categoryList.map((item,index)=>{
  259. // if(item.categoryId == data){
  260. // that.setData({
  261. // selCategory:data,
  262. // selIndex:index,
  263. // subCategoryList:res[index].children
  264. // })
  265. // }
  266. // })
  267. // });
  268. ths.getData(ths.data.subCategoryList[0])
  269. }
  270. };
  271. http.request(params);
  272. },
  273. getData(data){
  274. console.log(data);
  275. if(!data){
  276. return this.setData({
  277. thridCategoryList:[],
  278. showLoading:false
  279. })
  280. }
  281. this.setData({
  282. thridCategoryList:[],
  283. showLoading:true
  284. })
  285. // 二级分类有商品
  286. if(data.productBeBound == 1){
  287. this.getAllProList(data.categoryId)
  288. // this.getProdList(data.categoryId)
  289. }else{//二级分类有三级分类
  290. this.getAllProList(data.categoryId)
  291. this.setData({
  292. thridCategoryList:data.children,
  293. subCategoryId:data.categoryId
  294. })
  295. }
  296. },
  297. /**
  298. * 选择定位后更新列表
  299. */
  300. go_update() {
  301. this.get_neighborShop()
  302. },
  303. /**
  304. * 生命周期函数--监听页面初次渲染完成
  305. */
  306. onReady: function () {
  307. },
  308. /**
  309. * 生命周期函数--监听页面显示
  310. */
  311. onShow: function () {
  312. console.log('show');
  313. if(this.data.shopId){
  314. this.getCarData(this.data.shopId)
  315. }
  316. // if (getApp().globalData.categoryId) {
  317. // let categoryId = getApp().globalData.categoryId
  318. // let index = getApp().globalData.index
  319. // let pic = getApp().globalData.pic
  320. // this.setData({
  321. // categoryImg: pic,
  322. // selIndex: index
  323. // });
  324. // }else{
  325. // this.get_neighborShop()
  326. // }
  327. // 之前逻辑 每次重新加载
  328. // this.resetData()
  329. // this.get_neighborShop()
  330. },
  331. /**
  332. * 生命周期函数--监听页面隐藏
  333. */
  334. onHide: function () {},
  335. /**
  336. * 生命周期函数--监听页面卸载
  337. */
  338. onUnload: function () {
  339. },
  340. /**
  341. * 页面相关事件处理函数--监听用户下拉动作
  342. */
  343. onPullDownRefresh: function () {
  344. },
  345. /**
  346. * 页面上拉触底事件的处理函数
  347. */
  348. onReachBottom: function () {
  349. },
  350. /**
  351. * 用户点击右上角分享
  352. */
  353. onShareAppMessage: function () {
  354. },
  355. /**
  356. * 分类点击事件,获取子分类
  357. */
  358. onMenuTab: function (e) {
  359. var id = e.currentTarget.dataset.id;
  360. var index = e.currentTarget.dataset.index;
  361. // if(this.data.categoryList[index].children.length == 0){
  362. // return
  363. // }
  364. this.setData({
  365. show:false,
  366. categoryImg: this.data.categoryList[index].pic,
  367. subCategoryList:this.data.categoryList[index].children,
  368. selIndex: index,
  369. selCategory:id,
  370. productList:[],
  371. subIndex:0,
  372. paramsObj:{sort:0,orderBy:0},
  373. sort:0
  374. });
  375. // this.getProdList(this.data.subCategoryList[0].categoryId)
  376. this.getData(this.data.subCategoryList[0])
  377. getApp().globalData.categoryId = ''
  378. getApp().globalData.index = ''
  379. getApp().globalData.pic = ''
  380. },
  381. /**
  382. * 推荐词,type:1关键词,2热门搜索词,3推荐搜索词
  383. */
  384. getRecommended: function () {
  385. var params = {
  386. url: "/keyword/list",
  387. method: "GET",
  388. data: {
  389. type: 2
  390. },
  391. callBack: (res) => {
  392. let reslut = res.filter(item => item.status !== 2) //过滤下线词
  393. let timeReslut = reslut.filter(e => util.dateToTimestamp(e.effectiveTime) < new Date().getTime()) //过滤未生效词
  394. this.setData({
  395. hotList: timeReslut
  396. })
  397. }
  398. };
  399. http.request(params);
  400. },
  401. /**
  402. * 推荐词搜索
  403. */
  404. topSwiperChange: function (e) {
  405. this.setData({
  406. topCurrentIndex: e.detail.current // 获取当前滚动到的swiper-item的索引并更新到data中
  407. })
  408. },
  409. topHotSearch: function () {
  410. const topname = this.data.hotList[this.data.topCurrentIndex].name
  411. wx.navigateTo({
  412. url: `/pages/search-prod-show/search-prod-show?prodName=${topname}&shopId=${this.data.shopId}`,
  413. })
  414. },
  415. // 跳转搜索页
  416. toSearchPage: function () {
  417. wx.navigateTo({
  418. url: `/pages/search-page/search-page?shopId=${this.data.shopId}`,
  419. })
  420. },
  421. /**
  422. * 跳转到定位页面
  423. */
  424. toLocationPage: function () {
  425. wx.navigateTo({
  426. url: '/pages/locationAdd/locationAdd',
  427. })
  428. },
  429. // 点击二级分类
  430. onleftMenuTab(e){
  431. const {
  432. index,
  433. id
  434. } = e.currentTarget.dataset
  435. if(id){
  436. this.setData({
  437. type:'click'
  438. })
  439. console.log('click');
  440. }
  441. this.setData({
  442. subIndex:index,
  443. paramsObj:{sort:0,orderBy:0},
  444. sort:0
  445. })
  446. // this.getProdList(this.data.subCategoryList[index].categoryId)
  447. this.getData(this.data.subCategoryList[index])
  448. },
  449. // 根据分类id获取商品数据
  450. getProdList(categoryId) {
  451. var params = {
  452. url: "/search/searchProdPage",
  453. method: "GET",
  454. data: {
  455. categoryId,
  456. current: 1,
  457. size: 999999,
  458. orderBy:this.data.paramsObj.orderBy,
  459. sort:this.data.paramsObj.sort,
  460. isAllProdType: true,
  461. channelId:wx.getStorageSync('channelId')||3,
  462. // lat: wx.getStorageSync('LATITUDE'),
  463. // lon: wx.getStorageSync('LONGITUDE'),
  464. // distance: wx.getStorageSync('DISTANCE') || 0
  465. },
  466. callBack: (res) => {
  467. setTimeout(() => {
  468. this.setData({
  469. showLoading:false
  470. })
  471. }, 500);
  472. console.log(res);
  473. let productList = res.records
  474. let img = ''
  475. productList.map(e => {
  476. img = e.pic.split(',')
  477. e.pic = img[0]
  478. })
  479. this.setData({
  480. productList,
  481. topHeight:15
  482. })
  483. if(productList.length == 0){
  484. if(this.data.type == 'top'){
  485. this.scrollTop()
  486. }else{
  487. this.scrollBottom()
  488. }
  489. }
  490. this.addParmasProduct()
  491. }
  492. };
  493. http.request(params);
  494. },
  495. // 根据分类id获取所有三级商品数据
  496. getAllProList(categoryId) {
  497. var params = {
  498. url: "/prod/listProdByCategoryIdAndShopId",
  499. method: "post",
  500. data: {
  501. orderBy:this.data.paramsObj.orderBy,
  502. sort:this.data.paramsObj.sort,
  503. categoryId,
  504. current: 1,
  505. size: 999999,
  506. channelId:wx.getStorageSync('channelId')||3,
  507. shopId:this.data.testShopId||this.data.shopId
  508. },
  509. callBack: (res) => {
  510. setTimeout(() => {
  511. this.setData({
  512. showLoading:false
  513. })
  514. }, 500);
  515. let productList = res.records
  516. let img = ''
  517. productList.map(e => {
  518. img = e.pic.split(',')
  519. e.pic = img[0]
  520. })
  521. this.setData({
  522. productList,
  523. topHeight:15
  524. })
  525. if(productList.length == 0){
  526. if(this.data.type == 'top'){
  527. this.scrollTop()
  528. }else if(this.data.type == 'bottom'){
  529. this.scrollBottom()
  530. }
  531. }
  532. this.addParmasProduct()
  533. }
  534. };
  535. http.request(params);
  536. },
  537. // 点击三级分类
  538. handleThrid(e){
  539. let {
  540. index,
  541. categoryid
  542. } = e.currentTarget.dataset
  543. console.log(33333333333,e.currentTarget.dataset);
  544. this.setData({
  545. thridIndex:index
  546. })
  547. if(index == -1){
  548. categoryid = this.data.subCategoryId
  549. }
  550. this.getAllProList(categoryid)
  551. },
  552. // 除了产品列表数据 给产品列表添加显示和数量 同步购物车数据
  553. addParmasProduct(){
  554. if(this.data.productList.length >0){
  555. let productList = JSON.parse(JSON.stringify( this.data.productList))
  556. productList.map(i=>{
  557. i.show = false
  558. this.data.carData.map(item=>{
  559. if(item.prodId == i.prodId){
  560. i.prodCount = item.prodCount
  561. i.show = true
  562. }
  563. })
  564. })
  565. this.setData({
  566. productList
  567. })
  568. }
  569. },
  570. // 获取购物车数据
  571. getCarData() {
  572. var params = {
  573. url: "/p/shopCart/info/1",
  574. method: "post",
  575. data: [{
  576. "basketId": 0,
  577. "discountId": 0,
  578. "shopId":this.data.testShopId||this.data.shopId
  579. }],
  580. callBack: (res) => {
  581. if(res.length){
  582. let carData = res[0].shopCartItemDiscounts[0].shopCartItems
  583. let img = ''
  584. carData.map(e => {
  585. img = e.pic.split(',')
  586. e.pic = img[0]
  587. })
  588. this.setData({
  589. carData
  590. },()=>{
  591. this.getCarTotal()
  592. })
  593. }else{
  594. this.setData({
  595. carData:[]
  596. },()=>{
  597. this.getCarTotal()
  598. })
  599. }
  600. // 给产品列表添加显示和数量 同步购物车数据
  601. this.addParmasProduct()
  602. }
  603. };
  604. http.request(params);
  605. },
  606. // 获取购物车数量和总价
  607. getCarTotal() {
  608. let basketIds = this.data.carData.map(item=>{
  609. return item.basketId
  610. })
  611. var params = {
  612. url: "/p/shopCart/totalPay?t="+new Date().getTime(),
  613. method: "post",
  614. data: basketIds,
  615. callBack: (res) => {
  616. this.setData({
  617. totalInfo:res
  618. })
  619. }
  620. };
  621. http.request(params);
  622. http.getCartCount(); //重新计算购物车总数量
  623. },
  624. // 添加修改购物车数量
  625. changeCar(data) {
  626. var params = {
  627. url: "/p/shopCart/changeItem?t="+new Date().getTime(),
  628. method: "post",
  629. data,
  630. callBack: (res) => {
  631. if(res.code == 500){
  632. wx.showToast({
  633. title: res.msg,
  634. icon:'none'
  635. })
  636. }else{
  637. this.getCarData()
  638. }
  639. }
  640. };
  641. http.request(params);
  642. },
  643. /**
  644. * 跳转到商品详情页
  645. */
  646. toProdPage: function (e) {
  647. var prodid = e.currentTarget.dataset.prodid;
  648. var shopid = e.currentTarget.dataset.shopid;
  649. if (prodid) {
  650. wx.navigateTo({
  651. url: `/pages/prod/prod?prodid=${prodid}&shopid=${shopid}`,
  652. })
  653. }
  654. },
  655. handleBuy(){
  656. if(this.data.carData.length == 0){
  657. return
  658. }
  659. let basketIds = this.data.carData.map(item=>{
  660. return item.basketId
  661. })
  662. wx.setStorageSync("basketIds", JSON.stringify(basketIds));
  663. wx.navigateTo({
  664. url: '/pages/submit-order/submit-order?orderEntry=0&shopId='+this.data.carData[0].shopId,
  665. })
  666. }
  667. })