import router from '@system.router';
import { Input, InputType } from '@ohos.agp.components';
import { Column, Text, Button, FontWeight, Color, HorizontalAlign } from '@ohos.agp.components';
import fetch from '@ohos.fetch';
@Entry
@Component
struct RegisterPage {
@State username: string = ''
@State password: string = ''
@State phone: string = ''
@State account: string = ''
@State errorMessage: string = ''
@State isSubmitting: boolean = false
build() {
Column({ space: 50 }) {
Text('注册')
.fontSize(30)
.fontWeight(FontWeight.Bold)
if (this.errorMessage) {
Text(this.errorMessage)
.fontSize(14)
.fontColor(Color.Red)
}
Input({
placeholder: '用户名',
onInput: (value: string) => {
this.username = value
}
})
.width('80%')
.invalidStyle({ color: Color.Red })
Input({
type: InputType.Password,
placeholder: '密码(至少6位)',
onInput: (value: string) => {
this.password = value
}
})
.width('80%')
.invalidStyle({ color: Color.Red })
Input({
placeholder: '电话',
onInput: (value: string) => {
this.phone = value
}
})
.width('80%')
.invalidStyle({ color: Color.Red })
Input({
placeholder: '账号',
onInput: (value: string) => {
this.account = value
}
})
.width('80%')
.invalidStyle({ color: Color.Red })
Button('注册')
.width('80%')
.onClick(async () => {
await this.handleRegister()
})
.disabled(this.isSubmitting)
Button('返回登录')
.width('80%')
.onClick(() => {
router.back()
})
}
.width('100%')
.padding({ top: 100 })
.alignItems(HorizontalAlign.Center)
}
async handleRegister() {
try {
if (!this.validateForm()) return
this.isSubmitting = true
this.errorMessage = ''
const response = await fetch('http://localhost:3002/register', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: this.username,
password: this.password,
phone: this.phone,
account: this.account
})
})
const result = await response.json()
if (response.ok && result.success) {
router.back()
} else {
this.errorMessage = result.message || '注册失败,请检查信息'
}
} catch (error) {
console.error('注册失败:', error)
this.errorMessage = '网络错误,请稍后重试'
} finally {
this.isSubmitting = false
}
}
private validateForm(): boolean {
let isValid = true
this.errorMessage = ''
if (!this.username.trim()) {
this.showError('用户名不能为空')
isValid = false
}
if (this.password.length < 6) {
this.showError('密码至少需要6位')
isValid = false
}
if (!/^1[3-9]\d{9}$/.test(this.phone)) {
this.showError('请输入有效的手机号码')
isValid = false
}
return isValid
}
private showError(message: string) {
this.errorMessage = message
}
}
"D:\DevEco Studio\tools\node\node.exe" "D:\DevEco Studio\tools\hvigor\bin\hvigorw.js" --mode module -p module=entry@default -p product=default -p previewer.replace.page=pages/RegisterPage -p pageType=page -p compileResInc=true -p previewMode=true -p buildRoot=.preview PreviewBuild --watch --analyze=normal --parallel --incremental --daemon
> hvigor UP-TO-DATE :entry:default@PreBuild...
> hvigor UP-TO-DATE :entry:default@MergeProfile...
> hvigor UP-TO-DATE :entry:default@CreateBuildProfile...
> hvigor Finished :entry:default@PreCheckSyscap... after 1 ms
> hvigor UP-TO-DATE :entry:default@GeneratePkgContextInfo...
> hvigor UP-TO-DATE :entry:default@ProcessProfile...
> hvigor UP-TO-DATE :entry:default@ProcessRouterMap...
> hvigor Finished :entry:default@PreviewProcessResource... after 2 ms
> hvigor UP-TO-DATE :entry:default@GenerateLoaderJson...
> hvigor UP-TO-DATE :entry:default@PreviewCompileResource...
> hvigor Finished :entry:default@PreviewHookCompileResource... after 1 ms
> hvigor UP-TO-DATE :entry:default@CopyPreviewProfile...
> hvigor Finished :entry:default@ReplacePreviewerPage... after 4 ms
> hvigor Finished :entry:buildPreviewerResource... after 1 ms
> hvigor Finished :entry:default@PreviewUpdateAssets... after 5 ms
"@ohos.agp.components" is imported by "entry/src/main/ets/pages/RegisterPage.ets", but could not be resolved 鈥� treating it as an external dependency.
"@ohos.fetch" is imported by "entry/src/main/ets/pages/RegisterPage.ets", but could not be resolved 鈥� treating it as an external dependency.
> hvigor ERROR: Failed :entry:default@PreviewArkTS...
> hvigor ERROR: ArkTS:ERROR File: C:/Users/32837/DevEcoStudioProjects/MyApplication/entry/src/main/ets/pages/RegisterPage.ets:92:13
Use explicit types instead of "any", "unknown" (arkts-no-any-unknown)
ArkTS:ERROR File: C:/Users/32837/DevEcoStudioProjects/MyApplication/entry/src/main/ets/pages/RegisterPage.ets:105:13
Use explicit types instead of "any", "unknown" (arkts-no-any-unknown)
ArkTS:ERROR File: C:/Users/32837/DevEcoStudioProjects/MyApplication/entry/src/main/ets/pages/RegisterPage.ets:2:34
Cannot find module
ArkTS:ERROR File: C:/Users/32837/DevEcoStudioProjects/MyApplication/entry/src/main/ets/pages/RegisterPage.ets:3:74
Cannot find module
ArkTS:ERROR File: C:/Users/32837/DevEcoStudioProjects/MyApplication/entry/src/main/ets/pages/RegisterPage.ets:4:19
Cannot find module
ArkTS:ERROR File: C:/Users/32837/DevEcoStudioProjects/MyApplication/entry/src/main/ets/pages/RegisterPage.ets:29:7
placeholder:
onInput: (value: string) => {
this.username = value
}
})
.width(
.invalidStyle({ color: Color.Red })
type: InputType.Password,
placeholder:
onInput: (value: string) => {
this.password = value
}
})
.width(
.invalidStyle({ color: Color.Red })
placeholder:
onInput: (value: string) => {
this.phone = value
}
})
.width(
.invalidStyle({ color: Color.Red })
placeholder:
onInput: (value: string) => {
this.account = value
}
})
.width(
.invalidStyle({ color: Color.Red })
> hvigor ERROR: BUILD FAILED in 9 s 320 ms
Process finished with exit code -1