提交 c863f3f4 编写于 作者: d-u-a's avatar d-u-a

update: h5 ad 组件

上级 86da25e4
...@@ -78,9 +78,9 @@ class AdConfig { ...@@ -78,9 +78,9 @@ class AdConfig {
_doCallback (adpid, success, fail) { _doCallback (adpid, success, fail) {
AdConfig.IS++ AdConfig.IS++
var data = this._adConfig var { a, b } = this._adConfig
if (data[adpid]) { if (a[adpid]) {
success(data[adpid]) success(b, a[adpid])
} else { } else {
fail(this.ERROR_INVALID_ADPID) fail(this.ERROR_INVALID_ADPID)
} }
...@@ -95,7 +95,7 @@ class AdConfig { ...@@ -95,7 +95,7 @@ class AdConfig {
uni.request({ uni.request({
url: this.URL, url: this.URL,
method: 'GET', method: 'GET',
timeout: 5000, timeout: 8000,
data: { data: {
d: location.hostname, d: location.hostname,
a: adpid a: adpid
...@@ -150,11 +150,11 @@ class AdConfig { ...@@ -150,11 +150,11 @@ class AdConfig {
} }
} }
Object.assign(AdConfig.prototype, { Object.assign(AdConfig.prototype, {
URL: '//qy5y9ee9ch8r87pg72w5.dcloud.net.cn/hcs', URL: 'https://hac1.dcloud.net.cn/ah5',
KEY: 'uni_app_ad_config', KEY: 'uni_app_ad_config',
CACHE_TIME: 1000 * 60 * 10, CACHE_TIME: 1000 * 60 * 10,
ERROR_INVALID_ADPID: { ERROR_INVALID_ADPID: {
'-5002': '无效adpid' '-5002': 'invalid adpid'
} }
}) })
...@@ -234,17 +234,73 @@ class AdReport { ...@@ -234,17 +234,73 @@ class AdReport {
} }
} }
Object.assign(AdReport.prototype, { Object.assign(AdReport.prototype, {
URL: '//hp66hwpyev7yx2hfughh.dcloud.net.cn/ahl', URL: 'https://has1.dcloud.net.cn/ahl',
KEY: 'uni_app_ad_guid' KEY: 'uni_app_ad_guid'
}) })
const adProvider = { class AdScript {
hx: 'zswx_hx', static get instance () {
ky: 'zswx_ky' if (this._instance == null) {
this._instance = new AdScript()
}
return this._instance
}
constructor () {
this._instance = null
this._callback = {}
this._cache = {}
}
load (data, success, fail) {
const provider = data.provider
if (this._cache[provider] === undefined) {
this.loadScript(data)
}
if (this._cache[provider] === 1) {
success()
} else {
if (!this._callback[provider]) {
this._callback[provider] = []
}
this._callback[provider].push({
success,
fail
})
}
}
loadScript (data) {
const provider = data.provider
this._cache[provider] = 0
var ads = document.createElement('script')
ads.setAttribute('id', 'uniad_provider' + provider)
const script = data.script
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)
}
} }
const CHECK_RENDER_DELAY = 1000 const CHECK_RENDER_DELAY = 1000
const CHECK_RENDER_RETRY = 3 const CHECK_RENDER_RETRY = 5
const DEFAULT_WIDESCREEN_WIDTH = 750
export default { export default {
name: 'Ad', name: 'Ad',
...@@ -253,6 +309,14 @@ export default { ...@@ -253,6 +309,14 @@ export default {
adpid: { adpid: {
type: [Number, String], type: [Number, String],
default: '' default: ''
},
adpidWidescreen: {
type: [Number, String],
default: ''
},
widescreenWidth: {
type: [Number, String],
default: DEFAULT_WIDESCREEN_WIDTH
} }
}, },
watch: { watch: {
...@@ -260,6 +324,11 @@ export default { ...@@ -260,6 +324,11 @@ export default {
if (val) { if (val) {
this._loadData(val) this._loadData(val)
} }
},
adpidWidescreen (val) {
if (val) {
this._loadData(val)
}
} }
}, },
mounted () { mounted () {
...@@ -268,6 +337,7 @@ export default { ...@@ -268,6 +337,7 @@ export default {
this._pi = 0 this._pi = 0
this._checkTimer = null this._checkTimer = null
this._checkTimerCount = 0 this._checkTimerCount = 0
this._isWidescreen = this.$refs.container.clientWidth > parseInt(this.widescreenWidth)
this._loadData() this._loadData()
AdReport.instance.get({ AdReport.instance.get({
h: __uniConfig.compilerVersion, h: __uniConfig.compilerVersion,
...@@ -294,9 +364,10 @@ export default { ...@@ -294,9 +364,10 @@ export default {
}, },
_loadData (adpid) { _loadData (adpid) {
this._reset() this._reset()
AdConfig.instance.get(adpid || this.adpid, (data) => { const aid = adpid || (this._isWidescreen ? this.adpidWidescreen : this.adpid)
this._pd = data AdConfig.instance.get(aid, (b, a) => {
this._pl = data.psp.split(',') this._b = b
this._pl = a
this._renderAd() this._renderAd()
}, (err) => { }, (err) => {
this.$trigger('error', {}, err) this.$trigger('error', {}, err)
...@@ -307,56 +378,37 @@ export default { ...@@ -307,56 +378,37 @@ export default {
return return
} }
var ap = this._pl[this._pi] const data = this._pl[this._pi]
var data = this._pd[ap] const providerId = data.a1
switch (ap) { this._b[providerId].provider = providerId
case adProvider.hx:
this._renderHX(data)
break
case adProvider.ky:
this._renderKY(data)
break
}
},
_renderNext () {
if (this._pi >= this._pl.length - 1) {
return
}
this._pi++ AdScript.instance.load(this._b[providerId], () => {
this._renderAd() this._renderAdView(this._b[providerId], data)
}, (err) => {
this.$trigger('error', {}, err)
})
}, },
_renderHX (data) { _renderAdView (provider, data) {
if (document.querySelector('#' + adProvider.hx)) { var randomId = this._randomId()
this._renderNext()
return
}
var ad = document.createElement('script')
ad.src = data.src || data.url
var adView = document.createElement('div') var adView = document.createElement('div')
adView.setAttribute('id', adProvider.hx) adView.setAttribute('class', randomId)
adView.appendChild(ad)
this.$refs.container.innerHTML = '' this.$refs.container.innerHTML = ''
this.$refs.container.append(adView) this.$refs.container.append(adView)
let bindThis = window
provider.s.split('.').reduce((total, currentValue) => {
bindThis = total
return total[currentValue]
}, window).bind(bindThis)(data.a2, randomId, 2)
this._startCheckTimer() this._startCheckTimer()
}, },
_renderKY (data) { _renderNext () {
var randomId = this._randomId() if (this._pi >= this._pl.length - 1) {
var ad = document.createElement('script') return
ad.src = (data.src || data.url) + '&_ct=' + randomId }
var adView = document.createElement('div')
adView.setAttribute('id', randomId)
adView.appendChild(ad)
this.$refs.container.innerHTML = ''
this.$refs.container.append(adView)
this._startCheckTimer() this._pi++
this._renderAd()
}, },
_checkRender () { _checkRender () {
var hasContent = (this.$refs.container.children.length > 0 && this.$refs.container.clientHeight > 40) var hasContent = (this.$refs.container.children.length > 0 && this.$refs.container.clientHeight > 40)
...@@ -388,27 +440,18 @@ export default { ...@@ -388,27 +440,18 @@ export default {
} }
}, },
_report (type) { _report (type) {
var taskId = ''
if (this._pl.length > 0 && this._pi < this._pl.length) {
var data = this._pd[this._pl[this._pi]]
if (data) {
taskId = data.task_id
}
}
AdReport.instance.get({ AdReport.instance.get({
h: __uniConfig.compilerVersion, h: __uniConfig.compilerVersion,
a: this.adpid, a: this.adpid,
t: taskId,
at: type at: type
}) })
}, },
_randomId () { _randomId () {
var result = '' var result = ''
for (let i = 0; i < 2; i++) { for (let i = 0; i < 4; i++) {
result += (65536 * (1 + Math.random()) | 0).toString(16).substring(1) result += (65536 * (1 + Math.random()) | 0).toString(16).substring(1)
} }
return result return '_u' + result
} }
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册