import ColResult from '@/components/col-result'
import { promisefiyPostMessage } from '@/helpers/native-message'
import { setAuth } from '@/helpers/utils'
import { setLogin, state } from '@/state'
import { defineComponent } from 'vue'
import request from './request-home'
import styles from './index.module.less'

export default defineComponent({
  name: 'home-layout-center',
  data() {
    return {
      status: false
    }
  },
  mounted() {
    this.getUserInfo()
  },
  methods: {
    async getUserInfo() {
      try {
        const res = await request.post('/api-auth/smsLogin', {
          requestType: 'form',
          data: {
            clientId: 'student',
            clientSecret: 'student',
            phone: state.orchestraInfo.phone,
            token: state.orchestraInfo.token,
            isSurportRegister: true
          }
        })
        const { authentication } = res.data
        setAuth(authentication.token_type + ' ' + authentication.access_token)
        promisefiyPostMessage({
          api: 'setCache',
          content: {
            key: 'h5-colexiu-token',
            value: authentication.token_type + ' ' + authentication.access_token
          }
        })
        const userCash = await request.get(
          '/api-student/student/queryUserInfo',
          {
            initRequest: true // 初始化接口
          }
        )
        setLogin(userCash.data)
        this.$router.push('/home')
      } catch {
        //
        this.status = true
      }
    }
  },
  render() {
    return (
      <>
        {this.status ? (
          <div class={styles.error}>
            <ColResult
              type="notFond"
              classImgSize="CERT"
              tips="加载失败，请稍后重试"
              buttonText="重新加载"
              plain={true}
              onClick={this.getUserInfo}
            />
          </div>
        ) : (
          <div></div>
        )}
      </>
    )
  }
})
