category.js 14 KB

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