detail.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. console.log(D.content.match(/style=\"([a-z|A-Z|-|:|;|0-9]*)\"/g))
  28. D.content = D.content.replace(/<p/gi, '<p style="text-indent: 2em"').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%'")
  29. if (D.creat_time) D.creat_time = this.format(D.creat_time);
  30. _this.setData({
  31. detail: D
  32. })
  33. },
  34. /**
  35. * 生命周期函数--监听页面初次渲染完成
  36. */
  37. onReady: function () {
  38. },
  39. /**
  40. * 生命周期函数--监听页面显示
  41. */
  42. onShow: function () {
  43. },
  44. /**
  45. * 生命周期函数--监听页面隐藏
  46. */
  47. onHide: function () {
  48. },
  49. /**
  50. * 生命周期函数--监听页面卸载
  51. */
  52. onUnload: function () {
  53. },
  54. /**
  55. * 页面相关事件处理函数--监听用户下拉动作
  56. */
  57. onPullDownRefresh: function () {
  58. },
  59. /**
  60. * 页面上拉触底事件的处理函数
  61. */
  62. onReachBottom: function () {
  63. },
  64. /**
  65. * 用户点击右上角分享
  66. */
  67. onShareAppMessage: function () {
  68. },
  69. format: function (res) {
  70. if (!res) return "";
  71. let num = (res || 0) - 0;
  72. let T = new Date(num);
  73. let year = T.getFullYear();
  74. let month = T.getMonth() + 1;
  75. let day = T.getDate();
  76. let hour = T.getHours();
  77. let min = T.getMinutes() + 1;
  78. let sec = T.getSeconds();
  79. return year + "-" + (month > 9 ? month : "0" + month) + "-" + day + " " + (hour > 9 ? hour : '0' + hour) + ":" + (min > 9 ? min : '0' + min) + ":" + (sec > 9 ? sec : '0' + sec);
  80. },
  81. })