"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs2/helpers/interopRequireDefault");

exports.__esModule = true;
exports.default = void 0;

require("./Wechat.css");

var _vueQrcode = _interopRequireDefault(require("@xkeshi/vue-qrcode"));

var _Button = _interopRequireDefault(require("../Button"));

var _Icon = _interopRequireDefault(require("../Icon"));

var _Tips = _interopRequireDefault(require("../Tips"));

var _cache = require("../cache");

var _component = require("../_utils/component");

var _vueNormalizeComponent = _interopRequireDefault(require("../_utils/vue-normalize-component.js"));

/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
var __vue_script__ = {
  name: (0, _component.createComponentName)('auth-wechat'),
  components: {
    Button: _Button.default,
    Qrcode: _vueQrcode.default,
    Icon: _Icon.default
  },
  mixins: [_component.componentMixin],
  inject: ['vm'],

  data() {
    return {
      qrcodeUrl: '',
      qrcodeOption: {
        size: 160
      },
      // invalid: false,
      loading: false,
      error: false
    };
  },

  mounted() {
    this.init();
  },

  beforeDestroy() {
    if (!this.success && this.qrcodeUrl && this.qrcodeId) {
      _cache.sessionCache.set('wechat-qrcode', {
        url: this.qrcodeUrl,
        id: this.qrcodeId
      }, {
        time: '8m'
      });
    } else {
      _cache.sessionCache.remove('wechat-qrcode');
    }

    this.clearTimer();
  },

  methods: {
    init() {
      var data = _cache.sessionCache.get('wechat-qrcode');

      if (data) {
        this.qrcodeUrl = data.url;
        this.qrcodeId = data.id;
        this.queryWechatStatus(data.id);
      } else {
        this.createWechatQrcode();
      }
    },

    createWechatQrcode() {
      var _this = this;

      this.loading = true;
      this.error = false;
      this.clearTimer();
      this.vm.requestUms('createWechatQrcode', {
        native: 1
      }).then(function (res) {
        _this.qrcodeUrl = res.url;
        _this.qrcodeId = res.id;
        _this.loading = false;

        _this.queryWechatStatus(res.id);
      }).catch(function (e) {
        (0, _Tips.default)(e.message);
        _this.loading = false;
        _this.error = true;
      });
    },

    clearTimer() {
      clearTimeout(this.queryTimer);
    },

    queryWechatStatus(id) {
      var _this2 = this;

      this.queryTimer = setTimeout(function () {
        _this2.vm.requestUms('queryWechatStatus', {
          id: id
        }).then(function (res) {
          if (res.result !== 0) {
            _this2.clearTimer();
          }

          switch (res.result) {
            case 1:
              _this2.vm.bindphoneType = 'wechat';
              _this2.success = true;

              _this2.vm.switchType('bindphone');

              break;

            case 2:
              _this2.success = true;

              _this2.vm.onLogin(res.authorization_code, 'wechat');

              break;

            case 3:
              _this2.createWechatQrcode();

              break;

            default:
              _this2.queryWechatStatus(id);

              break;
          }
        }).catch(function (e) {
          (0, _Tips.default)(e.message);

          _this2.createWechatQrcode();
        });
      }, 1000);
    }

  }
};
/* template */

var __vue_render__ = function __vue_render__() {
  var _vm = this;

  var _h = _vm.$createElement;

  var _c = _vm._self._c || _h;

  return _c("div", {
    class: _vm.bem()
  }, [_c("div", {
    class: _vm.bem("title")
  }, [_c("img", {
    class: _vm.bem("icon-wechat"),
    attrs: {
      src: require("../../assets/img/wechat-inner.svg"),
      alt: "wechat"
    }
  }), _vm._v("微信扫码登录/注册")]), _vm._v(" "), _c("div", {
    class: _vm.bem("qrcode-wrapper")
  }, [_vm.loading ? _c("div", {
    class: _vm.bem("message")
  }, [_vm.loading ? _c("span", [_vm._v("生成二维码中...")]) : _vm._e()]) : _vm.error ? [_c("span", {
    class: _vm.bem("message")
  }, [_vm.error ? _c("span", [_vm._v("二维码生成失败")]) : _vm._e(), _c("br"), _vm._v(" "), _c("Button", {
    attrs: {
      type: "primary"
    },
    on: {
      click: _vm.createWechatQrcode
    }
  }, [_vm._v("点击重试")])], 1)] : [_c("qrcode", {
    class: _vm.bem("qrcode"),
    attrs: {
      value: _vm.qrcodeUrl,
      options: _vm.qrcodeOption
    }
  }), _vm._v(" "), _c("div", {
    class: _vm.bem("tips")
  }, [_vm._v("微信扫码关注即可登录")])]], 2), _vm._v(" "), _c("div", {
    class: _vm.bem("third-parties")
  }, [_c("div", {
    on: {
      click: function click($event) {
        _vm.vm.switchType("phone");
      }
    }
  }, [_c("img", {
    class: _vm.bem("icon-phone"),
    attrs: {
      src: require("../../assets/img/phone.svg"),
      alt: "phone"
    }
  }), _vm._v("\n            手机号登录\n        ")]), _vm._v(" "), _c("div", {
    on: {
      click: function click($event) {
        _vm.vm.switchType("qq");
      }
    }
  }, [_c("img", {
    class: _vm.bem("icon-qq"),
    attrs: {
      src: require("../../assets/img/qq.svg"),
      alt: "qq"
    }
  }), _vm._v("\n            QQ登录\n        ")])]), _vm._v(" "), _c("div", {
    class: _vm.bem("footer")
  }, [_c("Button", {
    attrs: {
      type: "text"
    },
    on: {
      click: function click($event) {
        _vm.vm.switchType("register");
      }
    }
  }, [_vm._v("\n            手机号注册\n            "), _c("Icon", {
    attrs: {
      name: "arrow-right"
    }
  })], 1)], 1)]);
};

var __vue_staticRenderFns__ = [];
__vue_render__._withStripped = true;
/* style */

var __vue_inject_styles__ = undefined;
/* scoped */

var __vue_scope_id__ = undefined;
/* module identifier */

var __vue_module_identifier__ = undefined;
/* functional template */

var __vue_is_functional_template__ = false;
/* component normalizer */

/* style inject */

/* style inject SSR */
var _default = (0, _vueNormalizeComponent.default)({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);

exports.default = _default;