HarmonyOS鸿蒙Next中点击登录按钮报错
HarmonyOS鸿蒙Next中点击登录按钮报错
import { promptAction, router } from ‘@kit.ArkUI’; import cloud, { AuthUser, VerifyCodeAction } from ‘@hw-agconnect/cloud’; import { BusinessError } from ‘@kit.BasicServicesKit’;
@Entry @Component struct Login { @State mobile:string = ‘’; @State password:string = ‘’ @State verifyCode:string = ‘’ //手机验证码 @State isEnable:boolean = true //是否发送验证码
curUser:AuthUser | null = null //用来保存当前登录用户的实体
//获取手机验证码 getVerifyCode(){ this.isEnable = false cloud.auth().requestVerifyCode({ action:VerifyCodeAction.REGISTER_LOGIN, lang:‘zh_CN’, sendInterval:60, verifyCodeType:{ phoneNumber:this.mobile, countryCode:‘86’, kind:‘phone’ } }).then(verifyCodeResult =>{ //验证码申请成功 console.log(‘hmlog–>’,‘验证码失效时间:’ +verifyCodeResult.getValidityPeriod()) promptAction.showToast({message:‘已经成功收到了验证码,请在1分钟内输入’}) this.isEnable = true }).catch((err:BusinessError) =>{ // 验证码申请失败 console.log(‘hmlog–>’,‘get VerifyCode Failed Err’ +JSON.stringify(err)) promptAction.showToast({message:‘验证码申请失败,请稍后再试’}) this.isEnable = true }) }
//手机+验证码登录
loginByVerifyCode(){ cloud.auth().signIn({ credentialInfo:{ kind:‘phone’, phoneNumber:this.mobile, countryCode:‘86’, verifyCode:this.verifyCode } }).then(user =>{ //登录成功 promptAction.showToast({message:‘用户登录成功’}) setTimeout(() =>{ router.replaceUrl({url:‘pages/Index/Index’}) },1000) }).catch((err:BusinessError) =>{ //登陆失败 console.log(‘hmlog–>’,‘Logine Failed Err’ +JSON.stringify(err)) AlertDialog.show({message:‘登陆失败’}) }) }
//手机+密码登录 loginByPassWord(){ cloud.auth().signIn({ credentialInfo:{ kind:‘phone’, phoneNumber:this.mobile, countryCode:‘86’, password:this.password } }).then(user =>{ //登录成功 promptAction.showToast({message:‘用户登录成功’}) setTimeout(() =>{ router.replaceUrl({url:‘pages/Index/Index’}) },1000) }).catch((err:BusinessError) =>{ //登陆失败 console.log(‘hmlog–>’,‘Logine Failed Err’ +JSON.stringify(err)) AlertDialog.show({message:‘登陆失败’}) }) }
build() { Column() { Text(“安安旅游”) .fontColor($r(“app.color.text_primary”)) .fontSize(32) .height(25) .fontWeight(FontWeight.Bold)
Row() {
Text("账号登录")
.fontColor($r("app.color.text_primary"))
.fontSize(24)
.fontWeight(FontWeight.Bold);
Row() {
Text("账号登录")
.fontColor($r("app.color.primary"))
.fontWeight(FontWeight.Bold);
Image($r("app.media.ic_angle"))
.width(10)
.aspectRatio(1)
.margin({ left: 5 });
}
}
.width('100%')
.justifyContent(FlexAlign.SpaceBetween)
.margin({ top: 50, bottom: 50 });
TextInput({ placeholder: "请输入手机号" })
.width('80%')
.type(InputType.Number)
.inputStyle()
.onChange(val =>{
this.mobile = val
})
TextInput({ placeholder: "请输入密码" })
.width('80%')
.type(InputType.Password)
.inputStyle()
.onChange(val =>{
this.password = val
})
Row({space:5}){
TextInput({placeholder:'手机验证码'})
.width('80%')
.type(InputType.Number)
.enabled(this.isEnable)
.backgroundColor(this.isEnable?Color.White:Color.Gray)
.onChange(val=>{
this.verifyCode = val
})
Button('获取验证码')
.width('30%')
.enabled(this.isEnable)
.backgroundColor(this.isEnable?Color.Red:Color.Gray)
.onClick(()=>{
if (this.mobile==='') {
promptAction.showToast({message:'请输入手机号'})
return
}
this.getVerifyCode()
})
}
Button('手机+验证码登录')
.width('80%')
.height(50)
.fontWeight(800)
.margin({ top: 20 })
.onClick(async()=>{
if (this.mobile === '') {
promptAction.showToast({"message":"请输入注册手机号"})
return
}
if (this.verifyCode === '') {
promptAction.showToast({"message":"请输入手机验证码"})
return
}
this.loginByVerifyCode()
})
Button('手机+密码登录')
.width('80%')
.height(50)
.fontWeight(800)
.margin({ top: 20 })
.onClick(async()=>{
if (this.mobile === '') {
promptAction.showToast({"message":"请输入注册手机号"})
return
}
if (this.password === '') {
promptAction.showToast({"message":"请输入注册密码"})
return
}
this.loginByPassWord()
})
Row() {
Text("新用户注册")
.height(20)
.margin({ top: 20 })
.fontColor($r('app.color.text_primary'))
.onClick(()=>{
router.pushUrl({ url: 'pages/Login/Register' });
});
Text("忘记密码")
.height(20)
.margin({ top: 20 })
.fontColor($r('app.color.text_primary'))
.onClick(()=>{
router.pushUrl({ url: 'pages/Login/ForgetPWD' });
});
}
.justifyContent(FlexAlign.SpaceAround)
.width('80%')
}
.width('100%')
.height('100%')
.padding({ left: 32, right: 32 })
.margin({ top: 40 });
}
@Styles inputStyle() { .backgroundColor(Color.White) .border({ width: { bottom: 1 }, color: $r(“app.color.background_divider”) }) .width(‘100%’) .height(58) .borderRadius(0) } }
<1759>GetDecorHeight: Get app window decor height failed
更多关于HarmonyOS鸿蒙Next中点击登录按钮报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,点击登录按钮报错可能涉及以下原因:
-
API调用错误:检查登录按钮绑定的API调用是否正确,确保API路径、参数及请求方法无误。
-
权限配置问题:确认应用已正确配置必要的权限,如网络访问权限,以支持登录功能。
-
数据格式不符:验证登录请求的数据格式是否符合服务器要求,包括字段名称、数据类型及编码格式。
-
网络连接问题:确保设备网络连接正常,能够访问登录接口所在的服务器。
-
服务器端异常:登录接口可能存在服务器端异常,如数据库连接失败或业务逻辑错误。
-
鸿蒙系统兼容性:确认应用适配的鸿蒙系统版本与当前设备系统版本兼容。
-
代码逻辑错误:检查登录按钮的点击事件处理逻辑,确保没有空指针或未捕获的异常。
-
资源文件缺失:确认登录功能所需的资源文件(如布局文件、图片等)已正确加载。
-
第三方依赖问题:如果登录功能依赖第三方库或服务,确认其版本兼容且配置正确。
-
日志分析:查看应用日志,获取详细的错误信息,以便定位问题根源。
根据具体错误信息,逐一排查上述可能性,可有效解决登录按钮报错问题。
在HarmonyOS鸿蒙Next中点击登录按钮报错,可能由以下原因引起:
- 网络问题:检查设备网络连接是否正常,确保可以访问服务器。
- 接口问题:确认登录接口地址和参数是否正确,服务端是否正常运行。
- 代码逻辑错误:检查登录按钮的点击事件处理逻辑,确保没有空指针或未处理异常。
- 权限问题:确保应用已获取必要的网络权限。
- 版本兼容性:确认应用与鸿蒙Next系统的版本兼容。
建议查看日志获取具体错误信息,针对性排查。