home.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. const pages = require('../../mixin/pages')
  2. const { getProductCategoryList } = require('../../api/common')
  3. const { getLunboList } = require('./api/index')
  4. const app = getApp()
  5. const tmplIds = '1kIBK-L1ksxYCxY7Pmezhp4FSS-BOqiIAl6K7PAGvDE'
  6. let leftHeight = 0
  7. let rightHeight = 0
  8. let query = wx.createSelectorQuery()
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. ...pages.data(),
  15. listUrl: '/api/user/home/product/list',
  16. searchForm: {
  17. key_words: '',
  18. category_id: ''
  19. },
  20. txtlist: [],
  21. headerNav: [
  22. {
  23. icon: 'partner@2x.png',
  24. name: '供应商',
  25. path: 'business'
  26. },
  27. {
  28. icon: 'guide@2x.png',
  29. name: '选购指南',
  30. path: 'guide'
  31. },
  32. {
  33. icon: 'collection@2x.png',
  34. name: '收藏',
  35. path: 'collection'
  36. },
  37. {
  38. icon: 'news.png',
  39. name: '农事天地',
  40. path: 'news'
  41. }
  42. ],
  43. categoryList: [],
  44. leftList: [],
  45. rightList: []
  46. },
  47. ...pages.methods,
  48. /**
  49. * 生命周期函数--监听页面加载
  50. */
  51. async onLoad(options) {
  52. this.fetchLunboList()
  53. await this.fetchProductCategoryList()
  54. if (this.data.searchForm.category_id) {
  55. this.fetchOrderList()
  56. }
  57. },
  58. /**
  59. * 生命周期函数--监听页面初次渲染完成
  60. */
  61. onReady() {
  62. },
  63. /**
  64. * 生命周期函数--监听页面显示
  65. */
  66. onShow() {
  67. this.getTabBar().init()
  68. },
  69. /**
  70. * 生命周期函数--监听页面隐藏
  71. */
  72. onHide() {
  73. },
  74. /**
  75. * 生命周期函数--监听页面卸载
  76. */
  77. onUnload() {
  78. },
  79. /**
  80. * 页面相关事件处理函数--监听用户下拉动作
  81. */
  82. onPullDownRefresh() {
  83. if (this.data.freshing) {
  84. return
  85. }
  86. this.setData({
  87. freshing: true
  88. })
  89. this.bindCallBack()
  90. },
  91. /**
  92. * 页面上拉触底事件的处理函数
  93. */
  94. onReachBottom() {
  95. this.fetchOrderList()
  96. },
  97. /**
  98. * 用户点击右上角分享
  99. */
  100. onShareAppMessage() {
  101. },
  102. bindCallBack() {
  103. this.refreshOrderList()
  104. this.fetchLunboList()
  105. },
  106. async fetchLunboList() {
  107. try {
  108. const { status, data, msg } = await getLunboList()
  109. if (status) {
  110. const { lunbos } = data
  111. this.setData({
  112. lunbos: lunbos
  113. })
  114. } else {
  115. wx.showToast({
  116. title: msg,
  117. icon: 'none'
  118. })
  119. }
  120. } catch (err) {}
  121. },
  122. async fetchProductCategoryList() {
  123. try {
  124. const { status, data, msg } = await getProductCategoryList()
  125. if (status && Array.isArray(data) && data.length > 0) {
  126. const temp = data.map(item => {
  127. return {
  128. ...item,
  129. text: item.category_name
  130. }
  131. })
  132. temp.unshift({
  133. category_img_url: '',
  134. category_name: '全部',
  135. id: 0
  136. })
  137. this.setData({
  138. categoryList: temp,
  139. 'searchForm.category_id': temp.length > 0 ? temp[0].id : ''
  140. })
  141. } else {
  142. wx.showToast({
  143. title: msg,
  144. icon: 'none'
  145. })
  146. }
  147. } catch (err) {}
  148. },
  149. handleTab(event) {
  150. const { name } = event.detail
  151. this.setData({
  152. 'searchForm.category_id': name
  153. }, () => {
  154. this.refreshOrderList()
  155. })
  156. },
  157. async isLeft(list) {
  158. const {
  159. leftList,
  160. rightList
  161. } = this.data
  162. for (const item of list) {
  163. if (leftHeight < rightHeight) {
  164. leftList.push(item)
  165. } else if (leftHeight === rightHeight) {
  166. leftList.length <= rightList ? leftList.push(item) : rightList.push(item)
  167. } else {
  168. rightList.push(item)
  169. }
  170. await this.getBoxHeight(leftList, rightList)
  171. }
  172. },
  173. getBoxHeight(leftList, rightList) {
  174. return new Promise((resolve, reject) => {
  175. this.setData({
  176. leftList,
  177. rightList
  178. }, () => {
  179. query.select('.waterfall-left').boundingClientRect()
  180. query.select('.waterfall-right').boundingClientRect()
  181. query.exec((res) => {
  182. leftHeight = res[0].height
  183. rightHeight = res[1].height
  184. resolve()
  185. })
  186. })
  187. })
  188. },
  189. jumpLeavingAMessage() {
  190. wx.navigateTo({
  191. url: '/pages/leavingAMessage/leavingAMessage'
  192. })
  193. },
  194. handleSwiperClick(e) {
  195. const isTab = app.globalData.tabBarList.map(item => item.pagePath)
  196. const { item } = e.currentTarget.dataset
  197. if (Object.prototype.toString.call(item) === '[object Object]') {
  198. const { lunbo_link_url } = item
  199. if (/^http/.test(lunbo_link_url)) {
  200. wx.navigateTo({
  201. url: '/pages/h5/h5?url=' + lunbo_link_url
  202. })
  203. } else if (/^\/pages/.test(lunbo_link_url)) {
  204. if (isTab.findIndex(item => lunbo_link_url.indexOf(item) > -1) > -1) {
  205. wx.switchTab({
  206. url: lunbo_link_url
  207. })
  208. } else {
  209. wx.navigateTo({
  210. url: lunbo_link_url
  211. })
  212. }
  213. }
  214. }
  215. },
  216. handleNav(item) {
  217. if (Object.prototype.toString.call(item) === '[object Object]' && item.path) {
  218. const path = item.path
  219. if (path === 'news' || path === 'partner') {
  220. wx.switchTab({
  221. url: `/pages/${path}/${path}`
  222. })
  223. return
  224. }
  225. wx.navigateTo({
  226. url: `/pages/${path}/${path}`
  227. })
  228. }
  229. },
  230. openMarketing(e) {
  231. const { page } = e.currentTarget.dataset
  232. wx.navigateTo({
  233. url: `/pages/${page}/${page}`
  234. })
  235. },
  236. async handleSubscribeMessage(e) {
  237. const { item } = e.currentTarget.dataset
  238. const that = this
  239. try {
  240. const { errMsg, subscriptionsSetting } = await wx.getSetting({ withSubscriptions: true })
  241. if (errMsg === 'getSetting:ok') {
  242. // 用户打开了订阅消息总开关
  243. if (subscriptionsSetting.mainSwitch) {
  244. // 用户同意总是保持是否推送消息的选择, 这里表示以后不会再拉起推送消息的授权
  245. if (subscriptionsSetting.itemSettings != null) {
  246. const moIdState = subscriptionsSetting.itemSettings[tmplIds]
  247. if (moIdState === 'accept') {
  248. // 接受了消息推送
  249. } else if (moIdState === 'reject') {
  250. // 拒绝消息推送
  251. } else if (moIdState === 'ban') {
  252. // 已被后台封禁
  253. }
  254. that.handleNav(item)
  255. } else {
  256. // 当用户没有点击 ’总是保持以上选择,不再询问‘ 按钮。那每次执到这都会拉起授权弹窗
  257. wx.showModal({
  258. title: '提示',
  259. content: '请授权开通服务通知',
  260. showCancel: true,
  261. success: function (res) {
  262. if (res.confirm) {
  263. wx.requestSubscribeMessage({
  264. tmplIds: [tmplIds],
  265. success(res) {
  266. that.handleNav(item)
  267. },
  268. fail(err) {
  269. that.handleNav(item)
  270. }
  271. })
  272. }
  273. }
  274. })
  275. }
  276. } else {
  277. // 订阅消息未开启
  278. }
  279. }
  280. } catch (err) {}
  281. }
  282. })