var jweixin = require("jweixin-module");
import { getShareGzh } from "@/utils/bill";
export default {
  //判断是否在微信中
  isWechat: function () {
    var ua = window.navigator.userAgent.toLowerCase();
    return ua.match(/micromessenger/i) == "micromessenger";
  },
  //初始化sdk配置
  initJssdkShare: function (callback) {
    getShareGzh().then((result) => {
      jweixin.config({
        debug: true,
        appId: result.appid,
        timestamp: result.timestamp,
        nonceStr: result.nonceStr,
        signature: result.signature,
        jsApiList: ["updateTimelineShareData", "updateAppMessageShareData"],
      });
      //配置完成后，再执行分享等功能
      if (callback) {
        callback(result);
      }
    });
  },
  //在需要自定义分享的页面中调用
  share: function (data) {
    // if (!this.isWechat()) {
    //   return;
    // }
    //每次都需要重新初始化配置，才可以进行分享
    this.initJssdkShare(function (signData) {
      let { title, desc, link, imgUrl } = data;
      jweixin.ready(function () {
        
        var shareData = {
          title,
          desc,
          link: link || window.location.href,
          imgUrl,
          success: function (res) {
            // 分享后的一些操作,比如分享统计等等
            console.log("分享成功");
          },
          cancel: function (res) {},
        };
        //分享给朋友接口
        // jweixin.updateAppMessageShareData(shareData);
        //分享到朋友圈接口
        jweixin.updateTimelineShareData(shareData);
      });
    });
  },
};
