fei.wang
9 天以前 ae7b22322555448d95fd56f505bafa325c167a26
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
 * 横屏适配工具类
 * 用于处理屏幕方向变化和布局适配
 */
 
class OrientationAdapter {
    constructor() {
        this.isLandscape = false;
        this.screenWidth = 0;
        this.screenHeight = 0;
        this.callbacks = [];
        this.deviceType = 'unknown'; // 设备类型
        this.init();
    }
    
    /**
     * 初始化适配器
     */
    init() {
        this.updateScreenInfo();
        this.detectDeviceType();
        this.bindEvents();
    }
    
    /**
     * 更新屏幕信息
     */
    updateScreenInfo() {
        try {
            const systemInfo = uni.getSystemInfoSync();
            this.screenWidth = systemInfo.screenWidth;
            this.screenHeight = systemInfo.screenHeight;
            this.isLandscape = this.screenWidth > this.screenHeight;
            
            console.log('屏幕信息更新:', {
                width: this.screenWidth,
                height: this.screenHeight,
                isLandscape: this.isLandscape,
                deviceType: this.deviceType
            });
        } catch (error) {
            console.error('获取屏幕信息失败:', error);
        }
    }
    
    /**
     * 检测设备类型
     */
    detectDeviceType() {
        const width = this.screenWidth;
        const height = this.screenHeight;
        const maxDimension = Math.max(width, height);
        const minDimension = Math.min(width, height);
        
        // H8-101车载平板检测(1024x768或类似分辨率)
        if (maxDimension >= 1024 && maxDimension <= 1366 && minDimension >= 768) {
            this.deviceType = 'H8-101';
        }
        // 其他车载平板
        else if (maxDimension >= 1024) {
            this.deviceType = 'car-tablet';
        }
        // 手机
        else if (maxDimension < 768) {
            this.deviceType = 'mobile';
        }
        // 桌面设备
        else {
            this.deviceType = 'desktop';
        }
        
        console.log('设备类型检测:', this.deviceType);
    }
    
    /**
     * 绑定事件监听
     */
    bindEvents() {
        // 监听窗口大小变化
        uni.onWindowResize(() => {
            this.handleResize();
        });
        
        // 监听屏幕方向变化(如果平台支持)
        if (typeof uni.onOrientationChange === 'function') {
            uni.onOrientationChange(() => {
                this.handleOrientationChange();
            });
        }
    }
    
    /**
     * 处理窗口大小变化
     */
    handleResize() {
        console.log('窗口大小变化');
        const oldIsLandscape = this.isLandscape;
        const oldDeviceType = this.deviceType;
        
        this.updateScreenInfo();
        this.detectDeviceType();
        
        // 如果方向或设备类型发生变化,触发方向变化回调
        if (oldIsLandscape !== this.isLandscape || oldDeviceType !== this.deviceType) {
            this.handleOrientationChange();
        } else {
            // 仅触发大小变化回调
            this.triggerCallbacks('resize');
        }
    }
    
    /**
     * 处理屏幕方向变化
     */
    handleOrientationChange() {
        console.log('屏幕方向变化:', this.isLandscape ? '横屏' : '竖屏', '设备类型:', this.deviceType);
        
        // 延迟执行,确保方向变化完成
        setTimeout(() => {
            this.triggerCallbacks('orientation');
        }, 300);
    }
    
    /**
     * 添加回调函数
     * @param {string} type - 回调类型:'orientation' | 'resize'
     * @param {Function} callback - 回调函数
     */
    addCallback(type, callback) {
        this.callbacks.push({ type, callback });
    }
    
    /**
     * 触发回调函数
     * @param {string} type - 回调类型
     */
    triggerCallbacks(type) {
        this.callbacks.forEach(({ type: callbackType, callback }) => {
            if (callbackType === type || callbackType === 'all') {
                try {
                    callback({
                        isLandscape: this.isLandscape,
                        screenWidth: this.screenWidth,
                        screenHeight: this.screenHeight,
                        deviceType: this.deviceType
                    });
                } catch (error) {
                    console.error('执行回调函数失败:', error);
                }
            }
        });
    }
    
