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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import {
    query
} from '../common/sqlite'
import {
    ActivationConfig
} from '../config/activation'
 
// 格式化日期为 YYYY-MM-DD
function formatDate(date) {
    const year = date.getFullYear()
    const month = (date.getMonth() + 1).toString().padStart(2, '0')
    const day = date.getDate().toString().padStart(2, '0')
    return `${year}-${month}-${day}`
}
 
// 计算剩余天数
function calculateDaysLeft(expiryDate) {
    const now = new Date()
    return Math.ceil((new Date(expiryDate) - now) / (1000 * 60 * 60 * 24))
}
 
export default {
 
    async checkDataCount() {
        try {
            if (ActivationConfig.isDebugVersion) {
                //#ifdef APP-PLUS
                const orderCounts = await query('select count(*) AS orderCount from orders');
                const orderCount = orderCounts[0].orderCount;
 
                const detailCounts = await query('select count(*) AS detailCount from scan_records');
                const detailCount = detailCounts[0].detailCount;
                
                if (orderCount > 20 || detailCount > 2000) {
                    return {
                        expStatus: true,
                        message: '试用版已到使用期,请联系管理员激活'
                    }
                }
                //#endif
            }
            // 临时使用,后续再改激活检测
            return {
                expStatus: false
            }
 
        } catch (e) {
            console.log(e);
            return {
                expStatus: true,
                message: e
            }
        }
    },
 
    // 使用激活码激活系统
    activateWithCode(activationCode) {
        // 验证激活码格式
        if (!activationCode || typeof activationCode !== 'string' || activationCode.length < 10) {
            return {
                success: false,
                message: '激活码格式不正确'
            }
        }
 
        // 在实际应用中,这里应该发送请求到服务器验证激活码
        // 这里简化处理,只检查激活码前缀
        if (activationCode.startsWith('SMTPDA-')) {
            const now = new Date()
            // 假设激活码有效期为1年
            const expiryDate = new Date()
            expiryDate.setFullYear(now.getFullYear() + 1)
 
            const activationInfo = {
                activatedDate: formatDate(now),
                expiryDate: formatDate(expiryDate),
                daysLeft: calculateDaysLeft(formatDate(expiryDate)),
                isTrial: false,
                isDebug: false,
                isValid: true
            }
 
            // 保存到本地存储
            uni.setStorageSync(ActivationConfig.storageKeys.activationInfo, JSON.stringify(activationInfo))
 
            return {
                success: true,
                message: '系统已成功激活',
                activationInfo
            }
        } else {
            return {
                success: false,
                message: '无效的激活码'
            }
        }
    },
 
    // 激活调试版本
    activateDebugVersion() {
        const now = new Date()
        const activationInfo = {
            activatedDate: formatDate(now),
            expiryDate: ActivationConfig.debugExpiryDate,
            daysLeft: calculateDaysLeft(ActivationConfig.debugExpiryDate),
            isTrial: false,
            isDebug: true
        }
 
        // 保存到本地存储
        uni.setStorageSync(ActivationConfig.storageKeys.debugActivationInfo, JSON.stringify(activationInfo))
 
        return activationInfo
    },
 
    // 激活试用版本
    activateTrialVersion() {
        const now = new Date()
        const expiryDate = new Date()
        expiryDate.setDate(now.getDate() + ActivationConfig.trialDays)
 
        const activationInfo = {
            activatedDate: formatDate(now),
            expiryDate: formatDate(expiryDate),
            daysLeft: ActivationConfig.trialDays,
            isTrial: true,
            isDebug: false
        }
 
        // 保存到本地存储
        uni.setStorageSync(ActivationConfig.storageKeys.activationInfo, JSON.stringify(activationInfo))
 
        return activationInfo
    },
 
    // 检查激活状态
    checkActivationStatus() {
 
        // 从本地存储获取激活状态
        const isActivated = uni.getStorageSync(ActivationConfig.storageKeys.isActivated)
        if (isActivated) {
            // 如果已激活,直接返回激活信息
            const activationInfo = uni.getStorageSync(ActivationConfig.storageKeys.activationInfo)
            if (activationInfo) {
                try {
                    const info = JSON.parse(activationInfo)
                    return {
                        ...info,
                        daysLeft: calculateDaysLeft(info.expiryDate),
                        isValid: true
                    }
                } catch (e) {
                    console.error('激活信息解析失败:', e)
                }
            }
        }
 
        // 检查是否为调试版本
        const isDebugVersion = ActivationConfig.isDebugVersion
 
        if (isDebugVersion) {
            // 调试版本逻辑
            const debugActivationInfo = uni.getStorageSync(ActivationConfig.storageKeys.debugActivationInfo)
 
            if (debugActivationInfo) {
                try {
                    const info = JSON.parse(debugActivationInfo)
                    const now = new Date()
                    const expiryDate = new Date(info.expiryDate)
 
                    if (expiryDate > now) {
                        uni.setStorageSync(ActivationConfig.storageKeys.isActivated, true)
                        // 调试版本激活有效
                        return {
                            ...info,
                            daysLeft: calculateDaysLeft(info.expiryDate),
                            isValid: true
                        }
                    } else {
                        return {
                            success: false,
                            daysLeft: calculateDaysLeft(info.expiryDate),
                            message: '调试版本已过期'
                        }
                    }
                } catch (e) {
                    console.error('调试激活信息解析失败:', e)
                }
            }
 
            // 如果没有有效的调试激活信息,重新激活
            return this.activateDebugVersion()
        } else {
            // 非调试版本逻辑
            const activationInfo = uni.getStorageSync(ActivationConfig.storageKeys.activationInfo)
 
            if (activationInfo) {
                try {
                    const info = JSON.parse(activationInfo)
                    const now = new Date()
                    const expiryDate = new Date(info.expiryDate)
 
                    if (expiryDate > now) {
                        // 试用版本激活有效
                        return {
                            ...info,
                            daysLeft: calculateDaysLeft(info.expiryDate),
                            isValid: true
                        }
                    }
                } catch (e) {
                    console.error('激活信息解析失败:', e)
                }
            }
 
            // 如果没有有效的激活信息,激活试用版本
            return this.activateTrialVersion()
        }
    },
 
    // 生成设备运行码
    generateRunningCode(deviceId) {
        // 获取当前时间戳
        const timestamp = Math.floor(Date.now() / 1000)
 
        // 生成随机数
        const randomStr = Math.random().toString(36).substr(2, 8).toUpperCase()
 
        // 格式化设备ID(只保留字母数字)
        const formattedDeviceId = (deviceId || '').replace(/[^a-zA-Z0-9]/g, '')
 
        // 生成运行码
        const prefix = ActivationConfig.isDebugVersion ? 'SMTPDA-DEBUG' : 'SMTPDA'
        return `${prefix}-${timestamp}-${formattedDeviceId}-${randomStr}`
    }
}