jyj-components 1.0.2 → 1.0.3

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.
@@ -0,0 +1,9 @@
1
+ {
2
+ "permissions": {
3
+ "allow": [
4
+ "Bash(dir:*)",
5
+ "Bash(tree:*)",
6
+ "WebFetch(domain:developers.weixin.qq.com)"
7
+ ]
8
+ }
9
+ }
package/README.md CHANGED
@@ -1,72 +1,128 @@
1
1
  # 聚优甲广告组件库
2
2
 
3
- 微信小程序广告组件库。
3
+ 微信小程序广告组件库,支持横幅广告、插屏广告、激励视频广告和开屏广告。
4
+
5
+ ## 目录
6
+
7
+ - [安装](#安装)
8
+ - [添加合法域名](#添加合法域名)
9
+ - [组件使用](#组件使用)
10
+ - [横幅广告](#1-横幅广告-ad-banner)
11
+ - [插屏广告](#2-插屏广告-ad-interstitial)
12
+ - [激励视频广告](#3-激励视频广告-ad-rewarded-video)
13
+ - [开屏广告](#4-开屏广告-ad-splash)
14
+ - [注意事项](#注意事项)
15
+ - [常见问题](#常见问题)
4
16
 
5
17
  ## 安装
6
18
 
7
- ### 方式一:npm 安装(推荐)
19
+ ### npm 安装(推荐)
8
20
 
9
21
  ```bash
10
22
  npm install jyj-components
11
23
  ```
12
24
 
13
- 在小程序 `app.json` 中配置:
25
+ #### 小程序原生项目配置
26
+
27
+ 参考[小程序npm支持](https://developers.weixin.qq.com/miniprogram/dev/devtools/npm.html)
28
+
29
+ 1. 小程序开发者工具 -> 详情(工具右上角) -> 本地设置 -> 使用npm模块
30
+ 2. 小程序开发者工具 -> 工具 -> 构建 npm
31
+ 3. 构建成功后小程序代码包中将产出 `miniprogram_npm` 文件夹
32
+
33
+ #### uni-app 框架配置
34
+
35
+ 参考[小程序自定义组件支持](https://uniapp.dcloud.io/frame?id=%e5%b0%8f%e7%a8%8b%e5%ba%8f%e8%87%aa%e5%ae%9a%e4%b9%89%e7%bb%84%e4%bb%b6%e6%94%af%e6%8c%81)
36
+
37
+ 1. 在根目录新建 `wxcomponents` 文件夹
38
+ 2. 打开 `node_modules/jyj-components` 文件,**复制目录中整个 src 文件到 wxcomponents 目录下新建 jyj-components 文件夹**,按需重命名使用
14
39
 
15
- ```json
16
- {
17
- "usingComponents": {
18
- "ad-banner": "jyj-components/ad-banner/index",
19
- "ad-interstitial": "jyj-components/ad-interstitial/index",
20
- "ad-rewarded-video": "jyj-components/ad-rewarded-video/index",
21
- "ad-splash": "jyj-components/ad-splash/index"
22
- }
23
- }
24
- ```
25
40
 
26
- ### 方式二:本地引入
27
41
 
28
- 将 `miniprogram_dist` 目录复制到项目中,然后在 `app.json` 中配置相对路径。
29
42
 
30
43
  ## 添加合法域名
31
44
 
32
- 1. 小程序账号登录[微信公众平台](https://mp.weixin.qq.com/)
33
- 2. 开发 —> 开发管理 —> 开发设置 —> 服务器域名
34
- 3. 在 `downloadFile合法域名` 中增加如下配置
45
+ 1. 小程序账号登录 [微信公众平台](https://mp.weixin.qq.com/)
46
+ 2. 开发 开发管理 开发设置 服务器域名
47
+ 3. 在 `downloadFile合法域名` 中增加如下配置:
35
48
 
36
49
  ```
37
50
  https://wxgo.cdn.adwke.com
38
51
  ```
39
52
 
40
- 4. 在request合法域名中增加
53
+ 4. 在 `request合法域名` 中增加:
41
54
 
42
55
  ```
43
- https://wxgo.adwke.com/
56
+ https://wxgo.adwke.com
44
57
  ```
45
58
 
46
59
  ## 组件使用
47
60
 
48
- ### 1. 横幅广告 (ad-banner)
61
+ 在使用广告的页面 json 配置文件中引入组件(按需配置):
49
62
 
63
+ ```json
64
+ {
65
+ "usingComponents": {
66
+ "ad-banner": "jyj-components/ad-banner/index",
67
+ "ad-interstitial": "jyj-components/ad-interstitial/index",
68
+ "ad-rewarded-video": "jyj-components/ad-rewarded-video/index",
69
+ "ad-splash": "jyj-components/ad-splash/index"
70
+ }
71
+ }
72
+ ```
50
73
 
74
+ ### 1. 横幅广告 (ad-banner)
75
+
76
+ 在页面 wxml 文件中使用:
51
77
 
52
78
  ```xml
53
79
  <ad-banner
54
80
  slotId="your-slot-id"
55
81
  bind:adLoad="onAdLoad"
56
82
  bind:adError="onAdError"
83
+ bind:adOpenMiniProgram="adOpenMiniProgram"
57
84
  />
58
85
  ```
59
86
 
60
- **属性:**
61
- - `slotId` (String, 必填):广告位 ID
87
+ **属性说明:**
88
+
89
+ | 属性 | 类型 | 必填 | 说明 |
90
+ |------|------|------|------|
91
+ | slotId | String | 是 | 广告位 ID |
92
+
93
+ **事件说明:**
94
+
95
+ | 事件名 | 说明 |
96
+ |--------|------|
97
+ | adLoad | 广告加载成功时触发 |
98
+ | adError | 广告加载失败时触发 |
99
+ | adOpenMiniProgram | 跳转小程序时触发 |
100
+
101
+ 在页面 js 文件中实现广告相关的事件回调函数:
102
+
103
+ ```javascript
104
+ Page({
105
+ adOpenMiniProgram(options) {
106
+ wx.openEmbeddedMiniProgram({
107
+ appId: options.detail.appId,
108
+ path: options.detail.path,
109
+ });
110
+ },
111
+
112
+ onAdLoad(options) {
113
+ console.log('广告加载成功');
114
+ },
115
+
116
+ onAdError(options) {
117
+ console.log('广告加载失败', options);
118
+ }
119
+ });
120
+ ```
62
121
 
63
- **事件:**
64
- - `adLoad`:广告加载成功
65
- - `adError`:广告加载失败
66
- - `adOpenMiniProgram`:跳转小程序(自定义广告)
67
122
 
68
123
  ### 2. 插屏广告 (ad-interstitial)
69
124
 
125
+ 在页面 wxml 文件中使用:
70
126
 
71
127
  ```xml
72
128
  <ad-interstitial
@@ -76,18 +132,28 @@ https://wxgo.adwke.com/
76
132
  bind:adLoad="onAdLoad"
77
133
  bind:adError="onAdError"
78
134
  bind:adClose="onAdClose"
135
+ bind:adOpenMiniProgram="adOpenMiniProgram"
79
136
  />
80
137
  ```
81
138
 
82
- **属性:**
83
- - `slotId` (String, 必填):广告位 ID
84
- - `isShow` (Boolean):控制广告显示
85
- - `isDestroy` (Boolean):控制广告销毁 默认false 每个广告实例只能修改一次
139
+ **属性说明:**
86
140
 
87
- **事件:**
88
- - `adLoad`:广告加载成功
89
- - `adError`:广告加载失败
90
- - `adClose`:广告关闭
141
+ | 属性 | 类型 | 必填 | 默认值 | 说明 |
142
+ |------|------|------|--------|------|
143
+ | slotId | String | 是 | - | 广告位 ID |
144
+ | isShow | Boolean | 否 | false | 控制广告显示,需要广告加载成功后设置为 true 时展示插屏广告 |
145
+ | isDestroy | Boolean | 否 | false | 控制广告销毁,每个广告实例只能修改一次 |
146
+
147
+ > **注意:** 小程序打开后至少 20 秒才可以展示插屏广告,与其他插屏/激励视频广告展示间隔也是一样。
148
+
149
+ **事件说明:**
150
+
151
+ | 事件名 | 说明 |
152
+ |--------|------|
153
+ | adLoad | 广告加载成功时触发 |
154
+ | adError | 广告加载失败时触发 |
155
+ | adClose | 广告关闭时触发 |
156
+ | adOpenMiniProgram | 跳转小程序时触发(自定义广告) |
91
157
 
92
158
  **使用示例:**
93
159
 
@@ -98,22 +164,35 @@ Page({
98
164
  destroyAd: false
99
165
  },
100
166
 
167
+ adOpenMiniProgram(options) {
168
+ wx.openEmbeddedMiniProgram({
169
+ appId: options.detail.appId,
170
+ path: options.detail.path,
171
+ });
172
+ },
173
+
174
+ // 显示插屏广告
101
175
  showInterstitialAd() {
102
176
  this.setData({ showAd: true });
103
177
  },
104
178
 
105
- onAdClose() {
106
- this.setData({
107
- showAd: false,
108
- destroyAd: true
109
- });
179
+ onAdLoad(e) {
180
+ console.log('广告加载成功');
181
+ },
182
+
183
+ onAdError(e) {
184
+ console.log('广告加载失败', e);
185
+ },
186
+
187
+ onAdClose(e) {
188
+ console.log('广告关闭');
110
189
  }
111
190
  });
112
191
  ```
113
192
 
114
193
  ### 3. 激励视频广告 (ad-rewarded-video)
115
194
 
116
-
195
+ 在页面 wxml 文件中使用:
117
196
 
118
197
  ```xml
119
198
  <ad-rewarded-video
@@ -127,90 +206,165 @@ Page({
127
206
  />
128
207
  ```
129
208
 
130
- **属性:**
131
- - `slotId` (String, 必填):广告位 ID
132
- - `isShow` (Boolean):控制视频播放
133
- - `isDestroy` (Boolean):控制广告销毁 默认false 每个广告实例只能修改一次
209
+ **属性说明:**
134
210
 
135
- **事件:**
136
- - `adLoad`:广告加载成功
137
- - `adError`:广告加载失败
138
- - `adClose`:广告关闭
139
- - `adFinished`:视频播放完成(用户获得奖励)
211
+ | 属性 | 类型 | 必填 | 默认值 | 说明 |
212
+ |------|------|------|--------|------|
213
+ | slotId | String | 是 | - | 广告位 ID |
214
+ | isShow | Boolean | 否 | false | 控制视频播放,当广告加载成功后设置为 true 时播放广告 |
215
+ | isDestroy | Boolean | 否 | false | 控制广告销毁,每个广告实例只能修改一次 |
216
+
217
+ **事件说明:**
218
+
219
+ | 事件名 | 说明 |
220
+ |--------|------|
221
+ | adLoad | 广告加载成功时触发 |
222
+ | adError | 广告加载失败时触发 |
223
+ | adClose | 广告关闭时触发 |
224
+ | adFinished | 视频播放完成时触发(用户获得奖励) |
140
225
 
141
226
  **使用示例:**
142
227
 
143
228
  ```javascript
144
229
  Page({
145
230
  data: {
146
- showVideo: false
231
+ showVideo: false,
232
+ destroyVideo: false
147
233
  },
148
234
 
235
+ // 观看视频获取奖励
149
236
  watchVideoForReward() {
150
237
  this.setData({ showVideo: true });
151
238
  },
152
239
 
240
+ onAdLoad(e) {
241
+ console.log('广告加载成功');
242
+ },
243
+
244
+ onAdError(e) {
245
+ console.log('广告加载失败', e);
246
+ },
247
+
248
+ onAdClose(e) {
249
+ console.log('广告关闭');
250
+ },
251
+
153
252
  onAdFinished(e) {
154
253
  if (e.detail.isEnded) {
155
254
  // 用户看完视频,发放奖励
255
+ console.log('用户看完视频,可以发放奖励');
156
256
  this.giveReward();
257
+ } else {
258
+ console.log('用户未看完视频');
157
259
  }
260
+ },
261
+
262
+ giveReward() {
263
+ // 发放奖励的逻辑
264
+ wx.showToast({
265
+ title: '奖励已发放',
266
+ icon: 'success'
267
+ });
158
268
  }
159
269
  });
160
270
  ```
161
271
 
162
272
  ### 4. 开屏广告 (ad-splash)
163
273
 
274
+ 在小程序首页页面中使用:
164
275
 
165
276
  ```xml
166
- <ad-splash
167
- slotId="your-slot-id"
168
- bind:adLoad="onAdLoad"
169
- bind:adError="onAdError"
170
- bind:adFinished="onAdFinished"
171
- />
277
+ <block wx:if="{{showSplash}}">
278
+ <ad-splash
279
+ slotId="your-slot-id"
280
+ bind:adLoad="onAdLoad"
281
+ bind:adError="onAdError"
282
+ bind:adFinished="onAdFinished"
283
+ />
284
+ </block>
172
285
  ```
173
286
 
174
- **属性:**
175
- - `slotId` (String, 必填):广告位 ID
287
+ **属性说明:**
288
+
289
+ | 属性 | 类型 | 必填 | 说明 |
290
+ |------|------|------|------|
291
+ | slotId | String | 是 | 广告位 ID |
176
292
 
177
- **事件:**
178
- - `adLoad`:广告加载成功
179
- - `adError`:广告加载失败
180
- - `adFinished`:广告结束(倒计时结束或用户跳过)
293
+ **事件说明:**
294
+
295
+ | 事件名 | 说明 |
296
+ |--------|------|
297
+ | adLoad | 广告加载成功时触发 |
298
+ | adError | 广告加载失败时触发 |
299
+ | adFinished | 广告结束时触发(倒计时结束或用户跳过) |
181
300
 
182
301
  **使用示例:**
183
302
 
184
303
  ```javascript
185
- // app.js 或首页
304
+ // 首页 index.js
186
305
  Page({
306
+ data: {
307
+ showSplash: true
308
+ },
309
+
187
310
  onLoad() {
188
311
  // 开屏广告会自动展示
189
312
  },
190
313
 
191
- onAdFinished() {
314
+ onAdLoad(e) {
315
+ console.log('开屏广告加载成功');
316
+ },
317
+
318
+ onAdError(e) {
319
+ console.log('开屏广告加载失败', e);
320
+ // 广告加载失败,直接进入主页面
321
+ this.setData({ showSplash: false });
322
+ },
323
+
324
+ onAdFinished(e) {
192
325
  // 广告结束,进入主页面
193
326
  console.log('开屏广告结束');
327
+ this.setData({ showSplash: false });
194
328
  }
195
329
  });
196
330
  ```
197
331
 
198
332
  ## 注意事项
199
333
 
200
- 1. 向运营人员获取授权链接 通过授权链接授权权限
334
+ 1. **授权配置**:向运营人员获取授权链接,通过授权链接授权权限
201
335
  2. **广告位 ID**:需要向运营人员申请有效的 `slotId`
336
+ 3. **展示时机**:插屏广告和激励视频广告需要在小程序打开至少 20 秒后才能展示
337
+ 4. **广告间隔**:插屏广告、激励视频广告之间需要保持一定的展示间隔(至少 20 秒)
338
+ 5. **合法域名**:务必在微信公众平台配置合法域名,否则广告无法正常加载
202
339
 
203
340
 
204
341
  ## 常见问题
205
342
 
206
343
  ### Q: 广告不显示?
207
- A: 检查 `slotId` 是否正确,网络请求是否成功,广告数据格式是否正确。
344
+
345
+ **A:** 请按以下步骤排查:
346
+ 1. 检查 `slotId` 是否正确
347
+ 2. 确认已在微信公众平台配置合法域名
348
+ 3. 检查网络请求是否成功(开发者工具 -> Network)
349
+ 4. 确认广告数据格式是否正确
350
+ 5. 检查是否已完成授权配置
208
351
 
209
352
  ### Q: 如何控制广告展示时机?
210
- A: 使用 `isShow` 属性控制插屏和激励视频的展示时机。
353
+
354
+ **A:**
355
+ - 横幅广告:组件加载后自动展示
356
+ - 插屏广告和激励视频:使用 `isShow` 属性控制展示时机,在广告加载成功后设置为 `true`
357
+ - 开屏广告:页面加载时自动展示
211
358
 
212
359
  ### Q: 如何销毁广告实例?
213
- A: 设置 `isDestroy` 为 `true` 即可销毁广告实例,释放内存。
360
+
361
+ **A:** 设置 `isDestroy` 为 `true` 即可销毁广告实例,释放内存。注意每个广告实例只能销毁一次。
362
+
363
+ ### Q: 插屏广告和激励视频为什么无法立即展示?
364
+
365
+ **A:** 根据微信小程序规范,插屏广告和激励视频广告需要在小程序打开至少 20 秒后才能展示,且两次广告展示之间需要间隔至少 20 秒。
366
+
367
+
214
368
 
215
369
 
216
370