captcha.vue 919 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <template>
  2. <div class="common-captcha" @click="refresh">
  3. <div class="svg" v-html="svg" v-if="svg"></div>
  4. <img class="base64" :src="base64" alt="" v-else />
  5. </div>
  6. </template>
  7. <script>
  8. export default {
  9. data() {
  10. return {
  11. svg: '',
  12. base64: ''
  13. };
  14. },
  15. mounted() {
  16. this.refresh();
  17. },
  18. methods: {
  19. refresh() {
  20. this.$service.common
  21. .captcha({
  22. height: 36,
  23. width: 110
  24. })
  25. .then(({ captchaId, data }) => {
  26. if (data.includes('data:image/png;base64,')) {
  27. this.base64 = data;
  28. } else {
  29. this.svg = data;
  30. }
  31. this.$emit('input', captchaId);
  32. this.$emit('change', { base64: this.base64, svg: this.svg, captchaId });
  33. })
  34. .catch(err => {
  35. this.$message.error(err);
  36. });
  37. }
  38. }
  39. };
  40. </script>
  41. <style lang="stylus" scoped>
  42. .common-captcha {
  43. height: 36px;
  44. cursor: pointer;
  45. .svg {
  46. height: 100%;
  47. }
  48. .base64 {
  49. height: 100%;
  50. }
  51. }
  52. </style>