online-sales-sdk
Version:
OnlineSales.ai Web Tag SDK 封装,适用于 Vue/React 项目,纯JS实现
177 lines (163 loc) • 6.58 kB
JavaScript
const DEFAULT_SCRIPT_URL = "https://c.o-s.io/osmos-web-sdk/sdk.js";
// 加载 SDK 脚本(优化:onload 后继续轮询 window.osmos)
function loadScript(src) {
return new Promise((resolve, reject) => {
if (typeof window === "undefined") return reject(new Error("Not in browser"));
if (window.osmos) return resolve(window.osmos);
// 防止重复插入
if (document.querySelector('script[data-osmos-sdk]')) {
const timer = setInterval(() => {
if (window.osmos) {
clearInterval(timer);
resolve(window.osmos);
}
}, 50);
setTimeout(() => {
clearInterval(timer);
reject(new Error("osmos SDK 加载超时"));
}, 5000);
return;
}
const script = document.createElement("script");
script.type = "text/javascript";
script.async = true;
script.src = src;
script.setAttribute('data-osmos-sdk', '1');
script.onload = () => {
// onload 后继续轮询 window.osmos
const timer = setInterval(() => {
if (window.osmos) {
clearInterval(timer);
resolve(window.osmos);
}
}, 50);
setTimeout(() => {
clearInterval(timer);
reject(new Error("osmos SDK 挂载超时"));
}, 5000);
};
script.onerror = (e) => reject(new Error("osmos SDK 加载失败: " + e?.message));
document.head.appendChild(script);
});
}
class OnlineSalesSDK {
constructor(options = {}) {
this.scriptUrl = options.scriptUrl || DEFAULT_SCRIPT_URL;
this.readyPromise = null;
this._inited = false;
}
async load() {
if (!this.readyPromise) {
if (typeof window !== 'undefined') {
// 浏览器环境
this.readyPromise = loadScript(this.scriptUrl);
} else {
// Node.js 环境
// 假设 scriptUrl 是本地或可 require 的模块路径
// 如果是远程 URL,需要先下载到本地或用 vm 执行
this.readyPromise = import(this.scriptUrl);
// 或者:this.readyPromise = Promise.resolve(require(this.scriptUrl));
}
}
return await this.readyPromise;
}
// 初始化
async initialize(options) {
const osmos = await this.load();
if (!osmos) throw new Error("osmos SDK 加载失败");
await osmos.initialize(options);
this._inited = true;
}
// 检查是否初始化
_checkInited() {
if (!this._inited) throw new Error("请先调用 initialize() 初始化 SDK");
}
// 通用广告方法
async fetchAd(method, params) {
this._checkInited();
const osmos = await this.load();
if (!osmos?.adFetcher?.[method]) throw new Error(`方法 ${method} 不存在`);
return osmos.adFetcher[method](params);
}
// 埋点
async trackerSdk(method, params) {
this._checkInited();
const osmos = await this.load();
if (!osmos?.tracker?.[method]) throw new Error(`方法 ${method} 不存在`);
return osmos.tracker[method](params);
}
registerAdClickEvent(params) { return this.trackerSdk('registerAdClickEvent', params); }
registerAdImpressionEvent(params) { return this.trackerSdk('registerAdImpressionEvent', params); }
registerViewProductEvent(params) { return this.trackerSdk('registerViewProductEvent', params); }
registerAdd2CartEvent(params) { return this.trackerSdk('registerAdd2CartEvent', params); }
registerPurchaseEvent(params) { return this.trackerSdk('registerPurchaseEvent', params); }
// 兼容原有方法
fetchDisplayAds(params) { return this.fetchAd('fetchDisplayAds', params); }
fetchPLAProductPageAds(params) { return this.fetchAd('fetchPLAProductPageAds', params); }
fetchPLASearchPageAds(params) { return this.fetchAd('fetchPLASearchPageAds', params); }
fetchPLACategoryPageAd(params) { return this.fetchAd('fetchPLACategoryPageAd', params); }
fetchPLATPAPageAd(params) { return this.fetchAd('fetchPLATPAPageAd', params); }
fetchPLAPurchasePageAd(params) { return this.fetchAd('fetchPLAPurchasePageAd', params); }
fetchPLAHomePageAd(params) { return this.fetchAd('fetchPLAHomePageAd', params); }
fetchPLACustomPageAd(params) { return this.fetchAd('fetchPLACustomPageAd', params); }
}
// 判断环境
function isBrowser() {
return typeof window !== "undefined" && typeof document !== "undefined";
}
// 服务端实现
class OnlineSalesSDKServer {
constructor(options = {}) {
this.scriptUrl = options.scriptUrl || DEFAULT_SCRIPT_URL;
this.readyPromise = null;
this._inited = false;
}
async load() {
if (!this.readyPromise) {
if (typeof window !== 'undefined') {
// 浏览器环境
this.readyPromise = loadScript(this.scriptUrl);
} else {
// Node.js 环境
// 假设 scriptUrl 是本地或可 require 的模块路径
// 如果是远程 URL,需要先下载到本地或用 vm 执行
this.readyPromise = import(this.scriptUrl);
// 或者:this.readyPromise = Promise.resolve(require(this.scriptUrl));
}
}
return await this.readyPromise;
}
// 初始化
async initialize(options) {
const osmos = await this.load();
if (!osmos) throw new Error("osmos SDK 加载失败");
await osmos.initialize(options);
this._inited = true;
}
_checkInited() { if (!this._inited) throw new Error("请先调用 initialize() 初始化 SDK"); }
// 通用广告方法
async fetchAd(method, params) {
this._checkInited();
if (!osmos?.adFetcher?.[method]) throw new Error(`方法 ${method} 不存在`);
return osmos.adFetcher[method](params);
}
// 兼容原有方法
fetchDisplayAds(params) { return this.fetchAd('fetchDisplayAds', params); }
fetchPLAProductPageAds(params) { return this.fetchAd('fetchPLAProductPageAds', params); }
fetchPLASearchPageAds(params) { return this.fetchAd('fetchPLASearchPageAds', params); }
fetchPLACategoryPageAd(params) { return this.fetchAd('fetchPLACategoryPageAd', params); }
fetchPLATPAPageAd(params) { return this.fetchAd('fetchPLATPAPageAd', params); }
fetchPLAPurchasePageAd(params) { return this.fetchAd('fetchPLAPurchasePageAd', params); }
fetchPLAHomePageAd(params) { return this.fetchAd('fetchPLAHomePageAd', params); }
fetchPLACustomPageAd(params) { return this.fetchAd('fetchPLACustomPageAd', params); }
}
// 浏览器端实现(保持你原有逻辑)
// 自动切换
let sdkInstance = null;
function getOnlineSalesSDK(options) {
if (!sdkInstance) {
sdkInstance = isBrowser() ? new OnlineSalesSDK(options) : new OnlineSalesSDK(options);
}
return sdkInstance;
}
export default getOnlineSalesSDK;