updatePasswordDialog.ets 2.8 KB
import { promptAction } from '@kit.ArkUI'
import { updatePassword } from '../api/user'
@CustomDialog
export default struct updatePasswordDialog{
  controller: CustomDialogController = new CustomDialogController({builder: ''})
  @State oldPassword: string = ''
  @State newPassword: string = ''
  @State requirePassword: string = ''
  build() {
    Column(){
      Row(){
        Row(){
          Image($r('app.media.require')).width(20)
          Text('旧密码')
        }.width(90)
        TextInput({placeholder: '请输入旧密码', text: $$this.oldPassword})
          .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.newPassword})
          .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.requirePassword})
          .backgroundColor('#fff').layoutWeight(1).type(InputType.Password)
      }.border({width: {bottom: 1}, color: '#eee'}).padding({top: 10, bottom: 10})
      Row({space: 10}){
        Text('取消').layoutWeight(1).textAlign(TextAlign.Center).onClick(() => {
          this.controller.close()
        })
        Text('确认').layoutWeight(1).fontColor('#1890ff').textAlign(TextAlign.Center)
          .onClick(async () => {
            if(this.oldPassword == '' && this.newPassword == '' && this.requirePassword == '') {
              promptAction.showToast({
                message: '请正确填写内容',
                duration: 2000
              })
            } else if(this.newPassword == this.oldPassword) {
              promptAction.showToast({
                message: '新密码和旧密码不能相同',
                duration: 2000
              })
            } else if(this.newPassword !== this.requirePassword) {
              promptAction.showToast({
                message: '两次密码输入不同',
                duration: 2000
              })
            } else {
              await updatePassword(this.oldPassword, this.newPassword)
              promptAction.showToast({
                message: '修改成功',
                duration: 2000
              })
              this.controller.close()
            }
          })
      }.width('100%').padding({top: 10, bottom: 10})
    }.backgroundColor('#fff').width('100%')
    .padding({top: 20,left: 13, right: 13})
  }
}