xugenyuan

ref |> 修改一键登录,按钮采用系统按钮

@@ -7,6 +7,8 @@ import { Logger, ToastUtils, CustomToast, EmitterUtils, EmitterEventId } from 'w @@ -7,6 +7,8 @@ import { Logger, ToastUtils, CustomToast, EmitterUtils, EmitterEventId } from 'w
7 import { LoginViewModel } from './LoginViewModel' 7 import { LoginViewModel } from './LoginViewModel'
8 import {InterestsHobbiesModel} from '../../../../../../../products/phone/src/main/ets/pages/viewModel/InterestsHobbiesModel' 8 import {InterestsHobbiesModel} from '../../../../../../../products/phone/src/main/ets/pages/viewModel/InterestsHobbiesModel'
9 import { TrackConstants, TrackingButton, TrackingPageBrowse } from 'wdTracking/Index' 9 import { TrackConstants, TrackingButton, TrackingPageBrowse } from 'wdTracking/Index'
  10 +import { loginComponentManager, LoginWithHuaweiIDButton } from '@kit.AccountKit'
  11 +import { JSON } from '@kit.ArkTS'
10 12
11 const TAG = "OneKeyLoginPage" 13 const TAG = "OneKeyLoginPage"
12 14
@@ -29,6 +31,30 @@ struct OneKeyLoginPage { @@ -29,6 +31,30 @@ struct OneKeyLoginPage {
29 customStyle: true, 31 customStyle: true,
30 maskColor:"#00000000" 32 maskColor:"#00000000"
31 }) 33 })
  34 + // 构造LoginWithHuaweiIDButton组件的控制器。
  35 + controller: loginComponentManager.LoginWithHuaweiIDButtonController =
  36 + new loginComponentManager.LoginWithHuaweiIDButtonController()
  37 + /**
  38 + * 当应用使用自定义的登录页时,如果用户未同意协议,需要设置协议状态为NOT_ACCEPTED,当用户同意协议后再设置
  39 + * 协议状态为ACCEPTED,才可以使用华为帐号一键登录功能。
  40 + */
  41 + .setAgreementStatus(loginComponentManager.AgreementStatus.NOT_ACCEPTED)
  42 + .onClickLoginWithHuaweiIDButton((error: BusinessError, response: loginComponentManager.HuaweiIDCredential) => {
  43 + if (error) {
  44 + Logger.debug("HuaweiOneKeyAuth", "onClickLoginWithHuaweiIDButton error: " + JSON.stringify(error))
  45 + this.toastText = "请阅读并勾选用户协议、隐私政策和华为账号用户认证协议"
  46 + this.dialogToast.open()
  47 + return;
  48 + }
  49 + if (response) {
  50 + // 获取到Authorization Code
  51 + let authorizationCode = response.authorizationCode;
  52 + // hilog.info(0x0000, 'testTag', 'response: %{public}s', JSON.stringify(response));
  53 + TrackingButton.click("oneClickLoginPageLoginButton", this.pageName, this.pageName)
  54 + this.requestLogin()
  55 + return;
  56 + }
  57 + });
