index.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // miniprogram/pages/home/index.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. newList: []
  8. },
  9. /**
  10. * 生命周期函数--监听页面加载
  11. */
  12. onLoad: async function (options) {
  13. const db = wx.cloud.database();
  14. const _ = db.command;
  15. let list = await db.collection('data_news').where({
  16. hot: 1,
  17. type: 1
  18. }).get();
  19. for (let i = 0; i < list.data.length; i++) {
  20. const v = list.data[i];
  21. v.creat_time = this.format(v.creat_time || 0)
  22. }
  23. this.setData({
  24. newList: list.data || []
  25. })
  26. },
  27. /**
  28. * 生命周期函数--监听页面初次渲染完成
  29. */
  30. onReady: function () {
  31. },
  32. /**
  33. * 生命周期函数--监听页面显示
  34. */
  35. onShow: function () {
  36. },
  37. /**
  38. * 生命周期函数--监听页面隐藏
  39. */
  40. onHide: function () {
  41. },
  42. /**
  43. * 生命周期函数--监听页面卸载
  44. */
  45. onUnload: function () {
  46. },
  47. /**
  48. * 页面相关事件处理函数--监听用户下拉动作
  49. */
  50. onPullDownRefresh: function () {
  51. },
  52. /**
  53. * 页面上拉触底事件的处理函数
  54. */
  55. onReachBottom: function () {
  56. },
  57. /**
  58. * 用户点击右上角分享
  59. */
  60. onShareAppMessage: function () {
  61. },
  62. format: function (res) {
  63. if (!res) return ""
  64. let T = new Date(res || 0);
  65. let year = T.getFullYear();
  66. let month = T.getMonth() + 1;
  67. let day = T.getDate();
  68. let hour = T.getHours();
  69. let min = T.getMinutes() + 1;
  70. let sec = T.getSeconds();
  71. return year + "-" + (month > 9 ? month : "0" + month) + "-" + day + " " + (hour > 9 ? hour : '0' + hour) + ":" + (min > 9 ? min : '0' + min) + ":" + (sec > 9 ? sec : '0' + sec);
  72. },
  73. toDetail: function (e) {
  74. let title = e.currentTarget.dataset.title, id = e.currentTarget.dataset.id;
  75. wx.navigateTo({
  76. url: '/pages/detail/detail?title=' + title + "&id=" + id,
  77. })
  78. },
  79. toMarvellous: function (e) {
  80. wx.navigateTo({
  81. url: '/pages/marvellous/index?title=' + e.currentTarget.dataset.title + "&type=" + e.currentTarget.dataset.type,
  82. })
  83. }
  84. })