| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import { Context } from '@ohos.abilityAccessCtrl'
- import { common } from '@kit.AbilityKit'
- import { permissionController } from './PermissionControl'
- import {
- AppStorageKeyCollect,
- jHStartAd,
- pushMessageUtil,
- registerFontUtil,
- YTLog,
- yTPreferences,
- YTToast,
- YTUserRequest
- } from '../../../../Index'
- import { identifier } from '@kit.AdsKit'
- import { BusinessError } from '@kit.BasicServicesKit'
- export class ContextHelper {
- private static declare _UIContext: UIContext
- static get UIContext() {
- if (!ContextHelper._UIContext) {
- throw new Error('UIContext is not set')
- }
- return ContextHelper._UIContext
- }
- static set UIContext(value: UIContext) {
- ContextHelper._UIContext = value
- const context = value.getHostContext()
- if (context && !ContextHelper._context) {
- ContextHelper.context = context
- ContextHelper.UIAbilityContext = context as common.UIAbilityContext
- }
- }
- private static declare _UIAbilityContext: common.UIAbilityContext
- static get UIAbilityContext() {
- if (!ContextHelper._UIAbilityContext) {
- throw new Error('UIAbilityContext is not set')
- }
- return ContextHelper._UIAbilityContext
- }
- static set UIAbilityContext(value: common.UIAbilityContext) {
- ContextHelper._UIAbilityContext = value
- if (!ContextHelper._context) {
- ContextHelper.context = value
- }
- }
- private static declare _context: Context
- static get context() {
- if (!ContextHelper._context) {
- throw new Error('context is not set')
- }
- return ContextHelper._context
- }
- static set context(value: Context) {
- ContextHelper._context = value
- }
- //依赖上下文的工具类初始化 需要在第一个页面中设置UIContext后调用
- static init(context: UIContext) {
- ContextHelper.UIContext = context
- PersistentStorage.persistProp(AppStorageKeyCollect.TOKEN, '')
- YTUserRequest.refreshUserInfo()
- permissionController.init(ContextHelper.context)
- .then(() => {
- permissionController
- .askPermissions(["ohos.permission.APP_TRACKING_CONSENT"], (res, err) => {
- if (err) {
- YTLog.error(err)
- } else {
- if (res?.authResults[0] === 0) {
- YTLog.info('succeeded in requesting permission');
- identifier.getOAID((err: BusinessError, data: string) => {
- if (err.code) {
- YTLog.error(`get oaid failed, error: ${err.code} ${err.message}`);
- } else {
- const oaid: string = data;
- jHStartAd.init(ContextHelper.UIAbilityContext, oaid)
- AppStorage.setOrCreate('OAID', oaid)
- YTLog.info(`succeeded in getting oaid by callback , oaid: ${oaid}`);
- }
- });
- } else {
- YTLog.error('user rejected');
- }
- }
- })
- })
- yTPreferences.init(ContextHelper.context)
- pushMessageUtil.init(ContextHelper.UIAbilityContext)
- registerFontUtil.registerFont([
- {
- familyName: 'MiSans VF',
- familySrc: $rawfile('MiSansVF.ttf')
- },
- {
- familyName: 'Source Han Sans SC',
- familySrc: $rawfile('SourceHanSansSC.otf')
- },
- {
- familyName: 'Source Han Sans CN',
- familySrc: $rawfile('SourceHanSansSC.otf')
- }
- ])
- new YTToast({
- key: 'loginRemind',
- options: {
- autoCancel: false,
- alignment: DialogAlignment.Bottom,
- maskColor: Color.Transparent,
- isModal: false,
- offset: { dy: -84, dx: 0 }
- }
- })
- }
- }
|