/** * this util module is about to provide support for device * @module angle-util/deviceUtil */ // 定义type type VersionResult = -1 | 0 | 1 | false // (-1 代表当前app版本号比比较的版本号低,0 代表当前app版本号与比较的版本号一致,1 代表当前app版本号与比较的版本号高。如果传入了非法的version号,返回值会是false) // 定义shape export interface NativeMsg { [propName: string]: any } export default { /** * 判断手机是否在app内部打开 * @author youngbeen * @param * @return { boolean } 是否在app内打开 * @example * deviceUtil.isApp() // true */ isApp (): boolean { const userAgent: string = window.navigator.userAgent.toLowerCase() if (userAgent.indexOf('umsopencore/1.0.0 51') > -1) { return true } return false }, /** * 判断是否是微信环境 * @author youngbeen * @param * @return { boolean } 是否在微信内打开 * @example * deviceUtil.isWechat() // true */ isWechat (): boolean { const userAgent: string = window.navigator.userAgent.toLowerCase() if (userAgent.indexOf('micromessenger') === -1) { return false } return true }, /** * 判断是否是IOS设备 * @author youngbeen * @param * @return { boolean } 是否在IOS内打开 * @example * deviceUtil.isIOS() // true */ isIOS (): boolean { const ua: string = window.navigator.userAgent let result: boolean = /iPad|iPhone|iPod/.test(ua) && !(window).MSStream return result }, /** * 判断是否是Android设备 * @author youngbeen * @param * @return { boolean } 是否在Android内打开 * @example * deviceUtil.isAndroid() // true */ isAndroid (): boolean { const ua: string = window.navigator.userAgent if (ua.indexOf('Android') > -1 || ua.indexOf('Adr') > -1) { return true } else { return false } }, downloadApp (): void { window.location.href = 'http://info.snapp.chinaums.com/snapp/info/www/qmcs_download/index.html' }, /** * 同ums native通信 * @author youngbeen * @param { object } 传递的js结构体 * @return * @example * deviceUtil.communicateToNative({ msg: 'test' }) */ communicateToNative (jsBridgeParam: NativeMsg = {}): void { if (this.isIOS()) { (window).webkit.messageHandlers.jsToNativeData.postMessage({ body: jsBridgeParam }) } else if (this.isAndroid()) { let paramJsonStr: string = JSON.stringify(jsBridgeParam); (window).Android.jsToNativeMethod(paramJsonStr) } }, /** * 比较app版本号 * @author youngbeen * @param { string } version - 要比较的目标版本号 * @return -1,0,1,false (-1 代表当前app版本号比比较的版本号低,0 代表当前app版本号与比较的版本号一致,1 代表当前app版本号与比较的版本号高。如果传入了非法的version号,返回值会是false) * @example * deviceUtil.compareVersion('4.16.0.0') // 1, 说明当前App版本号高于4.16.0.0 */ compareVersion (version: string): VersionResult { if (version) { let appVersion: RegExpMatchArray | string = navigator.userAgent.match(/feifan;([\d\.]+)/i) appVersion = appVersion ? appVersion[1] : '0.0.0.0' let appArray: Array = appVersion.split('.') let tarArray: Array = version.split('.') for (let i: number = 0; i < appArray.length || i < tarArray.length; i++) { let n1: string | number =+ appArray[i] let n2: string | number =+ tarArray[i] if (isNaN(n1)) n1 = 0 if (isNaN(n2)) n2 = 0 if (n1 < n2) return -1 if (n1 > n2) return 1 } return 0 } else { return false } }, /** * 转化H5 url为schema url * @author youngbeen * @param { string } url - 待转化的H5 url * @return { string } 转化好的schema url或者空字符串 * @example * let h5Url = 'https://www.abc.com/' * let schemaUrl = deviceUtil.toSchema(h5Url) // wandaappfeifan://app/CommonWebView?url=https%3A%2F%2Fwww.abc.com%2F * schemaUrl = deviceUtil.toSchema('') // '' */ toSchema (url: string): string { return `wandaappfeifan://app/CommonWebView?url=${encodeURIComponent(url)}` } }