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

eslint: ad-component

上级 d651121d
...@@ -36,3 +36,7 @@ node_modules ...@@ -36,3 +36,7 @@ node_modules
automator.js automator.js
uni.automator.js uni.automator.js
uni-polyfill.js uni-polyfill.js
packages/uni-cli-shared/components/ad-full-screen-video.vue
packages/uni-cli-shared/components/ad-interactive.vue
packages/uni-cli-shared/components/ad-interstitial.vue
packages/uni-cli-shared/components/ad-rewarded-video.vue
<template> <template>
<view @click="onclick"> <view @click="onclick">
<slot :options="options" :data="adData" /> <slot
:options="options"
:data="adData"
/>
</view> </view>
</template> </template>
<script> <script>
const AD_URL = 'https://wxac1.dcloud.net.cn/tuiaApplet/acs' const AD_URL = 'https://wxac1.dcloud.net.cn/tuiaApplet/acs'
const AD_REPORT_URL = 'https://wxac1.dcloud.net.cn/tuiaApplet/acs' const AD_REPORT_URL = 'https://wxac1.dcloud.net.cn/tuiaApplet/acs'
const WEBVIEW_PATH = '/uni_modules/uni-ad-interactive/pages/uni-ad-interactive/uni-ad-interactive' const WEBVIEW_PATH = '/uni_modules/uni-ad-interactive/pages/uni-ad-interactive/uni-ad-interactive'
const events = { const events = {
load: 'load', load: 'load',
close: 'close', close: 'close',
error: 'error' error: 'error'
} }
const OpenTypes = { const OpenTypes = {
Interactive: 'interactive' Interactive: 'interactive'
} }
export default { export default {
name: 'AdInteractive', name: 'AdInteractive',
props: { props: {
options: { options: {
type: [Object, Array], type: [Object, Array],
default () { default () {
return {} return {}
}
},
adpid: {
type: [Number, String],
default: ''
},
openUrl: {
type: String,
default: WEBVIEW_PATH
} }
}, },
data() { adpid: {
return { type: [Number, String],
adData: {} default: ''
}
}, },
created() { openUrl: {
this._uniAdPlugin = null type: String,
this._interactiveUrl = null default: WEBVIEW_PATH
if (this.openType == OpenTypes.Interactive) { }
this.getAdData() },
data () {
return {
adData: {}
}
},
created () {
this._uniAdPlugin = null
this._interactiveUrl = null
if (this.openType === OpenTypes.Interactive) {
this.getAdData()
}
},
methods: {
show () {
if (this._uniAdPlugin === null) {
this._uniAdPlugin = this.selectComponent('.uni-ad-plugin')
} }
this._uniAdPlugin.show()
}, },
methods: { getAdData () {
show() { if (!this.adpid) {
if (this._uniAdPlugin == null) { this.$emit(events.error, {
this._uniAdPlugin = this.selectComponent('.uni-ad-plugin') code: -5002,
} message: 'invalid adpid'
this._uniAdPlugin.show() })
}, return
getAdData() { }
if (!this.adpid) {
this.$emit(events.error, { uni.request({
code: -5002, url: AD_URL,
message: 'invalid adpid' method: 'POST',
}) data: {
return adpid: this.adpid
} },
timeout: 5000,
uni.request({ dataType: 'json',
url: AD_URL, success: (res) => {
method: 'POST', console.log(res.data)
data: {
adpid: this.adpid if (res.statusCode !== 200) {
},
timeout: 5000,
dataType: 'json',
success: (res) => {
console.log(res.data)
if (res.statusCode !== 200) {
this.$emit(events.error, {
code: res.statusCode,
message: res.statusCode
})
return
}
const responseData = res.data;
if (responseData.ret == 0) {
this._interactiveUrl = responseData.data.adp_url
this.adData.imgUrl = responseData.data.icon_url
this.adData.openUrl = this.openUrl + "?url=" + encodeURIComponent(this._interactiveUrl)
this.$emit(events.load, {})
} else {
this.$emit(events.error, {
code: responseData.ret,
message: responseData.msg
})
}
},
fail: (err) => {
this.$emit(events.error, { this.$emit(events.error, {
code: '', code: res.statusCode,
message: err.errMsg message: res.statusCode
}) })
return
} }
})
}, const responseData = res.data
onclick() { if (responseData.ret === 0) {
if (this.openType != OpenTypes.Interactive || !this._interactiveUrl) { this._interactiveUrl = responseData.data.adp_url
return this.adData.imgUrl = responseData.data.icon_url
} this.adData.openUrl = this.openUrl + '?url=' + encodeURIComponent(this._interactiveUrl)
this.$emit(events.load, {})
uni.navigateTo({ } else {
url: this.adData.openUrl this.$emit(events.error, {
}) code: responseData.ret,
message: responseData.msg
this._report() })
},
_report() {
uni.request({
url: AD_REPORT_URL,
data: {
adpid: this.adpid,
t: '10019'
},
timeout: 5000,
dataType: 'json',
success: (res) => {
},
fail: (err) => {
} }
}) },
fail: (err) => {
this.$emit(events.error, {
code: '',
message: err.errMsg
})
}
})
},
onclick () {
if (this.openType !== OpenTypes.Interactive || !this._interactiveUrl) {
return
} }
uni.navigateTo({
url: this.adData.openUrl
})
this._report()
},
_report () {
uni.request({
url: AD_REPORT_URL,
data: {
adpid: this.adpid,
t: '10019'
},
timeout: 5000,
dataType: 'json'
})
} }
} }
}
</script> </script>
<style> <style>
......
const ADType = { const ADType = {
RewardedVideo: "RewardedVideo", RewardedVideo: 'RewardedVideo',
FullScreenVideo: "FullScreenVideo", FullScreenVideo: 'FullScreenVideo',
Interstitial: "Interstitial" Interstitial: 'Interstitial'
} }
const EventType = { const EventType = {
...@@ -19,8 +19,7 @@ const ProviderType = { ...@@ -19,8 +19,7 @@ const ProviderType = {
const RETRY_COUNT = 1 const RETRY_COUNT = 1
class AdBase { class AdBase {
constructor (adInstance, options = {}, interstitial) {
constructor(adInstance, options = {}, interstitial) {
this._isLoad = false this._isLoad = false
this._isLoading = false this._isLoading = false
this._isPlaying = false this._isPlaying = false
...@@ -80,23 +79,23 @@ class AdBase { ...@@ -80,23 +79,23 @@ class AdBase {
}) })
} }
get isExpired() { get isExpired () {
return (this._lastLoadTime !== 0 && (Math.abs(Date.now() - this._lastLoadTime) > EXPIRED_TIME)) return (this._lastLoadTime !== 0 && (Math.abs(Date.now() - this._lastLoadTime) > EXPIRED_TIME))
} }
get isLoad() { get isLoad () {
return this._isLoad return this._isLoad
} }
get isLoading() { get isLoading () {
return this._isLoading return this._isLoading
} }
getProvider() { getProvider () {
return this._ad.getProvider() return this._ad.getProvider()
} }
load(onload, onerror) { load (onload, onerror) {
this._loadCallback = onload this._loadCallback = onload
this._errorCallback = onerror this._errorCallback = onerror
...@@ -119,7 +118,7 @@ class AdBase { ...@@ -119,7 +118,7 @@ class AdBase {
this._loadAd() this._loadAd()
} }
show(onclose, onshow) { show (onclose, onshow) {
this._closeCallback = onclose this._closeCallback = onclose
if (this._isLoading || this._isPlaying || !this._isLoad) { if (this._isLoading || this._isPlaying || !this._isLoad) {
...@@ -155,13 +154,13 @@ class AdBase { ...@@ -155,13 +154,13 @@ class AdBase {
} }
} }
onLoad(e) { onLoad (e) {
if (this._loadCallback != null) { if (this._loadCallback != null) {
this._loadCallback() this._loadCallback()
} }
} }
onClose(e) { onClose (e) {
if (this._closeCallback != null) { if (this._closeCallback != null) {
this._closeCallback({ this._closeCallback({
isEnded: e.isEnded isEnded: e.isEnded
...@@ -169,17 +168,17 @@ class AdBase { ...@@ -169,17 +168,17 @@ class AdBase {
} }
} }
onError(e) { onError (e) {
if (this._errorCallback != null) { if (this._errorCallback != null) {
this._errorCallback(e) this._errorCallback(e)
} }
} }
destroy() { destroy () {
this._ad.destroy() this._ad.destroy()
} }
_loadAd() { _loadAd () {
this._isLoad = false this._isLoad = false
this._isLoading = true this._isLoading = true
this._lastError = null this._lastError = null
...@@ -188,31 +187,30 @@ class AdBase { ...@@ -188,31 +187,30 @@ class AdBase {
} }
class RewardedVideo extends AdBase { class RewardedVideo extends AdBase {
constructor(options = {}) { constructor (options = {}) {
super(plus.ad.createRewardedVideoAd(options), options) super(plus.ad.createRewardedVideoAd(options), options)
} }
} }
class FullScreenVideo extends AdBase { class FullScreenVideo extends AdBase {
constructor(options = {}) { constructor (options = {}) {
super(plus.ad.createFullScreenVideoAd(options), options) super(plus.ad.createFullScreenVideoAd(options), options)
} }
} }
class Interstitial extends AdBase { class Interstitial extends AdBase {
constructor(options = {}, interstitial) { constructor (options = {}, interstitial) {
super(plus.ad.createInterstitialAd(options), options, interstitial) super(plus.ad.createInterstitialAd(options), options, interstitial)
} }
} }
class AdHelper { class AdHelper {
constructor (adType) {
constructor(adType) {
this._ads = {} this._ads = {}
this._adType = adType this._adType = adType
} }
load(options, onload, onerror) { load (options, onload, onerror) {
if (!options.adpid || this.isBusy(options.adpid)) { if (!options.adpid || this.isBusy(options.adpid)) {
return return
} }
...@@ -220,8 +218,8 @@ class AdHelper { ...@@ -220,8 +218,8 @@ class AdHelper {
this.get(options).load(onload, onerror) this.get(options).load(onload, onerror)
} }
show(options, onload, onerror, onclose, onshow) { show (options, onload, onerror, onclose, onshow) {
let ad = this.get(options) const ad = this.get(options)
if (ad.isLoad) { if (ad.isLoad) {
ad.show((e) => { ad.show((e) => {
...@@ -244,8 +242,8 @@ class AdHelper { ...@@ -244,8 +242,8 @@ class AdHelper {
} }
// 底价预载逻辑 // 底价预载逻辑
loadWaterfall(options, onload, onfail, index = 0) { loadWaterfall (options, onload, onfail, index = 0) {
let { const {
adpid, adpid,
urlCallback urlCallback
} = options } = options
...@@ -253,28 +251,28 @@ class AdHelper { ...@@ -253,28 +251,28 @@ class AdHelper {
return return
} }
let options2 = { const options2 = {
adpid: adpid[index], adpid: adpid[index],
urlCallback, urlCallback,
retry: false retry: false
}; }
this.load(options2, (res) => { this.load(options2, (res) => {
onload(options2); onload(options2)
}, (err) => { }, (err) => {
index++; index++
if (index >= adpid.length) { if (index >= adpid.length) {
onfail(err); onfail(err)
} else { } else {
console.log('loadWaterfall::index=' + index); console.log('loadWaterfall::index=' + index)
this.loadWaterfall(options, onload, onfail, index); this.loadWaterfall(options, onload, onfail, index)
} }
}); })
} }
// 底价逻辑,失败后下一个,无重试机制 // 底价逻辑,失败后下一个,无重试机制
showWaterfall(options, onload, onfail, onclose, onshow, index = 0) { showWaterfall (options, onload, onfail, onclose, onshow, index = 0) {
let { const {
adpid, adpid,
urlCallback urlCallback
} = options } = options
...@@ -282,70 +280,69 @@ class AdHelper { ...@@ -282,70 +280,69 @@ class AdHelper {
return return
} }
let options2 = { const options2 = {
adpid: adpid[index], adpid: adpid[index],
urlCallback, urlCallback,
retry: false retry: false
}; }
this.show(options2, () => { this.show(options2, () => {
onload(); onload()
}, (err) => { }, (err) => {
index++; index++
if (index >= adpid.length) { if (index >= adpid.length) {
onfail(err); onfail(err)
} else { } else {
this.showWaterfall(options, onload, onfail, onclose, onshow, index); this.showWaterfall(options, onload, onfail, onclose, onshow, index)
} }
}, (res) => { }, (res) => {
onclose(res); onclose(res)
}, () => { }, () => {
onshow(); onshow()
}); })
} }
// 预载底价瀑布流 // 预载底价瀑布流
preloadWaterfall(options, index = 0, step = 1) { preloadWaterfall (options, index = 0, step = 1) {
if (step === 1) { if (step === 1) {
this.loadWaterfall(options, (res) => { this.loadWaterfall(options, (res) => {
console.log("preloadWaterfall.success::", res); console.log('preloadWaterfall.success::', res)
}, (err) => { }, (err) => {
console.log("loadWaterfall.fail", err); console.log('loadWaterfall.fail', err)
}) })
return; return
} }
let { const {
adpid, adpid,
urlCallback urlCallback
} = options } = options
let ads = [];
for (let i = 0; i < step; i++) { for (let i = 0; i < step; i++) {
if (index < adpid.length) { if (index < adpid.length) {
let options2 = { const options2 = {
adpid: adpid[index], adpid: adpid[index],
urlCallback urlCallback
}; }
this.loadWaterfall(options2, (res) => { this.loadWaterfall(options2, (res) => {
console.log("preloadWaterfall.success::", res); console.log('preloadWaterfall.success::', res)
}, (err) => { }, (err) => {
console.log("loadWaterfall.fail", err); console.log('loadWaterfall.fail', err)
this.preloadWaterfall(options, index, step); this.preloadWaterfall(options, index, step)
}) })
index++; index++
} else { } else {
break; break
} }
} }
} }
isBusy(adpid) { isBusy (adpid) {
return (this._ads[adpid] && this._ads[adpid].isLoading) return (this._ads[adpid] && this._ads[adpid].isLoading)
} }
get(options) { get (options) {
const { const {
adpid, adpid
} = options } = options
if (!this._ads[adpid]) { if (!this._ads[adpid]) {
...@@ -355,18 +352,18 @@ class AdHelper { ...@@ -355,18 +352,18 @@ class AdHelper {
return this._ads[adpid] return this._ads[adpid]
} }
remove(adpid) { remove (adpid) {
if (this._ads[adpid]) { if (this._ads[adpid]) {
this._ads[adpid].destroy() this._ads[adpid].destroy()
delete this._ads[adpid] delete this._ads[adpid]
} }
} }
_createInstance(options) { _createInstance (options) {
const adType = options.adType || this._adType const adType = options.adType || this._adType
delete options.adType delete options.adType
let ad = null; let ad = null
if (adType === ADType.RewardedVideo) { if (adType === ADType.RewardedVideo) {
ad = new RewardedVideo(options) ad = new RewardedVideo(options)
} else if (adType === ADType.FullScreenVideo) { } else if (adType === ADType.FullScreenVideo) {
...@@ -410,25 +407,25 @@ export default { ...@@ -410,25 +407,25 @@ export default {
} }
} }
}, },
data() { data () {
return { return {
loading: false, loading: false,
errorMessage: null errorMessage: null
} }
}, },
watch: { watch: {
adpid(newValue, oldValue) { adpid (newValue, oldValue) {
this._removeInstance(oldValue) this._removeInstance(oldValue)
if (this.preload) { if (this.preload) {
this._loadAd() this._loadAd()
} }
}, },
// 服务器回调透传参数,仅在创建广告实例时可传递参数,如果发生变化需要重新创建广告实例 // 服务器回调透传参数,仅在创建广告实例时可传递参数,如果发生变化需要重新创建广告实例
urlCallback() { urlCallback () {
this._removeInstance() this._removeInstance()
} }
}, },
created() { created () {
this._adHelper = new AdHelper(this.adType) this._adHelper = new AdHelper(this.adType)
setTimeout(() => { setTimeout(() => {
...@@ -438,9 +435,9 @@ export default { ...@@ -438,9 +435,9 @@ export default {
}, 100) }, 100)
}, },
methods: { methods: {
load() { load () {
this._startLoading() this._startLoading()
const invoke = this._isWaterfall() ? "loadWaterfall" : "load" const invoke = this._isWaterfall() ? 'loadWaterfall' : 'load'
this._adHelper[invoke](this._getAdOptions(), () => { this._adHelper[invoke](this._getAdOptions(), () => {
this._onLoad() this._onLoad()
}, (err) => { }, (err) => {
...@@ -448,9 +445,9 @@ export default { ...@@ -448,9 +445,9 @@ export default {
}) })
}, },
show() { show () {
this._startLoading() this._startLoading()
const invoke = this._isWaterfall() ? "showWaterfall" : "show" const invoke = this._isWaterfall() ? 'showWaterfall' : 'show'
this._adHelper[invoke](this._getAdOptions(), () => { this._adHelper[invoke](this._getAdOptions(), () => {
this._onLoad() this._onLoad()
}, (err) => { }, (err) => {
...@@ -467,30 +464,30 @@ export default { ...@@ -467,30 +464,30 @@ export default {
}) })
}, },
_loadAd() { _loadAd () {
if (this._canCreateAd()) { if (this._canCreateAd()) {
this.load() this.load()
} }
}, },
_onclick() { _onclick () {
if (!this.disabled) { if (!this.disabled) {
this.show() this.show()
} }
}, },
_getAdOptions() { _getAdOptions () {
return { return {
adpid: this.adpid, adpid: this.adpid,
urlCallback: this.urlCallback urlCallback: this.urlCallback
} }
}, },
_isWaterfall() { _isWaterfall () {
return (Array.isArray(this.adpid) && this.adpid.length > 0) return (Array.isArray(this.adpid) && this.adpid.length > 0)
}, },
_canCreateAd() { _canCreateAd () {
let result = false let result = false
if (Array.isArray(this.adpid) && this.adpid.length > 0) { if (Array.isArray(this.adpid) && this.adpid.length > 0) {
result = true result = true
...@@ -502,8 +499,8 @@ export default { ...@@ -502,8 +499,8 @@ export default {
return result return result
}, },
_removeInstance(adpid) { _removeInstance (adpid) {
let id = adpid || this.adpid const id = adpid || this.adpid
if (Array.isArray(id)) { if (Array.isArray(id)) {
id.forEach((item) => { id.forEach((item) => {
this._adHelper.remove(item) this._adHelper.remove(item)
...@@ -513,23 +510,23 @@ export default { ...@@ -513,23 +510,23 @@ export default {
} }
}, },
_startLoading() { _startLoading () {
this.loading = true this.loading = true
this.errorMessage = null this.errorMessage = null
}, },
_onLoad(err) { _onLoad () {
this.loading = false this.loading = false
this._dispatchEvent(EventType.Load, {}) this._dispatchEvent(EventType.Load, {})
}, },
_onLoadFail(err) { _onLoadFail (err) {
this.loading = false this.loading = false
this.errorMessage = JSON.stringify(err) this.errorMessage = JSON.stringify(err)
this._dispatchEvent(EventType.Error, err) this._dispatchEvent(EventType.Error, err)
}, },
_dispatchEvent(type, data) { _dispatchEvent (type, data) {
this.$emit(type, { this.$emit(type, {
detail: data detail: data
}) })
......
...@@ -29,56 +29,56 @@ export default { ...@@ -29,56 +29,56 @@ export default {
default: false default: false
} }
}, },
data() { data () {
return { return {
loading: false, loading: false,
errorMessage: null errorMessage: null
} }
}, },
created() { created () {
this._ad = null this._ad = null
setTimeout(() => { setTimeout(() => {
if (this.preload && this._canCreateAd()) { if (this.preload && this._canCreateAd()) {
this.load(); this.load()
} }
}, 100) }, 100)
}, },
methods: { methods: {
load() { load () {
this.errorMessage = null this.errorMessage = null
}, },
show() { show () {
this.errorMessage = null this.errorMessage = null
this._ad = this.selectComponent('.uni-ad'); this._ad = this.selectComponent('.uni-ad')
this._ad.show(); this._ad.show()
}, },
_onclick() { _onclick () {
this.show() this.show()
}, },
_startLoading() { _startLoading () {
this.loading = true this.loading = true
this.errorMessage = null this.errorMessage = null
}, },
_onmpload(e) { _onmpload (e) {
this.loading = false this.loading = false
this._dispatchEvent(EventType.Load, {}) this._dispatchEvent(EventType.Load, {})
}, },
_onmpclose(e) { _onmpclose (e) {
this._dispatchEvent(EventType.Close, e.detail) this._dispatchEvent(EventType.Close, e.detail)
}, },
_onmperror(e) { _onmperror (e) {
this.loading = false this.loading = false
this.errorMessage = JSON.stringify(e.detail) this.errorMessage = JSON.stringify(e.detail)
this._dispatchEvent(EventType.Error, e.detail) this._dispatchEvent(EventType.Error, e.detail)
}, },
_dispatchEvent(type, data) { _dispatchEvent (type, data) {
this.$emit(type, { this.$emit(type, {
detail: data detail: data
}) })
......
<template> <template>
<view @click="onclick"> <view @click="onclick">
<uni-ad-plugin class="uni-ad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror"></uni-ad-plugin> <uni-ad-plugin
class="uni-ad-plugin"
:adpid="adpid"
:unit-id="unitId"
@load="_onmpload"
@close="_onmpclose"
@error="_onmperror"
/>
</view> </view>
</template> </template>
<script> <script>
import adMixin from "./ad.mixin.mp.js" import adMixin from './ad.mixin.mp.js'
export default { export default {
name: 'UniAd', name: 'UniAd',
mixins: [adMixin], mixins: [adMixin],
props: { props: {
}, },
methods: { methods: {
}
} }
}
</script> </script>
module.exports = function(appJson) { module.exports = function (appJson) {
if (!appJson.plugins) { if (!appJson.plugins) {
appJson.plugins = {} appJson.plugins = {}
} }
if (!appJson.plugins['uni-ad']) { if (!appJson.plugins['uni-ad']) {
appJson.plugins['uni-ad'] = { appJson.plugins['uni-ad'] = {
"version": "1.0.2", version: '1.0.2',
"provider": "wx999bf02c8e05dfc9" provider: 'wx999bf02c8e05dfc9'
} }
} }
if (!appJson.plugins['coral-adv']) { if (!appJson.plugins['coral-adv']) {
appJson.plugins['coral-adv'] = { appJson.plugins['coral-adv'] = {
"version": "1.0.7", version: '1.0.7',
"provider": "wx0e203209e27b1e66" provider: 'wx0e203209e27b1e66'
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册