function checkPlatform() {
// #ifdef H5
type SystemInfo = {
win: boolean,
mac: boolean,
x11: boolean
};
let system = {
win: false,
mac: false,
x11: 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.x11) {
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;
}
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
});
};
export {
checkPlatform,
getH5Env,
objectAssign,
getWeixinCode,
getAlipayCode
};