category.js 14 KB

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