<script>
import { loginInfo } from '@/utils/index'
import { zxclogin } from '@/utils/api'
import store from '@/store/index'
export default {
  async created () {
    // await this.checkLogin()
    const code = (await wx.login()).code
    const userinfo = await zxclogin(code)
    // console.log(userinfo)
    userinfo.token = 'Bearer ' + userinfo.token
    store.commit('changeUserInfo', userinfo)
  },
  methods: {
    demo () {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          console.log('等待2s')
          resolve()
        }, 4000)
      })
    },
    /**
     * 检查登录状态
     */
    checkLogin () {
      return new Promise((resolve, reject) => {
        wx.checkSession({
          success: async () => {
            let userInfo = wx.getStorageSync('userInfo')

            if (userInfo) {
              // set userInfo
              this.$store.commit('changeUserInfo', JSON.parse(userInfo))
            } else {
              await loginInfo()
            }
            resolve()
          },
          fail: async () => {
            await loginInfo()
            resolve()
          }
        })
      })
    }
  }
}
</script>

<style lang="less">
@import '../src/assets/less/base.less';
</style>
