ad.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // ad.js
  2. const ADType = {
  3. RewardedVideo: "RewardedVideo",
  4. FullScreenVideo: "FullScreenVideo"
  5. }
  6. class AdHelper {
  7. constructor() {
  8. this._ads = {}
  9. }
  10. load(options, onload, onerror) {
  11. let ops = this._fixOldOptions(options)
  12. let {
  13. adpid
  14. } = ops
  15. if (!adpid || this.isBusy(adpid)) {
  16. return
  17. }
  18. this.get(ops).load(onload, onerror)
  19. }
  20. show(options, onsuccess, onfail) {
  21. let ops = this._fixOldOptions(options)
  22. let {
  23. adpid
  24. } = ops
  25. if (!adpid) {
  26. return
  27. }
  28. uni.showLoading({
  29. mask: true
  30. })
  31. var ad = this.get(ops)
  32. ad.load(() => {
  33. uni.hideLoading()
  34. ad.show((e) => {
  35. onsuccess && onsuccess(e)
  36. })
  37. }, (err) => {
  38. uni.hideLoading()
  39. onfail && onfail(err)
  40. })
  41. }
  42. isBusy(adpid) {
  43. return (this._ads[adpid] && this._ads[adpid].isLoading)
  44. }
  45. get(options) {
  46. const {
  47. adpid,
  48. singleton = true
  49. } = options
  50. if (singleton === false) {
  51. if (this._ads[adpid]) {
  52. this._ads[adpid].destroy()
  53. delete this._ads[adpid]
  54. }
  55. }
  56. delete options.singleton
  57. if (!this._ads[adpid]) {
  58. this._ads[adpid] = this._createAdInstance(options)
  59. }
  60. return this._ads[adpid]
  61. }
  62. _createAdInstance(options) {
  63. const adType = options.adType || ADType.RewardedVideo
  64. delete options.adType
  65. let ad = null;
  66. if (adType === ADType.RewardedVideo) {
  67. ad = new RewardedVideo(options)
  68. } else if (adType === ADType.FullScreenVideo) {
  69. ad = new FullScreenVideo(options)
  70. }
  71. return ad
  72. }
  73. _fixOldOptions(options) {
  74. return (typeof options === "string") ? {
  75. adpid: options
  76. } : options
  77. }
  78. }
  79. const EXPIRED_TIME = 1000 * 60 * 30
  80. const ProviderType = {
  81. CSJ: 'csj',
  82. GDT: 'gdt'
  83. }
  84. const RETRY_COUNT = 1
  85. class AdBase {
  86. constructor(adInstance, options = {}) {
  87. this._isLoad = false
  88. this._isLoading = false
  89. this._lastLoadTime = 0
  90. this._lastError = null
  91. this._retryCount = 0
  92. this._loadCallback = null
  93. this._closeCallback = null
  94. this._errorCallback = null
  95. const ad = this._ad = adInstance
  96. ad.onLoad((e) => {
  97. this._isLoading = false
  98. this._isLoad = true
  99. this._lastLoadTime = Date.now()
  100. this.onLoad()
  101. })
  102. ad.onClose((e) => {
  103. this._isLoad = false
  104. this.onClose(e)
  105. })
  106. ad.onVerify && ad.onVerify((e) => {
  107. // e.isValid
  108. })
  109. ad.onError(({
  110. code,
  111. message
  112. }) => {
  113. this._isLoading = false
  114. const data = {
  115. code: code,
  116. errMsg: message
  117. }
  118. if (code === -5008) {
  119. this._loadAd()
  120. return
  121. }
  122. if (this._retryCount < RETRY_COUNT) {
  123. this._retryCount += 1
  124. this._loadAd()
  125. return
  126. }
  127. this._lastError = data
  128. this.onError(data)
  129. })
  130. }
  131. get isExpired() {
  132. return (this._lastLoadTime !== 0 && (Math.abs(Date.now() - this._lastLoadTime) > EXPIRED_TIME))
  133. }
  134. get isLoading() {
  135. return this._isLoading
  136. }
  137. getProvider() {
  138. return this._ad.getProvider()
  139. }
  140. load(onload, onerror) {
  141. this._loadCallback = onload
  142. this._errorCallback = onerror
  143. if (this._isLoading) {
  144. return
  145. }
  146. if (this._isLoad) {
  147. this.onLoad()
  148. return
  149. }
  150. this._retryCount = 0
  151. this._loadAd()
  152. }
  153. show(onclose) {
  154. this._closeCallback = onclose
  155. if (this._isLoading || !this._isLoad) {
  156. return
  157. }
  158. if (this._lastError !== null) {
  159. this.onError(this._lastError)
  160. return
  161. }
  162. const provider = this.getProvider()
  163. if (provider === ProviderType.CSJ && this.isExpired) {
  164. this._loadAd()
  165. return
  166. }
  167. this._ad.show()
  168. }
  169. onLoad(e) {
  170. if (this._loadCallback != null) {
  171. this._loadCallback()
  172. }
  173. }
  174. onClose(e) {
  175. if (this._closeCallback != null) {
  176. this._closeCallback({
  177. isEnded: e.isEnded
  178. })
  179. }
  180. }
  181. onError(e) {
  182. if (this._errorCallback != null) {
  183. this._errorCallback(e)
  184. }
  185. }
  186. destroy() {
  187. this._ad.destroy()
  188. }
  189. _loadAd() {
  190. this._isLoad = false
  191. this._isLoading = true
  192. this._lastError = null
  193. this._ad.load()
  194. }
  195. }
  196. class RewardedVideo extends AdBase {
  197. constructor(options = {}) {
  198. super(plus.ad.createRewardedVideoAd(options), options)
  199. }
  200. }
  201. class FullScreenVideo extends AdBase {
  202. constructor(options = {}) {
  203. super(plus.ad.createFullScreenVideoAd(options), options)
  204. }
  205. }
  206. export default new AdHelper()