index.tsx 3.1 KB

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