Captcha.vue 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <!--
  2. * @Author: wyd
  3. * @Date: 2024-03
  4. * @LastEditors: wyd
  5. * @LastEditTime: 2024-04
  6. * @Description: 验证码
  7. -->
  8. <script setup lang="ts">
  9. import { watch, ref, onMounted } from 'vue'
  10. import { getCaptchaImageApi } from '@/api/login'
  11. const props = defineProps({
  12. modelValue: String
  13. })
  14. const emit = defineEmits(['getCaptchaInfo', 'update:modelValue'])
  15. const form = {
  16. code: '',
  17. uuid: '',
  18. captchaEnabled: true
  19. }
  20. watch(
  21. () => props.modelValue,
  22. (val, pre) => {
  23. form.code = props.modelValue || ''
  24. }
  25. )
  26. // 更新code
  27. const codeChange = () => {
  28. emit('update:modelValue', form.code)
  29. emit('getCaptchaInfo', form)
  30. }
  31. const imgPath = ref('')
  32. // 获取验证码图片
  33. const getCaptchaImage = () => {
  34. getCaptchaImageApi({ isWeb: false }).then((res) => {
  35. form.captchaEnabled = res.data.captchaEnabled
  36. form.uuid = res.data.uuid
  37. if (res.data.captchaEnabled) {
  38. imgPath.value = 'data:image/jpeg;base64,' + res.data.img
  39. }
  40. emit('getCaptchaInfo', form)
  41. })
  42. }
  43. onMounted(() => {
  44. getCaptchaImage()
  45. })
  46. defineExpose({ getCaptchaImage })
  47. </script>
  48. <template>
  49. <view class="flex j-sb a-center">
  50. <uni-easyinput type="text" v-model="form.code" placeholder="请输入验证码" @change="codeChange" />
  51. <image :src="imgPath" mode="aspectFill" class="capcha-img ml10" @tap="getCaptchaImage" />
  52. </view>
  53. </template>
  54. <style lang="scss" scoped>
  55. .capcha-img {
  56. width: 100px;
  57. height: 35px;
  58. border-radius: 4px;
  59. }
  60. </style>