index.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // pages/home/index.js
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. height: app.globalData.height * 2 + 25,
  9. windowHeight: app.globalData.windowHeight,
  10. navbarData: {
  11. name: '新闻',
  12. },
  13. logo: '',
  14. currentTab: 0,
  15. // 國内新聞
  16. oneList: [],
  17. twoList: [],
  18. },
  19. // 禁止左右滑动
  20. stopTab: function (e) {
  21. return false;
  22. },
  23. //点击切换
  24. clickTab: function (e) {
  25. var that = this;
  26. if (this.data.currentTab === e.target.dataset.current) {
  27. return false;
  28. } else {
  29. that.setData({
  30. currentTab: e.target.dataset.current,
  31. });
  32. }
  33. },
  34. detail: function (data) {
  35. wx.navigateTo({
  36. url: `/pages/news/detail?id=${data.detail.id}`,
  37. });
  38. },
  39. /**
  40. * 生命周期函数--监听页面加载
  41. */
  42. onLoad: async function (options) {
  43. this.searchST();
  44. const res = await app.$get('/news', { type: '国内新闻' });
  45. let data = this.dealImg(res.data);
  46. this.setData({ oneList: data });
  47. const res2 = await app.$get('/news', { type: '健康资讯' });
  48. let data2 = this.dealImg(res2.data);
  49. this.setData({ twoList: data2 });
  50. },
  51. dealImg(list) {
  52. for (let i of list) {
  53. if (i.img && i.img.length > 0 && i.img[0]) i.url = `${app.globalData.fileUrl}${i.img[0].url}`;
  54. else i.url = this.data.logo;
  55. }
  56. return list;
  57. },
  58. searchST: async function () {
  59. const res = await app.$get('/config');
  60. const logo = `${res.data.logo[0].url || ''}`;
  61. this.setData({ logo });
  62. },
  63. /**
  64. * 生命周期函数--监听页面初次渲染完成
  65. */
  66. onReady: function () {},
  67. /**
  68. * 生命周期函数--监听页面显示
  69. */
  70. onShow: function () {
  71. if (typeof this.getTabBar === 'function' && this.getTabBar()) {
  72. this.getTabBar().setData({
  73. selected: 2,
  74. });
  75. }
  76. },
  77. /**
  78. * 生命周期函数--监听页面隐藏
  79. */
  80. onHide: function () {},
  81. /**
  82. * 生命周期函数--监听页面卸载
  83. */
  84. onUnload: function () {},
  85. /**
  86. * 页面相关事件处理函数--监听用户下拉动作
  87. */
  88. onPullDownRefresh: function () {},
  89. /**
  90. * 页面上拉触底事件的处理函数
  91. */
  92. onReachBottom: function () {},
  93. /**
  94. * 用户点击右上角分享
  95. */
  96. onShareAppMessage: function () {},
  97. });