index.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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: 'o-img-code',
  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. const origin = window.location.origin
  23. const suffix = state.platformApi
  24. return {
  25. isSuffix: suffix,
  26. showStatus: false,
  27. identifyingCode: origin + suffix + '/code/getImageCode?phone=' + this.phone,
  28. code: null
  29. }
  30. },
  31. mounted() {
  32. this.showStatus = this.value
  33. },
  34. watch: {
  35. value(val: any) {
  36. this.showStatus = val
  37. },
  38. code(val: any) {
  39. if (val.length >= 4) {
  40. this.checkVerifyLoginImage()
  41. }
  42. }
  43. },
  44. methods: {
  45. async updateIdentifyingCode() {
  46. // 刷新token
  47. const origin = window.location.origin
  48. this.identifyingCode = `${origin}${this.isSuffix}/code/getImageCode?phone=${
  49. this.phone
  50. }&token=${Math.random()}`
  51. },
  52. async checkVerifyLoginImage() {
  53. try {
  54. if ((this as any).code.length < 4) {
  55. return
  56. }
  57. await request.post(`${this.isSuffix}/code/verifyImageCode`, {
  58. requestType: 'form',
  59. data: {
  60. phone: this.phone,
  61. code: this.code
  62. }
  63. })
  64. await request.post(`${this.isSuffix}/code/sendSmsCode`, {
  65. requestType: 'form',
  66. data: {
  67. mobile: this.phone,
  68. type: 'LOGIN'
  69. }
  70. })
  71. Toast('验证码已发送')
  72. this.onClose()
  73. this.onSendCode()
  74. } catch {
  75. this.updateIdentifyingCode()
  76. }
  77. }
  78. },
  79. render() {
  80. return (
  81. <Popup
  82. show={this.showStatus}
  83. class={styles.imgCodePopup}
  84. closeOnClickOverlay={false}
  85. onClose={() => {
  86. this.onClose()
  87. }}
  88. closeable
  89. closeIcon="close"
  90. >
  91. <div class={styles.imgCode}>
  92. <p class={styles.codeTitle}>输入图形验证码</p>
  93. <Row>
  94. <Col span="14">
  95. <Field placeholder="请输入验证码" v-model={this.code} class={styles.field} />
  96. </Col>
  97. <Col span="10" class={styles.img}>
  98. <VanImage src={this.identifyingCode} onClick={() => this.updateIdentifyingCode()}>
  99. {{ loading: () => <Loading type="spinner" size="20" /> }}
  100. </VanImage>
  101. </Col>
  102. </Row>
  103. <Row style={{ display: 'flex', justifyContent: 'end' }}>
  104. <Col span="10">
  105. <span class={styles.imgChange} onClick={() => this.updateIdentifyingCode()}>
  106. 看不清?换一换
  107. </span>
  108. </Col>
  109. </Row>
  110. </div>
  111. </Popup>
  112. )
  113. }
  114. })