index.js 2.7 KB

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