<template>
  <view id="app">
    <!-- 牛只交易小程序入口 -->
  </view>
</template>

<script>
export default {
  name: 'App',
  
  onLaunch: function() {
    console.log('牛只交易小程序启动');
    this.initApp();
  },
  
  onShow: function() {
    console.log('牛只交易小程序显示');
  },
  
  onHide: function() {
    console.log('牛只交易小程序隐藏');
  },
  
  methods: {
    // 初始化应用
    initApp() {
      // 检查更新
      this.checkUpdate();
      
      // 初始化全局数据
      this.initGlobalData();
      
      // 检查登录状态
      this.checkLoginStatus();
    },
    
    // 检查小程序更新
    checkUpdate() {
      // #ifdef MP-WEIXIN
      if (uni.canIUse('getUpdateManager')) {
        const updateManager = uni.getUpdateManager();
        
        updateManager.onCheckForUpdate((res) => {
          if (res.hasUpdate) {
            console.log('发现新版本');
          }
        });
        
        updateManager.onUpdateReady(() => {
          uni.showModal({
            title: '更新提示',
            content: '新版本已经准备好，是否重启应用？',
            success: (res) => {
              if (res.confirm) {
                updateManager.applyUpdate();
              }
            }
          });
        });
        
        updateManager.onUpdateFailed(() => {
          uni.showModal({
            title: '更新失败',
            content: '新版本下载失败，请检查网络后重试',
            showCancel: false
          });
        });
      }
      // #endif
    },
    
    // 初始化全局数据
    initGlobalData() {
      getApp().globalData = {
        userInfo: null,
        token: '',
        baseUrl: 'https://api.cattle-trading.com',
        version: '1.0.0'
      };
    },
    
    // 检查登录状态
    checkLoginStatus() {
      const token = uni.getStorageSync('token');
      if (token) {
        getApp().globalData.token = token;
        this.validateToken(token);
      }
    },
    
    // 验证token
    async validateToken(token) {
      try {
        const res = await uni.request({
          url: getApp().globalData.baseUrl + '/auth/validate',
          method: 'POST',
          header: {
            'Authorization': 'Bearer ' + token
          }
        });
        
        if (res.data.code === 200) {
          getApp().globalData.userInfo = res.data.data;
        } else {
          uni.removeStorageSync('token');
          getApp().globalData.token = '';
        }
      } catch (error) {
        console.error('验证token失败：', error);
      }
    }
  }
};
</script>

<style lang="scss">
@import '@/uni_modules/uni-scss/index.scss';

page {
  background-color: #f5f5f5;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', sans-serif;
}

.container {
  padding: 20rpx;
}

.page-container {
  min-height: 100vh;
  background-color: #f5f5f5;
}

.card {
  background: #ffffff;
  border-radius: 16rpx;
  padding: 24rpx;
  margin-bottom: 20rpx;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.1);
}

.btn-primary {
  background: linear-gradient(135deg, #FF6B35, #FF8E53);
  color: #ffffff;
  border: none;
  border-radius: 12rpx;
  padding: 24rpx 48rpx;
  font-size: 32rpx;
  font-weight: 500;
}

.btn-secondary {
  background: #ffffff;
  color: #FF6B35;
  border: 2rpx solid #FF6B35;
  border-radius: 12rpx;
  padding: 22rpx 46rpx;
  font-size: 32rpx;
}

.text-primary {
  color: #FF6B35;
}

.text-secondary {
  color: #666666;
}

.text-muted {
  color: #999999;
}

.flex {
  display: flex;
}

.flex-column {
  flex-direction: column;
}

.flex-center {
  align-items: center;
  justify-content: center;
}

.flex-between {
  justify-content: space-between;
}

.flex-around {
  justify-content: space-around;
}

.flex-1 {
  flex: 1;
}

.ellipsis {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ellipsis-2 {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.fade-in {
  animation: fadeIn 0.3s ease-in-out;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(20rpx);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}
</style>