Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions components/mip-custom/mip-custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import url from './common/url'
import log from './common/log'
import dataProcessor from './common/data'
import renderFactory from './tplRender/renderFactory'
import novel from './novel-feature.js'

import novel from './novel/novel-feature'
import commonMIPNovel from './novel/common-mip-novel'
// import tools
const {CustomElement, util} = MIP
const {logData} = dataProcessor
Expand Down Expand Up @@ -115,6 +115,8 @@ export default class MipCustom extends CustomElement {
// 参数初始化
this.position = this.element.getAttribute('position') || ''
this.sourceType = this.element.getAttribute('source-type') || ''
// 通过参数判断是否为通用 mip 的小说广告
this.isCommonMIPNovel = this.element.hasAttribute('novel')
// 判断是否在mip-shell中,决定请求传递参数
this.commonUrl = url.get(this.element)
this.isPositionTop = getPosition(this.position) === 'top'
Expand Down Expand Up @@ -258,6 +260,8 @@ export default class MipCustom extends CustomElement {
performance.fetchStart = +new Date()
if (this.isNovel) {
url = novel.addNovelData(url, this.novelData, this.fromSearch)
} else if (this.isCommonMIPNovel) {
url = commonMIPNovel.addNovelData(url)
}
// fetch
fetch(url, {
Expand Down Expand Up @@ -285,6 +289,17 @@ export default class MipCustom extends CustomElement {
}).catch(err => {
logger.log('失败:' + err)
})
} else if (this.isCommonMIPNovel && data.data.schema) {
// 通用 mip 小说广告渲染,需要先处理数据
new Promise(resolve => {
commonMIPNovel.renderWithNovelData.apply(this, [data, resolve])
}).then(result => {
this.render(result)
// 性能日志:按照流量 1/500 发送日志
log.setPerformanceLogs(performance, data)
}).catch(err => {
logger.log('失败:' + err)
})
} else if (this.isPositionTop) {
this.renderQueue(data)
} else {
Expand Down
101 changes: 101 additions & 0 deletions components/mip-custom/novel/common-mip-novel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @file 通用 mip 小说可以出广告,特性模仿自定制化小说,但是功能简化
* @author chenyongle
*/
import {initFirstFetchCache} from './strategyCompute'
import novel from './novel-feature'
import {getHashData, getCurrentWindow, getRootWindow, state, getNovelInstanceId} from './util'
const rootWin = getRootWindow()
const currentWindow = getCurrentWindow()

function getNovelData () {
let novelInstance = rootWin.MIP.novelInstance
if (!novelInstance) {
novelInstance = {
currentPageMeta: {
pageType: 'page'
},
novelInstanceId: getNovelInstanceId()
}
rootWin.MIP.novelInstance = novelInstance
}

const {originalUrl, isRootPage} = state(currentWindow)
const pageType = novelInstance.currentPageMeta.pageType || ''
let isNeedAds = novelInstance.adsCache == null ? true : novelInstance.adsCache.isNeedAds
const novelInstanceId = novelInstance.novelInstanceId
// 基础novelData数据
let novelData = {
originalUrl,
pageType,
silentFollow: isRootPage,
isNeedAds,
novelInstanceId
}
// TODO: 当结果页卡片入口为断点续读时,添加entryFrom: 'from_nvl_toast', 需要修改SF里记录到hash里,等SF修改完成,此处添加

if (isNeedAds && novelInstance.adsCache) {
novelInstance.adsCache = undefined
}
if (novelInstance.adsCache && novelInstance.adsCache.fetchTpl && novelInstance.adsCache.fetchTpl.length !== 0) {
Object.assign(novelData, {tpl: novelInstance.adsCache.fetchTpl})
}
if (novelInstance.adsCache != null &&
novelInstance.adsCache.showedAds &&
JSON.stringify(novelInstance.adsCache.showedAds) !== '{}') {
Object.assign(novelData, {showedAds: novelInstance.adsCache.showedAds})
}
if (novelInstance.adsCache != null && novelInstance.adsCache.isFirstFetch) {
Object.assign(novelData, {ignoreSendLog: true})
}
if (getHashData('srcid')) {
Object.assign(novelData, {frsrcid: getHashData('srcid')})
}

// 2019-4-30 为后端增加几个UA相关的字段
let ua = window.navigator.userAgent
novelData.ua = ua

if (ua.indexOf('iPhone') !== -1 || ua.indexOf('iPad') !== -1) {
novelData.os = 'ios'
let match = ua.match(/OS (\d+)_(\d+)_?(\d+)?/)
if (match) {
let version = match[1] + '.' + match[2]
if (match[3]) {
version += '.' + match[3]
}
novelData.ov = version
}
} else if (ua.indexOf('Android') !== -1) {
novelData.os = 'android'
let match = ua.match(/Android\s([^\s]+)/)
if (match) {
novelData.ov = match[1]
}
}

// 只看手百,不看lite
if (ua.indexOf('baiduboxapp') !== -1 && ua.indexOf('lite baiduboxapp') === -1 && ua.indexOf('info baiduboxapp') === -1) {
let match = ua.match(/baiduboxapp\/([^\s]+)/)
if (match) {
novelData.ver = match[1]
}
}

return novelData
}
function addNovelData (url) {
let novelData = getNovelData()
const fromSearch = currentWindow.MIP.viewer.page.isRootPage ? 1 : 0
return novel.addNovelData(url, novelData, fromSearch)
}

function renderWithNovelData (data, resolve) {
const {novelInstance} = state(currentWindow)
initFirstFetchCache({adData: data.data}, novelInstance)
resolve(novelInstance.adsCache.adStrategyCacheData)
}
export default {
addNovelData,
renderWithNovelData
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,11 @@
*
*/

import {getCurrentWindow, getRootWindow} from './util'
const {util} = MIP
let globalCustomElementInstance
let initElement

/**
* 获取当前定制化页面的 window
*
* @returns {window} 当前 iframe 的 window
*/
function getCurrentWindow () {
let pageId = window.MIP.viewer.page.currentPageId || ''
let pageInfo = window.MIP.viewer.page.getPageById(pageId)
return pageInfo.targetWindow
}
/**
* 获取当前定制化页面的 root window
*
* @returns {window} 当前 iframe 的 root window
*/
function getRootWindow () {
let win = getCurrentWindow()
return win.MIP.viewer.page.isRootPage
? win
: win.parent
}
/**
* 根据当前 window 获取小说实例
*
Expand Down
Loading