category.js 14 KB

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