<template>
  <!-- 公告详情 -->
  <view class="place-desc">
    <!--  <rich-text :nodes="nodeHtml"></rich-text> -->
    <mp-html ref="articleRef" :content="nodeHtml" />
  </view>
</template>
<script>
const app = getApp();
import { getPlaceCultureByName } from "@/api/place";
import { httpApi } from "@/common/js/baseUrl";
export default {
  name: "placeDesc",
  data() {
    return {
      nodeHtml: "",
      areaCode: "",
    };
  },
  onLoad(options) {
    app.globalData.showPlaceCulture = true;
    this.street = options.street;
    this.community = options.community;
    this.from = options.from;
    this.getHtmlText();
  },
  methods: {
    async getHtmlTextStreet(areaName) {
      const res = await getPlaceCultureByName({
        areaName,
        current: 1,
        size: 1,
      });
      if (!res.data[0]) {
        uni.showModal({
          title: "提醒",
          content: "暂未编辑，快去催促小编",
          showCancel: false,
          success: () => {
            uni.navigateBack();
          },
        });
      } else {
        let html = this.replaceSrc(res.data[0].text);
        this.nodeHtml = html;
      }
    },
    async getHtmlText() {
      if (this.from === "home" && this.community) {
        const res = await getPlaceCultureByName({
          areaName: this.community,
          current: 1,
          size: 1,
        });
        if (!res.data[0]) {
          this.getHtmlTextStreet(this.street);
        } else {
          let html = this.replaceSrc(res.data[0].text);
          this.nodeHtml = html;
        }
      } else if (this.from === "home" && !this.community) {
        this.getHtmlTextStreet(this.street);
      } else {
        const areaName = this.community || this.street;
        this.getHtmlTextStreet(areaName);
      }
    },
    replaceSrc(str) {
      const imgReg = /<img.*?(?:>|\/>)/gi;
      const srcReg = /src=[\'\"]?([^\'\"]*)[\'\"]?/gi;
      return str.replace(imgReg, (img) => {
        // 在这里对图片路径进行加工
        return img.replace(srcReg, (srcMatch, src) => {
          return `src="https://zlszmp.fh.gov.cn:8088/digital-doorplate/file/download?${src.split("?")[1]}"`;
        });
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.place-desc {
  padding: 0 14px;
  ::v-deep table {
    border-top: 1px solid #ccc;
    border-left: 1px solid #ccc;
    border-spacing: 0px;
  }
  ::v-deep table td,
  ::v-deep table th {
    border-bottom: 1px solid #ccc;
    border-right: 1px solid #ccc;
    padding: 3px 5px;
  }
  ::v-deep table th {
    border-bottom: 2px solid #ccc;
    text-align: center;
  }
}
</style>
