category.js 14 KB

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