function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
function _iterableToArrayLimit(arr, i) { var _i = null == arr ? null : "undefined" != typeof Symbol && arr[Symbol.iterator] || arr["@@iterator"]; if (null != _i) { var _s, _e, _x, _r, _arr = [], _n = !0, _d = !1; try { if (_x = (_i = _i.call(arr)).next, 0 === i) { if (Object(_i) !== _i) return; _n = !1; } else for (; !(_n = (_s = _x.call(_i)).done) && (_arr.push(_s.value), _arr.length !== i); _n = !0); } catch (err) { _d = !0, _e = err; } finally { try { if (!_n && null != _i.return && (_r = _i.return(), Object(_r) !== _r)) return; } finally { if (_d) throw _e; } } return _arr; } }
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
import { ajax, BizGlobalDataContext, SocketClient } from '@zgfe/business-lib';
import React, { useContext, useEffect, useState } from 'react';
import apis from "../apis";
import "./index.less";
var VisualScanCode = function VisualScanCode(_ref) {
  var onScan = _ref.onScan;
  var _useState = useState(''),
    _useState2 = _slicedToArray(_useState, 2),
    url = _useState2[0],
    setUrl = _useState2[1];
  var _useContext = useContext(BizGlobalDataContext),
    currentApp = _useContext.currentApp;
  var _useState3 = useState(),
    _useState4 = _slicedToArray(_useState3, 2),
    client = _useState4[0],
    setClient = _useState4[1];
  console.log('应用信息', currentApp);
  var classPrefix = 'visual-scan';
  function getCode(token) {
    ajax(apis.getQrCode, {
      method: 'post',
      data: {
        appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.appId,
        socketToken: token,
        origin: 2,
        bootstrap: 'bootstrap'
      }
    }).then(function (res) {
      if (!res) return;
      setUrl(res.data.base64Str);
    }).catch(function (err) {
      console.error('获取二维码失败', err);
    });
  }
  function afterScan() {
    if (onScan) onScan();
  }
  function initSocket() {
    var socket = new SocketClient('wss://rt2.zhugeio.com/eventtracking/ws', currentApp === null || currentApp === void 0 ? void 0 : currentApp.appKey, {
      appId: currentApp === null || currentApp === void 0 ? void 0 : currentApp.id,
      origin: 3
    });
    setClient(socket);
    socket.on('scanSuccess', afterScan);
    socket.onConnected(function (token) {
      getCode(token);
    });
  }
  useEffect(function () {
    initSocket();
    return function () {
      client === null || client === void 0 ? void 0 : client.destroy();
    };
  }, []);
  return /*#__PURE__*/React.createElement("div", {
    className: classPrefix
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-title")
  }, "\u624B\u673A\u626B\u4E00\u626B\u5FEB\u901F\u8FDE\u63A5"), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-tocode")
  }, /*#__PURE__*/React.createElement("img", {
    src: "data:image/png;base64,".concat(url)
  })), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-tip")
  }, /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-tip-title")
  }, "\u64CD\u4F5C\u6B65\u9AA4"), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-tip-line")
  }, "1\u3001\u96C6\u6210SDK\u540E\u6253\u5F00\u5168\u57CB\u70B9\u5F00\u5173\uFF0C\u5373\u53EF\u91C7\u96C6\u9875\u9762\u8BBF\u95EE\u3001\u9875\u9762\u70B9\u51FB\uFF1B"), /*#__PURE__*/React.createElement("div", {
    className: "".concat(classPrefix, "-tip-line")
  }, "2\u3001\u5B89\u88C5\u8981\u5206\u6790\u7684\u5E94\u7528\uFF0C\u624B\u673A\u626B\u4E00\u626B\u8FDE\u63A5\u5E94\u7528\uFF0C\u8FDE\u63A5\u6210\u529F\u540E\u53EF\u8FDB\u884C\u53EF\u89C6\u5316\u57CB\u70B9\uFF1B")));
};
export default VisualScanCode;