yingyangdetail.vue 7.7 KB
<template>
  <div class="containerbox">
    <div class="sharetop flextwo" >
      <div class="leftrow" @click="back">
        <img src="../../../assets/leftrow.png" alt />
      </div>
      <div class="sharemiddle">营养小知识</div>
      <div class="flexone" @click.stop="collect">
        <div
          class="sharebtn"
          style="margin-right: 0.5rem; width: 0.4rem; height: 0.4rem"
        >
          <img
            src="../../../assets/nostar.png"
            alt
            v-if="yingyangdetail.is_favorite == 0"
          />
          <img src="../../../assets/star.png" alt v-else />
        </div>
        <!-- <div class="sharebtn">
          <img src="../../../assets/sharebtn.png" alt />
        </div> -->
      </div>
    </div>
    <div class="yingyangbox">
      <div class="yingyangtitle">{{ yingyangdetail.title }}</div>
      <div class="yingyangcontent" v-html="yingyangdetail.content"></div>
    </div>
  </div>
</template>

<script>
import wx from "jweixin-1.6.0";
import { Toast } from 'vant';
export default {
 
  data() {
    return {
      id: '',
      yingyangdetail: '',
      baseurl: ''
    }
  },
  created() {
    document.title = '唐元集'
    this.id = this.$route.query.id;
    this.baseurl = "http://" + location.host;
    this.getdetail();

  },

  beforeRouteEnter(to, from, next) {
    var u = navigator.userAgent;
    var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
    // XXX: 修复iOS版微信HTML5 History兼容性问题
    if (isiOS && to.path !== location.pathname) {
      // 此处不可使用location.replace
      location.assign(to.fullPath);
    } else {
      next();
    }
  },

  methods: {
   back() {
      this.$router.back(-1);
     
    },
    // 获取appid
    getappid() {
      let that = this;
      console.log(34734894890);
      // let urlk = window.location.href;
      let urlk = window.location.href;
      var url = "/api/user/wechat_jssdk";
      let param = {
        url: urlk
      };

      that.$axios
        .post(url, param)
        .then(function (res) {
          console.log(res);
          wx.config({
            debug: false,
            appId: res.data.jssdk.appId, // 和获取Ticke的必须一样------必填,公众号的唯一标识
            timestamp: res.data.jssdk.timestamp, // 必填,生成签名的时间戳
            nonceStr: res.data.jssdk.nonceStr, // 必填,生成签名的随机串
            signature: res.data.jssdk.signature, // 必填,签名,见附录1
            //需要分享的列表项:发送给朋友,分享到朋友圈,分享到QQ,分享到QQ空间
            jsApiList: [
              "updateAppMessageShareData",
              "updateTimelineShareData",

            ]
          });

          that.sharetofriend();
        })
        .catch(function (err) {
          console.log(err);
        });
    },
    // 分享给朋友
    sharetofriend() {
      var that = this;
      // 处理验证失败的信息
      wx.error(function (res) {
        logUtil.printLog("验证失败返回的信息:", res);
      });
      // 处理验证成功的信息
      wx.ready(function () {
        //       alert(window.location.href.split('#')[0]);
        // var share_title = that.sharemsg.title;
        // if (share_title.indexOf("${title}") >= 0) {
        //   share_title = share_title.replace(
        //     "${title}",
        //     that.details.details.title
        //   );
        // }
        // var share_desc = that.sharemsg.content;
        // if (share_desc.indexOf("${title}") >= 0) {
        //   share_desc = share_desc.replace(
        //     "${title}",
        //     that.details.details.title
        //   );
        // }
        // if (share_desc.indexOf("${text}") >= 0) {
        //   share_desc = share_desc.replace("${text}", that.details.details.text);
        // }
        // 分享到朋友圈
        wx.updateTimelineShareData({
          title: that.yingyangdetail.title, // 分享标题
          link:
            that.baseurl +
            "/redirect.html?shareRedirect=" +
            encodeURIComponent(window.location.href), // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
          imgUrl:
            "http://tangyuanji.t.brotop.cn/uploads/20200804/fcabfaf5aa3c856ced04703b0a78a467.png",

          // imgUrl: that.details.details.img
          //   ? that.details.details.img
          //   : that.sharemsg.img,

          // 分享图标
          //  desc: that.sharemsg.content,
          success: function (res) {
            // 用户确认分享后执行的回调函数
            console.log("suss");
            logUtil.printLog("分享到朋友圈成功返回的信息为:", res);
            that.showMsg("分享成功!");
          },
          cancel: function (res) {
            // 用户取消分享后执行的回调函数
            console.log("err");
            logUtil.printLog("取消分享到朋友圈返回的信息为:", res);
          }
        }),
          // 分享给朋友
          wx.updateAppMessageShareData({
            title: that.yingyangdetail.title, // 分享标题
            desc: "", // 分享描述
            link:
              that.baseurl +
              "/redirect.html?shareRedirect=" +
              encodeURIComponent(window.location.href), // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl:
              "http://tangyuanji.t.brotop.cn/uploads/20200804/fcabfaf5aa3c856ced04703b0a78a467.png",

            // imgUrl: that.details.details.img
            //   ? that.details.details.img
            //   : that.sharemsg.img,
            // 分享图标
            type: "", // 分享类型,music、video或link,不填默认为link
            dataUrl: "", // 如果type是music或video,则要提供数据链接,默认为空
            success: function (res) {
              // 用户确认分享后执行的回调函数
              logUtil.printLog("分享给朋友成功返回的信息为:", res);
            },
            cancel: function (res) {
              // 用户取消分享后执行的回调函数
              logUtil.printLog("取消分享给朋友返回的信息为:", res);
            }
          });
      });
    },

   
   
   getdetail() {
      let that = this;
      var url = "/api/index/knowledge_detail";
      let param = {
        knowledge_id: that.id
      };

      that.$axios
        .post(url, param)
        .then(function (res) {
          console.log(res);
          that.yingyangdetail = res.data.knowledge;
          that.getappid()

        })
        .catch(function (err) {
          console.log(err);
        });
    },
    collect() {
      let that = this;
      var url = "/api/user/favorite";
      let param = {
        type: 1,
        favorite_id: that.id
      };

      that.$axios
        .post(url, param)
        .then(function (res) {
          console.log(res);
          if (that.yingyangdetail.is_favorite == 0) {
            Toast('收藏成功')
            that.yingyangdetail.is_favorite = 1;
            that.yingyangdetailt = that.yingyangdetail
           
          } else {
            Toast('取消收藏成功')
            that.yingyangdetail.is_favorite = 0;
            that.yingyangdetail = that.yingyangdetail
            
          }

        })
        .catch(function (err) {
          console.log(err);
        });
    },

  },


}
</script>

<style scoped>
.containerbox {
  padding: 0.32rem;
  box-sizing: border-box;
  background: #fff;
}
.yingyangtitle {
  color: #02170b;
  font-size: 0.4rem;
}
.yingyangcontent {
  color: #666e69;
  font-size: 0.32rem;
}
.yingyangbox {
  margin-top: 0.6rem;
}
</style>