code-input-popup.vue 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <template>
  2. <mx-popup-template ref="popup" title="请输入图形验证码" container-class="p-50" mode="bottom" left="" right="确定"
  3. closeable @right="handleConfirm">
  4. <view class="fx-col fx-cen-cen bg-white gap-20">
  5. <view class="fx-row gap-20">
  6. <uv-image :src="img" height="50" width="auto" mode="heightFix"/>
  7. <uv-icon name="reload" size="24" @click="loadCodeImage(true)"/>
  8. </view>
  9. <view>
  10. <uv-input v-model="code" placeholder="请输入上图结果" custom-style="height:32px"/>
  11. </view>
  12. </view>
  13. </mx-popup-template>
  14. </template>
  15. <script setup>
  16. import {ref, nextTick} from 'vue'
  17. import {getCodeImg} from "@/api/login";
  18. import {toast} from "@/uni_modules/uv-ui-tools/libs/function";
  19. import {toValue} from "@vueuse/core";
  20. const img = ref('')
  21. const uuid = ref('')
  22. const code = ref('')
  23. const popup = ref(null)
  24. const emits = defineEmits(['complete'])
  25. const loadCodeImage = async function (force) {
  26. if (!force && uuid.value && img.value) return
  27. const res = await getCodeImg()
  28. uuid.value = res.uuid
  29. img.value = "data:image/gif;base64," + res.img
  30. }
  31. const reset = function () {
  32. uuid.value = ''
  33. img.value = ''
  34. code.value = ''
  35. }
  36. const handleConfirm = function () {
  37. if (!code.value) {
  38. return toast('请输入验证码')
  39. }
  40. close()
  41. const data = {code: toValue(code), uuid: toValue(uuid)}
  42. emits('complete', data)
  43. nextTick(() => reset())
  44. }
  45. const open = function () {
  46. loadCodeImage()
  47. popup.value.open()
  48. }
  49. const close = function () {
  50. popup.value.close()
  51. }
  52. defineExpose({open, close})
  53. </script>
  54. <style scoped>
  55. </style>