<template>
  <div class="page">
    <div class="tip">{{tip}}</div>
    <div class="title">今日早报</div>
    <div class="home" @click="showToday">
      <div class="bar">
        <img class="pic" :src="src">
      </div>
      <div class="tip1">{{title}}</div>
      <div class="tip2">{{subTitle1}}</div>
      <div class="tip2">{{subTitle2}}</div>
    </div>
  </div>
</template>

<script>
import { get, post } from "@js/request";
import { getUrlParameter, isWeChatApplet, formatUrl } from "@js/tooler";
import { shareConfig, updateLink } from "@js/share";

const SCID = getUrlParameter('scid');
const API = {
  getInitDaily: '/kjy/mp/content/getInitDaily',
  getScId: '/kjy/mp/seller/getScId'
}
const inMiniprogram = getUrlParameter("mini") == 1;
export default {
  data() {
    return {
      tip: "home",
      title: '',
      list: [1, 2, 3, 4],
      scid: 0,
      src: "//yun.dui88.com/h5/miniprogram/kejiji/web/rili.png",
      title: "",
      subTitle1: "",
      subTitle2: ""
    };
  },
  methods: {
    async showToday() {
      // this.$router.push("/today");
      console.log("inMiniprogram = " + inMiniprogram);
      let res = await get(API.getScId, {contentId: this.scid, showCard: 0});
      if (res.success) {
        let id = res.data;
        if(inMiniprogram){
          // wx.miniProgram.postMessage({
          //   data: JSON.stringify({
          //     title: this.title,
          //     src: this.src
          //   })
          // })

          let url = formatUrl('/pages/news/news_today/main', {
            scid: id,
            n: Math.random(),
            title: this.title,
            src: this.src
          })

          wx.miniProgram.navigateTo({
            url: url
          })
        }
        else{
          window.location.href = './daily?scid=' + id + '&n=' + Math.random()
        }
      }
      else{
        alert('请求数据出错');
      }
    },
    async initData(){
      shareConfig({
        link: window.location.href,
        title: "数据库",
        desc: '来自' + "功夫" +　'的分享',
        img: 'https://yun.dui88.com/h5/miniprogram/kejiji/icons.png',
        debug: false
      })

      if(inMiniprogram){
        this.tip = "在小程序中";
      }
      else{
        this.tip = "在普通浏览器中";
      }
      // this.tip = window.navigator.userAgent.toLowerCase();
      let res = await get(API.getInitDaily, {});
      console.log("1234");
      if (res.success) {
        let obj = res.data;
        this.scid = obj.id;
        this.src = obj.mainImgUrl;
        this.title = obj.title;
        console.log(obj.contentHead);
        if(obj.contentHead){
          let contentHead = JSON.parse(obj.contentHead);
          console.log(contentHead.subTitle1);
          this.subTitle1 = contentHead.subTitle1;
          this.subTitle2 = contentHead.subTitle2;
        }
      } else {
        if (res.code == '100010') {
          this.getH5Authorize()
        } else if (res.code == '100020') {
          
        }
      }
    },
    getH5Authorize(){
      let url = '/kjy/authorize/wxh5base?uri=/kjy/mp/content?mini=1';
      window.location.href = url;
    },
    jump(item){
      console.log(item);
    }
  },
  created() {
    this.initData();
  }
};
</script>
<style lang="less" scoped>
.page {
  background: rgb(250, 250, 250);
  font-size: 12px;
  width: 100%;
  overflow-x: hidden;
  padding-bottom: 60px; 
}
.title{
  font-size: 40px;
  color: #333333;
  letter-spacing: 0.44px;
  width: 670px;
  margin: 32px auto;
}
.home{
  background: #FFFFFF;
  border-radius: 20px;
  width: 670px;
  margin: 30px auto;
  padding: 16px 0;
  box-shadow: 4px 4px 16px #f0f0f0;
  .bar{
    border-radius: 20px;
    width: 626px;
    margin: 10px auto;
    overflow: hidden;
    .pic{
      width: 100%;
    }
  }
  .tip1{
    font-size: 32px;
    color: #333333;
    padding: 8px 20px;
  }
  .tip2{
    font-size: 24px;
    color: #888888;
    line-height: 40px;
    padding: 8px 20px;
  }
  .box{
    border-bottom: solid 1px #f0f0f0;
    padding: 20px 0;
    &:last-child{
      border: none;
    }
  }
}
.tip {
  width: 720px;
  height: 240px;
  background: rgb(15, 83, 1);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto;
  font-size: 24px;
}
</style>