'f'
mh-two-thousand-and-two
2024-04-12 26f2711ef9461961fb953e2b497bd314ef95e345
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
const EventType = {
  Load: 'load',
  Close: 'close',
  Error: 'error'
}
 
export default {
  props: {
    options: {
      type: [Object, Array],
      default () {
        return {}
      }
    },
    adpid: {
      type: [Number, String],
      default: ''
    },
    preload: {
      type: [Boolean, String],
      default: true
    },
    loadnext: {
      type: [Boolean, String],
      default: false
    }
  },
  watch: {
    adpid (val) {
      if (val) {
        this._loadData(val)
      }
    }
  },
  data () {
    return {
      loading: false,
      errorMessage: null
    }
  },
  created () {
    this._pc = {}
    this._pl = []
    this._loadData()
  },
  methods: {
    load () {
      this._dispatchEvent(EventType.Load, {})
    },
 
    show () {
      this.errorMessage = null
 
      const data = this._pl[0]
      const providerConfig = this._pc[data.a1][data.t]
 
      AdScript.instance.load(data.t, providerConfig.script, () => {
        this._renderData(data)
      }, (err) => {
        this.errorMessage = err.message
        this._dispatchEvent(EventType.Error, err)
      })
    },
 
    _onclick () {
      this.show()
    },
 
    _loadData (adpid) {
      this.loading = true
      const id = adpid || this.adpid
      AdConfig.instance.get(id, (a, b) => {
        this._pc = a
        this._pl = b
        this.loading = false
      }, (err) => {
        this.loading = false
        this.errorMessage = err
        this._dispatchEvent(EventType.Error, err)
      })
    },
 
    _renderData (data) {
      const id = this._createView()
 
      const coral = new window.CoralAdv({
        app_id: data.a2,
        placement_id: data.a3,
        type: data.a4,
        display_type: data.a5,
        container_id: id,
        count: 1
      })
      coral.ready().then(async (res) => {
        if (res.ret === 0) {
        } else {
          this._dispatchEvent(EventType.Error, res)
        }
      }).catch((err) => {
        this._dispatchEvent(EventType.Error, err)
      })
    },
 
    _dispatchEvent (type, data) {
      this.$emit(type, {
        detail: data
      })
    },
 
    _createView () {
      const id = this._randomId()
      const adView = document.createElement('div')
      adView.setAttribute('id', id)
      this.$refs.container.innerHTML = ''
      this.$refs.container.append(adView)
      return id
    },
 
    _randomId () {
      let result = ''
      for (let i = 0; i < 4; i++) {
        result += (65536 * (1 + Math.random()) | 0).toString(16).substring(1)
      }
      return '_u' + result
    }
  }
}
 
// let IC = 0
// let IS = 0
 
class AdConfig {
  static get instance () {
    if (this._instance == null) {
      this._instance = new AdConfig()
      this._instance._init()
    }
    return this._instance
  }
 
  constructor () {
    this._instance = null
    this._adConfig = null
    this._isLoading = false
    this._lastError = null
    this._callbacks = []
  }
 
  get adConfig () {
    return this._adConfig
  }
 
  get isExpired () {
    if (this._adConfig == null) {
      return true
    }
    return (Math.abs(Date.now() - this._adConfig.last) > this.CACHE_TIME)
  }
 
  _init () {
    var config = this._getConfig()
    if (config === null || !config.last) {
      return
    }
 
    if (!this.isExpired) {
      this._adConfig = config.data
    }
  }
 
  get (adpid, success, fail) {
    // IC++
    if (this._adConfig != null) {
      this._doCallback(adpid, success, fail)
      if (this.isExpired) {
        this._loadAdConfig(adpid)
      }
      return
    }
 
    this._callbacks.push({
      adpid: adpid,
      success: success,
      fail: fail
    })
 
    this._loadAdConfig(adpid)
  }
 
  _doCallback (adpid, success, fail) {
    // IS++
    var { a, b } = this._adConfig
    if (a[adpid]) {
      success(b, a[adpid])
    } else {
      fail(this.ERROR_INVALID_ADPID)
    }
  }
 
  _loadAdConfig (adpid) {
    if (this._isLoading === true) {
      return
    }
    this._isLoading = true
 
    uni.request({
      url: this.URL,
      method: 'GET',
      timeout: 8000,
      data: {
        d: location.hostname,
        a: adpid
      },
      dataType: 'json',
      success: (res) => {
        const rd = res.data
        if (rd.ret === 0) {
          const data = rd.data
 
          this._adConfig = data
          this._setConfig(data)
 
          this._callbacks.forEach(({ adpid, success, fail }) => {
            this._doCallback(adpid, success, fail)
          })
        } else {
          this._callbacks.forEach((i) => {
            i.fail({ errCode: rd.ret, errMsg: rd.msg })
          })
        }
        this._callbacks = []
      },
      fail: (err) => {
        this._callbacks.forEach((i) => {
          i.fail(err)
        })
        this._callbacks = []
      },
      complete: (c) => {
        this._isLoading = false
      }
    })
  }
 
  _getConfig () {
    if (!navigator.cookieEnabled || !window.localStorage) {
      return null
    }
    var data = localStorage.getItem(this.KEY)
    return data ? JSON.parse(data) : null
  }
 
  _setConfig (data) {
    if (!navigator.cookieEnabled || !window.localStorage) {
      return null
    }
    localStorage.setItem(this.KEY, JSON.stringify({
      last: Date.now(),
      data: data
    }))
  }
}
Object.assign(AdConfig.prototype, {
  URL: 'https://hac1.dcloud.net.cn/ah5',
  KEY: 'uni_app_ad_config',
  CACHE_TIME: 1000 * 60 * 10,
  ERROR_INVALID_ADPID: {
    '-5002': 'invalid adpid'
  }
})
 
class AdScript {
  static get instance () {
    if (this._instance == null) {
      this._instance = new AdScript()
    }
    return this._instance
  }
 
  constructor () {
    this._instance = null
    this._callback = {}
    this._cache = {}
  }
 
  load (provider, script, success, fail) {
    if (this._cache[provider] === undefined) {
      this.loadScript(provider, script)
    }
 
    if (this._cache[provider] === 1) {
      success()
    } else {
      if (!this._callback[provider]) {
        this._callback[provider] = []
      }
      this._callback[provider].push({
        success,
        fail
      })
    }
  }
 
  loadScript (provider, script) {
    this._cache[provider] = 0
    var ads = document.createElement('script')
    ads.setAttribute('id', 'uniad_provider' + provider)
    for (const var1 in script) {
      ads.setAttribute(var1, script[var1])
    }
    ads.onload = () => {
      this._cache[provider] = 1
      this._callback[provider].forEach(({ success }) => {
        success()
      })
      this._callback[provider].length = 0
    }
    ads.onerror = (err) => {
      this._cache[provider] = undefined
      this._callback[provider].forEach(({ fail }) => {
        fail(err)
      })
      this._callback[provider].length = 0
    }
    document.body.append(ads)
  }
}