| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- // ad.js
- const ADType = {
- RewardedVideo: "RewardedVideo",
- FullScreenVideo: "FullScreenVideo"
- }
- class AdHelper {
- constructor() {
- this._ads = {}
- }
- load(options, onload, onerror) {
- let ops = this._fixOldOptions(options)
- let {
- adpid
- } = ops
- if (!adpid || this.isBusy(adpid)) {
- return
- }
- this.get(ops).load(onload, onerror)
- }
- show(options, onsuccess, onfail) {
- let ops = this._fixOldOptions(options)
- let {
- adpid
- } = ops
- if (!adpid) {
- return
- }
- uni.showLoading({
- mask: true
- })
- var ad = this.get(ops)
- ad.load(() => {
- uni.hideLoading()
- ad.show((e) => {
- onsuccess && onsuccess(e)
- })
- }, (err) => {
- uni.hideLoading()
- onfail && onfail(err)
- })
- }
- isBusy(adpid) {
- return (this._ads[adpid] && this._ads[adpid].isLoading)
- }
- get(options) {
- const {
- adpid,
- singleton = true
- } = options
- if (singleton === false) {
- if (this._ads[adpid]) {
- this._ads[adpid].destroy()
- delete this._ads[adpid]
- }
- }
- delete options.singleton
- if (!this._ads[adpid]) {
- this._ads[adpid] = this._createAdInstance(options)
- }
- return this._ads[adpid]
- }
- _createAdInstance(options) {
- const adType = options.adType || ADType.RewardedVideo
- delete options.adType
- let ad = null;
- if (adType === ADType.RewardedVideo) {
- ad = new RewardedVideo(options)
- } else if (adType === ADType.FullScreenVideo) {
- ad = new FullScreenVideo(options)
- }
- return ad
- }
- _fixOldOptions(options) {
- return (typeof options === "string") ? {
- adpid: options
- } : options
- }
- }
- const EXPIRED_TIME = 1000 * 60 * 30
- const ProviderType = {
- CSJ: 'csj',
- GDT: 'gdt'
- }
- const RETRY_COUNT = 1
- class AdBase {
- constructor(adInstance, options = {}) {
- this._isLoad = false
- this._isLoading = false
- this._lastLoadTime = 0
- this._lastError = null
- this._retryCount = 0
- this._loadCallback = null
- this._closeCallback = null
- this._errorCallback = null
- const ad = this._ad = adInstance
- ad.onLoad((e) => {
- this._isLoading = false
- this._isLoad = true
- this._lastLoadTime = Date.now()
- this.onLoad()
- })
- ad.onClose((e) => {
- this._isLoad = false
- this.onClose(e)
- })
- ad.onVerify && ad.onVerify((e) => {
- // e.isValid
- })
- ad.onError(({
- code,
- message
- }) => {
- this._isLoading = false
- const data = {
- code: code,
- errMsg: message
- }
- if (code === -5008) {
- this._loadAd()
- return
- }
- if (this._retryCount < RETRY_COUNT) {
- this._retryCount += 1
- this._loadAd()
- return
- }
- this._lastError = data
- this.onError(data)
- })
- }
- get isExpired() {
- return (this._lastLoadTime !== 0 && (Math.abs(Date.now() - this._lastLoadTime) > EXPIRED_TIME))
- }
- get isLoading() {
- return this._isLoading
- }
- getProvider() {
- return this._ad.getProvider()
- }
- load(onload, onerror) {
- this._loadCallback = onload
- this._errorCallback = onerror
- if (this._isLoading) {
- return
- }
- if (this._isLoad) {
- this.onLoad()
- return
- }
- this._retryCount = 0
- this._loadAd()
- }
- show(onclose) {
- this._closeCallback = onclose
- if (this._isLoading || !this._isLoad) {
- return
- }
- if (this._lastError !== null) {
- this.onError(this._lastError)
- return
- }
- const provider = this.getProvider()
- if (provider === ProviderType.CSJ && this.isExpired) {
- this._loadAd()
- return
- }
- this._ad.show()
- }
- onLoad(e) {
- if (this._loadCallback != null) {
- this._loadCallback()
- }
- }
- onClose(e) {
- if (this._closeCallback != null) {
- this._closeCallback({
- isEnded: e.isEnded
- })
- }
- }
- onError(e) {
- if (this._errorCallback != null) {
- this._errorCallback(e)
- }
- }
- destroy() {
- this._ad.destroy()
- }
- _loadAd() {
- this._isLoad = false
- this._isLoading = true
- this._lastError = null
- this._ad.load()
- }
- }
- class RewardedVideo extends AdBase {
- constructor(options = {}) {
- super(plus.ad.createRewardedVideoAd(options), options)
- }
- }
- class FullScreenVideo extends AdBase {
- constructor(options = {}) {
- super(plus.ad.createFullScreenVideoAd(options), options)
- }
- }
- export default new AdHelper()
|