xugenyuan

ref |> 从新处理回华为账号一键登录

Signed-off-by: xugenyuan <xugenyuan@wondertek.com.cn>
... ... @@ -106,6 +106,7 @@ export class HttpUrlUtils {
* 资料编辑 更新普通用户名(用户名、身份证号、邮箱、手机号修改url)
*/
static readonly APPOINTMENT_editUserDetail1_PATH: string = "/api/rmrb-user-center/user/zh/c/completeUserInfo";
static readonly APPOINTMENT_editCreatorNickName_PATH: string = "/api/rmrb-creator-user/c/rmrb-creator-user/creator/updateName";
/**
* 资料编辑 地区拉取
*/
... ...
... ... @@ -9,8 +9,9 @@ import { encryptMessage } from '../../utils/cryptoUtil'
import { SpConstants } from 'wdConstant/Index'
import { UserDetail } from 'wdBean/Index';
import { HttpUtils } from 'wdNetwork/Index'
import { HttpUrlUtils, HttpUtils, ResponseDTO, WDHttp } from 'wdNetwork/Index'
import { LoginModule } from '../../LoginModule'
import HuaweiAuth from '../../utils/HuaweiAuth'
const TAG = "LoginViewModel"
... ... @@ -58,9 +59,34 @@ export class LoginViewModel {
huaweiOneKeyLogin(authCode: string) {
return new Promise<LoginBean>((success, fail) => {
this.loginModel.thirdPartLogin(9, {"idToken": authCode}).then((data: LoginBean) => {
this.loginModel.thirdPartLogin(9, {"idToken": authCode}).then(async (data: LoginBean) => {
this.dealWithLoginSuccess(data)
try {
await this.queryUserDetail()
// 1、暂时只能更新昵称
// 2、昵称需要审核,需要接口支持不带审核的?
// if (data.firstMark == 1) {
// let profileData = await HuaweiAuth.sharedInstance().requestProfile()
//
// if (profileData.nickName && profileData.nickName.length > 0) {
//
// let creatorId = SPHelper.default.getSync(SpConstants.USER_CREATOR_ID, "") as string
// if (data.userType == 1) {
// await this.uploadNormalUserNickNameFirstTime(profileData.nickName)
// } else if (creatorId.length > 0) {
// await this.uploadCreatorNickNameFirstTime(creatorId, profileData.nickName)
// }
// }
//
// }
} catch (e) {
} finally {
success(data)
}
}).catch((error:string) => {
fail(error)
})
... ... @@ -69,14 +95,14 @@ export class LoginViewModel {
//TODO: 这里要整体改掉
dealWithLoginSuccess(data: LoginBean) {
SPHelper.default.saveSync(SpConstants.USER_FIRST_MARK, data.firstMark)
SPHelper.default.saveSync(SpConstants.USER_FIRST_MARK, data.firstMark ?? 0)
SPHelper.default.saveSync(SpConstants.USER_ID, data.id)
SPHelper.default.saveSync(SpConstants.USER_JWT_TOKEN, data.jwtToken)
SPHelper.default.saveSync(SpConstants.USER_LONG_TIME_NO_LOGIN_MARK, data.longTimeNoLoginMark)
SPHelper.default.saveSync(SpConstants.USER_LONG_TIME_NO_LOGIN_MARK, data.longTimeNoLoginMark ?? 0)
SPHelper.default.saveSync(SpConstants.USER_REFRESH_TOKEN, data.refreshToken)
SPHelper.default.saveSync(SpConstants.USER_STATUS, data.status)
SPHelper.default.saveSync(SpConstants.USER_STATUS, data.status ?? 0)
SPHelper.default.saveSync(SpConstants.USER_Type, data.userType)
SPHelper.default.saveSync(SpConstants.USER_NAME, data.userName)
SPHelper.default.saveSync(SpConstants.USER_NAME, data.userName ?? "")
EmitterUtils.sendEmptyEvent(EmitterEventId.LOGIN_SUCCESS)
LoginModule.reportDeviceInfo()
... ... @@ -273,4 +299,33 @@ export class LoginViewModel {
}
return isMatch;
}
public uploadNormalUserNickNameFirstTime(nickName: string) {
return new Promise<boolean>((success, fail) => {
let requestUrl = HttpUrlUtils.getHost() + HttpUrlUtils.APPOINTMENT_editUserDetail1_PATH
let params = {"userName": nickName} as Record<string, string>
WDHttp.post<ResponseDTO<string>>(requestUrl,params).then((res) => {
success(true)
}).catch(() => {
fail()
})
})
}
public uploadCreatorNickNameFirstTime(creatorId: string, nickName: string) {
return new Promise<boolean>((success, fail) => {
let requestUrl = HttpUrlUtils.getHost() + HttpUrlUtils.APPOINTMENT_editCreatorNickName_PATH
let params = {"name": nickName, "creatorId": creatorId} as Record<string, string>
WDHttp.post<ResponseDTO<string>>(requestUrl,params).then((res) => {
success(true)
}).catch(() => {
fail()
})
})
}
}
\ No newline at end of file
... ...
... ... @@ -36,24 +36,26 @@ struct OneKeyLoginPage {
new loginComponentManager.LoginWithHuaweiIDButtonController()
/**
* 当应用使用自定义的登录页时,如果用户未同意协议,需要设置协议状态为NOT_ACCEPTED,当用户同意协议后再设置
* 协议状态为ACCEPTED,才可以使用华为号一键登录功能。
* 协议状态为ACCEPTED,才可以使用华为号一键登录功能。
*/
.setAgreementStatus(loginComponentManager.AgreementStatus.NOT_ACCEPTED)
.onClickLoginWithHuaweiIDButton((error: BusinessError, response: loginComponentManager.HuaweiIDCredential) => {
if (error) {
if (error || !response) {
Logger.debug("HuaweiOneKeyAuth", "onClickLoginWithHuaweiIDButton error: " + JSON.stringify(error))
this.toastText = "请阅读并勾选用户协议、隐私政策和华为账号用户认证协议"
this.dialogToast.open()
if (!this.agreeProtocol) {
this.showToastTip("请阅读并勾选用户协议、隐私政策和华为账号用户认证协议")
return
}
this.showToastTip("登录失败")
return;
}
if (response) {
// 获取到Authorization Code
let authorizationCode = response.authorizationCode;
Logger.debug("HuaweiOneKeyAuth", "第一次授权码: " + authorizationCode)
// hilog.info(0x0000, 'testTag', 'response: %{public}s', JSON.stringify(response));
TrackingButton.click("oneClickLoginPageLoginButton", TrackConstants.PageName.OneClick_Login, TrackConstants.PageName.OneClick_Login)
this.requestLogin()
return;
}
this.requestLogin(authorizationCode)
});
// 埋点计算页面浏览时长
... ... @@ -158,7 +160,7 @@ struct OneKeyLoginPage {
Row() {
Image($r("app.media.huawei_one_key_login_icon"))
.width(24).height(24)
Text("华为账号快速登录")
Text("华为账号一键登录")
.fontColor(Color.White)
.margin({left:5})
}
... ... @@ -167,41 +169,31 @@ struct OneKeyLoginPage {
.height(48)
.borderRadius(4)
.type(ButtonType.Normal)
.backgroundColor("#ED2800")
.onClick(() => {
.backgroundColor(!this.agreeProtocol ? "#60ED2800" : "#ED2800")
if (!this.agreeProtocol) {
this.toastText = "请阅读并勾选用户协议、隐私政策和华为账号用户认证协议"
this.dialogToast.open()
return
Stack(){
//目前不支持文字和图标同时展示,后面优化
LoginWithHuaweiIDButton({
params: {
// LoginWithHuaweiIDButton支持的样式。
style: loginComponentManager.Style.BUTTON_CUSTOM,
// LoginWithHuaweiIDButton的边框圆角半径。
// borderRadius: 4,
// LoginWithHuaweiIDButton支持的登录类型。
loginType: loginComponentManager.LoginType.QUICK_LOGIN,
// LoginWithHuaweiIDButton支持按钮的样式跟随系统深浅色模式切换。
// supportDarkMode: true,
customButtonParams: {
fontColor: loginComponentManager.FontColor.WHITE,
// backgroundColor:!this.agreeProtocol ? "#60ED2800" : "#ED2800"
backgroundColor:"#ED2800"
}
TrackingButton.click("oneClickLoginPageLoginButton", TrackConstants.PageName.OneClick_Login, TrackConstants.PageName.OneClick_Login)
this.requestLogin()
})
// Stack(){
// //目前不支持文字和图标同时展示,后面优化
// LoginWithHuaweiIDButton({
// params: {
// // LoginWithHuaweiIDButton支持的样式。
// style: loginComponentManager.Style.BUTTON_CUSTOM,
// // LoginWithHuaweiIDButton的边框圆角半径。
// // borderRadius: 4,
// // LoginWithHuaweiIDButton支持的登录类型。
// loginType: loginComponentManager.LoginType.QUICK_LOGIN,
// // LoginWithHuaweiIDButton支持按钮的样式跟随系统深浅色模式切换。
// // supportDarkMode: true,
// customButtonParams: {
// fontColor: loginComponentManager.FontColor.WHITE,
// // backgroundColor:!this.agreeProtocol ? "#60ED2800" : "#ED2800"
// backgroundColor:"#ED2800"
// }
// },
// controller: this.controller
// }).opacity(0)
// }
// .width('100%')
// .height('100%')
},
controller: this.controller
}).opacity(0)
}
.width('100%')
.height('100%')
}
}
.height(48)
... ... @@ -261,11 +253,9 @@ struct OneKeyLoginPage {
.height(44)
}
async requestLogin() {
async requestLogin(phoneAuthorizedCode: string) {
try {
let authorizeCode = await HuaweiAuth.sharedInstance().requestProfile()
let data = await this.viewModel.huaweiOneKeyLogin(authorizeCode)
let data = await this.viewModel.huaweiOneKeyLogin(phoneAuthorizedCode)
Logger.debug(TAG, "requestLogin: " + data.jwtToken)
this.showToastTip('登录成功')
... ... @@ -292,15 +282,16 @@ struct OneKeyLoginPage {
}
queryUserDetail(){
this.viewModel.queryUserDetail().then(()=>{
router.back({
url: `${WDRouterPage.getBundleInfo()}`
}
)
}).catch(()=>{
router.back({
url: `${WDRouterPage.getBundleInfo()}`
})
})
router.back({ url: `${WDRouterPage.getBundleInfo()}` })
// this.viewModel.queryUserDetail().then(()=>{
// router.back({
// url: `${WDRouterPage.getBundleInfo()}`
// }
// )
// }).catch(()=>{
// router.back({
// url: `${WDRouterPage.getBundleInfo()}`
// })
// })
}
}
\ No newline at end of file
... ...
... ... @@ -82,9 +82,8 @@ export default class HuaweiAuth {
})
}
// 华为账号一键登录授权
// 返回结果为 Authorization Code
requestProfile() : Promise<string> {
// 华为账号获取orofile信息
requestProfile() : Promise<WDHuaweiProfileData> {
// let loginRequest = new authentication.HuaweiIDProvider().createLoginWithHuaweiIDRequest();
// // 当用户未登录华为帐号时,是否强制拉起华为帐号登录界面
... ... @@ -94,7 +93,7 @@ export default class HuaweiAuth {
// 创建授权请求,并设置参数
let loginRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();
// 获取头像昵称需要传如下scope
loginRequest.scopes = ['profile', 'phone']; //, 'phone'
loginRequest.scopes = ['profile'];
// 若开发者需要进行服务端开发,则需传如下permission获取authorizationCode
loginRequest.permissions = ['serviceauthcode'];
// 用户是否需要登录授权,该值为true且用户未登录或未授权时,会拉起用户登录或授权页面
... ... @@ -107,29 +106,31 @@ export default class HuaweiAuth {
let controller = new authentication.AuthenticationController(getContext(this));
controller.executeRequest(loginRequest, (err, data) => {
if (err) {
Logger.error(TAG, 'login fail, ' + JSON.stringify(err))
Logger.error(TAG, 'requestProfile fail, ' + JSON.stringify(err))
fail(err)
return;
}
let loginWithHuaweiIDResponse = data as authentication.LoginWithHuaweiIDResponse;
let loginWithHuaweiIDResponse = data as authentication.AuthorizationWithHuaweiIDResponse;
let state = loginWithHuaweiIDResponse.state;
if (state != undefined && loginRequest.state != state) {
Logger.error(TAG, 'login fail, The state is different' + JSON.stringify(loginWithHuaweiIDResponse))
Logger.error(TAG, 'requestProfile fail, The state is different' + JSON.stringify(loginWithHuaweiIDResponse))
fail({
code: 99999,
name: "一键登录",
name: "一键登录获取用户信息失败",
message:"状态错误:" + `请求状态 ${loginRequest.state}, 结果状态 ${state}`
} as BusinessError)
return;
}
Logger.info(TAG, 'login success, ' + JSON.stringify(loginWithHuaweiIDResponse));
let loginWithHuaweiIDCredential = loginWithHuaweiIDResponse.data!;
let authorizationCode = loginWithHuaweiIDCredential.authorizationCode;
resolve(authorizationCode!)
Logger.info(TAG, 'requestProfile success, ' + JSON.stringify(loginWithHuaweiIDResponse));
let authorizationCode = loginWithHuaweiIDResponse.data?.authorizationCode;
let nickName = loginWithHuaweiIDResponse.data?.nickName
let avatarUri = loginWithHuaweiIDResponse.data?.avatarUri
resolve({avatarUri: avatarUri, nickName: nickName } as WDHuaweiProfileData)
});
} catch (error) {
Logger.error(TAG, 'login fail, ' + JSON.stringify(error))
Logger.error(TAG, 'requestProfile fail, ' + JSON.stringify(error))
fail(error)
}
});
... ... @@ -150,3 +151,8 @@ export default class HuaweiAuth {
}
}
}
export interface WDHuaweiProfileData {
avatarUri?: string
nickName?: string
}
\ No newline at end of file
... ...