category.js 15 KB

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