<script>
  import { silentLogin } from '@/apis/mineApi'
  import { getUserAccountInfo } from '@/apis/commonApi'
  export default {
    methods: {
      handleLogin() {
        getUserAccountInfo().then(({ openId, tmpAuthToken }) => {
          this.$store.commit('setOpenId', openId)
          silentLogin({ authCode: tmpAuthToken }).then((res) => {
            if (this.$store.state.userInfo.userId !== res.userId) {
              let userInfo = {
                userId: res.userId,
                name: res.nickName || '',
                avatar: res.avatarUrl || '',
                mobile: ''
              }
              this.$store.commit('setUserInfo', userInfo)
            }
            this.$store.commit('setToken', res.token)
          })
        })
      }
    },
    onLaunch: function () {
      const updateManager = uni.getUpdateManager()
      this.handleLogin()
      updateManager.onCheckForUpdate(function (res) {
        // 请求完新版本信息的回调
        console.log(res.hasUpdate)
      })

      updateManager.onUpdateReady(function (res) {
        uni.showModal({
          title: '更新提示',
          content: '新版本已经准备好，是否重启应用？',
          success(res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate()
            }
          }
        })
      })

      updateManager.onUpdateFailed(function (res) {
        // 新的版本下载失败
      })
    }
  }
</script>

<style lang="scss">
  /*每个页面公共css */
  @import url('./common/css/reset.scss');
  @import '@/static/icon/iconfont.css';
  page {
    height: 100%;
  }
  /* uni-dialog标题样式 */
  .uni-dialog-title-text {
    font-size: 32rpx !important;
    color: #323233 !important;
    font-weight: 500 !important;
  }
</style>
