category.js 16 KB

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