index.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import Taro, { useLoad, useReady, useUnload } from '@tarojs/taro'
  2. import { PropsWithChildren, useState } from 'react'
  3. import { View, Video, Image } from '@tarojs/components'
  4. import useGetCustomClient from '@/hooks/useGetCustomClient'
  5. import { wrapProps, generateUUID } from '@/plugin/share'
  6. import './index.less'
  7. import http from '@/http/index'
  8. import { adSelfPredict } from '@/http/api/index'
  9. import { adActionLog } from '@/logCenter/index'
  10. import { reportBusinessType } from '@/plugin/share/const'
  11. // 在一些场景下 loadedMetaFiredInOnce 被触发了很多次
  12. let loadedMetaFiredInOnce = 0
  13. function PqCustom(props: PropsWithChildren<CustomPropsType>) {
  14. let {
  15. adpId,
  16. autoplay,
  17. loop,
  18. muted,
  19. showMusk,
  20. zIndex,
  21. openEmbeddedMiniProgram,
  22. onPlay,
  23. onPause,
  24. onEnded,
  25. onTimeUpdate,
  26. onStop,
  27. onError,
  28. onLoad,
  29. onView,
  30. onClick,
  31. onClose
  32. } = wrapProps(props)
  33. const [show, setShow] = useState(true)
  34. const [adData, setAdData] = useState<any>({})
  35. useReady(() => {
  36. getAdConfig()
  37. adActionReport(reportBusinessType.buttonView, 'adView')
  38. })
  39. function getAdConfig() {
  40. http.post(`${adSelfPredict}?adpId=uh2321awe1`, {
  41. baseInfo: {},
  42. positionId: 1,
  43. phoneModel : '',
  44. careModelStatus : 1,
  45. videoId: 0
  46. }, {
  47. header: { 'content-type': 'application/json;' }
  48. }).then((res: RequestType) => {
  49. const { code, data } = res
  50. // TODO: onError、logUpload
  51. if(code !== 0) {
  52. onError()
  53. // logUpload()
  54. return
  55. }
  56. const { ownPlatformAlliance } = data || {}
  57. const { adType, platformCreativeInfo } = ownPlatformAlliance || {}
  58. setAdData({
  59. adType,
  60. ...platformCreativeInfo
  61. })
  62. console.log(data)
  63. }).catch(err => {
  64. console.log(err)
  65. })
  66. }
  67. function videoPlay() {
  68. onPlay()
  69. }
  70. function videoPause() {
  71. onPause()
  72. }
  73. function videoPlayEnded() {
  74. onEnded()
  75. }
  76. function videoTimeUpdate() {
  77. onTimeUpdate()
  78. }
  79. function videoStop() {
  80. onStop()
  81. }
  82. function videoClick() {
  83. closeCustom()
  84. }
  85. function closeCustom() {
  86. onClose()
  87. setShow(false)
  88. }
  89. function clickMask() {
  90. onClick()
  91. closeCustom()
  92. }
  93. function adActionReport(eventId, businessType) {
  94. const {
  95. adCode,
  96. adPosition,
  97. advertiserId,
  98. ownAdDetailId,
  99. ownAdPositionId,
  100. ownAdSystemType,
  101. planId,
  102. } = adData
  103. adActionLog([{
  104. adCode,
  105. adPosition,
  106. advertiserId,
  107. businessType,
  108. eventId,
  109. ownAdDetailId,
  110. ownAdPositionId,
  111. ownAdSystemType,
  112. pageSource: 'plugin',
  113. planId,
  114. pqtId: generateUUID(),
  115. videoId: '000'
  116. }])
  117. }
  118. return (
  119. <>
  120. {show && <View className='pq-custom' onClick={clickMask}>
  121. <View className='custom-container'>
  122. <View className='icon' style={{zIndex: (zIndex || 1) + 1}}>
  123. 广告
  124. </View>
  125. <View className='close-icon'style={{zIndex: (zIndex || 1) + 1}} onClick={closeCustom}>
  126. </View>
  127. {/* <Navigator url={WEB_VIEW_URL}> */}
  128. <Custom
  129. autoplay={autoplay}
  130. loop={loop}
  131. muted={muted}
  132. adData={adData}
  133. onPlay={videoPlay}
  134. onTimeUpdate={videoTimeUpdate}
  135. onPause={videoPause}
  136. onEnded={videoPlayEnded}
  137. onStop={videoStop}
  138. onClick={videoClick}
  139. onLoad={onLoad}
  140. onView={onView}
  141. onError={onError}
  142. />
  143. {/* </Navigator> */}
  144. </View>
  145. </View>}
  146. </>
  147. )
  148. }
  149. function Custom({
  150. autoplay, loop, muted, adData,
  151. onPlay, onPause, onEnded, onTimeUpdate, onStop, // 播放事件
  152. onClick, onLoad, onView, onError
  153. }) {
  154. const [style, setStyle] = useState({ width: '0px', height: 'px' })
  155. useReady(() => {
  156. loadedMetaFiredInOnce = 0
  157. })
  158. useGetCustomClient((customClient) => {
  159. let clientWidth = customClient.width
  160. let clientHeight = customClient.height
  161. setStyle({
  162. width: `${clientWidth}px`,
  163. height: `${clientHeight}px`
  164. })
  165. })
  166. function onLoadedMetaData() {
  167. if (loadedMetaFiredInOnce++ < 1) {
  168. // 上报load
  169. onLoad()
  170. }
  171. }
  172. return (
  173. <View className='custom' style={style}>
  174. <Video
  175. id='pq-custom'
  176. className='custom-video'
  177. objectFit='fill'
  178. controls={false}
  179. // video属性
  180. autoplay={autoplay}
  181. loop={loop}
  182. muted={muted}
  183. src={adData.materialAddress}
  184. poster={adData.materialCoverPic}
  185. // video事件
  186. onPlay={onPlay}
  187. onTimeUpdate={onTimeUpdate}
  188. onPause={onPause}
  189. onEnded={onEnded}
  190. onStop={onStop}
  191. onError={onError}
  192. // video交互
  193. onClick={onClick}
  194. onLoadedMetaData={onLoadedMetaData}
  195. />
  196. <View className='custom-bottom'>
  197. <Image src={adData.creativeLogoAddress} className='logo' />
  198. <View className='title'>{adData.creativeTitle}</View>
  199. <View className='button'>{adData.clickButtonText}</View>
  200. </View>
  201. </View>
  202. )
  203. }
  204. export default PqCustom