category.js 17 KB

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