LoginInput.ets 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import { IBestToast, yTRequest } from 'basic'
  2. import { CodeInputType } from '../models'
  3. @Component
  4. export struct LoginInput {
  5. item?: CodeInputType
  6. mgBottom: number = 24
  7. @Link inputData: string
  8. @Prop phoneNumber: string
  9. code: string = ''
  10. @State time: number = 61
  11. private declare sa: number
  12. needCode: boolean = true
  13. aboutToDisappear(): void {
  14. clearInterval(this.sa)
  15. }
  16. build() {
  17. Row({ space: 8 }) {
  18. Text(this.item?.txt)
  19. .width(73)
  20. .height(31)
  21. .fontSize(12)
  22. .border({ width: { bottom: 1 }, color: $r('[basic].color.main_na_color') })
  23. .textAlign(TextAlign.Start)
  24. .fontColor(Color.Black)
  25. TextInput({ placeholder: this.item?.placeHolder, text: $$this.inputData })
  26. .layoutWeight(1)
  27. .type(InputType.PhoneNumber)
  28. .borderRadius(0)
  29. .border({ width: { bottom: 1 }, color: $r('[basic].color.main_na_color') })
  30. .textAlign(TextAlign.Start)
  31. .height(31)
  32. .placeholderFont({ size: 12 })
  33. .placeholderColor($r('[basic].color.main_na_color'))
  34. .backgroundColor(Color.Transparent)
  35. .padding(0)
  36. .caretColor($r('[basic].color.main_ac_color_dark'))
  37. if (this.needCode) {
  38. Text(this.time == 61 ? '获取验证码' : this.time + '后重新发送')
  39. .position({ right: 0 })
  40. .height(31)
  41. .fontSize(12)
  42. .fontColor($r('[basic].color.main_ac_color_dark'))
  43. .onClick(() => {
  44. const rep = new RegExp('^1(3[0-9]|4[01456879]|5[0-35-9]|6[2567]|7[0-8]|8[0-9]|9[0-35-9])\\d{8}$');
  45. if (this.phoneNumber.match(rep) && this.time == 61) {
  46. this.time = 60
  47. this.sa = setInterval(() => {
  48. this.time--
  49. if (this.time < 1) {
  50. this.time = 61
  51. clearInterval(this.sa)
  52. }
  53. }, 1000)
  54. // TODO 此处放置验证码获取逻辑
  55. yTRequest.getCaptcha(this.phoneNumber, (res) => {
  56. if (!res) {
  57. IBestToast.show({ message: '请勿重复发送验证码' })
  58. return
  59. }
  60. AppStorage.setOrCreate<string>('uuid', res)
  61. }, (err) => {
  62. IBestToast.show({ message: '验证码发送失败' })
  63. this.time = 61
  64. })
  65. // yTRequest.post<CodeType, AskCodeNum>('/api/friendcase/member/sendSmsCode',
  66. // { phonenumber: this.phoneNumber })
  67. // .then(res => {
  68. // if (!res) {
  69. // IBestToast.show({ type: 'warning', message: '请勿重复发送验证码' })
  70. // return
  71. // }
  72. //
  73. // AppStorage.setOrCreate<CodeType>('smsCode', res)
  74. // })
  75. // .catch((err: Error) => {
  76. // IBestToast.show({ type: 'fail', message: '验证码发送失败' })
  77. // this.time = 61
  78. // })
  79. } else {
  80. if (!this.phoneNumber.match(rep)) {
  81. IBestToast.show({ message: '请输入正确的手机号' })
  82. }
  83. }
  84. })
  85. .enabled(this.time == 61)
  86. }
  87. }
  88. .margin({ bottom: this.mgBottom })
  89. }
  90. }