index.tsx 3.8 KB

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