<template>
  <div></div>
</template>

<script>
import wx from "weixin-js-sdk";
// import VConsole from "vconsole";
import { getStore } from "@/utils/storage";
import { mapState } from "vuex";
import { Toast } from "mint-ui";
// let VConsoles = new VConsole();

export default {
  data() {
    return {
      appId: "",
      timestamp: "",
      nonceStr: "",
      signature: "",

      localIds: "",
      serverId: "",
    };
  },
  mounted() {
    console.log("调用获取的地址", this.getSignUrl());
    this.initWeixin();
    console.log("当前地址", location.href.split("#")[0]);
  },
  methods: {
    //初始化
    initWeixin() {
      this.getData("/Weixinsign/getSignPackage", {
        token: getStore("token"),
        url: this.getSignUrl(),
      }).then(
        (res) => {
          const data = res.data;
          wx.config({
            debug: false,
            appId: data.appId,
            timestamp: data.timestamp,
            nonceStr: data.nonceStr,
            signature: data.signature,
            jsApiList: ["chooseImage", "previewImage", "uploadImage"],
          });
          // 检查是否拥有权限启用摄像头
          wx.checkJsApi({
            jsApiList: ["chooseImage", "previewImage", "uploadImage"],
            success: function (res) {
              var chooseImageAvailable = res.checkResult.chooseImage;
              var previewImageAvailable = res.checkResult.previewImage;
              var uploadImageAvailable = res.checkResult.uploadImage;
              if (chooseImageAvailable) {
                console.log(chooseImageAvailable,"-------")
                console.log("有chooseImageAvailable = res.checkResult.chooseImage权限");
              }else {
                console.log(chooseImageAvailable,"-------")
                alert("无chooseImageAvailable = res.checkResult.chooseImage权限");
              }
              if(previewImageAvailable) {
                console.log(previewImageAvailable,"-------")
                console.log("有previewImageAvailable = res.checkResult.previewImage权限");
              }else {
                console.log(chooseImageAvailable,"-------")
                alert("无previewImageAvailable = res.checkResult.previewImage权限");
              }

              if(uploadImageAvailable) {
                console.log(uploadImageAvailable,"-------")
                console.log("有uploadImageAvailable = res.checkResult.uploadImage权限");
              }else {
                console.log(chooseImageAvailable,"-------")
                alert("无uploadImageAvailable = res.checkResult.uploadImage权限");
              }
            },
            fail: function (error) {
              alert("未检测到摄像头<===>Failed to check camera access:", error);
            },
          });
          wx.error(function (res) {
            console.error("配置错误=>", res);
          });
        },
        (err) => {}
      );
    },

    // 获取url
    getSignUrl() {
      var signLink = "";
      var ua = navigator.userAgent.toLowerCase();
      if (/iphone|ipad|ipod/.test(ua)) {
        signLink = localStorage.getItem("WxVodeUrl");
        if (!signLink) signLink = location.href.split("#")[0];
      } else {
        signLink = location.href.split("#")[0];
      }
      return signLink;
    },

    //微信拍照
    takePhoto(num = 1) {
      var _this = this;
      wx.chooseImage({
        count: 1,
        sizeType: ["compressed"],
        sourceType: ["camera"],
        success: function (res) {
          _this.localIds = res.localIds;
          //上传照片
          wx.uploadImage({
            localId: _this.localIds.toString(),
            isShowProgressTips: 1,
            success: function (res) {
              _this.serverId = res.serverId;
              _this
                .getData("/Weixinsign/downloadWxPhoto", {
                  token: getStore("token"),
                  apitoken: getStore("token"),
                  serverId: _this.serverId,
                  numberUrl: _this.loginInfo.member_passport,
                })
                .then((res) => {
                  //调用人脸检测接口
                  if (res.code == 200) {
                    _this.$emit("result", "success", num);
                  } else {
                    Toast(res.msg);
                    _this.$emit("result", "error", num);
                  }
                });
            },
            fail: function (res) {
              console.error("uploadImage错误=>", res);
            },
          });
        },
        fail: function (res) {
          console.error("chooseImage错误=>", res);
          Toast("拍照错误");
          _this.$emit("result", "error", num);
        },
        cancel: function (res) {
          console.log("识别到用户的取消操作了！");
          Toast("用户已取消拍照");
          _this.$emit("result", "error", num);
        },
      });
    },
  },
  computed: {
    ...mapState(["loginInfo"]),
  },
};
</script>

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