qb-pc-sdk 1.1.0 → 1.1.1

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 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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "qb-pc-sdk",
3
- "version": "1.1.0",
4
- "description": "趣变广告 SDK 封装器 - 添加关闭按钮增加销毁广告选项",
3
+ "version": "1.1.1",
4
+ "description": "趣变广告 SDK 封装器 - 修复广告自适应问题",
5
5
  "main": "index.js",
6
6
  "files": [
7
7
  "index.js",
@@ -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: 200px; background: #000; position: relative; display: flex; align-items: center; justify-content: center; }
297
- .q-ad-img, .q-ad-video { width: 100%; height: 100%; object-fit: cover; display: block; }
298
- .q-content-container { padding: 12px; flex: 1; display: flex; flex-direction: column; }
299
- .q-ad-title { font-size: 16px; font-weight: bold; color: #333; margin: 0 0 5px 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
300
- .q-ad-desc { font-size: 12px; color: #666; margin: 0 0 10px 0; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
301
- .q-cta-btn { background: #007bff; color: #fff; border: none; padding: 6px 15px; border-radius: 4px; font-size: 12px; cursor: pointer; align-self: flex-start; }
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: 24px; height: 24px; 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: 16px; line-height: 1; z-index: 10; transition: background 0.2s; }
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="q-ad-wrapper">
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,15 @@
554
573
  </div>
555
574
  </div>
556
575
  `;
576
+
577
+ // 输出广告尺寸信息(调试用)
578
+ if (process.env.NODE_ENV !== 'production') {
579
+ console.log('[AdSDK] 广告尺寸信息:', {
580
+ '广告图片尺寸': `${adImageWidth}x${adImageHeight}`,
581
+ '容器尺寸': `${containerWidth}x${containerHeight}`,
582
+ '是否小尺寸': isSmallAd
583
+ });
584
+ }
557
585
 
558
586
  // 绑定关闭按钮事件
559
587
  const closeBtn = this.container.querySelector('.q-ad-close');
@@ -600,7 +628,33 @@
600
628
  imgEl.src = ad.getImageUrl();
601
629
  }
602
630
 
603
- if (this.config.onAdLoaded) this.config.onAdLoaded(ad);
631
+ // 调用 onAdLoaded 回调,传入广告实例和当前 AdSDK 实例
632
+ if (this.config.onAdLoaded) {
633
+ // 使用箭头函数或 bind 确保 this 指向正确
634
+ try {
635
+ this.config.onAdLoaded.call(this, ad, this);
636
+ } catch (e) {
637
+ // 如果回调中出错,不影响广告显示
638
+ console.warn('[AdSDK] onAdLoaded 回调执行出错:', e);
639
+ }
640
+ }
641
+ }
642
+
643
+ /**
644
+ * 获取广告尺寸信息
645
+ * @returns {Object} 包含广告图片宽度、高度等信息
646
+ */
647
+ getAdSize() {
648
+ if (!this.currentAd) {
649
+ return null;
650
+ }
651
+
652
+ return {
653
+ imageWidth: this.currentAd.getImageWidth ? this.currentAd.getImageWidth() : 0,
654
+ imageHeight: this.currentAd.getImageHeight ? this.currentAd.getImageHeight() : 0,
655
+ hasVideo: this.currentAd.hasVideo ? this.currentAd.hasVideo() : false,
656
+ videoUrl: this.currentAd.getVideoUrl ? this.currentAd.getVideoUrl() : undefined
657
+ };
604
658
  }
605
659
 
606
660
  /**
@@ -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: 300px;
12
- margin: 0 auto;
13
- background: #fff;
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: () => console.log('✅ 广告加载完成'),
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('🖱️ 用户点击了广告')