'a'
mh-two-thousand-and-two
2024-04-12 44d2c92345cd156a59fc327b3060292a282d2893
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
const eventNames = [
  'load',
  'close',
  'verify',
  'error',
  'adClicked'
]
 
const ERROR_CODE_LIST = [-5001, -5002, -5003, -5004, -5005, -5006]
const EXPIRED_TIME = 1000 * 60 * 30
const EXPIRED_TEXT = { code: -5008, errMsg: '广告数据已过期,请重新加载' }
const ProviderType = { CSJ: 'csj', GDT: 'gdt' }
 
class RewardedVideoAd {
  constructor (options = {}) {
    const _callbacks = this._callbacks = {}
    eventNames.forEach(item => {
      _callbacks[item] = []
      const name = item[0].toUpperCase() + item.substr(1)
      this[`on${name}`] = function (callback) {
        _callbacks[item].push(callback)
      }
    })
 
    this._preload = options.preload !== undefined ? options.preload : true
    this._isLoad = false
    this._isLoading = false
    this._adError = ''
    this._loadPromiseResolve = null
    this._loadPromiseReject = null
    this._lastLoadTime = 0
 
    const rewardAd = this._rewardAd = plus.ad.createRewardedVideoAd(options)
    rewardAd.onLoad((e) => {
      this._isLoad = true
      this._isLoading = false
      this._lastLoadTime = Date.now()
      this._dispatchEvent('load', {})
 
      if (this._loadPromiseResolve != null) {
        this._loadPromiseResolve()
        this._loadPromiseResolve = null
      }
    })
    rewardAd.onClose((e) => {
      this._isLoad = false
      this._isLoading = false
      if (this._preload) {
        this._loadAd()
      }
      this._dispatchEvent('close', { isEnded: e.isEnded })
    })
    rewardAd.onVerify && rewardAd.onVerify((e) => {
      this._dispatchEvent('verify', { isValid: e.isValid })
    })
    rewardAd.onError((e) => {
      this._isLoading = false
      const { code, message } = e
      const data = { code: code, errMsg: message }
      this._adError = message
      if (code === -5008) {
        this._isLoad = false
      }
      this._dispatchEvent('error', data)
      // TODO
      if ((code === -5005 || ERROR_CODE_LIST.index(code) === -1) && this._loadPromiseReject != null) {
        this._loadPromiseReject(data)
        this._loadPromiseReject = null
      }
    })
    rewardAd.onAdClicked((e) => {
      this._dispatchEvent('adClicked', {})
    })
 
    this._loadAd()
  }
 
  get isExpired () {
    return (this._lastLoadTime !== 0 && (Math.abs(Date.now() - this._lastLoadTime) > EXPIRED_TIME))
  }
 
  load () {
    return new Promise((resolve, reject) => {
      this._loadPromiseResolve = resolve
      this._loadPromiseReject = reject
      if (this._isLoading) {
        return
      }
      if (this._isLoad) {
        resolve()
        return
      }
      this._loadAd()
    })
  }
 
  show () {
    return new Promise((resolve, reject) => {
      if (this._isLoading) {
        return
      }
 
      const provider = this.getProvider()
      if (provider === ProviderType.CSJ && this.isExpired) {
        this._isLoad = false
        // TODO
        this._dispatchEvent('error', EXPIRED_TEXT)
        reject(new Error(EXPIRED_TEXT.errMsg))
        return
      }
 
      if (this._isLoad) {
        this._rewardAd.show()
        resolve()
      } else {
        reject(new Error(this._adError))
      }
    })
  }
 
  getProvider () {
    return this._rewardAd.getProvider()
  }
 
  destroy () {
    this._rewardAd.destroy()
  }
 
  _loadAd () {
    this._isLoad = false
    this._isLoading = true
    this._rewardAd.load()
  }
 
  _dispatchEvent (name, data) {
    this._callbacks[name].forEach(callback => {
      if (typeof callback === 'function') {
        callback(data || {})
      }
    })
  }
}
 
export function createRewardedVideoAd (options) {
  return new RewardedVideoAd(options)
}