z8018
8 天以前 d5317aef1dbb595923af02ede8bfa33ba37d6eb6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { checkActivation } from './activationService';
 
// 获取设备信息
export const getDeviceInfo = () => {
  try {
    const device = uni.getDeviceInfo();
    return {
      id: device.deviceId || 'unknown',
      model: device.model || 'unknown',
      brand: device.brand || 'unknown',
      system: device.system || 'unknown',
      version: device.version || 'unknown'
    };
  } catch (error) {
    console.error('获取设备信息失败:', error);
    return {
      id: 'error',
      model: 'error',
      brand: 'error',
      system: 'error',
      version: 'error'
    };
  }
};
 
// 检查试用期状态
export const checkTrialStatus = async () => {
  try {
    // 检查激活状态
    const activation = await checkActivation();
    if (activation.isValid) {
      return { isTrial: false, isValid: true };
    }
    
    // 检查试用期
    const trialEndDate = new Date('2023-12-31'); // 应从系统设置获取
    const today = new Date();
    const isTrial = today <= trialEndDate;
    
    return {
      isTrial,
      isValid: isTrial,
      daysLeft: isTrial ? Math.ceil((trialEndDate - today) / (1000 * 60 * 60 * 24)) : 0,
      trialEndDate: trialEndDate.toISOString().split('T')[0]
    };
    
  } catch (error) {
    console.error('检查试用期状态失败:', error);
    return { isTrial: false, isValid: false };
  }
};
 
// 全局试用期检查拦截
export const setupTrialInterceptor = () => {
  // 应用启动时检查
  uni.onAppShow(() => {
    checkTrialStatus().then(status => {
      if (!status.isValid) {
        uni.redirectTo({
          url: '/pages/system/activation'
        });
      }
    });
  });
  
  // 路由跳转前检查
  uni.addInterceptor('navigateTo', {
    invoke(args) {
      return checkTrialStatus().then(status => {
        if (!status.isValid && !args.url.includes('/system/activation')) {
          uni.redirectTo({
            url: '/pages/system/activation'
          });
          return false;
        }
        return true;
      });
    }
  });
};