32 58
33 // 埋点计算页面浏览时长 59 // 埋点计算页面浏览时长
34 private pageStartDate: number = 0 60 private pageStartDate: number = 0
@@ -85,30 +111,7 @@ struct OneKeyLoginPage { @@ -85,30 +111,7 @@ struct OneKeyLoginPage {
85 111
86 this.ProtocolRow() 112 this.ProtocolRow()
87 113
88 - Row() {  
89 - Button() {  
90 - Row() {  
91 - Image($r("app.media.huawei_one_key_login_icon"))  
92 - .width(24).height(24)  
93 - Text("华为账号一键登录")  
94 - .fontColor(Color.White)  
95 - }  
96 - }  
97 - .type(ButtonType.Normal)  
98 - .height(48)  
99 - .backgroundColor(!this.agreeProtocol ? "#60ED2800" : "#ED2800")  
100 - .width("100%")  
101 - .onClick((event) => {  
102 - if (!this.agreeProtocol) {  
103 - return  
104 - }  
105 -  
106 - TrackingButton.click("oneClickLoginPageLoginButton", this.pageName, this.pageName)  
107 - this.requestLogin()  
108 - })  
109 - }  
110 - .padding({ left: 25, right: 25 })  
111 - .margin({top: 20}) 114 + this.loginButton()
112 115
113 Button("账号密码登录") 116 Button("账号密码登录")
114 .type(ButtonType.Normal) 117 .type(ButtonType.Normal)
@@ -125,6 +128,58 @@ struct OneKeyLoginPage { @@ -125,6 +128,58 @@ struct OneKeyLoginPage {
125 .padding({top: `${this.topSafeHeight}px`}) 128 .padding({top: `${this.topSafeHeight}px`})
126 } 129 }
127 130
  131 + @Builder loginButton() {
  132 + Column() {
  133 + LoginWithHuaweiIDButton({
  134 + params: {
  135 + // LoginWithHuaweiIDButton支持的样式。
  136 + style: loginComponentManager.Style.BUTTON_CUSTOM,
  137 + // LoginWithHuaweiIDButton的边框圆角半径。
  138 + borderRadius: 4,
  139 + // LoginWithHuaweiIDButton支持的登录类型。
  140 + loginType: loginComponentManager.LoginType.QUICK_LOGIN,
  141 + // LoginWithHuaweiIDButton支持按钮的样式跟随系统深浅色模式切换。
  142 + supportDarkMode: true,
  143 + customButtonParams: {
  144 + fontColor: loginComponentManager.FontColor.WHITE,
  145 + // backgroundColor:!this.agreeProtocol ? "#60ED2800" : "#ED2800"
  146 + backgroundColor:"#ED2800"
  147 + }
  148 + },
  149 + controller: this.controller
  150 + })
  151 + }
  152 + .height(48)
  153 + .margin({ top: 20 ,left: 25, right: 25 })
  154 + }
  155 +
  156 + // @Builder loginButton() {
  157 + // Row() {
  158 + // Button() {
  159 + // Row() {
  160 + // Image($r("app.media.huawei_one_key_login_icon"))
  161 + // .width(24).height(24)
  162 + // Text("华为账号一键登录")
  163 + // .fontColor(Color.White)
  164 + // }
  165 + // }
  166 + // .type(ButtonType.Normal)
  167 + // .height(48)
  168 + // .backgroundColor(!this.agreeProtocol ? "#60ED2800" : "#ED2800")
  169 + // .width("100%")
  170 + // .onClick((event) => {
  171 + // if (!this.agreeProtocol) {
  172 + // return
  173 + // }
  174 + //
  175 + // TrackingButton.click("oneClickLoginPageLoginButton", this.pageName, this.pageName)
  176 + // this.requestLogin()
  177 + // })
  178 + // }
  179 + // .padding({ left: 25, right: 25 })
  180 + // .margin({top: 20})
  181 + // }
  182 +
128 @Builder ProtocolRow() { 183 @Builder ProtocolRow() {
129 Row({space: 4}) { 184 Row({space: 4}) {
130 Image(this.agreeProtocol ? $r('app.media.login_checkbox_select') : $r('app.media.login_checkbox_unselected')) 185 Image(this.agreeProtocol ? $r('app.media.login_checkbox_select') : $r('app.media.login_checkbox_unselected'))
@@ -133,6 +188,11 @@ struct OneKeyLoginPage { @@ -133,6 +188,11 @@ struct OneKeyLoginPage {
133 .margin({top: 3}) 188 .margin({top: 3})
134 .onClick(() => { 189 .onClick(() => {
135 this.agreeProtocol = !this.agreeProtocol 190 this.agreeProtocol = !this.agreeProtocol
  191 + if (this.agreeProtocol) {
  192 + this.controller.setAgreementStatus(loginComponentManager.AgreementStatus.ACCEPTED)
  193 + } else {
  194 + this.controller.setAgreementStatus(loginComponentManager.AgreementStatus.NOT_ACCEPTED)
  195 + }
136 }) 196 })
137 Text() { 197 Text() {
138 Span("我已阅读并同意").fontColor("#999999").fontSize(12).lineHeight(18) 198 Span("我已阅读并同意").fontColor("#999999").fontSize(12).lineHeight(18)
@@ -173,7 +233,7 @@ struct OneKeyLoginPage { @@ -173,7 +233,7 @@ struct OneKeyLoginPage {
173 233
174 async requestLogin() { 234 async requestLogin() {
175 try { 235 try {
176 - let authorizeCode = await HuaweiAuth.sharedInstance().oneKeyLogin() 236 + let authorizeCode = await HuaweiAuth.sharedInstance().requestProfile()
177 237
178 let data = await this.viewModel.huaweiOneKeyLogin(authorizeCode) 238 let data = await this.viewModel.huaweiOneKeyLogin(authorizeCode)
179 239
@@ -84,7 +84,7 @@ export default class HuaweiAuth { @@ -84,7 +84,7 @@ export default class HuaweiAuth {
84 84
85 // 华为账号一键登录授权 85 // 华为账号一键登录授权
86 // 返回结果为 Authorization Code 86 // 返回结果为 Authorization Code
87 - oneKeyLogin() : Promise<string> { 87 + requestProfile() : Promise<string> {
88 88
89 // let loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest(); 89 // let loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
90 // // 当用户未登录华为帐号时,是否强制拉起华为帐号登录界面 90 // // 当用户未登录华为帐号时,是否强制拉起华为帐号登录界面
@@ -94,7 +94,7 @@ export default class HuaweiAuth { @@ -94,7 +94,7 @@ export default class HuaweiAuth {
94 // 创建授权请求,并设置参数 94 // 创建授权请求,并设置参数
95 let loginRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest(); 95 let loginRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
96 // 获取头像昵称需要传如下scope 96 // 获取头像昵称需要传如下scope
97 - loginRequest.scopes = ['profile', 'phone']; 97 + loginRequest.scopes = ['profile']; //, 'phone'
98 // 若开发者需要进行服务端开发,则需传如下permission获取authorizationCode 98 // 若开发者需要进行服务端开发,则需传如下permission获取authorizationCode
99 loginRequest.permissions = ['serviceauthcode']; 99 loginRequest.permissions = ['serviceauthcode'];
100 // 用户是否需要登录授权,该值为true且用户未登录或未授权时,会拉起用户登录或授权页面 100 // 用户是否需要登录授权,该值为true且用户未登录或未授权时,会拉起用户登录或授权页面