SplashIndex.ets 13 KB

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