UserRegister.ets
14.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
import UserCommit from '../dialog/UserCommit'
import AreaPickerDialog from '../dialog/AreaPickerDialog'
import { ValidateInputHandle } from '../utils/validateInputHandle'
import { personData } from '../api/userType'
import baseUrl from '../utils/baseUrl'
import { userRegister, queryUserNameType, queryUsername, queryIdNoType, queryIdNo } from '../api/user'
import { promptAction, router } from '@kit.ArkUI';
import { AxiosResponse } from '@ohos/axios'
import { uploadFile, uploadResult } from '../utils/uploadFile'
import { request } from '@kit.BasicServicesKit'
@Entry
@Component
struct UserRegister {
@Builder indicatorBuilder(icon: ResourceStr) {
Image(icon)
}
@State userErrorMsg: string = ''
@State idErrorMsg: string = ''
@State emailErrorMsg: string = ''
@State phoneErrorMsg: string = ''
@State userInfo: personData = {
username: '',
password: '',
personName: '',
email: '',
gender: '0',
birthDate: '选择日期',
code: '',
phone: '',
idNo: '',
personalImg: '',
idImgFront: '',
idImgBack: '',
address: '广西壮族自治区 南宁市 青秀区',
companyId: 0,
personId: 0,
userId: 0,
state: ''
}
private selectedDate: Date = new Date('2000-01-01')
@State confirmPassword: string = ''
dialogController: CustomDialogController = new CustomDialogController({
builder: UserCommit(),
autoCancel: false,
customStyle: true
})
@State pickerValue:string[] = ['广西壮族自治区','南宁市','青秀区']//省市区选中值
@State initEmail: string = this.userInfo.email
areaController: CustomDialogController = new CustomDialogController({
builder: AreaPickerDialog({
value:this.pickerValue,//首次默认选中值
onChange: (value:string[]) => {//选择改变回调
this.pickerValue = value
this.userInfo.address = this.pickerValue.join(' ')
}
}),
customStyle: true
})
async aboutToAppear() {
this.dialogController.open()
}
// 查询用户名是否被占用
queryName = async (userName: string) => {
const res: AxiosResponse<queryUserNameType> = await queryUsername(userName)
this.userErrorMsg = res.data.data == 1 ? '' : res.data.msg
}
// 查身份证是否被占用
queryId = async (id: string) => {
const res: AxiosResponse<queryIdNoType> = await queryIdNo(id)
this.idErrorMsg = res.data.total == 0 ? '' : '身份证号码已存在,请重新输入'
}
build() {
Column(){
Scroll(){
Column(){
Column(){
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('用户名')
}.width(90)
TextInput({placeholder: '4-20位,数字/字母组成', text: $$this.userInfo.username})
.backgroundColor('#fff').layoutWeight(1).showError(this.userErrorMsg).onBlur(() => {
this.queryName(this.userInfo.username)
}).onChange((value) => {
this.userErrorMsg = ValidateInputHandle(value, 'username') ? '' : '4-20位数字字母组成'
})
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('密码')
}.width(90)
TextInput({placeholder: '请输入密码', text: $$this.userInfo.password})
.backgroundColor('#fff').layoutWeight(1).type(InputType.Password)
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('确认密码')
}.width(90)
TextInput({placeholder: '请输入密码', text: $$this.confirmPassword})
.backgroundColor('#fff').layoutWeight(1).type(InputType.Password).borderWidth(0)
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('姓名')
}.width(90)
TextInput({placeholder: '请输入姓名', text: $$this.userInfo.personName})
.backgroundColor('#fff').layoutWeight(1)
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('邮箱')
}.width(90)
TextInput({placeholder: '请输入邮箱', text: $$this.userInfo.email})
.backgroundColor('#fff').layoutWeight(1).type(InputType.Email)
.showError(this.emailErrorMsg).onChange(() => {
this.emailErrorMsg = ValidateInputHandle(this.userInfo.email, 'email') ? '' : '请正确输入邮箱'
})
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('性别')
}.width(90)
Flex({ direction: FlexDirection.Row, alignItems: ItemAlign.Center }) {
Row() {
Radio({ value: '0', group: 'radioGroup',
indicatorType:RadioIndicatorType.CUSTOM,
indicatorBuilder:()=>{this.indicatorBuilder($r('app.media.man'))}
}).checked(this.userInfo.gender == '0')
.height(20)
.width(20)
.onChange(() => {
this.userInfo.gender = '0'
})
Text('男')
}
Row() {
Radio({ value: '1', group: 'radioGroup',
indicatorType:RadioIndicatorType.CUSTOM,
indicatorBuilder:()=>{this.indicatorBuilder($r('app.media.pain_woman'))}
}).checked(this.userInfo.gender == '1')
.height(20)
.width(20)
.onChange(() => {
this.userInfo.gender = '1'
})
Text('女')
}
}
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('出生日期')
}.width(90)
Text(this.userInfo.birthDate).padding({top: 8, bottom: 8, left: 16, right: 16})
.backgroundColor('#fff').layoutWeight(1).fontColor('#000').onClick(() => {
CalendarPickerDialog.show({
selected: this.selectedDate,
onAccept: (value) => {
this.userInfo.birthDate = JSON.stringify(value).slice(1, 11)
}
})
})
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('身份证号')
}.width(90)
TextInput({placeholder: '请输入身份证号', text: $$this.userInfo.idNo})
.backgroundColor('#fff').layoutWeight(1).type(InputType.Number)
.showError(this.idErrorMsg).onBlur(() => {
this.queryId(this.userInfo.idNo)
}).onChange(() => {
this.idErrorMsg = ValidateInputHandle(this.userInfo.idNo, 'idCard') ? '' : '请正确输入身份证'
})
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('手机号')
}.width(90)
TextInput({placeholder: '请输入手机号', text: $$this.userInfo.phone})
.backgroundColor('#fff').layoutWeight(1).type(InputType.PhoneNumber)
.showError(this.phoneErrorMsg).onChange(() => {
this.phoneErrorMsg = ValidateInputHandle(this.userInfo.phone, 'phone') ? '' : '请正确输入手机号'
})
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('个人照片')
}.width(90)
Row(){
Image(this.userInfo.personalImg !== '' ? baseUrl + this.userInfo.personalImg : $r('app.media.userAvatar')).width(80)
.onClick(async () => {
let that = this
let uploader = await uploadFile() as request.UploadTask
// 3. 监控上传错误
uploader.on('fail', (err) => {
console.log('上传错误--->', JSON.stringify(err))
})
// 4. 获取服务器返回来的数据
uploader.on('headerReceive',(res)=>{
let uploadInfo = JSON.parse(res['body']) as uploadResult
console.log(JSON.stringify(uploadInfo))
that.userInfo.personalImg = uploadInfo.fileName as string
})
})
}.layoutWeight(1).margin({left: 20})
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('证件正面')
}.width(90)
Row(){
Image(this.userInfo.idImgFront !== '' ? baseUrl + this.userInfo.idImgFront : $r('app.media.idCard')).width(120)
.onClick(async () => {
let that = this
let uploader = await uploadFile() as request.UploadTask
// 3. 监控上传错误
uploader.on('fail', (err) => {
console.log('上传错误--->', JSON.stringify(err))
})
// 4. 获取服务器返回来的数据
uploader.on('headerReceive',(res)=>{
let uploadInfo = JSON.parse(res['body']) as uploadResult
that.userInfo.idImgFront = uploadInfo.fileName as string
})
})
}.layoutWeight(1).margin({left: 20})
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('证件反面')
}.width(90)
Row(){
Image(this.userInfo.idImgBack !== '' ? baseUrl + this.userInfo.idImgBack : $r('app.media.unIdCard')).width(120)
.onClick(async () => {
let that = this
let uploader = await uploadFile() as request.UploadTask
// 3. 监控上传错误
uploader.on('fail', (err) => {
console.log('上传错误--->', JSON.stringify(err))
})
// 4. 获取服务器返回来的数据
uploader.on('headerReceive',(res)=>{
let uploadInfo = JSON.parse(res['body']) as uploadResult
that.userInfo.idImgBack = uploadInfo.fileName as string
})
})
}.layoutWeight(1).margin({left: 20})
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
Row(){
Row(){
Image($r('app.media.require')).width(20)
Text('地址')
}.width(70)
Text(this.pickerValue.join(' ')).layoutWeight(1).padding({top: 8, bottom: 8, left: 16, right: 16})
.backgroundColor('#fff').fontColor('#999').textOverflow({overflow: TextOverflow.Ellipsis}).maxLines(2)
.onClick(() => {
this.areaController?.open()
})
}.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
}.backgroundColor('#fff').width('100%').padding({left: 5, right: 5}).borderRadius(10)
}.padding(10).margin({bottom: 20, top: 10})
}.layoutWeight(1).scrollBar(BarState.Off)
Row({ space: 10}){
Text('提交').borderRadius(5).layoutWeight(1).height(30).fontColor('#fff')
.backgroundColor('#1B65FD').fontSize(14).textAlign(TextAlign.Center)
.onClick(async () => {
if(this.confirmPassword !== this.userInfo.password) {
return promptAction.showToast({message: '两次密码输入不一致'})
}
if(this.userInfo?.personName == '') {
return promptAction.showToast({message: '姓名不能为空'})
} else if(this.userInfo?.email == '') {
return promptAction.showToast({message: '邮箱不能为空'})
} else if(this.userInfo?.idNo == '') {
return promptAction.showToast({message: '身份证不能为空'})
} else if(this.userInfo?.phone == '') {
return promptAction.showToast({message: '手机号不能为空'})
} else if(this.userInfo.idImgFront == '') {
return promptAction.showToast({message: '身份证件不能为空'})
} else if(this.userInfo.idImgBack == '') {
return promptAction.showToast({message: '身份证件不能为空'})
} else if(this.userInfo?.personalImg == '') {
return promptAction.showToast({message: '请上传头像'})
}
await userRegister(this.userInfo)
promptAction.showToast({
message: '注册成功',
duration: 2000
})
router.back()
})
}.width('100%').height(40).backgroundColor('#fff').padding({left: 10, right: 10})
}.width('100%').height('100%').backgroundColor('#f2f3f7')
}
}