index.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. export default defineComponent({
  6. name: "imgCode",
  7. props: {
  8. value: Boolean,
  9. phone: [String, Number],
  10. onClose: {
  11. type: Function,
  12. default: () => {},
  13. },
  14. onSendCode: {
  15. type: Function,
  16. default: () => {},
  17. },
  18. },
  19. data() {
  20. let origin = window.location.origin
  21. return {
  22. showStatus: false,
  23. identifyingCode: origin + '/api-student/code/getLoginImage?phone=' + this.phone,
  24. code: null,
  25. }
  26. },
  27. mounted() {
  28. this.showStatus = this.value;
  29. },
  30. watch: {
  31. value(val: any) {
  32. this.showStatus = val
  33. },
  34. code(val: any) {
  35. if(val.length >= 4) {
  36. this.checkVerifyLoginImage();
  37. }
  38. }
  39. },
  40. methods: {
  41. async updateIdentifyingCode() { // 刷新token
  42. let origin = window.location.origin
  43. this.identifyingCode = `${origin}/api-student/code/getLoginImage?phone=${this.phone}&token=${Math.random()}`
  44. },
  45. async checkVerifyLoginImage() {
  46. try {
  47. if((this as any).code.length < 4) {
  48. return
  49. }
  50. await request('/api-student/code/verifyLoginImage', {
  51. data: {
  52. phone: this.phone,
  53. code: this.code
  54. }
  55. })
  56. await request('/api-student/code/sendSms', {
  57. data: {
  58. mobile: this.phone
  59. }
  60. })
  61. Toast('验证码已发送')
  62. this.onClose();
  63. this.onSendCode();
  64. } catch {
  65. this.updateIdentifyingCode()
  66. }
  67. },
  68. },
  69. render() {
  70. return (
  71. <Popup show={this.showStatus} class={styles.imgCodePopup} closeOnClickOverlay={false} onClose={this.onClose} closeable closeIcon="close">
  72. <div class={styles.imgCode}>
  73. <p class={styles.codeTitle}>输入图形验证码</p>
  74. <Row>
  75. <Col span="14">
  76. <Field placeholder="请输入验证码" v-model={this.code} class={styles.field} />
  77. </Col>
  78. <Col span="10" class={styles.img}>
  79. <VanImage src={this.identifyingCode} onClick={() => this.updateIdentifyingCode()}
  80. // @ts-ignore
  81. vSlots={{
  82. loading: () => <Loading type="spinner" size="20" />
  83. }} />
  84. </Col>
  85. </Row>
  86. <Row style={{ display: 'flex', justifyContent: 'end' }}>
  87. <Col span="10">
  88. <span class={styles.imgChange} onClick={() => this.updateIdentifyingCode()}>看不清?换一换</span>
  89. </Col>
  90. </Row>
  91. </div>
  92. </Popup>
  93. )
  94. }
  95. })