ylh-quick-app-ad-sdk
Version:
安装命令: 通过 `npm` 安装: ```sh npm install ylh-quick-app-ad-sdk ``` 通过 `yarn` 安装: ```sh yarn add ylh-quick-app-ad-sdk ```
99 lines (92 loc) • 3.07 kB
JavaScript
export function dispatchAdEvent(eventName, params) {
try {
if (this.isAdDestroyed) return;
if (this.lifetimes && this.lifetimes?.[eventName]) {
this.lifetimes[eventName].call(this, params);
} else if (this.events && this.events?.[eventName]) {
this.events[eventName].call(this, params);
}
} catch (error) {
console.warn('dispatchAdEvent error: ', eventName, error.message, params);
}
}
function registerProps(componentName, adTimer) {
if (this.$app) {
const component = this.$app.$def?.ylh_sdk?.components?.[componentName];
if (component) {
Object.keys(component).forEach((prop) => {
this[prop] = component[prop];
});
dispatchAdEvent.call(this, 'onInit', componentName);
if (adTimer) {
clearInterval(adTimer);
}
}
}
}
export function defineAdComponent(componentName) {
try {
if (this.adTimer) {
clearInterval(this.adTimer);
}
if (this.$app?.$def?.ylh_sdk?.components) {
registerProps.call(this, componentName, undefined);
} else {
let time = 0;
const adTimer = setInterval(() => {
if (time > 1000 * 5) {
clearInterval(adTimer);
}
registerProps.call(this, componentName, adTimer);
time += 10;
}, 10);
}
} catch (error) {
console.error(error.message);
if (this.onYLHSDKError) {
this.onYLHSDKError(error.message);
}
}
}
export function defineAdScene(scene) {
defineAdComponent.call(this, scene);
}
/**
* 检查类函数是否通过 new 关键字正确调用
* @param {Object} sdkInstance 实例对象
* @param {function} sdkConstructor 构造函数
*/
export function classCallCheck(sdkInstance, sdkConstructor) {
if (!(sdkInstance instanceof sdkConstructor)) {
throw new TypeError('Cannot call a class as a function');
}
}
// https://qzs.gdtimg.com/union/res/ylh-sdk-core-v1.3.1030.min.js?d=13333' 从url解析出['1.3', 1030]信息
export function getCoreVersionByUrl(jsUrl = '') {
const res = jsUrl.match(/ylh-sdk-(?:[a-z]+)-v(\d+\.\d+\.\d+)(?:@debug)?\.min\.js/);
const str = res && res[1] ? res[1] : ''; // '1.3.1030.min/1.3.1030@debug.min'
const [a, b, c] = str.split('.');
return [`${a}.${b}.0`, Number(c.substr(0, 4))];
}
// 初始化时, 为onYlhReady添加链式调用方法
export function initYLHSDK($app) {
if ($app.$def?.ylh_sdk) {
return;
}
// eslint-disable-next-line no-param-reassign
$app.$def.ylh_sdk = {
// eslint-disable-next-line consistent-return
onYlhReady(callback) {
const { onYlhReadyCallback = [] } = $app.$def.ylh_sdk;
// console.log('app.$def.ylh_sdk', app.$def.ylh_sdk, onYlhReadyCallback);
if (typeof callback !== 'function') {
console.warn('onYlhReady: then回调必须传递一个方法');
} else if ($app.$def.ylh_sdk.SDK_CORE_READY) {
return callback();
} else {
// eslint-disable-next-line no-param-reassign
$app.$def.ylh_sdk.onYlhReadyCallback = [...onYlhReadyCallback, callback];
}
},
};
}