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

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

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

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