index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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: true,
  17. type: 1
  18. }).get();
  19. let video = await db.collection("data_asset").where({
  20. type: "video"
  21. }).get();
  22. console.log(list)
  23. list.data = list.data.concat(video.data);
  24. list.data = list.data.sort((a,b)=>{
  25. if(a._createTime) a.create_time = a._createTime;
  26. if(b._createTime) b.create_time = b._createTime;
  27. return b.create_time - a.create_time
  28. })
  29. console.log(list)
  30. for (let i = 0; i < list.data.length; i++) {
  31. const v = list.data[i];
  32. v.create_time = this.format(v.create_time || 0)
  33. }
  34. this.setData({
  35. newList: list.data || []
  36. })
  37. },
  38. /**
  39. * 生命周期函数--监听页面初次渲染完成
  40. */
  41. onReady: function () {
  42. },
  43. /**
  44. * 生命周期函数--监听页面显示
  45. */
  46. onShow: function () {
  47. },
  48. /**
  49. * 生命周期函数--监听页面隐藏
  50. */
  51. onHide: function () {
  52. },
  53. /**
  54. * 生命周期函数--监听页面卸载
  55. */
  56. onUnload: function () {
  57. },
  58. /**
  59. * 页面相关事件处理函数--监听用户下拉动作
  60. */
  61. onPullDownRefresh: function () {
  62. },
  63. /**
  64. * 页面上拉触底事件的处理函数
  65. */
  66. onReachBottom: function () {
  67. },
  68. /**
  69. * 用户点击右上角分享
  70. */
  71. onShareAppMessage: function () {
  72. },
  73. play: function (e) {
  74. var that = this;
  75. var id = e.currentTarget.id;
  76. for (var i = 0; i < that.data.newList.length; i++) {
  77. if (id !== 'newsVideo' + i && that.data.newList[i].type == 'video') {
  78. //console.log('暂停其他正在播放的视频');
  79. var videoContext = wx.createVideoContext("newsVideo" + i, that);
  80. videoContext.pause();
  81. }else{
  82. continue
  83. }
  84. }
  85. },
  86. format: function (res) {
  87. if (!res) return ""
  88. let T = new Date(res || 0);
  89. let year = T.getFullYear();
  90. let month = T.getMonth() + 1;
  91. let day = T.getDate();
  92. let hour = T.getHours();
  93. let min = T.getMinutes() + 1;
  94. let sec = T.getSeconds();
  95. return year + "-" + (month > 9 ? month : "0" + month) + "-" + (day > 9 ? day : "0" + day) + " " + (hour > 9 ? hour : '0' + hour) + ":" + (min > 9 ? min : '0' + min) + ":" + (sec > 9 ? sec : '0' + sec);
  96. },
  97. toDetail: function (e) {
  98. let title = e.currentTarget.dataset.title, id = e.currentTarget.dataset.id;
  99. wx.navigateTo({
  100. url: '/pages/detail/detail?title=' + title + "&id=" + id,
  101. })
  102. },
  103. toMarvellous: function (e) {
  104. wx.navigateTo({
  105. url: '/pages/marvellous/index?title=' + e.currentTarget.dataset.title + "&type=" + e.currentTarget.dataset.type,
  106. })
  107. }
  108. })