<template>
<span class="verifycode" @click="changeCode" title="点击刷新">
<img :src="codeUrl" @error="error" v-if="codeUrl" />
<template v-else>
<em>验证码图片</em>
</template>
<i class="el-icon-loading" v-if="imgCodeLoading"></i>
</span>
</template>
<script>
import { mapActions } from 'vuex'
export default {
name: 'User',
components: {
},
model: {
prop: 'val',
event: 'update'
},
props: {
val: {
type: Boolean
}
},
data () {
return {
imgCodeLoading: false,
codeUrl: ''
}
},
computed: {
},
methods: {
...mapActions(['getImgCode']),
error (e) {
this.codeUrl = ''
},
changeCode () {
const { imgCodeLoading, getCode } = this
if (!imgCodeLoading) {
this.$emit('change')
getCode()
}
},
async getCode () {
const { getImgCode, imgCodeLoading } = this
if (!imgCodeLoading) {
this.imgCodeLoading = true
try {
const { blob } = await getImgCode()
this.codeUrl = window.URL.createObjectURL(blob)
} catch (error) {
}
this.val && this.$emit('update', false)
this.imgCodeLoading = false
}
}
},
mounted () {
this.getCode()
},
watch: {
val: {
handler (newVal) {
newVal && this.getCode()
},
immediate: true
}
}
}
</script>
<style lang="scss">
.verifycode {
img,
em {
cursor: pointer;
}
}
</style>
console