LoginModule.ets
1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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();
}
})
}
}