index.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { defineComponent } from 'vue'
  2. import { Col, Popup, Row, Image as VanImage, Loading, Field, Toast } from 'vant'
  3. import styles from './index.module.less'
  4. import request from '@/helpers/request'
  5. import { state } from '@/state'
  6. export default defineComponent({
  7. name: 'imgCode',
  8. props: {
  9. value: Boolean,
  10. phone: [String, Number],
  11. onClose: {
  12. type: Function,
  13. // (...args: any[]) => any) | undefined
  14. default: () => {}
  15. },
  16. onSendCode: {
  17. type: Function,
  18. default: () => {}
  19. }
  20. },
  21. data() {
  22. let origin = window.location.origin
  23. let suffix =
  24. state.platformType === 'STUDENT' ? '/api-student' : '/api-teacher'
  25. return {
  26. isSuffix: suffix,
  27. showStatus: false,
  28. identifyingCode:
  29. origin + suffix + '/code/getImageCode?phone=' + this.phone,
  30. code: null
  31. }
  32. },
  33. mounted() {
  34. this.showStatus = this.value
  35. },
  36. watch: {
  37. value(val: any) {
  38. this.showStatus = val
  39. },
  40. code(val: any) {
  41. if (val.length >= 4) {
  42. this.checkVerifyLoginImage()
  43. }
  44. }
  45. },
  46. methods: {
  47. async updateIdentifyingCode() {
  48. // 刷新token
  49. let origin = window.location.origin
  50. this.identifyingCode = `${origin}${
  51. this.isSuffix
  52. }/code/getImageCode?phone=${this.phone}&token=${Math.random()}`
  53. },
  54. async checkVerifyLoginImage() {
  55. try {
  56. if ((this as any).code.length < 4) {
  57. return
  58. }
  59. await request.post(`${this.isSuffix}/code/verifyImageCode`, {
  60. requestType: 'form',
  61. data: {
  62. phone: this.phone,
  63. code: this.code
  64. }
  65. })
  66. await request.post(`${this.isSuffix}/code/sendSmsCode`, {
  67. requestType: 'form',
  68. data: {
  69. mobile: this.phone,
  70. type: 'LOGIN'
  71. }
  72. })
  73. Toast('验证码已发送')
  74. this.onClose()
  75. this.onSendCode()
  76. } catch {
  77. this.updateIdentifyingCode()
  78. }
  79. }
  80. },
  81. render() {
  82. return (
  83. // @ts-ignore
  84. <Popup
  85. show={this.showStatus}
  86. class={styles.imgCodePopup}
  87. closeOnClickOverlay={false}
  88. onClose={() => {
  89. this.onClose()
  90. }}
  91. closeable
  92. closeIcon="close"
  93. >
  94. <div class={styles.imgCode}>
  95. <p class={styles.codeTitle}>输入图形验证码</p>
  96. <Row>
  97. <Col span="14">
  98. <Field
  99. placeholder="请输入验证码"
  100. v-model={this.code}
  101. class={styles.field}
  102. maxlength="4"
  103. />
  104. </Col>
  105. <Col span="10" class={styles.img}>
  106. <VanImage
  107. src={this.identifyingCode}
  108. onClick={() => this.updateIdentifyingCode()}
  109. // @ts-ignore
  110. vSlots={{
  111. loading: () => <Loading type="spinner" size="20" />
  112. }}
  113. />
  114. </Col>
  115. </Row>
  116. <Row style={{ display: 'flex', justifyContent: 'end' }}>
  117. <Col span="10">
  118. <span
  119. class={styles.imgChange}
  120. onClick={() => this.updateIdentifyingCode()}
  121. >
  122. 看不清?换一换
  123. </span>
  124. </Col>
  125. </Row>
  126. </div>
  127. </Popup>
  128. )
  129. }
  130. })