category.js 14 KB

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