MImgCode.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <van-popup v-model="showImgCode" class="imgCodePopup" :close-on-click-overlay="false" @close="onClose" closeable close-icon="close">
  3. <div class="imgCode">
  4. <p class="codeTitle">输入图形验证码</p>
  5. <van-row type="flex">
  6. <van-col span="14">
  7. <van-field placeholder="请输入验证码" v-model="code" style="background: #F4F4F4; padding: .12rem .16rem;" />
  8. </van-col>
  9. <van-col span="10" class="img">
  10. <van-image :src="identifyingCode" @click="updateIdentifyingCode(true)">
  11. <template v-slot:loading>
  12. <van-loading type="spinner" size="20" />
  13. </template>
  14. </van-image>
  15. </van-col>
  16. </van-row>
  17. <van-row>
  18. <van-col span="14"></van-col>
  19. <van-col span="10">
  20. <span class="imgChange" @click="updateIdentifyingCode(true)">看不清?换一换</span>
  21. </van-col>
  22. </van-row>
  23. </div>
  24. </van-popup>
  25. </template>
  26. <script>
  27. import { verifyLoginImage, sendSmsRequest } from '@/api/app'
  28. export default {
  29. name: 'imgCode',
  30. props: {
  31. value: Boolean,
  32. phone: String || Number,
  33. },
  34. data() {
  35. let origin = window.location.origin
  36. return {
  37. showImgCode: this.value,
  38. identifyingCode: origin + '/api-teacher/code/getLoginImage?phone=' + this.phone,
  39. code: null,
  40. }
  41. },
  42. async mounted() {
  43. // let res = await getLoginImage({ phone: 15907121013 })
  44. // this.img = res.data
  45. },
  46. methods: {
  47. async updateIdentifyingCode() { // 刷新token
  48. let origin = window.location.origin
  49. this.identifyingCode = `${origin}/api-teacher/code/getLoginImage?phone=${this.phone}&token=${Math.random()}`
  50. },
  51. async checkVerifyLoginImage() {
  52. try {
  53. if(this.code.length < 4) {
  54. return
  55. }
  56. await verifyLoginImage({ phone: this.phone, code: this.code })
  57. await sendSmsRequest({ mobile: this.phone })
  58. this.$toast('验证码已发送')
  59. this.$emit('input', false)
  60. this.$emit('onCodeSend', true)
  61. } catch {
  62. //
  63. this.updateIdentifyingCode()
  64. }
  65. },
  66. onClose() {
  67. this.$emit('input', false)
  68. }
  69. },
  70. watch: {
  71. code(newVal) {
  72. // 图形验证码位数大于4位时
  73. if(newVal.length >= 4) {
  74. this.checkVerifyLoginImage()
  75. }
  76. }
  77. }
  78. }
  79. </script>
  80. <style lang="less" scoped>
  81. .imgCode {
  82. padding: .16rem;
  83. .codeTitle {
  84. text-align: center;
  85. font-size: .16rem;
  86. color: #4F4F4F;
  87. padding-bottom: .16rem;
  88. }
  89. .img {
  90. display: flex;
  91. align-items: center;
  92. justify-content: center;
  93. }
  94. .imgChange {
  95. display: block;
  96. color: #AAAAAA;
  97. font-size: 0.12rem;
  98. text-align: center;
  99. padding-top: .05rem;
  100. }
  101. }
  102. .imgCodePopup {
  103. width: 90%;
  104. border-radius: .05rem;
  105. overflow: inherit;
  106. /deep/.van-popup__close-icon {
  107. top: -37px !important;
  108. right: 0 !important;
  109. font-size: .25rem;
  110. color: #fff;
  111. }
  112. }
  113. </style>