index.tsx 3.5 KB

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