js_sdk.uts 2.3 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
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;
}

VK1688's avatar
VK1688 已提交
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
function getWeixinCode () : Promise<string>{
	return new Promise((resolve, reject) => {
		// #ifdef MP-WEIXIN
		uni.login({
			provider: 'weixin',
			success(res) {
				resolve(res.code)
			},
			fail(err) {
				reject(new Error('获取微信code失败'))
			}
		})
		// #endif
		// #ifndef MP-WEIXIN
		resolve('')
		// #endif
	})
};

function getAlipayCode () : Promise<string>{
	return new Promise((resolve, reject) => {
		// #ifdef MP-ALIPAY
		uni.login({
			provider: 'alipay',
			success(res) {
				resolve(res.code);
			},
			fail(err) {
				reject(new Error('获取支付宝code失败,可能是没有关联appid或你的支付宝开发者工具还没有登录'));
			}
		});
		// #endif
		// #ifndef MP-ALIPAY
		resolve('');
		// #endif
	});
	
};

VK1688's avatar
VK1688 已提交
109 110 111 112
export {
	checkPlatform,
	getH5Env,
	objectAssign,
VK1688's avatar
VK1688 已提交
113 114
	getWeixinCode,
	getAlipayCode
VK1688's avatar
VK1688 已提交
115
};