detail.js 2.5 KB

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