|
@@ -1,4 +1,4 @@
|
|
|
-import Taro, { useLoad, useReady, useUnload } from '@tarojs/taro'
|
|
|
+import Taro, { useLoad, useReady } from '@tarojs/taro'
|
|
|
import { PropsWithChildren, useState } from 'react'
|
|
|
import { View, Video, Image } from '@tarojs/components'
|
|
|
import useGetCustomClient from '@/hooks/useGetCustomClient'
|
|
@@ -8,14 +8,15 @@ import http from '@/http/index'
|
|
|
import { adSelfPredict } from '@/http/api/index'
|
|
|
import { adActionLog } from '@/logCenter/index'
|
|
|
import { reportBusinessType } from '@/plugin/share/const'
|
|
|
+import { queryURLParams } from '@/plugin/share'
|
|
|
|
|
|
// 在一些场景下 loadedMetaFiredInOnce 被触发了很多次
|
|
|
let loadedMetaFiredInOnce = 0
|
|
|
+let pqtId
|
|
|
|
|
|
function PqCustom(props: PropsWithChildren<CustomPropsType>) {
|
|
|
let {
|
|
|
adpId,
|
|
|
- zIndex,
|
|
|
openEmbeddedMiniProgram,
|
|
|
onError,
|
|
|
onLoad,
|
|
@@ -25,9 +26,21 @@ function PqCustom(props: PropsWithChildren<CustomPropsType>) {
|
|
|
const [adData, setAdData] = useState<any>({})
|
|
|
|
|
|
useReady(() => {
|
|
|
+ init()
|
|
|
+ pqtId = generateUUID()
|
|
|
getAdConfig()
|
|
|
})
|
|
|
|
|
|
+ function init() {
|
|
|
+ if (
|
|
|
+ typeof adpId !== 'string' ||
|
|
|
+ typeof openEmbeddedMiniProgram !== 'function'
|
|
|
+ ) {
|
|
|
+ catchError('参数错误', 1031)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
function getAdConfig() {
|
|
|
http.post(`${adSelfPredict}?adpId=uh2321awe1`, {
|
|
|
baseInfo: {},
|
|
@@ -39,23 +52,36 @@ function PqCustom(props: PropsWithChildren<CustomPropsType>) {
|
|
|
header: { 'content-type': 'application/json;' }
|
|
|
}).then((res: RequestType) => {
|
|
|
const { code, data } = res
|
|
|
- // TODO: onError、logUpload
|
|
|
- if(code !== 0) {
|
|
|
- onError()
|
|
|
- // logUpload()
|
|
|
+ if(code === 10001) {
|
|
|
+ catchError('广告资源加载失败', 1022)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if(code === 10002) {
|
|
|
+ catchError('adpId 无效', 1021)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ if(code === 10003) {
|
|
|
+ catchError('初始化失败', 1011)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
const { ownPlatformAlliance } = data || {}
|
|
|
const { adType, platformCreativeInfo } = ownPlatformAlliance || {}
|
|
|
+ const { landingPageAddress } = platformCreativeInfo
|
|
|
+
|
|
|
+ if (!landingPageAddress) {
|
|
|
+ catchError('数据异常', 1013)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
setAdData({
|
|
|
adType,
|
|
|
...platformCreativeInfo
|
|
|
})
|
|
|
-
|
|
|
- console.log(data)
|
|
|
- }).catch(err => {
|
|
|
- console.log(err)
|
|
|
+ }).catch(() => {
|
|
|
+ catchError('初始化失败', 1011)
|
|
|
})
|
|
|
}
|
|
|
|
|
@@ -64,30 +90,41 @@ function PqCustom(props: PropsWithChildren<CustomPropsType>) {
|
|
|
}
|
|
|
|
|
|
function videoClick() {
|
|
|
- adActionReport(reportBusinessType.adPlay, 'adClick')
|
|
|
- adActionReport(reportBusinessType.adPlay, 'adOpen')
|
|
|
- openEmbeddedMiniProgram()
|
|
|
+ adActionReport(reportBusinessType.adClick, 'adClick')
|
|
|
+ videoOpenMiniProgram()
|
|
|
closeCustom()
|
|
|
}
|
|
|
|
|
|
+ function videoOpenMiniProgram() {
|
|
|
+ const { landingPageAddress } = adData
|
|
|
+ const query = queryURLParams(createAdActionReportData())
|
|
|
+
|
|
|
+ adActionReport(reportBusinessType.adPlay, 'adOpen')
|
|
|
+ openEmbeddedMiniProgram(
|
|
|
+ 'wx5ef216d1caf4a0ea',
|
|
|
+ `/pages/ad-launch-page/index?path=${encodeURIComponent(landingPageAddress)}&${query}`
|
|
|
+ )
|
|
|
+
|
|
|
+ console.log(`/pages/ad-launch-page/index?path=${encodeURIComponent(landingPageAddress)}&${query}`)
|
|
|
+ }
|
|
|
+
|
|
|
function videoLoad() {
|
|
|
- adActionReport(reportBusinessType.adPlay, 'adLoad')
|
|
|
+ adActionReport(reportBusinessType.adLoad, 'adLoad')
|
|
|
onLoad()
|
|
|
}
|
|
|
|
|
|
function videoError() {
|
|
|
- adActionReport(reportBusinessType.adPlay, 'adError')
|
|
|
- onError()
|
|
|
+ catchError('广告加载失败', 1012)
|
|
|
}
|
|
|
|
|
|
function videoView() {
|
|
|
- adActionReport(reportBusinessType.adPlay, 'adView')
|
|
|
+ adActionReport(reportBusinessType.adView, 'adView')
|
|
|
}
|
|
|
|
|
|
function videoTimeUpdate() {}
|
|
|
|
|
|
function closeCustom() {
|
|
|
- adActionReport(reportBusinessType.adPlay, 'adClose')
|
|
|
+ adActionReport(reportBusinessType.adClose, 'adClose')
|
|
|
onClose()
|
|
|
setShow(false)
|
|
|
}
|
|
@@ -96,42 +133,75 @@ function PqCustom(props: PropsWithChildren<CustomPropsType>) {
|
|
|
closeCustom()
|
|
|
}
|
|
|
|
|
|
- function adActionReport(eventId, businessType) {
|
|
|
- const {
|
|
|
- adCode,
|
|
|
- adPosition,
|
|
|
- advertiserId,
|
|
|
- ownAdDetailId,
|
|
|
- ownAdPositionId,
|
|
|
- ownAdSystemType,
|
|
|
- planId,
|
|
|
- } = adData
|
|
|
+ function adActionReport(eventId, businessType, extParams = {}) {
|
|
|
+ const data = createAdActionReportData
|
|
|
|
|
|
adActionLog([{
|
|
|
- adCode,
|
|
|
- adPosition,
|
|
|
- advertiserId,
|
|
|
businessType,
|
|
|
eventId,
|
|
|
- ownAdDetailId,
|
|
|
- ownAdPositionId,
|
|
|
- ownAdSystemType,
|
|
|
- pageSource: 'plugin',
|
|
|
- planId,
|
|
|
- pqtId: generateUUID(),
|
|
|
- videoId: '000',
|
|
|
- adpId
|
|
|
+ ...data,
|
|
|
+ ...extParams
|
|
|
}])
|
|
|
}
|
|
|
|
|
|
+ function createAdActionReportData() {
|
|
|
+ const {
|
|
|
+ trafficCode, // 流量主code
|
|
|
+ // 广告主
|
|
|
+ advertiserCode,
|
|
|
+ advertiserId,
|
|
|
+ // 广告计划
|
|
|
+ campaignCode,
|
|
|
+ campaignId,
|
|
|
+ // 广告
|
|
|
+ adCode,
|
|
|
+ adId,
|
|
|
+ // 创意
|
|
|
+ creativeCode,
|
|
|
+ id,
|
|
|
+ // 广告位
|
|
|
+ positionId,
|
|
|
+ // 竞价
|
|
|
+ bidType = '',
|
|
|
+ unitPrice = '', // 单价
|
|
|
+ bidCreativeParam = {}, // {}
|
|
|
+ } = adData
|
|
|
+
|
|
|
+ return {
|
|
|
+ adpCode: trafficCode,
|
|
|
+ advertiserCode,
|
|
|
+ advertiserId,
|
|
|
+ campaignCode,
|
|
|
+ campaignId,
|
|
|
+ adCode,
|
|
|
+ adId,
|
|
|
+ creativeCode,
|
|
|
+ id,
|
|
|
+ positionId,
|
|
|
+ bidType,
|
|
|
+ unitPrice, // 单价
|
|
|
+ bidCreativeParam: JSON.stringify(bidCreativeParam || {}), // {}
|
|
|
+ adpId,
|
|
|
+ pqtId
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ function catchError(message, code) {
|
|
|
+ onError({ message, code })
|
|
|
+ setShow(false)
|
|
|
+ adActionReport(reportBusinessType.adError, 'adError', {
|
|
|
+ errorCode: code
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
return (
|
|
|
<>
|
|
|
{show && <View className='pq-custom' onClick={clickMask}>
|
|
|
<View className='custom-container'>
|
|
|
- <View className='icon' style={{zIndex: (zIndex || 1) + 1}}>
|
|
|
+ <View className='icon'>
|
|
|
广告
|
|
|
</View>
|
|
|
- <View className='close-icon'style={{zIndex: (zIndex || 1) + 1}} onClick={closeCustom}>
|
|
|
+ <View className='close-icon' onClick={closeCustom}>
|
|
|
</View>
|
|
|
<Custom
|
|
|
adData={adData}
|
|
@@ -152,7 +222,7 @@ function Custom({
|
|
|
adData, onPlay, // 播放事件
|
|
|
onClick, onLoad, onView, onError, onTimeUpdate
|
|
|
}) {
|
|
|
- const [style, setStyle] = useState({ width: '0px', height: 'px' })
|
|
|
+ const [style, setStyle] = useState({ width: '0px', height: '0px' })
|
|
|
|
|
|
useReady(() => {
|
|
|
loadedMetaFiredInOnce = 0
|
|
@@ -161,7 +231,6 @@ function Custom({
|
|
|
useGetCustomClient((customClient) => {
|
|
|
let clientWidth = customClient.width
|
|
|
let clientHeight = customClient.height
|
|
|
-
|
|
|
setStyle({
|
|
|
width: `${clientWidth}px`,
|
|
|
height: `${clientHeight}px`
|
|
@@ -172,7 +241,6 @@ function Custom({
|
|
|
if (loadedMetaFiredInOnce++ < 1) {
|
|
|
// 上报load
|
|
|
onLoad()
|
|
|
- console.log('load')
|
|
|
}
|
|
|
}
|
|
|
|