<script>
  import { getLogin } from '@/services/modules/login.js'
  export default {
    data() {
      return {
        uid: '',
        start: '',
        end: ''
      }
    },
    onLaunch: function(e) {
      this.uid = window.location.href
      this.start = window.location.href.indexOf('uid=')
      this.end = window.location.href.indexOf('&code=')
      this.uid = this.uid ? window.location.href.slice(this.start + 4, this.end) : ''
      if (this.uid) {
        uni.setStorageSync('uid', this.uid)
      }
      if (!e.query.code) {
        window.location.href =
          `https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx70f94d685f50c2d5&redirect_uri=${encodeURIComponent(window.location.href)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
      } else {
        this.uid = uni.getStorageSync('uid')
        getApp().getLogin(e.query.code, this.uid)
      }
    },
    watch: {
      uid: function(newVal, oldVal) {
        this.uid = newVal
      }
    },
    methods: {
      async getLogin(code, uid) {
        try {
          const res = await getLogin(code, uid)
          uni.setStorageSync('token', res.data.token)
          console.log('getLogin', res)
        } catch (err) {
          uni.showToast({ title: err, icon: 'none' })
          console.log('错误', err)
        }
      }
    },
    onShow: function() {
      console.log('App Show')
    },
    onHide: function() {
      console.log('App Hide')
    }
  }
</script>

<style lang="scss">
  /* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
  @import "uview-plus/index.scss";

  page {
    background-color: #000;
    font-size: 28rpx;
    color: #fff;
  }
</style>