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.
@@ -22,7 +22,8 @@ Component({
22
22
  ads: [],
23
23
  currentAd: {},
24
24
  currentAdIndex: 0,
25
- currentAdShowCount: 0
25
+ currentAdShowCount: 0,
26
+ wxAdFailed: false // 微信广告失败标记(仅内存,不持久化)
26
27
  },
27
28
  lifetimes: {
28
29
  async attached() {
@@ -31,6 +32,18 @@ Component({
31
32
  }
32
33
  },
33
34
  methods: {
35
+ checkAndResetDaily() {
36
+ return adBase.checkAndResetDaily.call(this)
37
+ },
38
+ isWxAdFailed() {
39
+ return adBase.isWxAdFailed.call(this)
40
+ },
41
+ markWxAdFailed() {
42
+ adBase.markWxAdFailed.call(this)
43
+ },
44
+ isAllCustomAdsReachedLimit() {
45
+ return adBase.isAllCustomAdsReachedLimit.call(this)
46
+ },
34
47
  restoreAdState() {
35
48
  adBase.restoreAdState.call(this)
36
49
  },
@@ -14,15 +14,52 @@ Component({
14
14
  currentAdIndex: 0,
15
15
  currentAdShowCount: 0,
16
16
  countDown: 5,
17
- splashStatus: true
17
+ splashStatus: true,
18
+ adBannerTop: 60,
19
+ wxAdFailed: false // 微信广告失败标记(仅内存,不持久化)
18
20
  },
19
21
  lifetimes: {
20
22
  async attached() {
21
- await adBase.initAds.call(this)
22
- this.startCountDown()
23
+ this.calculateAdBannerTop()
24
+
25
+ const canUse = this.canUseCoverAdAPI()
26
+
27
+ if (canUse){
28
+ setTimeout(() => {
29
+ wx.getShowSplashAdStatus({
30
+ success: async res => {
31
+ if (res.status === 'fail'){
32
+ await adBase.initAds.call(this)
33
+ this.startCountDown()
34
+ } else {
35
+ this.jump()
36
+ }
37
+ },
38
+ fail: async err => {
39
+ await adBase.initAds.call(this)
40
+ this.startCountDown()
41
+ }
42
+ })
43
+ }, 100)
44
+ } else {
45
+ await adBase.initAds.call(this)
46
+ this.startCountDown()
47
+ }
23
48
  }
24
49
  },
25
50
  methods: {
51
+ checkAndResetDaily() {
52
+ return adBase.checkAndResetDaily.call(this)
53
+ },
54
+ isWxAdFailed() {
55
+ return adBase.isWxAdFailed.call(this)
56
+ },
57
+ markWxAdFailed() {
58
+ adBase.markWxAdFailed.call(this)
59
+ },
60
+ isAllCustomAdsReachedLimit() {
61
+ return adBase.isAllCustomAdsReachedLimit.call(this)
62
+ },
26
63
  restoreAdState() {
27
64
  adBase.restoreAdState.call(this)
28
65
  },
@@ -55,6 +92,49 @@ Component({
55
92
  }, 1100)
56
93
  },
57
94
 
95
+ calculateAdBannerTop() {
96
+ try {
97
+ const menuRect = wx.getMenuButtonBoundingClientRect();
98
+ const systemInfo = wx.getSystemInfoSync();
99
+
100
+ const isCustomNavBar = (systemInfo.screenHeight === systemInfo.windowHeight);
101
+
102
+ let top
103
+ if (isCustomNavBar) {
104
+ top = menuRect.bottom + 10
105
+ } else {
106
+ top = systemInfo.statusBarHeight + 20
107
+ }
108
+
109
+ const topInRpx = top * 750 / systemInfo.windowWidth
110
+
111
+ this.setData({
112
+ adBannerTop: topInRpx
113
+ })
114
+ } catch (err) {
115
+ }
116
+ },
117
+
118
+ compareVersion(v1, v2) {
119
+ const s1 = v1.split('.').map(Number);
120
+ const s2 = v2.split('.').map(Number);
121
+ const len = Math.max(s1.length, s2.length);
122
+
123
+ for (let i = 0; i < len; i++) {
124
+ const n1 = s1[i] || 0;
125
+ const n2 = s2[i] || 0;
126
+ if (n1 > n2) return 1;
127
+ if (n1 < n2) return -1;
128
+ }
129
+ return 0;
130
+ },
131
+ canUseCoverAdAPI() {
132
+ const REQUIRED_VERSION = '3.7.8';
133
+ const systemInfo = wx.getSystemInfoSync();
134
+ const sdkVersion = systemInfo.SDKVersion;
135
+ return this.compareVersion(sdkVersion, REQUIRED_VERSION) >= 0;
136
+ },
137
+
58
138
  jump() {
59
139
  this.setData({
60
140
  splashStatus: false
@@ -1,5 +1,5 @@
1
1
  <view wx:if="{{splashStatus && currentAd.splash.unitId}}" class="splash-container">
2
- <view class="ad-banner-top">
2
+ <view class="ad-banner-top" style="top: {{adBannerTop}}rpx;">
3
3
  <text class="ad-tag">广告</text>
4
4
  <text class="ad-timer">{{countDown}} 秒</text>
5
5
  <view class="skip-btn" bindtap="jump">跳过</view>
@@ -5,7 +5,6 @@
5
5
 
6
6
  .ad-banner-top {
7
7
  position: absolute;
8
- top: 160rpx;
9
8
  right: 20rpx;
10
9
  display: flex;
11
10
  align-items: center;
@@ -1,55 +1,159 @@
1
1
  const {getAds} = require('../request.js')
2
2
 
3
3
  module.exports = {
4
- restoreAdState() {
4
+ // 检查并重置每日数据
5
+ checkAndResetDaily() {
5
6
  const slotId = this.properties.slotId
6
7
  const type = this.properties.type
7
- const adIndexKey = `adIndex_${slotId}_${type}`
8
- const adShowCountKey = `adShowCount_${slotId}_${type}`
8
+ const dateKey = `adDate_${slotId}_${type}`
9
9
 
10
- const savedIndex = wx.getStorageSync(adIndexKey) || 0
11
- const savedShowCount = wx.getStorageSync(adShowCountKey) || 0
10
+ const today = new Date().toDateString()
11
+ const savedDate = wx.getStorageSync(dateKey)
12
12
 
13
- this.setData({
14
- currentAdIndex: savedIndex,
15
- currentAdShowCount: savedShowCount
16
- })
13
+ // 如果日期不同,重置所有数据
14
+ if (savedDate !== today) {
15
+ const customAdIndexKey = `customAdIndex_${slotId}_${type}`
16
+ const customAdShowCountKey = `customAdShowCount_${slotId}_${type}`
17
+
18
+ wx.setStorageSync(dateKey, today)
19
+ wx.setStorageSync(customAdIndexKey, 1) // 自有广告从索引1开始
20
+ wx.setStorageSync(customAdShowCountKey, 0)
21
+
22
+ return true // 表示已重置
23
+ }
24
+
25
+ return false // 表示未重置
26
+ },
27
+
28
+ restoreAdState() {
29
+ // 检查并重置每日数据
30
+ this.checkAndResetDaily()
17
31
  },
18
32
 
19
33
  saveAdState() {
20
34
  const slotId = this.properties.slotId
21
35
  const type = this.properties.type
22
- const adIndexKey = `adIndex_${slotId}_${type}`
23
- const adShowCountKey = `adShowCount_${slotId}_${type}`
36
+ const customAdIndexKey = `customAdIndex_${slotId}_${type}`
37
+ const customAdShowCountKey = `customAdShowCount_${slotId}_${type}`
24
38
 
25
- wx.setStorageSync(adIndexKey, this.data.currentAdIndex)
26
- wx.setStorageSync(adShowCountKey, this.data.currentAdShowCount)
39
+ // 只保存自有广告的状态(索引>0)
40
+ if (this.data.currentAdIndex > 0) {
41
+ wx.setStorageSync(customAdIndexKey, this.data.currentAdIndex)
42
+ wx.setStorageSync(customAdShowCountKey, this.data.currentAdShowCount)
43
+ }
44
+ },
45
+
46
+ // 检查微信广告是否失败(只在内存中,不持久化)
47
+ isWxAdFailed() {
48
+ return this.data.wxAdFailed || false
49
+ },
50
+
51
+ // 标记微信广告失败(只在内存中,不持久化)
52
+ markWxAdFailed() {
53
+ this.setData({
54
+ wxAdFailed: true
55
+ })
56
+ },
57
+
58
+ // 检查所有自有广告是否都达到上限
59
+ isAllCustomAdsReachedLimit() {
60
+ const ads = this.data.ads
61
+ if (!ads || ads.length <= 1) return true // 只有微信广告或没有广告
62
+
63
+ const currentIndex = this.data.currentAdIndex
64
+ const showCount = this.data.currentAdShowCount
65
+
66
+ // 如果当前不是自有广告,返回false
67
+ if (currentIndex === 0) return false
68
+
69
+ // 找到最后一个有效的自有广告索引
70
+ let lastValidAdIndex = -1
71
+ for (let i = ads.length - 1; i >= 1; i--) {
72
+ if (ads[i][this.properties.type] !== undefined) {
73
+ lastValidAdIndex = i
74
+ break
75
+ }
76
+ }
77
+
78
+ // 如果没有有效的自有广告,返回true
79
+ if (lastValidAdIndex === -1) return true
80
+
81
+ // 如果当前索引已经是最后一个有效广告,且达到展示上限,返回true
82
+ if (currentIndex === lastValidAdIndex) {
83
+ const ad = ads[currentIndex]
84
+ const maxShowCount = ad.showCount || 1
85
+ return showCount >= maxShowCount
86
+ }
87
+
88
+ return false
27
89
  },
28
90
 
29
91
  loadNextAd() {
30
92
  const ads = this.data.ads
31
93
  if (!ads || ads.length === 0) return
32
94
 
95
+ // 检查并重置每日数据
96
+ this.checkAndResetDaily()
97
+
33
98
  let currentIndex = this.data.currentAdIndex
34
99
  let showCount = this.data.currentAdShowCount
35
100
 
36
- const currentAd = ads[currentIndex % ads.length]
101
+ // 如果是微信广告(索引0)且未失败,直接加载微信广告
102
+ if (currentIndex === 0 && !this.isWxAdFailed()) {
103
+ const wxAd = ads[0]
104
+ if (wxAd && wxAd[this.properties.type] !== undefined) {
105
+ this.setData({currentAd: wxAd})
106
+ return
107
+ } else {
108
+ // 微信广告没有配置,标记为失败,切换到自有广告
109
+ this.markWxAdFailed()
110
+ currentIndex = 1
111
+ showCount = 0
112
+ }
113
+ }
114
+
115
+ // 如果微信广告失败,从自有广告开始
116
+ if (currentIndex === 0 && this.isWxAdFailed()) {
117
+ // 从存储中恢复自有广告的状态
118
+ const slotId = this.properties.slotId
119
+ const type = this.properties.type
120
+ const customAdIndexKey = `customAdIndex_${slotId}_${type}`
121
+ const customAdShowCountKey = `customAdShowCount_${slotId}_${type}`
122
+
123
+ currentIndex = wx.getStorageSync(customAdIndexKey) || 1
124
+ showCount = wx.getStorageSync(customAdShowCountKey) || 0
125
+ }
126
+
127
+ // 如果没有自有广告,不展示任何广告
128
+ if (ads.length <= 1) {
129
+ return
130
+ }
37
131
 
38
- const maxShowCount = currentAd.showCount || 1
132
+ // 检查所有自有广告是否都达到上限
133
+ if (this.isAllCustomAdsReachedLimit()) {
134
+ return
135
+ }
136
+
137
+ // 处理自有广告的展示次数限制
138
+ let ad = ads[currentIndex]
139
+ const maxShowCount = ad.showCount || 1
140
+
141
+ // 如果当前自有广告达到展示上限,切换到下一个
39
142
  if (showCount >= maxShowCount) {
40
143
  currentIndex = currentIndex + 1
41
144
  showCount = 0
42
- this.setData({
43
- currentAdIndex: currentIndex,
44
- currentAdShowCount: showCount
45
- })
46
- this.saveAdState()
47
- }
48
145
 
49
- const ad = ads[currentIndex % ads.length]
50
- this.setData({currentAd: ad})
146
+ // 如果超出广告列表范围,说明所有广告都达到上限
147
+ if (currentIndex >= ads.length) {
148
+ return
149
+ }
150
+
151
+ ad = ads[currentIndex]
152
+ }
51
153
 
154
+ // 检查当前广告是否有配置
52
155
  if (ad[this.properties.type] === undefined) {
156
+ // 没有配置,跳过并尝试下一个
53
157
  this.setData({
54
158
  currentAdIndex: currentIndex + 1,
55
159
  currentAdShowCount: 0
@@ -59,8 +163,11 @@ module.exports = {
59
163
  return
60
164
  }
61
165
 
166
+ // 设置当前广告
62
167
  this.setData({
63
- currentAdShowCount: showCount + 1
168
+ currentAd: ad,
169
+ currentAdIndex: currentIndex,
170
+ currentAdShowCount: showCount
64
171
  })
65
172
  this.saveAdState()
66
173
  },
@@ -86,9 +193,23 @@ module.exports = {
86
193
  this.triggerEvent('adLoad', e.detail || {})
87
194
  }
88
195
  this.report('load')
196
+
197
+ // 只有自有广告(索引>0)才增加展示计数
198
+ if (this.data.currentAdIndex > 0) {
199
+ const showCount = this.data.currentAdShowCount
200
+ this.setData({
201
+ currentAdShowCount: showCount + 1
202
+ })
203
+ this.saveAdState()
204
+ }
89
205
  },
90
206
 
91
207
  adError(e) {
208
+ // 如果是微信广告(索引0)失败,标记为失败状态
209
+ if (this.data.currentAdIndex === 0) {
210
+ this.markWxAdFailed()
211
+ }
212
+
92
213
  if (e === 'undefined') {
93
214
  this.triggerEvent('adError', {msg: 'ad adError'})
94
215
  } else {
@@ -119,7 +240,13 @@ module.exports = {
119
240
  const slotId = this.properties.slotId
120
241
  const ads = await getAds(slotId)
121
242
  this.setData({ads})
122
- this.restoreAdState()
243
+
244
+ // 每次初始化时,总是从微信广告开始(索引0)
245
+ this.setData({
246
+ currentAdIndex: 0,
247
+ currentAdShowCount: 0
248
+ })
249
+
123
250
  this.loadNextAd()
124
251
  }
125
252
  }
@@ -1,3 +0,0 @@
1
- page {
2
- width: 100%;
3
- }
Binary file
@@ -1,5 +0,0 @@
1
- module.exports = {
2
- getFlag() {
3
- return true
4
- },
5
- }
@@ -1,3 +0,0 @@
1
- page {
2
- width: 100%;
3
- }
Binary file
package/src/common.wxss DELETED
@@ -1,5 +0,0 @@
1
- @import "./reset.wxss";
2
-
3
- .other {
4
- font-size: 20px;
5
- }
package/src/index.js DELETED
@@ -1,24 +0,0 @@
1
- const _ = require('./utils')
2
-
3
- Component({
4
- properties: {
5
- prop: {
6
- type: String,
7
- value: 'index.properties'
8
- },
9
- },
10
- data: {
11
- flag: false,
12
- },
13
- lifetimes: {
14
- attached() {
15
- wx.getSystemInfo({
16
- success: () => {
17
- this.setData({
18
- flag: _.getFlag(),
19
- })
20
- }
21
- })
22
- }
23
- }
24
- })
package/src/index.json DELETED
@@ -1,9 +0,0 @@
1
- {
2
- "component": true,
3
- "usingComponents": {
4
- "other": "./other"
5
- },
6
- "componentGenerics": {
7
- "genericsTest": true
8
- }
9
- }
package/src/index.wxml DELETED
@@ -1,2 +0,0 @@
1
- <view class="index other">{{prop}}-{{flag}}</view>
2
- <other></other>
package/src/index.wxss DELETED
@@ -1,6 +0,0 @@
1
- @import "./common.wxss";
2
- @import "./reset.wxss";
3
-
4
- .index {
5
- color: green;
6
- }
package/src/lib.ts DELETED
@@ -1,5 +0,0 @@
1
- export default {
2
- printf() {
3
- return 'miniprogram-custom-component api demo'
4
- }
5
- }
package/src/reset.wxss DELETED
@@ -1,3 +0,0 @@
1
- .reset {
2
- background: white;
3
- }
package/src/utils.js DELETED
@@ -1,5 +0,0 @@
1
- module.exports = {
2
- getFlag() {
3
- return true
4
- },
5
- }