import "./wechat.css";
/* script */
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
//
import Qrcode from '@xkeshi/vue-qrcode';
import Button from '../button';
import Icon from '../icon';
import tips from '../tips';
import { sessionCache } from '../../service/cache';
import { componentMixin, createComponentName } from '../../utils/base';
const __vue_script__ = {
  name: createComponentName('auth-wechat'),
  components: {
    Button,
    Qrcode,
    Icon
  },
  mixins: [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) {
      sessionCache.set('wechat-qrcode', {
        url: this.qrcodeUrl,
        id: this.qrcodeId
      }, {
        time: '8m'
      });
    } else {
      sessionCache.remove('wechat-qrcode');
    }

    this.clearTimer();
  },

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

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

    createWechatQrcode() {
      this.loading = true;
      this.error = false;
      this.clearTimer();
      this.vm.requestUms('createWechatQrcode', {
        native: 1
      }).then(res => {
        this.qrcodeUrl = res.url;
        this.qrcodeId = res.id;
        this.loading = false;
        this.queryWechatStatus(res.id);
      }).catch(e => {
        tips(e.message);
        this.loading = false;
        this.error = true;
      });
    },

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

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

          switch (res.result) {
            case 1:
              this.vm.bindphoneType = 'wechat';
              this.success = true;
              this.vm.switchType('bindphone');
              break;

            case 2:
              this.success = true;
              this.vm.onLogin(res.authorization_code, 'wechat');
              break;

            case 3:
              this.createWechatQrcode();
              break;

            default:
              this.queryWechatStatus(id);
              break;
          }
        }).catch(e => {
          tips(e.message);
          this.createWechatQrcode();
        });
      }, 1000);
    }

  }
};
/* template */

var __vue_render__ = function () {
  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 ($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 ($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 ($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 */

const __vue_inject_styles__ = undefined;
/* scoped */

const __vue_scope_id__ = undefined;
/* module identifier */

const __vue_module_identifier__ = undefined;
/* functional template */

const __vue_is_functional_template__ = false;
/* component normalizer */

import __vue_normalize__ from '../../utils/vue-normalize-component.js';
/* style inject */

/* style inject SSR */

export default __vue_normalize__({
  render: __vue_render__,
  staticRenderFns: __vue_staticRenderFns__
}, __vue_inject_styles__, __vue_script__, __vue_scope_id__, __vue_is_functional_template__, __vue_module_identifier__, undefined, undefined);