<template>
  <div class="hello"></div>
</template>

<script>
// eslint-disable-next-line no-unused-vars
import { getOpenId } from '@/common/api/loginApi.js'
// import { Indicator } from "mint-ui";
// import { mapMutations, mapState } from "vuex";
export default {
  name: 'login',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  },
  created () {
    // 'snake'、'fading-circle'、'double-bounce'、'triple-bounce'
    // Indicator.open({
    //   text: "Loading...",
    //   spinnerType: "triple-bounce"
    // });
    this.getOpenId()
  },
  methods: {
    getQueryString (name) {
      var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
      var r = window.location.search.substr(1).match(reg)
      if (r != null) return unescape(r[2])
      return null
    },
    getOpenId: function () {
      var code = this.getQueryString('code')
      this.$store.commit('setWxCode', code)
      getOpenId(code).then(res => {
        let obj = res
        if (!obj.isSuccess) {
          this.$toast(obj.errMsg)
          return
        }
        this.$store.commit('setResult', obj.isSuccess)
        this.$store.commit('setCode', obj.code)
        this.$store.commit('setMsg', obj.errMsg)
        // eslint-disable-next-line eqeqeq
        if (obj.msg == 'bind') {
          // Indicator.close()
          this.$store.commit('setwxopenid', obj.openId)

          this.$store.commit('setData', obj.data)
          this.$store.commit('setToken', obj.tnToken)

          this.$cookie.set('obj', JSON.stringify(obj)) // 设置cookie
          this.$router.push('/BindHouse')
        // eslint-disable-next-line eqeqeq
        } else if (obj.msg == 'newopenid') {
          this.$store.commit('setwxopenid', obj.openId)
          this.$store.commit('clearData', '')
          // Indicator.close()
          this.$router.push('/loginbytel')
        }
      })
        // eslint-disable-next-line handle-callback-err
        .catch(err => {})
    }
  }
}
</script>

<style scoped>
</style>
