import {Injectable} from "@angular/core"; import {DeviceInfoInterface} from "../../core/interfaces/device-info.interface"; import * as dd from 'dingtalk-jsapi'; 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 DeviceInfoDingtalkService 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 = false; this._platformInfo.appId = AppData.config.APP_ID; this._platformInfo.userAgent = navigator.userAgent.toLowerCase(); this.getScreenInfo(); const usMatchStrWin = this._platformInfo.userAgent.match(/dingtalk-win/i); if (usMatchStrWin && usMatchStrWin[0] === "dingtalk-win") { const matches = this._platformInfo.userAgent.match(/dingtalk\(([a-zA-Z0-9.-]+)\)/); const version = matches && matches[1]; this._platformInfo.runtimeEnvironment = "DingWin" + " (版本" + version + ")"; } const usMatchStr = this._platformInfo.userAgent.match(/aliapp/i); if (usMatchStr && usMatchStr[0] === "aliapp") { let matches = this._platformInfo.userAgent.match(/aliapp\(\w+\/([a-zA-Z0-9.-]+)\)/); // android兼容处理 if (matches === null) { matches = this._platformInfo.userAgent.match(/dingtalk\/([a-zA-Z0-9.-]+)/); } const version = matches && matches[1]; this._platformInfo.runtimeEnvironment = "DingApp" + " (版本" + version + ")"; const usMatchStrDeviceIOS = this._platformInfo.userAgent.match(/iPhone/i); // |iPad|iPod if (usMatchStrDeviceIOS && (usMatchStrDeviceIOS[0] === "iphone")) { // "iPad"、"iPod" this._platformInfo.platform = PLATFORM.IOS; } const usMatchStrDeviceAND = this._platformInfo.userAgent.match(/android/i); if (usMatchStrDeviceAND && usMatchStrDeviceAND[0] === "android") { this._platformInfo.platform = PLATFORM.Android; } if (this._platformInfo.platform == null) { this._platformInfo.platform = PLATFORM.Other; } this._platformInfo.serial = "dingtalk"; // 设备硬件序列号 this._platformInfo.cordovaVersion = "dingtalk"; // cordova 版本 this._platformInfo.isCordova = false; this._platformInfo.appVersion = "1.0.0"; this._platformInfo.remoteVersion = "1.0.0"; this._platformInfo.forceUpgrade = false; this._platformInfo.upgradeContent = "钉钉运行环境无法升级"; // 鉴权后获取 dd.device.base.getUUID({ onSuccess: (data) => { this._platformInfo.uuid = data.uuid; }, onFail : function(err) { this.m2log.log("钉钉手机UUID获取失败:" + JSON.stringify(err)); } }); // 钉钉手机基础信息 dd.device.base.getPhoneInfo({ onSuccess: (data) => { this._platformInfo.screenWidth = data.screenWidth.toString(); this._platformInfo.screenHeight = data.screenHeight.toString(); this._platformInfo.manufacturer = data.brand.toString(); this._platformInfo.model = data.model.toString(); this._platformInfo.osVersion = data.version.toString(); resolve(this._platformInfo); // ????????? }, onFail : function(err) { this.m2log.log("钉钉手机基础信息获取失败:" + JSON.stringify(err)); reject(err); } }); // 查看鉴权错误信息 dd.error(function(err) { /* 错误信息": 信息会展示出钉钉服务端生成签名使用的参数,请和计算签名的参数作对比,找出错误的参数 */ this.m2log.log('查看鉴权错误信息----->: ' + JSON.stringify(err)); }); } }); } // 屏幕信息 getScreenInfo() { this._platformInfo.deviceWidth = this.screenService.screenInfo.deviceWidth; this._platformInfo.deviceHeight = this.screenService.screenInfo.deviceHeight; this._platformInfo.devicePixelRatio = this.screenService.screenInfo.devicePixelRatio; } }