qb-pc-sdk 1.1.0 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +42 -0
- package/index.js +2 -1
- package/package.json +2 -2
- package/src/ad-sdk-wrapper.js +66 -11
- package/src/example-simple.html +21 -4
package/README.md
CHANGED
|
@@ -141,6 +141,31 @@ new AdSDK(config)
|
|
|
141
141
|
|
|
142
142
|
### 实例方法
|
|
143
143
|
|
|
144
|
+
#### getAdSize()
|
|
145
|
+
|
|
146
|
+
获取广告尺寸信息。
|
|
147
|
+
|
|
148
|
+
```javascript
|
|
149
|
+
const adSDK = new AdSDK({
|
|
150
|
+
appId: 'your-app-id',
|
|
151
|
+
placementId: 'your-placement-id',
|
|
152
|
+
container: '#ad-container'
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
// 在广告加载成功后获取尺寸
|
|
156
|
+
adSDK.onAdLoaded = (ad) => {
|
|
157
|
+
const size = adSDK.getAdSize();
|
|
158
|
+
console.log('广告尺寸:', size);
|
|
159
|
+
// 输出: { imageWidth: 640, imageHeight: 360, hasVideo: false, videoUrl: undefined }
|
|
160
|
+
};
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
**返回值**:
|
|
164
|
+
- `imageWidth` (number): 广告图片宽度(像素)
|
|
165
|
+
- `imageHeight` (number): 广告图片高度(像素)
|
|
166
|
+
- `hasVideo` (boolean): 是否有视频
|
|
167
|
+
- `videoUrl` (string | undefined): 视频URL(如果有)
|
|
168
|
+
|
|
144
169
|
#### destroy()
|
|
145
170
|
|
|
146
171
|
销毁广告实例,清理 DOM、解绑事件、释放资源。
|
|
@@ -162,6 +187,23 @@ adSDK.destroy();
|
|
|
162
187
|
- 广告会自动显示关闭按钮(右上角 ×),点击即可关闭
|
|
163
188
|
- 也可以手动调用 `destroy()` 方法销毁广告
|
|
164
189
|
|
|
190
|
+
### 广告尺寸自适应
|
|
191
|
+
|
|
192
|
+
广告会自动适应容器大小。建议在 CSS 中为容器设置明确的宽高:
|
|
193
|
+
|
|
194
|
+
```css
|
|
195
|
+
#ad-container {
|
|
196
|
+
width: 200px;
|
|
197
|
+
height: 120px;
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
**自适应规则**:
|
|
202
|
+
- 广告会完全填充容器,不会超出
|
|
203
|
+
- 图片使用 `object-fit: contain` 模式,完整显示不裁剪
|
|
204
|
+
- 小尺寸容器(宽度<300px 或高度<200px)会自动应用优化样式
|
|
205
|
+
- 文字和按钮会根据容器大小自动调整
|
|
206
|
+
|
|
165
207
|
### 示例
|
|
166
208
|
|
|
167
209
|
#### 基本使用
|
package/index.js
CHANGED
|
@@ -309,7 +309,8 @@ if (isBrowser) {
|
|
|
309
309
|
} catch (err) {
|
|
310
310
|
// 这里不抛出,保持封装层逻辑可继续运行并给出错误回调
|
|
311
311
|
// 在 Vue/React 等环境中,用户可能需要手动引入并挂载
|
|
312
|
-
|
|
312
|
+
const isDev = typeof process !== 'undefined' && process.env && process.env.NODE_ENV !== 'production';
|
|
313
|
+
if (isDev) {
|
|
313
314
|
console.warn('[AdSDK] 底层 SDK 自动加载失败,若在模块化环境中,请手动引入:',
|
|
314
315
|
'import \'qb-pc-ad-sdk-origin/dist/qb-pc-ad-sdk.v1.0.0.js\';', err);
|
|
315
316
|
}
|
package/package.json
CHANGED
package/src/ad-sdk-wrapper.js
CHANGED
|
@@ -290,20 +290,26 @@
|
|
|
290
290
|
GDT_SDK_ID: 2 // 标识
|
|
291
291
|
};
|
|
292
292
|
|
|
293
|
-
//
|
|
293
|
+
// 内部样式,确保广告一定有默认样式,不会塌缩,并支持自适应容器大小
|
|
294
294
|
const INJECT_CSS = `
|
|
295
|
-
.q-ad-wrapper { display: flex; flex-direction: column; width: 100%; height: 100%; background: #fff; border: 1px solid #eee; overflow: hidden; font-family: sans-serif; position: relative; }
|
|
296
|
-
.q-media-container { width: 100%; height:
|
|
297
|
-
.q-ad-img, .q-ad-video { width: 100%; height: 100%; object-fit:
|
|
298
|
-
.q-content-container { padding:
|
|
299
|
-
.q-ad-title { font-size:
|
|
300
|
-
.q-ad-desc { font-size:
|
|
301
|
-
.q-cta-btn { background: #007bff; color: #fff; border: none; padding:
|
|
295
|
+
.q-ad-wrapper { display: flex; flex-direction: column; width: 100%; height: 100%; background: #fff; border: 1px solid #eee; overflow: hidden; font-family: sans-serif; position: relative; box-sizing: border-box; }
|
|
296
|
+
.q-media-container { width: 100%; flex: 1; min-height: 0; background: #000; position: relative; display: flex; align-items: center; justify-content: center; overflow: hidden; }
|
|
297
|
+
.q-ad-img, .q-ad-video { width: 100%; height: 100%; object-fit: contain; display: block; }
|
|
298
|
+
.q-content-container { padding: 8px; flex-shrink: 0; display: flex; flex-direction: column; justify-content: space-between; min-height: 0; }
|
|
299
|
+
.q-ad-title { font-size: 14px; font-weight: bold; color: #333; margin: 0 0 4px 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; line-height: 1.2; }
|
|
300
|
+
.q-ad-desc { font-size: 11px; color: #666; margin: 0 0 6px 0; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; line-height: 1.3; flex-shrink: 0; }
|
|
301
|
+
.q-cta-btn { background: #007bff; color: #fff; border: none; padding: 4px 12px; border-radius: 4px; font-size: 11px; cursor: pointer; align-self: flex-start; flex-shrink: 0; }
|
|
302
302
|
.q-ad-tag { position: absolute; right: 5px; bottom: 5px; background: rgba(0,0,0,0.3); color: #fff; font-size: 10px; padding: 2px 4px; border-radius: 2px; }
|
|
303
|
-
.q-ad-close { position: absolute; top: 5px; right: 5px; width:
|
|
303
|
+
.q-ad-close { position: absolute; top: 5px; right: 5px; width: 20px; height: 20px; background: rgba(0,0,0,0.5); color: #fff; border: none; border-radius: 50%; cursor: pointer; display: flex; align-items: center; justify-content: center; font-size: 14px; line-height: 1; z-index: 10; transition: background 0.2s; flex-shrink: 0; }
|
|
304
304
|
.q-ad-close:hover { background: rgba(0,0,0,0.7); }
|
|
305
305
|
.q-ad-close:active { background: rgba(0,0,0,0.9); }
|
|
306
306
|
.q-ad-destroyed { display: none !important; }
|
|
307
|
+
/* 小尺寸广告优化 */
|
|
308
|
+
.q-ad-wrapper.q-ad-small .q-media-container { flex: 1.2; }
|
|
309
|
+
.q-ad-wrapper.q-ad-small .q-content-container { padding: 6px; }
|
|
310
|
+
.q-ad-wrapper.q-ad-small .q-ad-title { font-size: 12px; }
|
|
311
|
+
.q-ad-wrapper.q-ad-small .q-ad-desc { font-size: 10px; margin-bottom: 4px; }
|
|
312
|
+
.q-ad-wrapper.q-ad-small .q-cta-btn { padding: 3px 10px; font-size: 10px; }
|
|
307
313
|
`;
|
|
308
314
|
|
|
309
315
|
class AdSDKWrapper {
|
|
@@ -538,9 +544,22 @@
|
|
|
538
544
|
renderAd(ad) {
|
|
539
545
|
this.currentAd = ad;
|
|
540
546
|
|
|
547
|
+
// 获取广告尺寸信息
|
|
548
|
+
const adImageWidth = ad.getImageWidth ? ad.getImageWidth() : 0;
|
|
549
|
+
const adImageHeight = ad.getImageHeight ? ad.getImageHeight() : 0;
|
|
550
|
+
|
|
551
|
+
// 获取容器尺寸
|
|
552
|
+
const containerRect = this.container.getBoundingClientRect();
|
|
553
|
+
const containerWidth = containerRect.width || this.container.offsetWidth || 200;
|
|
554
|
+
const containerHeight = containerRect.height || this.container.offsetHeight || 120;
|
|
555
|
+
|
|
556
|
+
// 判断是否为小尺寸广告(宽度或高度小于300px)
|
|
557
|
+
const isSmallAd = containerWidth < 300 || containerHeight < 200;
|
|
558
|
+
const wrapperClass = isSmallAd ? 'q-ad-wrapper q-ad-small' : 'q-ad-wrapper';
|
|
559
|
+
|
|
541
560
|
// 构建 HTML 结构
|
|
542
561
|
this.container.innerHTML = `
|
|
543
|
-
<div class="
|
|
562
|
+
<div class="${wrapperClass}">
|
|
544
563
|
<button class="q-ad-close" title="关闭广告">×</button>
|
|
545
564
|
<div class="q-media-container">
|
|
546
565
|
<div class="q-ad-video" style="display:none"></div>
|
|
@@ -554,6 +573,16 @@
|
|
|
554
573
|
</div>
|
|
555
574
|
</div>
|
|
556
575
|
`;
|
|
576
|
+
|
|
577
|
+
// 输出广告尺寸信息(调试用)
|
|
578
|
+
const isDev = typeof process !== 'undefined' && process.env && process.env.NODE_ENV !== 'production';
|
|
579
|
+
if (isDev) {
|
|
580
|
+
console.log('[AdSDK] 广告尺寸信息:', {
|
|
581
|
+
'广告图片尺寸': `${adImageWidth}x${adImageHeight}`,
|
|
582
|
+
'容器尺寸': `${containerWidth}x${containerHeight}`,
|
|
583
|
+
'是否小尺寸': isSmallAd
|
|
584
|
+
});
|
|
585
|
+
}
|
|
557
586
|
|
|
558
587
|
// 绑定关闭按钮事件
|
|
559
588
|
const closeBtn = this.container.querySelector('.q-ad-close');
|
|
@@ -600,7 +629,33 @@
|
|
|
600
629
|
imgEl.src = ad.getImageUrl();
|
|
601
630
|
}
|
|
602
631
|
|
|
603
|
-
|
|
632
|
+
// 调用 onAdLoaded 回调,传入广告实例和当前 AdSDK 实例
|
|
633
|
+
if (this.config.onAdLoaded) {
|
|
634
|
+
// 使用箭头函数或 bind 确保 this 指向正确
|
|
635
|
+
try {
|
|
636
|
+
this.config.onAdLoaded.call(this, ad, this);
|
|
637
|
+
} catch (e) {
|
|
638
|
+
// 如果回调中出错,不影响广告显示
|
|
639
|
+
console.warn('[AdSDK] onAdLoaded 回调执行出错:', e);
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
/**
|
|
645
|
+
* 获取广告尺寸信息
|
|
646
|
+
* @returns {Object} 包含广告图片宽度、高度等信息
|
|
647
|
+
*/
|
|
648
|
+
getAdSize() {
|
|
649
|
+
if (!this.currentAd) {
|
|
650
|
+
return null;
|
|
651
|
+
}
|
|
652
|
+
|
|
653
|
+
return {
|
|
654
|
+
imageWidth: this.currentAd.getImageWidth ? this.currentAd.getImageWidth() : 0,
|
|
655
|
+
imageHeight: this.currentAd.getImageHeight ? this.currentAd.getImageHeight() : 0,
|
|
656
|
+
hasVideo: this.currentAd.hasVideo ? this.currentAd.hasVideo() : false,
|
|
657
|
+
videoUrl: this.currentAd.getVideoUrl ? this.currentAd.getVideoUrl() : undefined
|
|
658
|
+
};
|
|
604
659
|
}
|
|
605
660
|
|
|
606
661
|
/**
|
package/src/example-simple.html
CHANGED
|
@@ -8,9 +8,9 @@
|
|
|
8
8
|
body { margin: 0; padding: 20px; font-family: sans-serif; background: #f0f2f5; }
|
|
9
9
|
#ad-slot-1 {
|
|
10
10
|
width: 400px;
|
|
11
|
-
height:
|
|
12
|
-
margin:
|
|
13
|
-
|
|
11
|
+
height: 220px;
|
|
12
|
+
margin: 20px auto;
|
|
13
|
+
box-sizing: border-box;
|
|
14
14
|
}
|
|
15
15
|
</style>
|
|
16
16
|
</head>
|
|
@@ -42,7 +42,24 @@
|
|
|
42
42
|
appId: '1999364640831725629',
|
|
43
43
|
placementId: '2003009002186760245',
|
|
44
44
|
container: '#ad-slot-1',
|
|
45
|
-
onAdLoaded: () =>
|
|
45
|
+
onAdLoaded: (ad, adSDKInstance) => {
|
|
46
|
+
console.log('✅ 广告加载完成');
|
|
47
|
+
// 获取广告尺寸信息(使用传入的 adSDKInstance 或外部的 adSDK 都可以)
|
|
48
|
+
try {
|
|
49
|
+
const instance = adSDKInstance || adSDK;
|
|
50
|
+
if (instance && typeof instance.getAdSize === 'function') {
|
|
51
|
+
const size = instance.getAdSize();
|
|
52
|
+
if (size) {
|
|
53
|
+
console.log('📐 广告尺寸:', {
|
|
54
|
+
'图片尺寸': `${size.imageWidth}x${size.imageHeight}`,
|
|
55
|
+
'是否有视频': size.hasVideo
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
} catch (e) {
|
|
60
|
+
console.warn('获取广告尺寸失败:', e);
|
|
61
|
+
}
|
|
62
|
+
},
|
|
46
63
|
onAdError: (err, msg) => console.log('❌ 广告报错:', err, msg),
|
|
47
64
|
onAdExpose: () => console.log('👀 广告曝光'),
|
|
48
65
|
onAdClick: () => console.log('🖱️ 用户点击了广告')
|