index.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import GoodApi from '../../../services/good'
  2. import { share } from '../../../utils/http'
  3. import Navi from '../../../utils/navi'
  4. const app = getApp<IAppOption>()
  5. // pages/goods/category/index.ts
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. pageTitle: '栏目',
  12. data: [] as any[],
  13. scrollMaxHeight: 0,
  14. active: 0,
  15. advs: [],
  16. albums: [],
  17. columnSize: '',
  18. loading: true,
  19. isText: false,
  20. },
  21. /**
  22. * 生命周期函数--监听页面加载
  23. */
  24. onLoad() {
  25. GoodApi.GetCateList().then((rsp) => {
  26. if (rsp.result && rsp.result.length > 0) {
  27. const nowid = app.globalData.showcateid
  28. let idx = this.data.active
  29. if (Number(nowid) > 0) {
  30. idx = rsp.result.findIndex((v) => v.value === nowid)
  31. }
  32. const tab = rsp.result[idx]
  33. this.setData({
  34. active: idx,
  35. data: rsp.result,
  36. isText: tab.isText,
  37. advs: tab.advs || [],
  38. albums: tab.children || [],
  39. columnSize: tab.columns,
  40. loading: false,
  41. })
  42. }
  43. })
  44. wx.createSelectorQuery()
  45. .in(this)
  46. .select('.scroll-wrap')
  47. .boundingClientRect((rect) => {
  48. this.setData({
  49. scrollMaxHeight: rect.height - 48,
  50. })
  51. })
  52. .exec()
  53. },
  54. /**
  55. * 生命周期函数--监听页面初次渲染完成
  56. */
  57. onReady() {},
  58. /**
  59. * 生命周期函数--监听页面显示
  60. */
  61. onShow() {
  62. this.getTabBar().init()
  63. const nowid = app.globalData.showcateid
  64. if (Number(nowid) > 0 && this.data.data.length > 0) {
  65. const idx = this.data.data.findIndex((v) => v.value === nowid)
  66. this.onSidebarChange({
  67. detail: {
  68. current: idx,
  69. },
  70. } as any)
  71. }
  72. },
  73. /**
  74. * 生命周期函数--监听页面隐藏
  75. */
  76. onHide() {},
  77. /**
  78. * 生命周期函数--监听页面卸载
  79. */
  80. onUnload() {},
  81. /**
  82. * 页面相关事件处理函数--监听用户下拉动作
  83. */
  84. onPullDownRefresh() {},
  85. /**
  86. * 页面上拉触底事件的处理函数
  87. */
  88. onReachBottom() {},
  89. /**
  90. * 用户点击右上角分享
  91. */
  92. onShareAppMessage() {
  93. let pages = getCurrentPages(); //获取所有页面栈实例列表
  94. let nowPage = pages[pages.length - 1]; //当前页页面实例
  95. return {
  96. title: share.title,
  97. path: `/${nowPage.route}`,
  98. imageUrl: share.imageUrl,
  99. success(res) {
  100. console.log('success(res)==', res);
  101. },
  102. fail(res) {
  103. console.log('fail(res)==', res);
  104. }
  105. }
  106. },
  107. onGoBack() {
  108. Navi.switchTab({
  109. url: '/pages/index/index',
  110. })
  111. },
  112. onGoSearch() {
  113. Navi.navigateTo({
  114. url: '/pages/search/search?from=cate',
  115. })
  116. },
  117. onSidebarChange(e: SidebarChangeEvent) {
  118. console.log(e.detail.current, this.data.data)
  119. const tab = this.data.data[e.detail.current]
  120. this.setData({
  121. active: e.detail.current,
  122. advs: tab.advs || [],
  123. albums: tab.children || [],
  124. columnSize: tab.columns,
  125. isText: tab.isText,
  126. })
  127. console.log(
  128. `repeat(${tab.columns}, calc(${100 / tab.columns}%${tab.columns > 1 ? '- 15rpx' : ''}))`,
  129. )
  130. },
  131. gotoDetail(e: WechatMiniprogram.CustomEvent<{}, {}, { value: number }>) {
  132. const cateid = e.target.dataset.value
  133. Navi.navigateTo({
  134. url: `/pages/goods/list/index?cate=${cateid}`,
  135. })
  136. },
  137. gotoAdvtag(e: WechatMiniprogram.CustomEvent<{}, {}, { tagid: string }>) {
  138. const { tagid } = e.currentTarget.dataset
  139. if (Number(tagid) > 0) {
  140. Navi.navigateTo({
  141. url: '/pages/advtag/index?tag=' + tagid,
  142. })
  143. }
  144. },
  145. })