"use strict";

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

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

var _assign = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/object/assign"));

var _regenerator = _interopRequireDefault(require("@babel/runtime-corejs2/regenerator"));

require("regenerator-runtime/runtime");

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

require("./Bindphone.css");

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

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

var _PhoneNumber = _interopRequireDefault(require("./PhoneNumber"));

var _Input = _interopRequireDefault(require("../Input"));

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

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

/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
var __vue_script__ = {
  name: (0, _component.createComponentName)('auth-bindphone'),
  components: {
    Button: _Button.default,
    PhoneNumber: _PhoneNumber.default,
    Input: _Input.default
  },
  mixins: [_component.componentMixin],
  inject: ['vm'],

  data() {
    return {
      phoneNumberIsUsed: null,
      needRegister: false,
      codePendingRemain: null,
      phoneCheckStatus: 0,
      checkingPhone: false,
      loading: false
    };
  },

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

  methods: {
    // TODO: 向商目前只支持校验注册状态，无法区别QQ， Wechat 所以暂时关闭已注册的情况的错误处理
    checkIsBinded() {
      var _this = this;

      return (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee() {
        var _ref, result;

        return _regenerator.default.wrap(function _callee$(_context) {
          while (1) {
            switch (_context.prev = _context.next) {
              case 0:
                if (_this.vm.form.phone.number) {
                  _context.next = 2;
                  break;
                }

                return _context.abrupt("return");

              case 2:
                _this.checkingPhone = true;
                _context.prev = 3;
                _context.next = 6;
                return _this.vm.requestUms('checkPhoneNumberStatus', _this.vm.form);

              case 6:
                _ref = _context.sent;
                result = _ref.result;
                _this.phoneCheckStatus = result;

                if (!(result === 1)) {
                  _context.next = 16;
                  break;
                }

                (0, _Tips.default)('手机号码未注册，填写密码注册');
                _this.phoneNumberIsUsed = false;
                _this.needRegister = true;
                return _context.abrupt("return");

              case 16:
                if (!(result === 3)) {
                  _context.next = 20;
                  break;
                }

                _this.phoneNumberIsUsed = true;
                _context.next = 25;
                break;

              case 20:
                if (!(result === 2)) {
                  _context.next = 24;
                  break;
                }

                _this.phoneNumberIsUsed = false;
                _context.next = 25;
                break;

              case 24:
                throw new Error('未知错误');

              case 25:
                _this.needRegister = false;
                _context.next = 31;
                break;

              case 28:
                _context.prev = 28;
                _context.t0 = _context["catch"](3);
                (0, _Tips.default)(_context.t0);

              case 31:
                _context.prev = 31;
                _this.checkingPhone = false;
                return _context.finish(31);

              case 34:
              case "end":
                return _context.stop();
            }
          }
        }, _callee, this, [[3, 28, 31, 34]]);
      }))();
    },

    beforeDestroy() {
      clearTimeout(this.codePendingTimer);
    },

    sendVerifyCode() {
      var _this2 = this;

      return (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee2() {
        return _regenerator.default.wrap(function _callee2$(_context2) {
          while (1) {
            switch (_context2.prev = _context2.next) {
              case 0:
                _context2.prev = 0;

                if (!(_this2.phoneCheckStatus === 0)) {
                  _context2.next = 4;
                  break;
                }

                _context2.next = 4;
                return _this2.checkIsBinded();

              case 4:
                _context2.next = 6;
                return _this2.vm.requestUms('sendVerifyCode', (0, _assign.default)({}, _this2.vm.form, {
                  type: _this2.needRegister ? 'register' : 'login'
                }));

              case 6:
                _this2.startCheckCodePending();

                _context2.next = 12;
                break;

              case 9:
                _context2.prev = 9;
                _context2.t0 = _context2["catch"](0);
                (0, _Tips.default)(_context2.t0);

              case 12:
              case "end":
                return _context2.stop();
            }
          }
        }, _callee2, this, [[0, 9]]);
      }))();
    },

    startCheckCodePending() {
      var _this3 = this;

      var check = function check() {
        _this3.codePendingRemain -= 1;

        if (_this3.codePendingRemain <= 0) {
          _this3.codePendingRemain = null;
          return;
        }

        _this3.codePendingTimer = setTimeout(check, 1000);
      };

      clearTimeout(this.codePendingTimer);
      this.codePendingTimer = setTimeout(check, 1000);
      this.codePendingRemain = 60;
    },

    skip() {
      var _this4 = this;

      return (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee3() {
        var res;
        return _regenerator.default.wrap(function _callee3$(_context3) {
          while (1) {
            switch (_context3.prev = _context3.next) {
              case 0:
                _context3.prev = 0;

                if (!(_this4.vm.bindphoneType === 'qq')) {
                  _context3.next = 7;
                  break;
                }

                _context3.next = 4;
                return _this4.vm.requestUms('skipBindQQ');

              case 4:
                res = _context3.sent;
                _context3.next = 14;
                break;

              case 7:
                if (!(_this4.vm.bindphoneType === 'wechat')) {
                  _context3.next = 13;
                  break;
                }

                _context3.next = 10;
                return _this4.vm.requestUms('skipBindWechat');

              case 10:
                res = _context3.sent;
                _context3.next = 14;
                break;

              case 13:
                throw new Error('尚未通过第三方登录');

              case 14:
                _this4.vm.$emit('success', {
                  action: 'register',
                  type: _this4.vm.bindphoneType
                });

                _this4.vm.onLogin(res.authorization_code, _this4.vm.bindphoneType);

                _this4.loading = false;
                _context3.next = 24;
                break;

              case 19:
                _context3.prev = 19;
                _context3.t0 = _context3["catch"](0);
                (0, _Tips.default)(_context3.t0);
                _this4.loading = false;

                _this4.vm.$emit('error', {
                  action: 'bindphone',
                  type: _this4.vm.bindphoneType,
                  error: _context3.t0
                });

              case 24:
              case "end":
                return _context3.stop();
            }
          }
        }, _callee3, this, [[0, 19]]);
      }))();
    },

    onBindPhone() {
      var _this5 = this;

      return (0, _asyncToGenerator2.default)(
      /*#__PURE__*/
      _regenerator.default.mark(function _callee4() {
        var res;
        return _regenerator.default.wrap(function _callee4$(_context4) {
          while (1) {
            switch (_context4.prev = _context4.next) {
              case 0:
                if (!_this5.loading) {
                  _context4.next = 2;
                  break;
                }

                return _context4.abrupt("return");

              case 2:
                _this5.loading = true;
                _context4.prev = 3;

                if (!(_this5.phoneCheckStatus === 0)) {
                  _context4.next = 6;
                  break;
                }

                throw new Error('请检查手机号或等待手机号检查完成');

              case 6:
                if (!_this5.needRegister) {
                  _context4.next = 23;
                  break;
                }

                if (!(_this5.vm.bindphoneType === 'qq')) {
                  _context4.next = 13;
                  break;
                }

                _context4.next = 10;
                return _this5.vm.requestUms('registerByQQ', _this5.vm.form);

              case 10:
                res = _context4.sent;
                _context4.next = 20;
                break;

              case 13:
                if (!(_this5.vm.bindphoneType === 'wechat')) {
                  _context4.next = 19;
                  break;
                }

                _context4.next = 16;
                return _this5.vm.requestUms('registerByWechat', _this5.vm.form);

              case 16:
                res = _context4.sent;
                _context4.next = 20;
                break;

              case 19:
                throw new Error('尚未通过第三方登录');

              case 20:
                _this5.vm.$emit('success', {
                  action: 'register',
                  type: _this5.vm.bindphoneType
                });

                _context4.next = 37;
                break;

              case 23:
                if (!(_this5.vm.bindphoneType === 'qq')) {
                  _context4.next = 29;
                  break;
                }

                _context4.next = 26;
                return _this5.vm.requestUms('bindQQ', (0, _assign.default)({}, _this5.vm.form));

              case 26:
                res = _context4.sent;
                _context4.next = 36;
                break;

              case 29:
                if (!(_this5.vm.bindphoneType === 'wechat')) {
                  _context4.next = 35;
                  break;
                }

                _context4.next = 32;
                return _this5.vm.requestUms('bindWechat', (0, _assign.default)({}, _this5.vm.form));

              case 32:
                res = _context4.sent;
                _context4.next = 36;
                break;

              case 35:
                throw new Error('尚未通过第三方登录');

              case 36:
                _this5.vm.$emit('success', {
                  action: 'bindphone',
                  type: _this5.vm.bindphoneType
                });

              case 37:
                _this5.vm.onLogin(res.authorization_code, _this5.vm.bindphoneType);

                _this5.loading = false;
                _context4.next = 46;
                break;

              case 41:
                _context4.prev = 41;
                _context4.t0 = _context4["catch"](3);
                (0, _Tips.default)(_context4.t0);
                _this5.loading = false;

                _this5.vm.$emit('error', {
                  action: 'bindphone',
                  type: _this5.vm.bindphoneType,
                  error: _context4.t0
                });

              case 46:
              case "end":
                return _context4.stop();
            }
          }
        }, _callee4, this, [[3, 41]]);
      }))();
    }

  }
};
/* 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")
  }, [_vm._v("绑定手机号")]), _vm._v(" "), _c("div", {
    class: _vm.bem("skip"),
    on: {
      click: _vm.skip
    }
  }, [_vm._v("跳过")]), _vm._v(" "), _c("div", {
    class: _vm.bem("item")
  }, [_c("PhoneNumber", {
    attrs: {
      countries: _vm.vm.countries,
      loading: _vm.checkingPhone
    },
    on: {
      blur: _vm.checkIsBinded
    },
    model: {
      value: _vm.vm.form.phone,
      callback: function callback($$v) {
        _vm.$set(_vm.vm.form, "phone", $$v);
      },
      expression: "vm.form.phone"
    }
  })], 1), _vm._v(" "), _c("div", {
    class: _vm.bem("item")
  }, [_c("Input", {
    attrs: {
      maxlength: "4",
      autocomplete: "off",
      placeholder: "输入短信中的验证码"
    },
    model: {
      value: _vm.vm.form.verifyCode,
      callback: function callback($$v) {
        _vm.$set(_vm.vm.form, "verifyCode", $$v);
      },
      expression: "vm.form.verifyCode"
    }
  }), _vm._v(" "), _c("Button", {
    class: _vm.bem("sms-btn"),
    attrs: {
      rect: "",
      nativeType: "button",
      disabled: !!_vm.codePendingRemain
    },
    on: {
      click: _vm.sendVerifyCode
    }
  }, [_vm._v("\n            " + _vm._s(_vm.codePendingRemain ? _vm.codePendingRemain + "秒后获取" : "获取验证码") + "\n        ")])], 1), _vm._v(" "), _c("div", {
    directives: [{
      name: "show",
      rawName: "v-show",
      value: _vm.needRegister,
      expression: "needRegister"
    }],
    class: _vm.bem("item")
  }, [_c("Input", {
    attrs: {
      id: "password",
      type: "password",
      label: "密码",
      name: "new-password",
      autocomplete: "off",
      placeholder: "输入6-30位新密码",
      maxlength: "30"
    },
    on: {
      enter: _vm.onBindPhone
    },
    model: {
      value: _vm.vm.form.password,
      callback: function callback($$v) {
        _vm.$set(_vm.vm.form, "password", $$v);
      },
      expression: "vm.form.password"
    }
  })], 1), _vm._v(" "), _c("Button", {
    class: _vm.bem("submit"),
    attrs: {
      type: "primary",
      size: "large",
      nativeType: "submit",
      disabled: _vm.loading
    },
    on: {
      click: _vm.onBindPhone
    }
  }, [_vm._v(_vm._s(_vm.needRegister ? "注册" : "绑定手机号"))])], 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;