import {Injectable} from "@angular/core"; import {DeviceInfoInterface} from "../../core/interfaces/device-info.interface"; import {ScreenService} from "../../core/app-error/service/screen-service"; import {PlatFormInfo} from "../../core/app-error/models/plat_form_info"; import {PLATFORM} from '../../core/enum/platform'; import {AppData} from '../../../df-ext/app-data'; @Injectable() export class DeviceInfoWechatService implements DeviceInfoInterface { private deviceReady: Promise; private _platformInfo: PlatFormInfo; constructor(private screenService: ScreenService) { this.deviceInit(); } getPlatformInfo(): PlatFormInfo { return this._platformInfo; } ready(): Promise { return this.deviceReady; } reload(): Promise { this.deviceInit(); return this.deviceReady; } // 初始化设备信息 deviceInit() { this._platformInfo = new PlatFormInfo(); this.deviceReady = new Promise((resolve, reject) => { // 微信 this._platformInfo.isVirtual = true; // 不是虚拟设备 this._platformInfo.appId = AppData.config.APP_ID; this.getScreenInfo(); this._platformInfo.osVersion = parseFloat(navigator.appVersion).toString(); // 浏览器版本 this._platformInfo.userAgent = navigator.userAgent.toLowerCase(); const matches = this._platformInfo.userAgent.match(/micromessenger\/([a-zA-Z0-9.-]+)/); const version = matches && matches[1]; this._platformInfo.runtimeEnvironment = "Wechart" + " (版本" + version + ")"; const usMatchStrDeviceIOS = this._platformInfo.userAgent.match(/iphone/i); if (usMatchStrDeviceIOS && usMatchStrDeviceIOS[0] === "iphone") { this._platformInfo.platform = PLATFORM.IOS; } const usMatchStrDeviceAND = this._platformInfo.userAgent.match(/android/i); if (usMatchStrDeviceAND && usMatchStrDeviceAND[0] === "android") { this._platformInfo.platform = PLATFORM.Android; } this._platformInfo.serial = "wechat"; // 设备硬件序列号 this._platformInfo.uuid = "wechat"; // 设备UUID this._platformInfo.cordovaVersion = "wechat"; // cordova 版本 this._platformInfo.isCordova = false; this._platformInfo.appVersion = "1.0.0"; this._platformInfo.remoteVersion = "1.0.0"; this._platformInfo.forceUpgrade = false; this._platformInfo.upgradeContent = "微信运行环境无法升级"; if (this._platformInfo.platform == null) { this._platformInfo.platform = PLATFORM.Other; } resolve(this._platformInfo); }); } // 屏幕信息 getScreenInfo() { this._platformInfo.screenWidth = this.screenService.screenInfo.screenWidth; this._platformInfo.screenHeight = this.screenService.screenInfo.screenHeight; this._platformInfo.deviceWidth = this.screenService.screenInfo.deviceWidth; this._platformInfo.deviceHeight = this.screenService.screenInfo.deviceHeight; this._platformInfo.devicePixelRatio = this.screenService.screenInfo.devicePixelRatio; } }