vtils
Version:
一个面向业务的 JavaScript/TypeScript 实用程序库。
48 lines (47 loc) • 1.54 kB
JavaScript
import _createForOfIteratorHelperLoose from "@babel/runtime/helpers/esm/createForOfIteratorHelperLoose";
import { castArray } from 'lodash-uni';
var brands = ['微信', 'QQ', '支付宝', '京东', '百度', '字节跳动', '钉钉'];
var factories = {
微信: function 微信() {
return typeof wx !== 'undefined' && wx;
},
QQ: function QQ() {
return typeof qq !== 'undefined' && qq;
},
支付宝: function 支付宝() {
return typeof my !== 'undefined' && my;
},
京东: function 京东() {
return typeof jd !== 'undefined' && jd;
},
百度: function 百度() {
return typeof swan !== 'undefined' && swan;
},
字节跳动: function 字节跳动() {
return typeof tt !== 'undefined' && tt;
},
钉钉: function 钉钉() {
return typeof dd !== 'undefined' && dd;
}
};
/**
* 检查是否在指定品牌的小程序中,若在,返回承载其 API 的全局对象,若不在,返回 false。
*
* @param brand 指定的小程序品牌,若未指定,则表示所有小程序品牌
* @returns 返回检查结果
*/
export function inMiniProgram(brand) {
for (var _iterator = _createForOfIteratorHelperLoose(brand ? castArray(brand) : brands), _step; !(_step = _iterator()).done;) {
var currentBrand = _step.value;
if (factories[currentBrand]) {
var mp = factories[currentBrand]();
if (mp && typeof mp.getSystemInfoSync === 'function') {
// @ts-ignore
;
mp.$brand = currentBrand;
return mp;
}
}
}
return false;
}