    /**
     * H8-101车载平板专用画布尺寸计算
     * @param {Object} options - 配置选项
     * @returns {Object} 画布尺寸信息
     */
    calculateH8CanvasSize(options = {}) {
        const {
            padding = 100,
            controlAreaWidth = 750,
            maxCanvasSize = 800,
            minCanvasSize = 500
        } = options;
        
        const availableWidth = this.screenWidth - (padding * 2) - controlAreaWidth;
        const availableHeight = this.screenHeight - (padding * 2);
        
        // H8-101专用计算逻辑
        let canvasSize = Math.max(
            Math.min(availableWidth, availableHeight, maxCanvasSize),
            minCanvasSize
        );
        
        // 确保画布不会太小
        if (canvasSize < minCanvasSize) {
            canvasSize = minCanvasSize;
        }
        
        return {
            canvasSize,
            availableWidth,
            availableHeight,
            padding,
            controlAreaWidth,
            deviceType: 'H8-101'
        };
    }
    
    /**
     * 计算横屏模式下的画布尺寸
     * @param {Object} options - 配置选项
     * @returns {Object} 画布尺寸信息
     */
    calculateLandscapeCanvasSize(options = {}) {
        // 如果是H8-101设备,使用专用计算方法
        if (this.deviceType === 'H8-101') {
            return this.calculateH8CanvasSize(options);
        }
        
        const {
            padding = 80,
            controlAreaWidth = 500,
            maxCanvasSize = 700,
            minCanvasSize = 450
        } = options;
        
        const availableWidth = this.screenWidth - (padding * 2) - controlAreaWidth;
        const availableHeight = this.screenHeight - (padding * 2);
        
        const canvasSize = Math.max(
            Math.min(availableWidth, availableHeight, maxCanvasSize),
            minCanvasSize
        );
        
        return {
            canvasSize,
            availableWidth,
            availableHeight,
            padding,
            controlAreaWidth
        };
    }
    
    /**
     * 计算竖屏模式下的画布尺寸
     * @param {Object} options - 配置选项
     * @returns {Object} 画布尺寸信息
     */
    calculatePortraitCanvasSize(options = {}) {
        const {
            padding = 60,
            buttonAreaHeight = 600,
            maxCanvasSize = 550,
            minCanvasSize = 400
        } = options;
        
        const availableWidth = this.screenWidth - (padding * 2);
        const availableHeight = this.screenHeight - (padding * 2) - buttonAreaHeight;
        
        const canvasSize = Math.max(
            Math.min(availableWidth, availableHeight, maxCanvasSize),
            minCanvasSize
        );
        
        return {
            canvasSize,
            availableWidth,
            availableHeight,
            padding,
            buttonAreaHeight
        };
    }
    
    /**
     * 根据屏幕方向计算画布尺寸
     * @param {Object} options - 配置选项
     * @returns {Object} 画布尺寸信息
     */
    calculateCanvasSize(options = {}) {
        if (this.isLandscape) {
            return this.calculateLandscapeCanvasSize(options);
        } else {
            return this.calculatePortraitCanvasSize(options);
        }
    }
    
    /**
     * 获取当前屏幕信息
     * @returns {Object} 屏幕信息
     */
    getScreenInfo() {
        return {
            isLandscape: this.isLandscape,
            screenWidth: this.screenWidth,
            screenHeight: this.screenHeight,
            aspectRatio: this.screenWidth / this.screenHeight,
            deviceType: this.deviceType
        };
    }
    
    /**
     * 判断是否为车载平板
     * @returns {boolean}
     */
    isCarTablet() {
        return this.deviceType === 'H8-101' || this.deviceType === 'car-tablet';
    }
    
    /**
     * 判断是否为H8-101设备
     * @returns {boolean}
     */
    isH8Device() {
        return this.deviceType === 'H8-101';
    }
    
    /**
     * 判断是否为超宽屏
     * @returns {boolean}
     */
    isUltraWide() {
        return this.screenWidth >= 1366 || this.screenHeight >= 1366;
    }
    
    /**
     * 获取H8-101设备的推荐配置
     * @returns {Object} 推荐配置
     */
    getH8Recommendations() {
        return {
            buttonSize: {
                minWidth: 180,
                minHeight: 120,
                fontSize: 40
            },
            canvasSize: {
                max: 800,
                min: 500
            },
            padding: 100,
            controlAreaWidth: 750
        };
    }
}
 
// 创建单例实例
const orientationAdapter = new OrientationAdapter();
 
export default orientationAdapter;