This commit is contained in:
327
node_modules/@dcloudio/uni-components/lib/ad/ad-interstitial.web.js
generated
vendored
Normal file
327
node_modules/@dcloudio/uni-components/lib/ad/ad-interstitial.web.js
generated
vendored
Normal file
@@ -0,0 +1,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)
|
||||
}
|
||||
}
|
||||
556
node_modules/@dcloudio/uni-components/lib/ad/ad.mixin.app.js
generated
vendored
Normal file
556
node_modules/@dcloudio/uni-components/lib/ad/ad.mixin.app.js
generated
vendored
Normal file
@@ -0,0 +1,556 @@
|
||||
const ADType = {
|
||||
RewardedVideo: 'RewardedVideo',
|
||||
FullScreenVideo: 'FullScreenVideo',
|
||||
Interstitial: 'Interstitial'
|
||||
}
|
||||
|
||||
const EventType = {
|
||||
Load: 'load',
|
||||
Close: 'close',
|
||||
Error: 'error'
|
||||
}
|
||||
|
||||
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._isPlaying = false
|
||||
this._lastLoadTime = 0
|
||||
this._lastError = null
|
||||
this._retryCount = 0
|
||||
if (options.retry !== undefined) {
|
||||
this._retry = options.retry
|
||||
} else {
|
||||
this._retry = true
|
||||
}
|
||||
|
||||
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._isPlaying = false
|
||||
this.onClose(e)
|
||||
})
|
||||
ad.onVerify && ad.onVerify((e) => {
|
||||
// e.isValid
|
||||
})
|
||||
ad.onError(({
|
||||
code,
|
||||
message,
|
||||
detail
|
||||
}) => {
|
||||
this._isLoading = false
|
||||
const data = {
|
||||
code: code,
|
||||
errMsg: message,
|
||||
detail: detail
|
||||
}
|
||||
|
||||
if (code === -5008 && this._retry && 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 isLoad () {
|
||||
return this._isLoad
|
||||
}
|
||||
|
||||
get isLoading () {
|
||||
return this._isLoading
|
||||
}
|
||||
|
||||
getProvider () {
|
||||
return this._ad.getProvider()
|
||||
}
|
||||
|
||||
load (onload, onerror) {
|
||||
this._loadCallback = onload
|
||||
this._errorCallback = onerror
|
||||
|
||||
if (this._isPlaying) {
|
||||
onerror && onerror()
|
||||
return
|
||||
}
|
||||
|
||||
if (this._isLoading) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this._isLoad) {
|
||||
this.onLoad()
|
||||
return
|
||||
}
|
||||
|
||||
this._retryCount = 0
|
||||
|
||||
this._loadAd()
|
||||
}
|
||||
|
||||
show (onclose, onshow) {
|
||||
this._closeCallback = onclose
|
||||
|
||||
if (this._isLoading || this._isPlaying || !this._isLoad) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this._lastError !== null) {
|
||||
this.onError(this._lastError)
|
||||
return
|
||||
}
|
||||
|
||||
const provider = this.getProvider()
|
||||
if (provider === ProviderType.CSJ && this.isExpired) {
|
||||
if (this._retry) {
|
||||
this._loadAd()
|
||||
} else {
|
||||
this.onError(this._lastError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
this._isPlaying = true
|
||||
this._ad.show()
|
||||
onshow && onshow()
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
class Interstitial extends AdBase {
|
||||
constructor (options = {}) {
|
||||
super(plus.ad.createInterstitialAd(options), options)
|
||||
}
|
||||
}
|
||||
|
||||
class AdHelper {
|
||||
constructor (adType) {
|
||||
this._ads = {}
|
||||
this._adType = adType
|
||||
this._lastWaterfallIndex = -1
|
||||
}
|
||||
|
||||
load (options, onload, onerror) {
|
||||
if (!options.adpid || this.isBusy(options.adpid)) {
|
||||
return
|
||||
}
|
||||
|
||||
this.get(options).load(onload, onerror)
|
||||
}
|
||||
|
||||
show (options, onload, onerror, onclose, onshow) {
|
||||
const ad = this.get(options)
|
||||
|
||||
if (ad.isLoad) {
|
||||
this._lastWaterfallIndex = -1
|
||||
ad.show((e) => {
|
||||
onclose && onclose(e)
|
||||
}, () => {
|
||||
onshow && onshow()
|
||||
})
|
||||
} else {
|
||||
ad.load(() => {
|
||||
this._lastWaterfallIndex = -1
|
||||
onload && onload()
|
||||
ad.show((e) => {
|
||||
onclose && onclose(e)
|
||||
}, () => {
|
||||
onshow && onshow()
|
||||
})
|
||||
}, (err) => {
|
||||
onerror && onerror(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 底价预载逻辑
|
||||
loadWaterfall (options, onload, onfail, index = 0) {
|
||||
const {
|
||||
adpid,
|
||||
urlCallback
|
||||
} = options
|
||||
if (!Array.isArray(adpid)) {
|
||||
return
|
||||
}
|
||||
|
||||
const options2 = {
|
||||
adpid: adpid[index],
|
||||
urlCallback,
|
||||
retry: false
|
||||
}
|
||||
|
||||
console.log('ad.loadWaterfall::index=' + index)
|
||||
|
||||
this.load(options2, (res) => {
|
||||
this._lastWaterfallIndex = index
|
||||
onload(options2)
|
||||
}, (err) => {
|
||||
index++
|
||||
if (index >= adpid.length) {
|
||||
onfail(err)
|
||||
} else {
|
||||
this.loadWaterfall(options, onload, onfail, index)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 底价逻辑,失败后下一个,无重试机制
|
||||
showWaterfall (options, onload, onfail, onclose, onshow, index = 0) {
|
||||
const {
|
||||
adpid,
|
||||
urlCallback
|
||||
} = options
|
||||
if (!Array.isArray(adpid)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this._lastWaterfallIndex > -1) {
|
||||
index = this._lastWaterfallIndex
|
||||
}
|
||||
|
||||
const options2 = {
|
||||
adpid: adpid[index],
|
||||
urlCallback,
|
||||
retry: false
|
||||
}
|
||||
|
||||
console.log('ad.showWaterfall::index=' + index)
|
||||
|
||||
this.show(options2, () => {
|
||||
onload()
|
||||
}, (err) => {
|
||||
index++
|
||||
if (index >= adpid.length) {
|
||||
onfail(err)
|
||||
} else {
|
||||
this.showWaterfall(options, onload, onfail, onclose, onshow, index)
|
||||
}
|
||||
}, (res) => {
|
||||
onclose(res)
|
||||
}, () => {
|
||||
onshow()
|
||||
})
|
||||
}
|
||||
|
||||
// 预载底价瀑布流
|
||||
preloadWaterfall (options, index = 0, step = 1) {
|
||||
if (step === 1) {
|
||||
this.loadWaterfall(options, (res) => {
|
||||
console.log('preloadWaterfall.success::', res)
|
||||
}, (err) => {
|
||||
console.log('loadWaterfall.fail', err)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const {
|
||||
adpid,
|
||||
urlCallback
|
||||
} = options
|
||||
for (let i = 0; i < step; i++) {
|
||||
if (index < adpid.length) {
|
||||
const options2 = {
|
||||
adpid: adpid[index],
|
||||
urlCallback
|
||||
}
|
||||
this.loadWaterfall(options2, (res) => {
|
||||
console.log('preloadWaterfall.success::', res)
|
||||
}, (err) => {
|
||||
console.log('loadWaterfall.fail', err)
|
||||
this.preloadWaterfall(options, index, step)
|
||||
})
|
||||
index++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isBusy (adpid) {
|
||||
return (this._ads[adpid] && this._ads[adpid].isLoading)
|
||||
}
|
||||
|
||||
get (options) {
|
||||
const {
|
||||
adpid
|
||||
} = options
|
||||
|
||||
if (!this._ads[adpid]) {
|
||||
this._ads[adpid] = this._createInstance(options)
|
||||
}
|
||||
|
||||
return this._ads[adpid]
|
||||
}
|
||||
|
||||
getProvider (adpid) {
|
||||
if (this._ads[adpid]) {
|
||||
return this._ads[adpid].getProvider()
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
remove (adpid) {
|
||||
if (this._ads[adpid]) {
|
||||
this._ads[adpid].destroy()
|
||||
delete this._ads[adpid]
|
||||
}
|
||||
}
|
||||
|
||||
_createInstance (options) {
|
||||
const adType = options.adType || this._adType
|
||||
delete options.adType
|
||||
|
||||
let ad = null
|
||||
if (adType === ADType.RewardedVideo) {
|
||||
ad = new RewardedVideo(options)
|
||||
} else if (adType === ADType.FullScreenVideo) {
|
||||
ad = new FullScreenVideo(options)
|
||||
} else if (adType === ADType.Interstitial) {
|
||||
ad = new Interstitial(options, true)
|
||||
}
|
||||
|
||||
return ad
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
props: {
|
||||
options: {
|
||||
type: [Object, Array],
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
},
|
||||
disabled: {
|
||||
type: [Boolean, String],
|
||||
default: false
|
||||
},
|
||||
adpid: {
|
||||
type: [Number, String, Array],
|
||||
default: ''
|
||||
},
|
||||
preload: {
|
||||
type: [Boolean, String],
|
||||
default: true
|
||||
},
|
||||
loadnext: {
|
||||
type: [Boolean, String],
|
||||
default: false
|
||||
},
|
||||
urlCallback: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
loading: false,
|
||||
errorMessage: null
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
adpid (newValue, oldValue) {
|
||||
this._removeInstance(oldValue)
|
||||
if (this.preload) {
|
||||
this._loadAd()
|
||||
}
|
||||
},
|
||||
// 服务器回调透传参数,仅在创建广告实例时可传递参数,如果发生变化需要重新创建广告实例
|
||||
urlCallback: {
|
||||
deep: true,
|
||||
handler() {
|
||||
this._removeInstance()
|
||||
}
|
||||
}
|
||||
},
|
||||
created () {
|
||||
this._adHelper = new AdHelper(this.adType)
|
||||
|
||||
setTimeout(() => {
|
||||
if (this.preload) {
|
||||
this._loadAd()
|
||||
}
|
||||
}, 100)
|
||||
},
|
||||
methods: {
|
||||
load () {
|
||||
if (this.isLoading) {
|
||||
return
|
||||
}
|
||||
this._startLoading()
|
||||
const invoke = this._isWaterfall() ? 'loadWaterfall' : 'load'
|
||||
this._adHelper[invoke](this._getAdOptions(), () => {
|
||||
this._onLoad()
|
||||
}, (err) => {
|
||||
this._onLoadFail(err)
|
||||
})
|
||||
},
|
||||
|
||||
show () {
|
||||
if (this.isLoading) {
|
||||
return
|
||||
}
|
||||
this._startLoading()
|
||||
const invoke = this._isWaterfall() ? 'showWaterfall' : 'show'
|
||||
this._adHelper[invoke](this._getAdOptions(), () => {
|
||||
this._onLoad()
|
||||
}, (err) => {
|
||||
this._onLoadFail(err)
|
||||
}, (res) => {
|
||||
this._dispatchEvent(EventType.Close, res)
|
||||
|
||||
if (this.loadnext) {
|
||||
this.load()
|
||||
}
|
||||
}, () => {
|
||||
// show
|
||||
this.loading = false
|
||||
})
|
||||
},
|
||||
|
||||
getProvider () {
|
||||
if (Array.isArray(this.adpid)) {
|
||||
return null
|
||||
}
|
||||
return this._adHelper.getProvider(this.adpid)
|
||||
},
|
||||
|
||||
_loadAd () {
|
||||
if (this._canCreateAd()) {
|
||||
this.load()
|
||||
}
|
||||
},
|
||||
|
||||
_onclick () {
|
||||
if (!this.disabled) {
|
||||
this.show()
|
||||
}
|
||||
},
|
||||
|
||||
_getAdOptions () {
|
||||
return {
|
||||
adpid: this.adpid,
|
||||
urlCallback: this.urlCallback
|
||||
}
|
||||
},
|
||||
|
||||
_isWaterfall () {
|
||||
return (Array.isArray(this.adpid) && this.adpid.length > 0)
|
||||
},
|
||||
|
||||
_canCreateAd () {
|
||||
let result = false
|
||||
if (Array.isArray(this.adpid) && this.adpid.length > 0) {
|
||||
result = true
|
||||
} else if (typeof this.adpid === 'string' && this.adpid.length > 0) {
|
||||
result = true
|
||||
} else if (typeof this.adpid === 'number') {
|
||||
result = true
|
||||
}
|
||||
return result
|
||||
},
|
||||
|
||||
_removeInstance (adpid) {
|
||||
const id = adpid || this.adpid
|
||||
if (Array.isArray(id)) {
|
||||
id.forEach((item) => {
|
||||
this._adHelper.remove(item)
|
||||
})
|
||||
} else if (id) {
|
||||
this._adHelper.remove(id)
|
||||
}
|
||||
},
|
||||
|
||||
_startLoading () {
|
||||
this.loading = true
|
||||
this.errorMessage = null
|
||||
},
|
||||
|
||||
_onLoad () {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
},
|
||||
|
||||
_onLoadFail (err) {
|
||||
this.loading = false
|
||||
this.errorMessage = JSON.stringify(err)
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
},
|
||||
|
||||
_dispatchEvent (type, data) {
|
||||
this.$emit(type, {
|
||||
detail: data
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
556
node_modules/@dcloudio/uni-components/lib/ad/ad.mixin.js
generated
vendored
Normal file
556
node_modules/@dcloudio/uni-components/lib/ad/ad.mixin.js
generated
vendored
Normal file
@@ -0,0 +1,556 @@
|
||||
const ADType = {
|
||||
RewardedVideo: 'RewardedVideo',
|
||||
FullScreenVideo: 'FullScreenVideo',
|
||||
Interstitial: 'Interstitial'
|
||||
}
|
||||
|
||||
const EventType = {
|
||||
Load: 'load',
|
||||
Close: 'close',
|
||||
Error: 'error'
|
||||
}
|
||||
|
||||
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._isPlaying = false
|
||||
this._lastLoadTime = 0
|
||||
this._lastError = null
|
||||
this._retryCount = 0
|
||||
if (options.retry !== undefined) {
|
||||
this._retry = options.retry
|
||||
} else {
|
||||
this._retry = true
|
||||
}
|
||||
|
||||
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._isPlaying = 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 (this._retry && code === -5008) {
|
||||
this._loadAd()
|
||||
return
|
||||
}
|
||||
|
||||
if (this._retry && 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 isLoad () {
|
||||
return this._isLoad
|
||||
}
|
||||
|
||||
get isLoading () {
|
||||
return this._isLoading
|
||||
}
|
||||
|
||||
getProvider () {
|
||||
return this._ad.getProvider()
|
||||
}
|
||||
|
||||
load (onload, onerror) {
|
||||
this._loadCallback = onload
|
||||
this._errorCallback = onerror
|
||||
|
||||
if (this._isPlaying) {
|
||||
onerror && onerror()
|
||||
return
|
||||
}
|
||||
|
||||
if (this._isLoading) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this._isLoad) {
|
||||
this.onLoad()
|
||||
return
|
||||
}
|
||||
|
||||
this._retryCount = 0
|
||||
|
||||
this._loadAd()
|
||||
}
|
||||
|
||||
show (onclose, onshow) {
|
||||
this._closeCallback = onclose
|
||||
|
||||
if (this._isLoading || this._isPlaying || !this._isLoad) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this._lastError !== null) {
|
||||
this.onError(this._lastError)
|
||||
return
|
||||
}
|
||||
|
||||
const provider = this.getProvider()
|
||||
if (provider === ProviderType.CSJ && this.isExpired) {
|
||||
if (this._retry) {
|
||||
this._loadAd()
|
||||
} else {
|
||||
this.onError(this._lastError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
this._isPlaying = true
|
||||
this._ad.show()
|
||||
onshow && onshow()
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
class Interstitial extends AdBase {
|
||||
constructor (options = {}) {
|
||||
super(plus.ad.createInterstitialAd(options), options)
|
||||
}
|
||||
}
|
||||
|
||||
class AdHelper {
|
||||
constructor (adType) {
|
||||
this._ads = {}
|
||||
this._adType = adType
|
||||
this._lastWaterfallIndex = -1
|
||||
}
|
||||
|
||||
load (options, onload, onerror) {
|
||||
if (!options.adpid || this.isBusy(options.adpid)) {
|
||||
return
|
||||
}
|
||||
|
||||
this.get(options).load(onload, onerror)
|
||||
}
|
||||
|
||||
show (options, onload, onerror, onclose, onshow) {
|
||||
const ad = this.get(options)
|
||||
|
||||
if (ad.isLoad) {
|
||||
this._lastWaterfallIndex = -1
|
||||
ad.show((e) => {
|
||||
onclose && onclose(e)
|
||||
}, () => {
|
||||
onshow && onshow()
|
||||
})
|
||||
} else {
|
||||
ad.load(() => {
|
||||
this._lastWaterfallIndex = -1
|
||||
onload && onload()
|
||||
ad.show((e) => {
|
||||
onclose && onclose(e)
|
||||
}, () => {
|
||||
onshow && onshow()
|
||||
})
|
||||
}, (err) => {
|
||||
onerror && onerror(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 底价预载逻辑
|
||||
loadWaterfall (options, onload, onfail, index = 0) {
|
||||
const {
|
||||
adpid,
|
||||
urlCallback
|
||||
} = options
|
||||
if (!Array.isArray(adpid)) {
|
||||
return
|
||||
}
|
||||
|
||||
const options2 = {
|
||||
adpid: adpid[index],
|
||||
urlCallback,
|
||||
retry: false
|
||||
}
|
||||
|
||||
console.log('ad.loadWaterfall::index=' + index)
|
||||
|
||||
this.load(options2, (res) => {
|
||||
this._lastWaterfallIndex = index
|
||||
onload(options2)
|
||||
}, (err) => {
|
||||
index++
|
||||
if (index >= adpid.length) {
|
||||
onfail(err)
|
||||
} else {
|
||||
this.loadWaterfall(options, onload, onfail, index)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 底价逻辑,失败后下一个,无重试机制
|
||||
showWaterfall (options, onload, onfail, onclose, onshow, index = 0) {
|
||||
const {
|
||||
adpid,
|
||||
urlCallback
|
||||
} = options
|
||||
if (!Array.isArray(adpid)) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this._lastWaterfallIndex > -1) {
|
||||
index = this._lastWaterfallIndex
|
||||
}
|
||||
|
||||
const options2 = {
|
||||
adpid: adpid[index],
|
||||
urlCallback,
|
||||
retry: false
|
||||
}
|
||||
|
||||
console.log('ad.showWaterfall::index=' + index)
|
||||
|
||||
this.show(options2, () => {
|
||||
onload()
|
||||
}, (err) => {
|
||||
index++
|
||||
if (index >= adpid.length) {
|
||||
onfail(err)
|
||||
} else {
|
||||
this.showWaterfall(options, onload, onfail, onclose, onshow, index)
|
||||
}
|
||||
}, (res) => {
|
||||
onclose(res)
|
||||
}, () => {
|
||||
onshow()
|
||||
})
|
||||
}
|
||||
|
||||
// 预载底价瀑布流
|
||||
preloadWaterfall (options, index = 0, step = 1) {
|
||||
if (step === 1) {
|
||||
this.loadWaterfall(options, (res) => {
|
||||
console.log('preloadWaterfall.success::', res)
|
||||
}, (err) => {
|
||||
console.log('loadWaterfall.fail', err)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const {
|
||||
adpid,
|
||||
urlCallback
|
||||
} = options
|
||||
for (let i = 0; i < step; i++) {
|
||||
if (index < adpid.length) {
|
||||
const options2 = {
|
||||
adpid: adpid[index],
|
||||
urlCallback
|
||||
}
|
||||
this.loadWaterfall(options2, (res) => {
|
||||
console.log('preloadWaterfall.success::', res)
|
||||
}, (err) => {
|
||||
console.log('loadWaterfall.fail', err)
|
||||
this.preloadWaterfall(options, index, step)
|
||||
})
|
||||
index++
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
isBusy (adpid) {
|
||||
return (this._ads[adpid] && this._ads[adpid].isLoading)
|
||||
}
|
||||
|
||||
get (options) {
|
||||
const {
|
||||
adpid
|
||||
} = options
|
||||
|
||||
if (!this._ads[adpid]) {
|
||||
this._ads[adpid] = this._createInstance(options)
|
||||
}
|
||||
|
||||
return this._ads[adpid]
|
||||
}
|
||||
|
||||
getProvider (adpid) {
|
||||
if (this._ads[adpid]) {
|
||||
return this._ads[adpid].getProvider()
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
remove (adpid) {
|
||||
if (this._ads[adpid]) {
|
||||
this._ads[adpid].destroy()
|
||||
delete this._ads[adpid]
|
||||
}
|
||||
}
|
||||
|
||||
_createInstance (options) {
|
||||
const adType = options.adType || this._adType
|
||||
delete options.adType
|
||||
|
||||
let ad = null
|
||||
if (adType === ADType.RewardedVideo) {
|
||||
ad = new RewardedVideo(options)
|
||||
} else if (adType === ADType.FullScreenVideo) {
|
||||
ad = new FullScreenVideo(options)
|
||||
} else if (adType === ADType.Interstitial) {
|
||||
ad = new Interstitial(options, true)
|
||||
}
|
||||
|
||||
return ad
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
props: {
|
||||
options: {
|
||||
type: [Object, Array],
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
},
|
||||
disabled: {
|
||||
type: [Boolean, String],
|
||||
default: false
|
||||
},
|
||||
adpid: {
|
||||
type: [Number, String, Array],
|
||||
default: ''
|
||||
},
|
||||
preload: {
|
||||
type: [Boolean, String],
|
||||
default: true
|
||||
},
|
||||
loadnext: {
|
||||
type: [Boolean, String],
|
||||
default: false
|
||||
},
|
||||
urlCallback: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
loading: false,
|
||||
errorMessage: null
|
||||
}
|
||||
},
|
||||
created() {
|
||||
this.$watch('adpid', (newValue, oldValue) => {
|
||||
this._removeInstance(oldValue)
|
||||
if (this.preload) {
|
||||
this._loadAd()
|
||||
}
|
||||
})
|
||||
|
||||
// 服务器回调透传参数,仅在创建广告实例时可传递参数,如果发生变化需要重新创建广告实例
|
||||
this.$watch('urlCallback', () => {
|
||||
this._removeInstance()
|
||||
})
|
||||
|
||||
this._adHelper = new AdHelper(this.adType)
|
||||
|
||||
setTimeout(() => {
|
||||
if (this.preload) {
|
||||
this._loadAd()
|
||||
}
|
||||
}, 100)
|
||||
},
|
||||
methods: {
|
||||
load () {
|
||||
if (this.isLoading) {
|
||||
return
|
||||
}
|
||||
this._startLoading()
|
||||
const invoke = this._isWaterfall() ? 'loadWaterfall' : 'load'
|
||||
this._adHelper[invoke](this._getAdOptions(), () => {
|
||||
this._onLoad()
|
||||
}, (err) => {
|
||||
this._onLoadFail(err)
|
||||
})
|
||||
},
|
||||
|
||||
show () {
|
||||
if (this.isLoading) {
|
||||
return
|
||||
}
|
||||
this._startLoading()
|
||||
const invoke = this._isWaterfall() ? 'showWaterfall' : 'show'
|
||||
this._adHelper[invoke](this._getAdOptions(), () => {
|
||||
this._onLoad()
|
||||
}, (err) => {
|
||||
this._onLoadFail(err)
|
||||
}, (res) => {
|
||||
this._dispatchEvent(EventType.Close, res)
|
||||
|
||||
if (this.loadnext) {
|
||||
this.load()
|
||||
}
|
||||
}, () => {
|
||||
// show
|
||||
this.loading = false
|
||||
})
|
||||
},
|
||||
|
||||
getProvider () {
|
||||
if (Array.isArray(this.adpid)) {
|
||||
return null
|
||||
}
|
||||
return this._adHelper.getProvider(this.adpid)
|
||||
},
|
||||
|
||||
_loadAd () {
|
||||
if (this._canCreateAd()) {
|
||||
this.load()
|
||||
}
|
||||
},
|
||||
|
||||
_onclick () {
|
||||
if (!this.disabled) {
|
||||
this.show()
|
||||
}
|
||||
},
|
||||
|
||||
_getAdOptions () {
|
||||
return {
|
||||
adpid: this.adpid,
|
||||
urlCallback: this.urlCallback
|
||||
}
|
||||
},
|
||||
|
||||
_isWaterfall () {
|
||||
return (Array.isArray(this.adpid) && this.adpid.length > 0)
|
||||
},
|
||||
|
||||
_canCreateAd () {
|
||||
let result = false
|
||||
if (Array.isArray(this.adpid) && this.adpid.length > 0) {
|
||||
result = true
|
||||
} else if (typeof this.adpid === 'string' && this.adpid.length > 0) {
|
||||
result = true
|
||||
} else if (typeof this.adpid === 'number') {
|
||||
result = true
|
||||
}
|
||||
return result
|
||||
},
|
||||
|
||||
_removeInstance (adpid) {
|
||||
const id = adpid || this.adpid
|
||||
if (Array.isArray(id)) {
|
||||
id.forEach((item) => {
|
||||
this._adHelper.remove(item)
|
||||
})
|
||||
} else if (id) {
|
||||
this._adHelper.remove(id)
|
||||
}
|
||||
},
|
||||
|
||||
_startLoading () {
|
||||
this.loading = true
|
||||
this.errorMessage = null
|
||||
},
|
||||
|
||||
_onLoad () {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
},
|
||||
|
||||
_onLoadFail (err) {
|
||||
this.loading = false
|
||||
this.errorMessage = JSON.stringify(err)
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
},
|
||||
|
||||
_dispatchEvent (type, data) {
|
||||
this.$emit(type, {
|
||||
detail: data
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
110
node_modules/@dcloudio/uni-components/lib/ad/ad.mixin.mp-alipay.js
generated
vendored
Normal file
110
node_modules/@dcloudio/uni-components/lib/ad/ad.mixin.mp-alipay.js
generated
vendored
Normal file
@@ -0,0 +1,110 @@
|
||||
const EventType = {
|
||||
Load: 'load',
|
||||
Close: 'close',
|
||||
Error: 'error'
|
||||
}
|
||||
|
||||
export default {
|
||||
props: {
|
||||
options: {
|
||||
type: [Object, Array],
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
},
|
||||
adpid: {
|
||||
type: [Number, String],
|
||||
default: ''
|
||||
},
|
||||
unitId: {
|
||||
type: [Number, String],
|
||||
default: ''
|
||||
},
|
||||
preload: {
|
||||
type: [Boolean, String],
|
||||
default: true
|
||||
},
|
||||
loadnext: {
|
||||
type: [Boolean, String],
|
||||
default: false
|
||||
},
|
||||
urlCallback: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
loading: false,
|
||||
errorMessage: null
|
||||
}
|
||||
},
|
||||
created () {
|
||||
this._ad = null
|
||||
},
|
||||
methods: {
|
||||
load () {
|
||||
if (this.loading) {
|
||||
return
|
||||
}
|
||||
this._startLoading()
|
||||
},
|
||||
|
||||
show () {
|
||||
this.errorMessage = null
|
||||
this._refAdPlugin.show()
|
||||
},
|
||||
|
||||
_handleAdRef(c) {
|
||||
this._refAdPlugin = c.detail
|
||||
if (this.preload && this._canCreateAd()) {
|
||||
this._startLoading()
|
||||
}
|
||||
},
|
||||
|
||||
_onclick () {
|
||||
this.show()
|
||||
},
|
||||
|
||||
_startLoading () {
|
||||
this.loading = true
|
||||
this.errorMessage = null
|
||||
this._refAdPlugin.load()
|
||||
},
|
||||
|
||||
_canCreateAd () {
|
||||
let result = false
|
||||
if (typeof this.adpid === 'string' && this.adpid.length > 0) {
|
||||
result = true
|
||||
} else if (typeof this.adpid === 'number') {
|
||||
result = true
|
||||
}
|
||||
return result
|
||||
},
|
||||
|
||||
_hasCallback () {
|
||||
return (typeof this.urlCallback === 'object' && Object.keys(this.urlCallback).length > 0)
|
||||
},
|
||||
|
||||
_onmpload (e) {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
},
|
||||
|
||||
_onmpclose (e) {
|
||||
this._dispatchEvent(EventType.Close, e)
|
||||
},
|
||||
|
||||
_onmperror (e) {
|
||||
this.loading = false
|
||||
this.errorMessage = JSON.stringify(e)
|
||||
this._dispatchEvent(EventType.Error, e)
|
||||
},
|
||||
|
||||
_dispatchEvent (type, data) {
|
||||
this.$emit(type, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
397
node_modules/@dcloudio/uni-components/lib/ad/ad.mixin.mp-weixin.js
generated
vendored
Normal file
397
node_modules/@dcloudio/uni-components/lib/ad/ad.mixin.mp-weixin.js
generated
vendored
Normal file
@@ -0,0 +1,397 @@
|
||||
const adPlugin = requirePlugin("uni-ad");
|
||||
|
||||
const EventType = {
|
||||
Load: 'load',
|
||||
Close: 'close',
|
||||
Error: 'error'
|
||||
}
|
||||
|
||||
const AdType = {
|
||||
Banner: "banner",
|
||||
RewardedVideo: "rewardedVideo",
|
||||
Interstitial: "interstitial"
|
||||
}
|
||||
|
||||
const ProviderType = {
|
||||
WeChat: 10018,
|
||||
UserWeChat: 10017,
|
||||
ShanHu: 10020
|
||||
}
|
||||
|
||||
export default {
|
||||
props: {
|
||||
options: {
|
||||
type: [Object, Array],
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
},
|
||||
adpid: {
|
||||
type: [Number, String],
|
||||
default: ''
|
||||
},
|
||||
unitId: {
|
||||
type: [Number, String],
|
||||
default: ''
|
||||
},
|
||||
preload: {
|
||||
type: [Boolean, String],
|
||||
default: true
|
||||
},
|
||||
loadnext: {
|
||||
type: [Boolean, String],
|
||||
default: false
|
||||
},
|
||||
urlCallback: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
loading: false,
|
||||
userwx: false,
|
||||
userUnitId: "",
|
||||
customFullscreen: '',
|
||||
wxchannel: false,
|
||||
errorMessage: null
|
||||
}
|
||||
},
|
||||
created () {
|
||||
this._ad = null
|
||||
this._loading = false
|
||||
this._wxRewardedAd = null
|
||||
this._wxInterstitialAd = null
|
||||
this._userInvokeShowFlag = false
|
||||
this._providerType = ProviderType.ShanHu
|
||||
if (this.preload && this._canCreateAd()) {
|
||||
this.load()
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
load () {
|
||||
if (this.loading) {
|
||||
return
|
||||
}
|
||||
this._startLoading()
|
||||
if (this._providerType == ProviderType.ShanHu) {
|
||||
} else if (this._providerType == ProviderType.WeChat) {
|
||||
this.selectComponent('.uniad-plugin-wx').load()
|
||||
} else if (this._providerType == ProviderType.UserWeChat) {
|
||||
this._loadWxAd()
|
||||
}
|
||||
},
|
||||
|
||||
show (e) {
|
||||
this.errorMessage = null
|
||||
if (this.loading) {
|
||||
this._userInvokeShowFlag = true
|
||||
return
|
||||
}
|
||||
if (this._providerType == ProviderType.ShanHu) {
|
||||
this._showAdInPlugin(this.selectComponent('.uniad-plugin'))
|
||||
} else if (this._providerType == ProviderType.WeChat) {
|
||||
this._showAdInPlugin(this.selectComponent('.uniad-plugin-wx'))
|
||||
} else if (this._providerType == ProviderType.UserWeChat) {
|
||||
this._showWxAd(e)
|
||||
}
|
||||
},
|
||||
|
||||
_onclick () {
|
||||
this.show()
|
||||
},
|
||||
|
||||
_startLoading () {
|
||||
this.loading = true
|
||||
this.errorMessage = null
|
||||
},
|
||||
|
||||
_canCreateAd () {
|
||||
let result = false
|
||||
if (typeof this.adpid === 'string' && this.adpid.length > 0) {
|
||||
result = true
|
||||
} else if (typeof this.adpid === 'number') {
|
||||
result = true
|
||||
}
|
||||
return result
|
||||
},
|
||||
|
||||
_hasCallback () {
|
||||
return (typeof this.urlCallback === 'object' && Object.keys(this.urlCallback).length > 0)
|
||||
},
|
||||
|
||||
_onmpload (e) {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
if (this._userInvokeShowFlag) {
|
||||
this._userInvokeShowFlag = false
|
||||
setTimeout(() => {
|
||||
this.show();
|
||||
}, 1)
|
||||
}
|
||||
},
|
||||
|
||||
_onmpclose (e) {
|
||||
const detail = e.detail || e;
|
||||
this._dispatchEvent(EventType.Close, detail)
|
||||
if (detail.adsdata) {
|
||||
const adv = detail.adv
|
||||
const adsdata = detail.adsdata
|
||||
const version = detail.version
|
||||
|
||||
/* eslint-disable no-undef */
|
||||
uniCloud.callFunction({
|
||||
name: 'uniAdCallback',
|
||||
data: {
|
||||
adv: adv,
|
||||
adsdata: adsdata,
|
||||
version: version
|
||||
},
|
||||
secretType: 'both',
|
||||
success: (res) => {
|
||||
},
|
||||
fail: (err) => {
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
}
|
||||
})
|
||||
|
||||
delete detail.adv
|
||||
delete detail.adsdata
|
||||
delete detail.version
|
||||
}
|
||||
},
|
||||
|
||||
_onmperror (e) {
|
||||
this.loading = false
|
||||
this.errorMessage = JSON.stringify(e.detail)
|
||||
this._dispatchEvent(EventType.Error, e.detail)
|
||||
},
|
||||
|
||||
_onnextchannel (e) {
|
||||
this.wxchannel = true
|
||||
const adData = e.detail[0];
|
||||
this.$nextTick(() => {
|
||||
if (adData.provider == 10017) {
|
||||
this._providerType = ProviderType.UserWeChat
|
||||
switch(adData._nt_) {
|
||||
case 4:
|
||||
this.wxAdType = AdType.Banner
|
||||
this.userwx = true
|
||||
this.userUnitId = adData.posid
|
||||
if (adData.tmpl_type === 24) {
|
||||
this.customFullscreen = 'uni-ad-custom-fullscreen'
|
||||
}
|
||||
break;
|
||||
case 9:
|
||||
this.wxAdType = AdType.RewardedVideo
|
||||
this._createRewardedAd(adData.posid)
|
||||
break;
|
||||
case 15:
|
||||
this.wxAdType = AdType.Interstitial
|
||||
this._createInterstitialAd(adData.posid)
|
||||
break;
|
||||
}
|
||||
} else if (adData.provider == 10018) {
|
||||
this._providerType = ProviderType.WeChat
|
||||
if (adData.tmpl_type === 24) {
|
||||
this.customFullscreen = 'uni-ad-custom-fullscreen'
|
||||
}
|
||||
this.loading = true
|
||||
if (!adData.dcloudAdpid) {
|
||||
adData.dcloudAdpid = this.adpid
|
||||
}
|
||||
this.selectComponent('.uniad-plugin-wx').setConfig(adData)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
_onwxchannelerror(e) {
|
||||
this._dispatchEvent(EventType.Error, e.detail || e)
|
||||
},
|
||||
|
||||
_dispatchEvent (type, data) {
|
||||
this.$emit(type, {
|
||||
detail: data
|
||||
})
|
||||
},
|
||||
|
||||
_showAdInPlugin(adComponent) {
|
||||
if (this._hasCallback()) {
|
||||
const userCryptoManager = wx.getUserCryptoManager()
|
||||
userCryptoManager.getLatestUserKey({
|
||||
success: ({
|
||||
encryptKey,
|
||||
iv,
|
||||
version,
|
||||
expireTime
|
||||
}) => {
|
||||
const uniOptions = {
|
||||
adpid: this.adpid
|
||||
};
|
||||
adComponent.show({
|
||||
userId: this.urlCallback.userId || '',
|
||||
extra: this.urlCallback.extra || '',
|
||||
encryptKey,
|
||||
iv,
|
||||
version,
|
||||
expireTime,
|
||||
uniOptions
|
||||
})
|
||||
},
|
||||
fail: (err) => {
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
adComponent.show({
|
||||
userId: this.urlCallback.userId || '',
|
||||
extra: this.urlCallback.extra || ''
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
_loadWxAd () {
|
||||
switch (this.wxAdType) {
|
||||
case AdType.RewardedVideo:
|
||||
if (this._wxRewardedAd) {
|
||||
this._wxRewardedAd.load();
|
||||
}
|
||||
break;
|
||||
case AdType.Interstitial:
|
||||
if (this._wxInterstitialAd) {
|
||||
this._wxInterstitialAd.load();
|
||||
}
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
// 加载/显示广告
|
||||
_showWxAd (options) {
|
||||
this._urlCallback = options || this.urlCallback;
|
||||
if (this.loading == true) {
|
||||
this._userInvokeShowFlag = true
|
||||
return
|
||||
}
|
||||
switch (this.wxAdType) {
|
||||
case AdType.RewardedVideo:
|
||||
if (!this._wxRewardedAd) {
|
||||
return;
|
||||
}
|
||||
this._wxRewardedAd.show().catch((err) => {
|
||||
this._wxRewardedAd.load().then(() => {
|
||||
this._wxRewardedAd.show();
|
||||
}).catch((err) => {
|
||||
this._dispatchEvent(EventType.Error, err);
|
||||
});
|
||||
});
|
||||
break;
|
||||
case AdType.Interstitial:
|
||||
if (!this._wxInterstitialAd) {
|
||||
return;
|
||||
}
|
||||
this._wxInterstitialAd.show().catch((err) => {
|
||||
this._wxInterstitialAd.load().then(() => {
|
||||
this._wxInterstitialAd.show();
|
||||
}).catch((err) => {
|
||||
this._dispatchEvent(EventType.Error, err);
|
||||
});
|
||||
});
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
// 微信激励视频
|
||||
_createRewardedAd(adUnitId) {
|
||||
if (this._wxRewardedAd) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._wxRewardedAd = wx.createRewardedVideoAd({ adUnitId: adUnitId, multiton: true });
|
||||
|
||||
this._wxRewardedAd.onLoad(() => {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
if (this._userInvokeShowFlag) {
|
||||
this._userInvokeShowFlag = false;
|
||||
this._wxRewardedAd.show();
|
||||
}
|
||||
});
|
||||
|
||||
this._wxRewardedAd.onError(err => {
|
||||
this.loading = false
|
||||
this.errorMessage = JSON.stringify(err)
|
||||
this._dispatchEvent(EventType.Error, err);
|
||||
});
|
||||
|
||||
this._wxRewardedAd.onClose(res => {
|
||||
this._dispatchEvent(EventType.Close, res);
|
||||
if (res.isEnded && this._hasCallback()) {
|
||||
this._callServer()
|
||||
}
|
||||
});
|
||||
|
||||
this._wxRewardedAd.load().then(() => {
|
||||
}).catch((_) => {
|
||||
});
|
||||
|
||||
this.loading = true
|
||||
},
|
||||
|
||||
// 微信插屏
|
||||
_createInterstitialAd(adUnitId) {
|
||||
if (this._wxInterstitialAd) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._wxInterstitialAd = wx.createInterstitialAd({ adUnitId: adUnitId });
|
||||
|
||||
this._wxInterstitialAd.onLoad(() => {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
if (this._userInvokeShowFlag) {
|
||||
this._userInvokeShowFlag = false;
|
||||
this._wxInterstitialAd.show();
|
||||
}
|
||||
});
|
||||
|
||||
this._wxInterstitialAd.onError(err => {
|
||||
this.loading = false
|
||||
this.errorMessage = JSON.stringify(err)
|
||||
this._dispatchEvent(EventType.Error, err);
|
||||
});
|
||||
|
||||
this._wxInterstitialAd.onClose(res => {
|
||||
this._dispatchEvent(EventType.Close, res);
|
||||
});
|
||||
|
||||
this._wxInterstitialAd.load().then(() => { }).catch((err) => { });
|
||||
|
||||
this.loading = true
|
||||
},
|
||||
|
||||
_callServer() {
|
||||
const userCryptoManager = wx.getUserCryptoManager()
|
||||
userCryptoManager.getLatestUserKey({
|
||||
success: (encryptConfig) => {
|
||||
const callbackData = adPlugin.buildCallbackData(this.adpid, this.urlCallback, {}, encryptConfig)
|
||||
uniCloud.callFunction({
|
||||
name: 'uniAdCallback',
|
||||
data: callbackData,
|
||||
secretType: 'both',
|
||||
success: (res) => {
|
||||
this._dispatchEvent(EventType.Close, res);
|
||||
},
|
||||
fail: (err) => {
|
||||
this._dispatchEvent(EventType.Error, err);
|
||||
}
|
||||
})
|
||||
},
|
||||
fail: (err) => {
|
||||
this._dispatchEvent(EventType.Error, err);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
159
node_modules/@dcloudio/uni-components/lib/ad/ad.mixin.mp.js
generated
vendored
Normal file
159
node_modules/@dcloudio/uni-components/lib/ad/ad.mixin.mp.js
generated
vendored
Normal file
@@ -0,0 +1,159 @@
|
||||
const EventType = {
|
||||
Load: 'load',
|
||||
Close: 'close',
|
||||
Error: 'error'
|
||||
}
|
||||
|
||||
export default {
|
||||
props: {
|
||||
options: {
|
||||
type: [Object, Array],
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
},
|
||||
adpid: {
|
||||
type: [Number, String],
|
||||
default: ''
|
||||
},
|
||||
unitId: {
|
||||
type: [Number, String],
|
||||
default: ''
|
||||
},
|
||||
preload: {
|
||||
type: [Boolean, String],
|
||||
default: true
|
||||
},
|
||||
loadnext: {
|
||||
type: [Boolean, String],
|
||||
default: false
|
||||
},
|
||||
urlCallback: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
loading: false,
|
||||
errorMessage: null
|
||||
}
|
||||
},
|
||||
created () {
|
||||
this._ad = null
|
||||
setTimeout(() => {
|
||||
if (this.preload && this._canCreateAd()) {
|
||||
this.load()
|
||||
}
|
||||
}, 100)
|
||||
},
|
||||
methods: {
|
||||
load () {
|
||||
if (this.loading) {
|
||||
return
|
||||
}
|
||||
this._startLoading()
|
||||
},
|
||||
|
||||
show () {
|
||||
this.errorMessage = null
|
||||
this._ad = this.selectComponent('.uniad-plugin')
|
||||
if (this._hasCallback()) {
|
||||
const userCryptoManager = wx.getUserCryptoManager()
|
||||
userCryptoManager.getLatestUserKey({
|
||||
success: ({
|
||||
encryptKey,
|
||||
iv,
|
||||
version,
|
||||
expireTime
|
||||
}) => {
|
||||
this._ad.show({
|
||||
userId: this.urlCallback.userId || '',
|
||||
extra: this.urlCallback.extra || '',
|
||||
encryptKey,
|
||||
iv,
|
||||
version,
|
||||
expireTime
|
||||
})
|
||||
},
|
||||
fail: (err) => {
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
this._ad.show()
|
||||
}
|
||||
},
|
||||
|
||||
_onclick () {
|
||||
this.show()
|
||||
},
|
||||
|
||||
_startLoading () {
|
||||
this.loading = true
|
||||
this.errorMessage = null
|
||||
},
|
||||
|
||||
_canCreateAd () {
|
||||
let result = false
|
||||
if (typeof this.adpid === 'string' && this.adpid.length > 0) {
|
||||
result = true
|
||||
} else if (typeof this.adpid === 'number') {
|
||||
result = true
|
||||
}
|
||||
return result
|
||||
},
|
||||
|
||||
_hasCallback () {
|
||||
return (typeof this.urlCallback === 'object' && Object.keys(this.urlCallback).length > 0)
|
||||
},
|
||||
|
||||
_onmpload (e) {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
},
|
||||
|
||||
_onmpclose (e) {
|
||||
this._dispatchEvent(EventType.Close, e.detail)
|
||||
if (e.detail.adsdata) {
|
||||
const adv = e.detail.adv
|
||||
const adsdata = e.detail.adsdata
|
||||
const version = e.detail.version
|
||||
|
||||
/* eslint-disable no-undef */
|
||||
uniCloud.callFunction({
|
||||
name: 'uniAdCallback',
|
||||
data: {
|
||||
adv: adv,
|
||||
adsdata: adsdata,
|
||||
version: version
|
||||
},
|
||||
secretType: 'both',
|
||||
success: (res) => {
|
||||
},
|
||||
fail: (err) => {
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
}
|
||||
})
|
||||
|
||||
delete e.detail.adv
|
||||
delete e.detail.adsdata
|
||||
delete e.detail.version
|
||||
}
|
||||
},
|
||||
|
||||
_onmperror (e) {
|
||||
this.loading = false
|
||||
this.errorMessage = JSON.stringify(e.detail)
|
||||
this._dispatchEvent(EventType.Error, e.detail)
|
||||
},
|
||||
|
||||
_dispatchEvent (type, data) {
|
||||
this.$emit(type, {
|
||||
detail: data
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
173
node_modules/@dcloudio/uni-components/lib/ad/ad.mixin.web.js
generated
vendored
Normal file
173
node_modules/@dcloudio/uni-components/lib/ad/ad.mixin.web.js
generated
vendored
Normal file
@@ -0,0 +1,173 @@
|
||||
const AD_SERVER_URL = 'https://hac1.dcloud.net.cn/h5/gs'
|
||||
|
||||
const EventType = {
|
||||
Load: 'load',
|
||||
Close: 'close',
|
||||
Error: 'error'
|
||||
}
|
||||
|
||||
const ProviderType = 'wm'
|
||||
|
||||
class Process {
|
||||
static Start (cmd, args) {
|
||||
return new Process().openScheme(cmd)
|
||||
}
|
||||
|
||||
constructor () {
|
||||
this._a = null
|
||||
}
|
||||
|
||||
openScheme (url) {
|
||||
if (this._a == null) {
|
||||
this._a = document.createElement('a')
|
||||
}
|
||||
this._a.href = url
|
||||
this._a.click()
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
},
|
||||
urlCallback: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
adData: null,
|
||||
loading: false,
|
||||
showModel: false,
|
||||
errorMessage: null
|
||||
}
|
||||
},
|
||||
created () {
|
||||
this._loading = false
|
||||
this.adConfigData = null
|
||||
},
|
||||
methods: {
|
||||
load () {
|
||||
setTimeout(() => {
|
||||
this._onmpload()
|
||||
}, 200)
|
||||
},
|
||||
|
||||
show (options) {
|
||||
if (!this._isMobile()) {
|
||||
this._dispatchEvent(EventType.Error, {
|
||||
errCode: -1,
|
||||
errMsg: '当前设备环境无效'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
this.errorMessage = null
|
||||
if (this._loading) {
|
||||
return
|
||||
}
|
||||
this._loading = true
|
||||
|
||||
this._requestScheme(options)
|
||||
},
|
||||
|
||||
getProvider () {
|
||||
return ProviderType
|
||||
},
|
||||
|
||||
_onclick () {
|
||||
if (this.disabled) {
|
||||
return
|
||||
}
|
||||
|
||||
this.show()
|
||||
},
|
||||
|
||||
_requestScheme (options = {}) {
|
||||
const urlCallback = options.urlCallback || this.urlCallback
|
||||
uni.request({
|
||||
url: AD_SERVER_URL,
|
||||
method: 'POST',
|
||||
data: {
|
||||
adpid: this.adpid,
|
||||
userId: urlCallback.userId,
|
||||
extra: urlCallback.extra
|
||||
},
|
||||
timeout: 5000,
|
||||
dataType: 'json',
|
||||
success: (res) => {
|
||||
if (res.statusCode !== 200) {
|
||||
this._dispatchEvent(EventType.Error, {
|
||||
errCode: res.statusCode,
|
||||
errMsg: res.statusCode
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const responseData = res.data
|
||||
if (responseData.ret === 0) {
|
||||
Process.Start(responseData.data.openlink)
|
||||
} else {
|
||||
this._dispatchEvent(EventType.Error, {
|
||||
errCode: responseData.ret,
|
||||
errMsg: responseData.msg
|
||||
})
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
this.$emit(EventType.Error, {
|
||||
errCode: '',
|
||||
errMsg: err.errMsg
|
||||
})
|
||||
},
|
||||
complete: () => {
|
||||
this.loading = false
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
_isMobile () {
|
||||
return /android|iphone/i.test(navigator.userAgent.toLowerCase())
|
||||
},
|
||||
|
||||
_onmpload (e) {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
},
|
||||
|
||||
_onmpclose (e) {
|
||||
this._dispatchEvent(EventType.Close, e.detail)
|
||||
},
|
||||
|
||||
_onmperror (e) {
|
||||
this.loading = false
|
||||
this.errorMessage = JSON.stringify(e.detail)
|
||||
this._dispatchEvent(EventType.Error, e.detail)
|
||||
},
|
||||
|
||||
_dispatchEvent (type, data) {
|
||||
this.$emit(type, {
|
||||
detail: data
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user