index.vue 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <!-- index.wxml -->
  3. <view class="wrap">
  4. <view class="wrap_nr">
  5. <rich-text :nodes="htmlSnip"></rich-text>
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. // index.js
  11. let myPro = require('../../utils/wxRequest.js');
  12. let util = require('../../utils/util.js');
  13. export default {
  14. data() {
  15. return {
  16. id: '',
  17. htmlSnip: ''
  18. };
  19. },
  20. onLoad: function (options) {
  21. // Do some initialize when page load.
  22. let that = this;
  23. that.setData({
  24. id: options.id
  25. });
  26. },
  27. onShow: function () {
  28. // Do something when page show.
  29. let that = this;
  30. that.getContent();
  31. },
  32. onReady: function () {
  33. // Do something when page ready.
  34. },
  35. onHide: function () {
  36. // Do something when page hide.
  37. },
  38. onUnload: function () {
  39. // Do something when page close.
  40. },
  41. onPullDownRefresh: function () {
  42. // Do something when pull down.
  43. },
  44. onReachBottom: function () {
  45. // Do something when page reach bottom.
  46. },
  47. onPageScroll: function () {
  48. // Do something when page scroll
  49. },
  50. onResize: function () {
  51. // Do something when page resize
  52. },
  53. methods: {
  54. // 获取单页内容
  55. getContent() {
  56. let that = this;
  57. let params = {
  58. id: that.id
  59. };
  60. myPro
  61. .wxRequest('user/v3/article/detail', 'GET', params)
  62. .then((res) => {
  63. let contents = res.result.content; // 小程序内置,转富文本标签,仅对rich-text 中的 class 生效
  64. contents = contents.replace(/\<img /g, '<img class="richimg" '); // contents = contents.replace(/\<img src\=\"\/upload/g, '<img class="richimg" src="'+ that.data.domain +'upload');
  65. contents = contents.replace(/\<p/g, '<p class="richtxt" ');
  66. contents = contents.replace(/\<br\>/g, '');
  67. that.setData({
  68. htmlSnip: contents
  69. });
  70. })
  71. .catch((err) => {
  72. console.log('报错信息', err);
  73. uni.showToast({
  74. title: err,
  75. icon: 'none'
  76. });
  77. });
  78. }
  79. }
  80. };
  81. </script>
  82. <style>
  83. /**index.wxss**/
  84. .richimg {
  85. width: 100% !important;
  86. height: auto !important;
  87. }
  88. .wrap_nr {
  89. padding: 30rpx;
  90. }
  91. </style>