index.tsx 3.6 KB

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