"use strict";

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

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

require("core-js/modules/es7.array.includes");

require("core-js/modules/web.dom.iterable");

require("./index.css");

var _promise = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/promise"));

var _parseInt2 = _interopRequireDefault(require("@babel/runtime-corejs2/core-js/parse-int"));

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

var _Phone = _interopRequireDefault(require("./Phone"));

var _Wechat = _interopRequireDefault(require("./Wechat"));

var _Register = _interopRequireDefault(require("./Register"));

var _Resetpwd = _interopRequireDefault(require("./Resetpwd"));

var _Bindphone = _interopRequireDefault(require("./Bindphone"));

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

var _request = _interopRequireDefault(require("../request"));

var _QQ = _interopRequireDefault(require("./QQ"));

var _constant = require("../_utils/constant");

var _url = require("../_utils/url");

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

/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
var __vue_script__ = {
  name: (0, _component.createComponentName)('auth'),
  components: {
    Phone: _Phone.default,
    Register: _Register.default,
    Wechat: _Wechat.default,
    Resetpwd: _Resetpwd.default,
    QQ: _QQ.default,
    BindPhone: _Bindphone.default
  },
  mixins: [_component.componentMixin],

  provide() {
    return {
      vm: this
    };
  },

  props: {
    type: {
      type: String
    },
    defaultType: {
      type: String,
      default: 'login'
    },
    // 游客标识
    guestFinger: {
      type: String
    }
  },

  data() {
    var type = 'login';
    if (this.$hasProp('type')) type = this.type;else if (this.$hasProp('defaultType')) type = this.defaultType;
    if (!type) throw new Error('无效的type属性');
    return {
      loading: true,
      countries: [{
        name: '中国',
        code: 86
      }, {
        name: '中国香港',
        code: 852
      }, {
        name: '中国澳门',
        code: 853
      }, {
        name: '中国台湾地区',
        code: 886
      }],
      form: {
        phone: {
          number: '',
          code: 86
        },
        password: '',
        verifyCode: ''
      },
      innerType: type,
      bindphoneType: ''
    };
  },

  watch: {
    type(value) {
      this.innerType = value;
      this.form.password = '';
      this.form.verifyCode = '';
    }

  },

  created() {
    this.src = `${_constant.UMS_URL}?client_id=gaodingx&client_secret=7da458070e57b98e11d00d9286f23537&redirect_uri=${_constant.UMS_URL}/api/oauth/redirect`;
  },

  mounted() {
    this.ums = this.$refs.ums.contentWindow;
  },

  methods: {
    submit(e) {
      e.preventDefault();
    },

    init() {
      var _this = this;

      this.loading = false;
      this.requestUms('getCountries').then(function (data) {
        var list = _this.countries;
        data.forEach(function (c) {
          list = list.concat(c.list.filter(function (n) {
            return !n.name.includes('中国');
          }));
        });
        list.forEach(function (c) {
          c.code = (0, _parseInt2.default)(c.code);
        });
        _this.countries = list;
      });
    },

    switchType(type) {
      if (type === this.innerType) return;
      this.lastType = this.innerType;

      if (this.$hasProp('type')) {
        this.$emit('update:type', type);
      } else {
        this.innerType = type;
        this.form.password = '';
        this.form.verifyCode = '';
      }
    },

    onLogin(code, type) {
      var _this2 = this;

      var query = (0, _url.getQueryParams)(location.href) || {};
      var formData = `code=${code}&last_screen_width=${window.screen.width}&last_screen_height=${window.screen.height}`;

      if (query && query.hmsr) {
        formData += `&source=${query.hmsr}`;
      }

      return _request.default.post('/api/sessions/ums', formData, {
        headers: {
          'Content-type': 'application/x-www-form-urlencoded'
        }
      }).then(function (res) {
        (0, _Tips.default)('登录成功'); // submit 成功
        // @arg action 操作类型 'login' 'register' 'resetpwd' 等

        _this2.$emit('success', {
          action: 'login',
          type,
          data: res.data
        });
      }).catch(function (error) {
        (0, _Tips.default)(error); // submit 失败
        // @arg action 操作类型 'login' 'register' 'resetpwd' 等
        // @arg error 失败信息

        _this2.$emit('error', {
          action: 'login',
          type,
          error: error
        });
      });
    },

    requestUms(action, data) {
      var _this3 = this;

      if (data === void 0) {
        data = {};
      }

      return new _promise.default(function (resolve) {
        if (_this3.loading) {
          var unwatch = _this3.$watch('loading', function () {
            unwatch();
            resolve();
          });
        } else {
          resolve();
        }
      }).then(function () {
        var timer = null;
        return new _promise.default(function (resolve, reject) {
          var callback = function callback(event) {
            if (event.origin !== _constant.UMS_URL) {
              return false;
            }

            if (event.data.action !== action + '.success' && event.data.action !== action + '.error') {
              return false;
            }

            if (event.data.action === action + '.success') {
              resolve(event.data.data);
            } else {
              var error = new Error(event.data.data.message);
              if (event.data.data.status) error.status = event.data.data.status;
              reject(error);
            }

            clearTimeout(timer);
            window.removeEventListener('message', callback);
          };

          timer = setTimeout(function () {
            reject(new Error('网络超时'));
            window.removeEventListener('message', callback);
          }, 20000);
          window.addEventListener('message', callback);

          _this3.ums.postMessage({
            action,
            data
          }, _constant.UMS_URL);
        });
      });
    }

  }
};
/* template */

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

  var _h = _vm.$createElement;

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

  return _c("form", {
    class: _vm.bem([_vm.innerType]),
    on: {
      submit: _vm.submit
    }
  }, [_vm.innerType === "phone" ? _c("Phone", {
    attrs: {
      countries: _vm.countries
    }
  }) : _vm.innerType === "wechat" ? _c("Wechat") : _vm.innerType == "register" ? _c("Register") : _vm.innerType == "resetpwd" ? _c("Resetpwd") : _vm.innerType == "bindphone" ? _c("BindPhone") : _vm.innerType == "qq" ? _c("QQ") : _c("Wechat"), _vm._v(" "), _vm._m(0)], 1);
};

var __vue_staticRenderFns__ = [function () {
  var _vm = this;

  var _h = _vm.$createElement;

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

  return _c("iframe", {
    ref: "ums",
    class: _vm.bem("ums"),
    attrs: {
      src: _vm.src,
      frameborder: "0"
    },
    on: {
      load: _vm.init
    }
  });
}];
__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;