<template>
  <view class="login">
    <en-loading></en-loading>
  </view>
</template>
<script>
  import EnLoading from "../../components/en-utils/en-loading/en-loading";
  import tools from "../../common/js/tools";
  import {login, register} from "@/api/member";
  import tokenpocketBnb from "@/common/wallet/tokenpocket-wallet/tokenpocket-bnb";
  export default {
    components: {
      EnLoading
    },
    data() {
      return {
        formData: {
          address: '',
          inviteCode: ''
        }
      }
    },
    onLoad(query) {
      this.getAccount()
    },
    mounted() {

    },
    methods: {
      async getAccount(){
        console.log('---------------------------------')
        tokenpocketBnb.getAccounts().then(async (Address) => {
          console.log(Address)
          if (Address) {
            this.formData.address=Address
            this.login()
          } else {
            tools.goToError(2)
          }
        }).catch((e)=>{
          console.log(e)
          tools.goToError(2)
        })
      },
      login(Address){
        register(this.formData).then((res)=>{
          if(res.code===1){
            tools.setLoginInfo(res.data)
            setTimeout(()=>{
              uni.reLaunch({
                url: '/pages/index/index'
              })
            },50)
          }else if(res.code===403){
            uni.reLaunch({
              url: 'pages/login/invite?address='+Address
            })
          }else {
            tools.goToError(1)
          }
        })
      }
    },
  }
</script>
<style lang="scss" scoped>
@import url("/static/css/icon-common.css") ;
</style>
