uc-parse.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <template>
  2. <view>
  3. <view >
  4. <rich-text :nodes="getnodes" :preview="true"></rich-text>
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. import htmlParser from '@/common/html-parser';
  10. export default {
  11. name: 'timuJiexi',
  12. props: {
  13. node: {
  14. type: String,
  15. default: '',
  16. },
  17. from:{
  18. type:Number,
  19. default:1
  20. },
  21. display_type:{
  22. type:Number,
  23. default:1
  24. }
  25. },
  26. data(){
  27. return{
  28. imageWidth:0,
  29. imageHeight:0,
  30. myNode:'',
  31. }
  32. },
  33. created() {
  34. },
  35. computed:{
  36. getnodes(){
  37. if(this.node != ''){
  38. this.myNode = this.node
  39. let nodes = [];
  40. try{
  41. nodes = htmlParser("<p>&nbsp;"+this.myNode.trim()+"&nbsp;</p>");
  42. }catch{
  43. return ''
  44. // 解析错误,请联系管理员!
  45. }
  46. return this.parseImgs(nodes) || ''
  47. }
  48. },
  49. },
  50. methods:{
  51. parseImgs(nodes) {
  52. nodes.forEach(node => {
  53. if (node.name === 'img') {
  54. node.attrs.style = `max-width:100%;vertical-align:middle;`
  55. // let width = '',that = this;
  56. // uni.getSystemInfo({
  57. // success: function (res) {
  58. // if(that.from == 1){
  59. // width = res.windowWidth - 20*2
  60. // }else{
  61. // if(that.display_type == 1){
  62. // width = res.windowWidth - (3+2)*2*0.01*res.windowWidth -30 -16
  63. // }else{
  64. // width = res.windowWidth*0.45 -30 -16
  65. // }
  66. // }
  67. // }
  68. // });
  69. // node.attrs.style = `width:${width}px; vertical-align:middle;`
  70. }else{
  71. node.style = `vertical-align:middle;`
  72. }
  73. // node.style = `vertical-align:middle;`
  74. if (Array.isArray(node.children)) {
  75. this.parseImgs(node.children);
  76. }
  77. });
  78. return nodes;
  79. }
  80. }
  81. }
  82. </script>
  83. <style>
  84. img {
  85. max-width: 100px;
  86. }
  87. </style>