detail.js 2.0 KB

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