SplashIndex.ets 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. import UserPrivacyDialog from '../dialog/UserPrivacyDialog'
  2. import { common } from '@kit.AbilityKit'
  3. import data_preferences from '@ohos.data.preferences'
  4. import Logger from '../common/util/Logger'
  5. import { router, window } from '@kit.ArkUI'
  6. import { CommonConstants } from '../common/constants/CommonConstants'
  7. import { CSJUtil } from '../common/util/CSJUtil'
  8. import { AdSlotBuilder, CSJAdCreator, CSJAdSdk, CSJSplashAd,
  9. CSJSplashAdCloseType,
  10. CSJSplashAdInteractionListener,
  11. CSJSplashAdLoadListener,
  12. CSJSplashAdLoadParam,} from '@csj/openadsdk'
  13. import { UIUtil } from '../common/util/UIUtil'
  14. import { PrintBiddingTokenUtils } from '../common/util/PrintBiddintTokenUtils'
  15. import { DemoConstants } from '../entryability/DemoConstants'
  16. import { Base64Util, FileUtil, LogUtil, StrUtil, ToastUtil } from '@pura/harmony-utils'
  17. import { Utility } from '../common/util/Utility'
  18. import { VideoItem } from '../viewmodel/VideoItem'
  19. import { LocalMusic } from '../view/LocalMusic'
  20. import { fileIo, fileUri, picker, ReadTextOptions } from '@kit.CoreFileKit'
  21. import { VipPage } from './VipPage'
  22. import { VipData } from '../viewmodel/VipData'
  23. //定义常量存储首选项中的键
  24. const H_STORE:string = 'wifiAppStore'
  25. const IS_PRIVACY:string = 'isPrivacy'
  26. //排序模式参数
  27. interface VideoNameParts {
  28. nonNumeric: string;
  29. numeric: number;
  30. }
  31. function extractParts(name: string): VideoNameParts {
  32. const match = name.match(/^(\D*)(\d*)/);
  33. return {
  34. nonNumeric: match?.[1] || '',
  35. numeric: parseInt(match?.[2] || '0', 10)
  36. };
  37. }
  38. @Builder
  39. function bottomViewBuilder() {
  40. Text('Bottom View')
  41. .backgroundColor(Color.Yellow)
  42. .width(UIUtil.getScreenWidthVp())
  43. .height(UIUtil.getScreenHeightVp() - adHeight)
  44. }
  45. @Builder
  46. function closeBtnBuilder(closeBlock: () => void) {
  47. Text('close')
  48. .textAlign(TextAlign.Center)
  49. .backgroundColor(Color.Yellow)
  50. .width(60)
  51. .height(60)
  52. .borderRadius(30)
  53. .margin({ top: 50, right: 40 })
  54. .onClick((event) => {
  55. closeBlock()
  56. })
  57. }
  58. let globalBuilder: WrappedBuilder<[]> = wrapBuilder(bottomViewBuilder)
  59. let globalCloseBtnBuilder: WrappedBuilder<[closeBlock: () => void]> = wrapBuilder(closeBtnBuilder)
  60. let screenWidth: number = UIUtil.getScreenWidthVp()
  61. let screenHeight: number = UIUtil.getScreenHeightVp()
  62. let adWidth: number = Math.round(screenWidth)
  63. let adHeight: number = Math.round(screenHeight)
  64. @Entry
  65. @Component
  66. struct SplashIndex{
  67. context:common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
  68. @Provide isToPri:boolean = false
  69. //穿山甲属性
  70. private mAdCreator: CSJAdCreator = CSJAdSdk.getAdCreator()
  71. private splashAd: CSJSplashAd | undefined
  72. @State adLoadSuccess: Boolean = false
  73. @State errorMsg: string | undefined = undefined;
  74. @State startLoad: boolean = false;
  75. @State appName:string = ''
  76. private mLoadListener: CSJSplashAdLoadListener = {
  77. onAdLoaded: (splashAd: CSJSplashAd) => {
  78. // ToastUtil.showToast("开屏回调 - 加载广告成功")
  79. console.log("开屏回调 -加载广告成功 onAdLoaded")
  80. this.splashAd = splashAd
  81. this.adLoadSuccess = true
  82. this.startLoad = false
  83. this.showKP()
  84. },
  85. onError: (code: number, message: string) => {
  86. this.errorMsg = "onError\ncode: " + code + "\nmessage: " + message
  87. console.log(`开屏回调 - onError code: ${code} message: ${message}`)
  88. this.junpToMain()
  89. },
  90. onRenderSuccess: (splashAd: CSJSplashAd) => {
  91. console.log("开屏回调 - onRenderSuccess")
  92. },
  93. onRenderFail:(code: number, message: string) => {
  94. console.log("开屏回调 - onRenderFail")
  95. this.junpToMain()
  96. }
  97. }
  98. @State widthLabelText: string = "宽:" + adWidth
  99. @State heightLabelText: string = "高:" + adHeight
  100. private useCustomCloseBtn: boolean = false
  101. private mBiddingAdm = '' //服务端bidding才需要设置
  102. private loadTimeout: number = 5000
  103. private mWindow: window.Window | undefined = undefined
  104. dialogController:CustomDialogController = new CustomDialogController({
  105. builder:UserPrivacyDialog({
  106. cancel:()=>{this.exitApp()},
  107. confirm:()=>{this.onConfirm()}
  108. }
  109. )})
  110. @State rootPath:string = ''
  111. @State currentPath:string = ''
  112. @State videoLocalList: Array<VideoItem> = []
  113. @State fileList: Array<String> = []
  114. @State dirList: Array<VideoItem> = []
  115. @State expireDate:string = ''
  116. async mkDownLoadDir(){
  117. const documentViewPicker = new picker.DocumentViewPicker()
  118. let documentSaveResult = await documentViewPicker.save({ pickerMode: picker.DocumentPickerMode.DOWNLOAD })
  119. let download_path = new fileUri.FileUri(documentSaveResult[0]).path
  120. this.rootPath = download_path
  121. let vPath = download_path + '/'+ LocalMusic.STR_LOCK_VIDEO+ '/' + VipPage.VIP_FILEPATH
  122. this.expireDate = this.readDataFromFile(vPath)
  123. Utility.setNoble(this.expireDate)
  124. Logger.info('onecold rootPath = '+ this.rootPath)
  125. this.getSortedFiles(this.rootPath)
  126. }
  127. readDataFromFile(filePath:string){
  128. if(!FileUtil.accessSync(filePath)){
  129. return ''
  130. }
  131. let readTextOptions: ReadTextOptions = {
  132. offset: 0,
  133. length: 0,
  134. encoding: 'utf-8'
  135. };
  136. let stat = fileIo.statSync(filePath);
  137. readTextOptions.length = stat.size;
  138. let str = fileIo.readTextSync(filePath, readTextOptions);
  139. const parsedData: VipData = JSON.parse(str);
  140. // LogUtil.info('onecold parsedData.expireDate = ' + parsedData.expireDate)
  141. let result = Base64Util.decodeSync( parsedData.expireDate)
  142. // LogUtil.info('onecold 解密的result = ' +StrUtil.unit8ArrayToStr(result))
  143. return StrUtil.unit8ArrayToStr(result);
  144. }
  145. async getSortedFiles(curPath:string){
  146. if(!Utility.isNoble()&&Utility.isPassInstallTime(2)){
  147. //判断用户安装app是否超过2天,超过2天才展示广告
  148. CSJUtil.get().init()
  149. CSJUtil.get().start()
  150. setTimeout(()=>{
  151. this.loadKPCSJ()
  152. },200)
  153. }else{
  154. LogUtil.info('onecold 尊敬的赞助用户,欢迎您!' )
  155. this.junpToMain()
  156. }
  157. }
  158. //点击同意后保存数据
  159. onConfirm(){
  160. this.dialogController.close()
  161. let preferences = data_preferences.getPreferences(this.context,H_STORE)
  162. //异步处理首选项的数据
  163. preferences.then((res)=>{
  164. res.put(IS_PRIVACY,true).then(()=>{
  165. res.flush()
  166. //记录日志
  167. Logger.debug('SplashIndex','isPricacy记录成功')
  168. }).catch((err:Error) =>{
  169. Logger.error('SplashIndex','isPricacy记录失败,原因:'+err)
  170. })
  171. })
  172. // this.junpToMainNow();
  173. this.initCSJSDK()
  174. }
  175. //初始化SDK
  176. initCSJSDK(){
  177. this.mkDownLoadDir()
  178. }
  179. //退出App
  180. exitApp(){
  181. this.context.terminateSelf()
  182. }
  183. //判断是否弹出隐私政策对话框
  184. aboutToAppear(): void {
  185. Utility.getAppName(getContext(this)).then((appName:string)=>{
  186. this.appName = appName
  187. })
  188. let preferences = data_preferences.getPreferences(this.context,H_STORE)
  189. preferences.then((res) =>{
  190. res.get(IS_PRIVACY,false).then((isPrivacy) =>{
  191. if(isPrivacy == true){
  192. //说明用户已经点击过同意
  193. this.initCSJSDK()
  194. }else {
  195. //弹出弹窗
  196. this.dialogController.open()
  197. }
  198. })
  199. })
  200. }
  201. //加载开屏广告
  202. loadKPCSJ(){
  203. this.errorMsg = undefined;
  204. this.adLoadSuccess = false;
  205. this.startLoad = true;
  206. let adSlot = new AdSlotBuilder()
  207. .setCodeId(CSJUtil.getWelcomeD())
  208. .setAcceptSize(adWidth,
  209. adHeight)
  210. // .setBidAdm(this.mBiddingAdm)
  211. .build()
  212. let param = new CSJSplashAdLoadParam(adSlot, this.mLoadListener, this.getUIContext(),this.loadTimeout)
  213. PrintBiddingTokenUtils.printBiddingToken(adSlot, this.mAdCreator);
  214. this.mAdCreator.loadSplashAd(param)
  215. }
  216. showKP(){
  217. if (this.splashAd != undefined) {
  218. this.splashAd.setInteractionListener(this.mSplashAdInteractionListener)
  219. // 方式一:直接showSplashAd
  220. if (adHeight < screenHeight) {
  221. this.splashAd?.showSplashAd(DemoConstants.windowStage, globalBuilder,
  222. this.useCustomCloseBtn ? globalCloseBtnBuilder : undefined)
  223. } else {
  224. this.splashAd?.showSplashAd(DemoConstants.windowStage, undefined,
  225. this.useCustomCloseBtn ? globalCloseBtnBuilder : undefined)
  226. }
  227. }
  228. }
  229. // CSJSplashAdInteractionListener
  230. private mSplashAdInteractionListener: CSJSplashAdInteractionListener = {
  231. onDidShow: () => {
  232. console.log("开屏回调 - onDidShow")
  233. },
  234. onDidClose: (closeType: CSJSplashAdCloseType) => {
  235. console.log(`开屏回调 - onDidClose 关闭原因:${closeType}`)
  236. this.splashAd = undefined
  237. setTimeout(()=>{
  238. this.junpToMain()
  239. },200)
  240. // 方式二:需自行移除广告
  241. // this.mWindow?.destroyWindow()
  242. },
  243. onDidClick: () => {
  244. console.log("开屏回调 - onDidClick")
  245. },
  246. onVideoDidPlayFinish: () => {
  247. console.log("开屏回调 - onVideoDidPlayFinish")
  248. this.junpToMain()
  249. },
  250. onVideoDidPlayFail: () => {
  251. console.log("开屏回调 - onVideoDidPlayFail")
  252. this.junpToMain()
  253. }
  254. }
  255. onPageShow(): void {
  256. if(this.isToPri){//判断是否跳转到隐私正常或者用户协议,如果是就弹出同意dialog
  257. this.aboutToAppear()
  258. this.isToPri = false
  259. }
  260. }
  261. onPageHide(): void {
  262. if (this.dialogController != null) {
  263. this.dialogController.close()
  264. }
  265. }
  266. pageTransition() {
  267. // 定义页面进入时的效果,从左侧滑入,时长为1200ms,无论页面栈发生push还是pop操作均可生效
  268. PageTransitionEnter({ type: RouteType.None, duration: 888 })
  269. .slide(SlideEffect.Left)
  270. // 定义页面退出时的效果,向左侧滑出,时长为1000ms,无论页面栈发生push还是pop操作均可生效
  271. PageTransitionExit({ type: RouteType.None, duration: 888 })
  272. .slide(SlideEffect.Left)
  273. }
  274. junpToMain(){
  275. router.pushUrl({
  276. url: 'pages/NewIndex',
  277. params: {
  278. videoList: this.videoLocalList
  279. }
  280. });
  281. }
  282. aboutToDisappear(): void {
  283. }
  284. build() {
  285. Column(){
  286. Column(){
  287. Image($r('app.media.icon'))
  288. .width(66)
  289. .height(66)
  290. .clip(true)
  291. .borderRadius('100%')
  292. .onClick(()=>{
  293. this.isToPri = true
  294. router.pushUrl({url:'pages/WebIndex',
  295. params:{ titleName:'ICP备案',webUrl:CommonConstants.ICP_URL}
  296. });
  297. })
  298. Text(this.appName)
  299. .fontColor(Color.Grey)
  300. .fontSize(18)
  301. .margin({top:20})
  302. Text(CommonConstants.ICP_NO)
  303. .fontColor(Color.Grey)
  304. .fontSize(9)
  305. .visibility(Visibility.None)
  306. .onClick(()=>{
  307. this.isToPri = true
  308. router.pushUrl({url:'pages/WebIndex',
  309. params:{ titleName:'ICP备案',webUrl:CommonConstants.ICP_URL}
  310. });
  311. })
  312. }
  313. .height(66)
  314. .width('100%')
  315. .margin({bottom:30})
  316. .justifyContent(FlexAlign.Center)
  317. // .backgroundColor(Color.White)
  318. }
  319. .justifyContent(FlexAlign.Center)
  320. .width('100%')
  321. .height('100%')
  322. // .backgroundImage($r('app.media.welcomeBg'))
  323. // .backgroundImageSize({height:'100%'})
  324. // .backgroundImagePosition(Alignment.Center)
  325. .expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP])
  326. }
  327. }