js_sdk.uts 1.5 KB
Newer Older
VK1688's avatar
VK1688 已提交
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
function checkPlatform() {
	// #ifdef H5
	type SystemInfo = {
		win: boolean,
		mac: boolean,
		xll: boolean
	};
	let system = {
		win: false,
		mac: false,
		xll: false
	} as SystemInfo;
	let p = navigator.platform;
	
	system.win = p.indexOf("Win") == 0;
	system.mac = p.indexOf("Mac") == 0;
	system.x11 = p == "X11" || p.indexOf("Linux") == 0;
	if (system.win || system.mac || system.xll) {
		let ua = navigator.userAgent.toLowerCase();
		if (ua.indexOf("micromessenger") > -1) {
			// 微信开发者工具下访问(注意微信开发者工具下无法唤起微信公众号支付)
			return "pc-weixin";
		} else {
			return "pc";
		}
	} else {
		if (p.indexOf("iPhone") > -1 || p.indexOf("iPad") > -1) {
			return "ios";
		} else {
			return "android";
		}
	}
	// #endif
}

/**
 * 获取当前H5所在的环境
 */
function getH5Env() {
	// #ifdef H5
	const ua = window.navigator.userAgent.toLowerCase();
	const isWeixin = /micromessenger/i.test(ua);
	const isAlipay = /alipay/i.test(ua);
	const isMiniProgram = /miniprogram/i.test(ua);
	if (isWeixin) {
		if (isMiniProgram) {
			return "mp-weixin";
		} else {
			return "h5-weixin";
		}
	} else if (isAlipay) {
		if (isMiniProgram) {
			return "mp-alipay";
		} else {
			return "h5-alipay";
		}
	}
	return "h5";
	// #endif
}

// json2的属性全部赋值给json1
function objectAssign(json1:UTSJSONObject,json2:UTSJSONObject) :UTSJSONObject{
	for(let key in json2) {
		json1[key] = json2[key];
	}
	return json1;
}

export {
	checkPlatform,
	getH5Env,
	objectAssign,
};