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";
  },
  jsApiList: [
    "updateTimelineShareData",
    "updateAppMessageShareData",
    "onMenuShareAppMessage",
    "onMenuShareTimeline",
  ],
  //初始化sdk配置
  initJssdkShare: function (callback) {
    getShareGzh({
      url: window.location.href,
    }).then((result) => {
      jweixin.config({
        debug: false,
        appId: result.appid,
        timestamp: result.timeStamp,
        nonceStr: result.nonceStr,
        signature: result.signature,
        jsApiList: this.jsApiList,
      });
      if (callback) {
        callback(result);
      }
    });
  },
  //在需要自定义分享的页面中调用
  share: function (data) {
    return new Promise((resolve, reject) => {
      if (!this.isWechat()) {
        reject();
      }
      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) {
              resolve(res);
            },
            cancel: function (res) {},
          };
          // 旧的
          // 分享给朋友接口
          jweixin.onMenuShareAppMessage(shareData);
          // 分享到朋友圈
          jweixin.onMenuShareTimeline(shareData);

          // 新的
          // 分享给朋友接口
          // jweixin.updateAppMessageShareData(shareData);
          // //分享到朋友圈接口
          // jweixin.updateTimelineShareData(shareData);
        });
      });
    });
  },
};
