LoginModule.ets 2.04 KB
import HuaweiAuth from './utils/HuaweiAuth'
import { JumpInterceptorAction, RouterJumpInterceptor, WDRouterPage } from 'wdRouter'
import { BusinessError } from '@kit.BasicServicesKit'
import { router } from '@kit.ArkUI'
import { AccountManagerUtils, SPHelper } from 'wdKit/Index'
import { LoginViewModel } from './pages/login/LoginViewModel'
import { SpConstants } from 'wdConstant/Index'
import { ReportDeviceInfo } from './reportDeviceInfo/ReportDeviceInfo'
import { common } from '@kit.AbilityKit'

class LoginJumpHandler implements JumpInterceptorAction {

  private logining = false
  
  /// 说明是调用了跳转 WDRouterPage.loginPage 页面的行为
  on(params?: object | undefined, singleMode?: boolean | undefined): boolean {

    if (this.logining) {
      return true
    }
    this.logining = true

    HuaweiAuth.sharedInstance().fetchAnonymousPhone().then((anonymousPhone) => {

      router.pushUrl({url: WDRouterPage.oneKeyLoginPage.url()})
      this.logining = false
    }).catch((error: string) => {
      router.pushUrl({url: WDRouterPage.loginPage.url()})
      this.logining = false
    })
    return true
  }
}

export class LoginModule {

  static startup() {

    /// 初始化华为一键登录相关
    if (HuaweiAuth.enable) {

      HuaweiAuth.sharedInstance().registerEvents()

      // 这里启动不在预先取匿名手机号
      // AccountManagerUtils.isLogin().then((login) => {
      //   if (!login) {
      //    HuaweiAuth.sharedInstance().rePrefetchAnonymousPhone()
      // }
      // })
      RouterJumpInterceptor.register(WDRouterPage.loginPage, new LoginJumpHandler())
    }

  }

  // 启动进入主页 和 每次登录成功调用
  static reportDeviceInfo(context?: common.UIAbilityContext) {
    ReportDeviceInfo.reportDeviceInfo().then((res) => {
      let nickName = res.touristNickName
      if (res.touristNickName) {
        SPHelper.default.save(SpConstants.TOURIST_NICK_NAME, res.touristNickName)
      }

      if (res.blockStatus == -2) {
        // Exit the application.
        context?.terminateSelf();
      }
    })
  }
}