index.tsx 3.6 KB

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