base64.vue 3.7 KB
Newer Older
B
baiy 已提交
1
<template>
B
baiy 已提交
2 3 4 5 6 7 8 9
    <heightResize ignore :append="['.page-option-block']" @resize="resize">
        <input-block bottom="0px" right="10px">
            <autoHeightTextarea :height="height1" v-model="current.input" :placeholder="$t('base64_input')" />
            <Upload slot="extra" action="#" :before-upload="handleUpload">
                <Button size="small" type="primary" icon="ios-cloud-upload-outline">{{ $t('base64_upload_file') }}</Button>
            </Upload>
        </input-block>
        <option-block class="page-option-block">
B
baiy 已提交
10 11
            <FormItem>
                <ButtonGroup>
B
baiy 已提交
12 13
                    <Button type="primary" @click="handle('encode')">{{ $t('base64_encode') }}</Button>
                    <Button type="primary" @click="handle('decode')">{{ $t('base64_decode') }}</Button>
B
baiy 已提交
14 15
                </ButtonGroup>
            </FormItem>
B
baiy 已提交
16
            <FormItem>
B
baiy 已提交
17
                <Checkbox v-model="current.isUriSafe">{{ $t('base64_url_safe') }}</Checkbox>
B
baiy 已提交
18
            </FormItem>
B
baiy 已提交
19
        </option-block>
B
baiy 已提交
20 21 22 23 24 25
        <input-block right="10px">
            <pasteClipboardFlie @on-paste-file="handleUpload">
                <autoHeightTextarea :height="height2" :value="current.output" :placeholder="$t('base64_output')" />
            </pasteClipboardFlie>
        </input-block>
    </heightResize>
B
baiy 已提交
26 27
</template>
<script>
B
baiy 已提交
28 29 30
import {Base64} from 'js-base64'
import mimeType from 'mime-types'
import moment from "moment";
B
baiy 已提交
31 32 33
import pasteClipboardFlie from "./components/pasteClipboardFlie";
import heightResize from "./components/heightResize";
import autoHeightTextarea from "./components/autoHeightTextarea";
B
baiy 已提交
34
export default {
B
baiy 已提交
35 36 37 38 39
    components: {
        pasteClipboardFlie,
        heightResize,
        autoHeightTextarea
    },
B
baiy 已提交
40
    created() {
B
baiy 已提交
41
        this.$initToolData('input')
B
baiy 已提交
42 43
    },
    methods: {
B
baiy 已提交
44
        handle(v) {
B
baiy 已提交
45
            if (this.current.input) {
B
baiy 已提交
46 47 48
                if (v === "encode") {
                    this.current.output = Base64.encode(this.current.input, this.current.isUriSafe)
                } else {
B
baiy 已提交
49
                    if (this.current.input.trim().indexOf('data:') === 0) {
B
baiy 已提交
50 51 52 53 54
                        // 文件 base64 内容
                        this.exportFile();
                    }
                    else{
                        this.current.output = Base64.decode(this.current.input)
B
baiy 已提交
55
                        this.$clipboardCopy(this.current.output)
B
baiy 已提交
56
                    }
B
baiy 已提交
57
                }
B
baiy 已提交
58 59
                this.current.operation = v
                this.$saveToolData(this.current)
B
baiy 已提交
60 61
            }
        },
B
baiy 已提交
62 63 64 65 66 67 68 69 70 71
        handleUpload(file) {
            let r = new FileReader()
            r.readAsDataURL(file)
            r.onloadend = () => {
                this.current.output = r.result
            }
            return false
        },
        exportFile() {
            let arr = this.current.input.split(','), mime = arr[0].match(/:(.*?);/)[1];
B
baiy 已提交
72
            let objectUrl = window.URL.createObjectURL(new Blob([Base64.toUint8Array(arr[1])], {type: mime}));
B
baiy 已提交
73
            let aEle = document.createElement("a");
B
baiy 已提交
74
            aEle.download = `ctool-base64-decode-${moment().unix()}` + (mimeType.extension(mime) ? `.${mimeType.extension(mime)}` : "");
B
baiy 已提交
75 76 77 78
            aEle.href = objectUrl;
            aEle.click();
            aEle.remove();
            window.URL.revokeObjectURL(objectUrl);
B
baiy 已提交
79 80 81 82
        },
        resize(height){
            this.height1 = Math.min(160,Math.ceil(height/2))
            this.height2 = height - this.height1
B
baiy 已提交
83
        }
B
baiy 已提交
84
    },
B
baiy 已提交
85
    data() {
B
baiy 已提交
86 87 88 89 90 91 92
        return {
            current: {
                input: '',
                output: '',
                operation: '',
                isUriSafe: false,
            },
B
baiy 已提交
93 94
            height1:100,
            height2:100
B
baiy 已提交
95 96 97
        }
    },
}
B
baiy 已提交
98
</script>