<template>
  <view>
    <artical-deatil :model="model"></artical-deatil>
  </view>
</template>

<script>
import md5 from "@/common/md5.js";
import ArticalDeatil from "../../components/artical-deatil/index";
export default {
  data() {
    return {
      model: {
        title: "",
        way: "",
        time: "",
        artical: "",
        id: "",
      },
    };
  },
  components: {
    ArticalDeatil,
  },
  onLoad(op) {
    this.model.id = op.id;
    this.getNotice();
  },
  methods: {
    getNotice() {
      let md5Sign = md5(
        "method=" +
          "common" +
          "&timestamp=" +
          getApp().globalData.globalTimestamp +
          "&secret=" +
          getApp().globalData.secret
      );
      let url =
        getApp().globalData.shareUrl +
        "api/api.php" +
        "?method=common&source=notice&action=info_by_id&timestamp=" +
        getApp().globalData.globalTimestamp +
        "&sign=" +
        md5Sign;
      let postData = {
        id: this.model.id,
      };
      //获取文章
      uni.request({
        url: url,
        method: "POST",
        header: {
          "content-type": "application/x-www-form-urlencoded",
        },
        data: postData,
        success: (res) => {
          if (res.data.code === 200) {
            let data = res.data.data;
            this.model.title = data.title;
            this.model.way = data.sponsor;
            let time = this.$options.filters["globalTime"](data.publish_time);
            let timeSecond = this.$options.filters["globalTimeSecond"](
              data.publish_time
            );
            this.model.time = time + " " + timeSecond;
            this.getRich();
          }
        },
        fail: () => {
          console.log("连接失败");
        },
      });
    },
    replaceImg(html) {
      let result = html.replace(
        /<img [^>]*src=['"]([^'"]+)[^>]*>/gi,
        function (match, capture) {
		  if(capture.includes('http')){
			  return (
				"<img src=" +
				capture +
				' style="max-width:100%;height:auto;display:block;margin:10px 0;"/>'
			  );
		  }else {
			return (
			  "<img src=" +
			  getApp().globalData.shareUrl +
			  capture +
			  ' style="max-width:100%;height:auto;display:block;margin:10px 0;"/>'
			);
		  }	
        }
      );
      return result;
    },
    getRich() {
      uni.request({
        url:
          getApp().globalData.shareUrl +
          `content/notice/${Math.floor(this.model.id / 1000)}/${
            this.model.id
          }.html`,
        method: "GET",
        header: {
          "content-type": "application/x-www-form-urlencoded",
        },

        success: (res) => {
          if (res.statusCode === 200) {
            this.model.artical = this.replaceImg(res.data);
          }
        },
        fail: () => {
          console.log("连接失败");
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
</style>