这三个月一直在进行鸿蒙原生开发,已经在应用市场发了App和元服务,期间陆续整理了一些笔记,现发布出来,也可以留言探讨。
下载三方包
下载三方包,需要配置 ohpm。前提是安装nodejs,去nodejs官网下载后配置PATH。然后再下载ohpm工具包,进行初始化后,再进行配置。
不过最新的编辑器好像直接自带了nodejs
users/michelle 资料库(Library)里有Huawei onpm。 /Users/michelle/Library/Huawei/ohpm
就是说要下载2个包:nodejs和ohpm,然后初始化./init
后,再配置到环境变量里。文档:ohpm使用指导
interface 回调使用
interface写法变了
export interface OnCallBack<T = object> {// 这里不一样,不是这样:onSuccess(bean: T): void;onSuccess: (bean: T) => void;onError: (error: AxiosError) => void;
}
使用:
public static searchPageData(o: string, callBack: OnCallBack<BaseResultBean<SearchBean>>) {HttpUtil.getInstance().post<null, AxiosResponse<BaseResultBean<SearchBean>>, HomeBean>("/xx/xx", {o: o}).then((res: AxiosResponse<BaseResultBean<SearchBean>>) => {callBack?.onSuccess(res.data)console.error("1222------" + res.data.result!!.searchGoods)// if (res.data.result != null) {// }}).catch((error: AxiosError) => {callBack?.onError(error)console.log(JSON.stringify(error))})}
}
引用:
HttpUtil.searchPageData("harmonyos", {onSuccess: (bean: BaseResultBean<SearchBean>): void => {if (bean.result != null) {this.message = bean.result}},onError: (error: AxiosError): void => {console.log('error:' + error.message);}})
华为登录
最新的next版本,不能获取到手机号。
- 需要用真机;
- 签名不用自动签名;
Failed to login. Code: 1001500001, message: Failed to check the fingerprint of the app bundle.Fingerprint verification error.
Authorization Code。临时凭据,有效时间5分钟,并且只能使用一次
module.json5里加:
"metadata": [{"name": "client_id","value": "775544333" // 配置为获取的Client ID}
]
配置证书指纹(指纹配置成功后大约10分钟左右生效,请您耐心等待):
1.在module.json5 配置metadata --> client_id
2.AppGallery Connect平台 找到对应的项目,在里面配置“SHA256证书/公钥指纹”
需要注意包名要和开发的包名对应,client_id也是在这个应用取的
https://developer.huawei.com/consumer/cn/blog/topic/03147440682731014 获取公钥指纹步骤
(期间使用:hdc shell bm dump -n 包名 | findstr appId
findstr错误,可以将findstr改为grep,即:hdc shell bm dump -n 包名 | grep appId
)
(hdc 需要配置环境变量,且只有在当前的命令窗口里有效)
最后登录:
huaweiLogin() {LogBevolUtil.error("---------start login")this.isShowLoading = true// 创建登录请求,并设置参数let loginRequest = new authentication.HuaweiIDProvider().createAuthorizationWithHuaweiIDRequest();// loginRequest.scopes = ['phone']; // 元服务不支持profile,需传其他支持的scope。配置openid的scope可以在响应中获取到openID、unionID// loginRequest.scopes = ['quickLoginAnonymousPhone','openid']; // 元服务不支持profile,需传其他支持的scope。配置openid的scope可以在响应中获取到openID、unionIDloginRequest.scopes = ['openid', "profile"]; // 元服务不支持profile,需传其他支持的scope。配置openid的scope可以在响应中获取到openID、unionID// loginRequest.scopes = ["openid"]; // 元服务不支持profile,需传其他支持的scope。配置openid的scope可以在响应中获取到openID、unionID// loginRequest.permissions = ['serviceauthcode'];// loginRequest.permissions = ['quickLoginAnonymousPhone'];loginRequest.forceAuthorization = true;// false表示当用户未登录华为账号时,不会拉起华为账号登录界面;直接返回1001502001错误码。// loginRequest.forceLogin = false;// authRequest.state = util.generateRandomUUID(); // 应用建议使用generateRandomUUID生成stateloginRequest.state = 'xxx'; // 元服务自定义生成loginRequest.idTokenSignAlgorithm = authentication.IdTokenSignAlgorithm.PS256;// 执行登录请求,并处理结果try {let controller = new authentication.AuthenticationController(getContext(this));controller.executeRequest(loginRequest, (error: BusinessError<Object>, data) => {if (error) {this.dealAllError(error);return;}let loginWithHuaweiIDResponse = data as authentication.AuthorizationWithHuaweiIDResponse;let state = loginWithHuaweiIDResponse.state;if (state != undefined && loginRequest.state != state) {this.handleLoginFail()hilog.error(0x0000, 'testTag', `Failed to login. The state is different, response state: ${state}`);return;}hilog.info(0x0000, 'testTag', 'Succeeded in login. %{public}s', JSON.stringify(loginWithHuaweiIDResponse));let loginWithHuaweiIDCredential = loginWithHuaweiIDResponse.data!;let code = loginWithHuaweiIDCredential.authorizationCode;// 得到code请求接口登录let idToken = loginWithHuaweiIDCredential.idToken;let openID = loginWithHuaweiIDCredential.openID;let unionID = loginWithHuaweiIDCredential.unionID;// loginWithHuaweiIDResponse.data.let anonymousPhone = loginWithHuaweiIDCredential.extraInfo?.quickLoginAnonymousPhone;LogBevolUtil.error("----anonymousPhone:" + anonymousPhone)LogBevolUtil.error("----openID:" + openID)LogBevolUtil.error("----unionID:" + unionID)// if (anonymousPhone) {// hilog.info(0x0000, 'testTag', 'Succeeded in authentication');// // 将匿名手机号传给第3步使用// return;// }// 开发者处理code, idToken, openID, unionID// Authorization Code。临时凭据,有效时间5分钟,并且只能使用一次。证书不能使用自动获取形式。// Logger.error("----code:" + code)if (!isEmpty(unionID) && !isEmpty(openID)) {this.login(unionID, openID, code)} else {this.handleLoginFail()}});} catch (error) {this.dealAllError(error);}}
延迟执行
// 延迟显示
setTimeout(() => {this.isShowFooterView = true
}, 200)
元服务发布后域名访问不通
将域名添加到后台