qrCode.vue 8.2 KB
Newer Older
B
baiy 已提交
1
<template>
B
baiy 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  <div>
    <Tabs v-model="current.operation">
      <TabPane label="二维码生成" name="generate">
        <Row :gutter="16">
          <Col span="14">
            <Input v-model="current.generateInput" :rows="14" type="textarea" placeholder="内容"></Input>
            <option-block>
              <FormItem>
                <Button type="primary" @click="generate()">生成</Button>
              </FormItem>
              <FormItem v-if="generateHistory.length() > 0">
                <Dropdown placement="top-start" trigger="click" style="margin-left: 10px" @on-click="history">
                  <a href="javascript:void(0)">
                    历史记录
                    <Icon type="ios-arrow-down"></Icon>
                  </a>
                  <DropdownMenu slot="list">
                    <DropdownItem v-for="(v,i) in generateHistory.lists()" :key="i" :name="i">{{substr(v.input)}}</DropdownItem>
                    <DropdownItem divided name="clear">清空历史记录</DropdownItem>
                  </DropdownMenu>
                </Dropdown>
              </FormItem>
              <FormItem>
                <Checkbox v-model="current.generateIsShort">生成短连接</Checkbox>
              </FormItem>
              <FormItem v-if="current.generateIsShort">
                <Alert>短链接API由 t.cn 提供</Alert>
              </FormItem>
            </option-block>
          </Col>
          <Col span="10">
            <div style="text-align: center" v-html="current.generateOutput"></div>
            <p style="text-align: center" v-if="current.generateIsShort && current.generateShortUrl">
              短连接:{{current.generateShortUrl}}</p>
          </Col>
        </Row>
      </TabPane>
      <TabPane label="二维码解析" name="reader">
        <Row :gutter="16">
          <Col span="14">
            <Input v-model="current.readerInput" :rows="5" type="textarea" placeholder="请输入二维码图片地址或点击下方按钮上传图片"></Input>
            <option-block>
              <FormItem>
                <Button type="primary" @click="reader()">解析</Button>
              </FormItem>
              <FormItem>
                <Upload action="#" :before-upload="handleUpload">
                  <Button icon="ios-cloud-upload-outline">上传图片</Button>
                </Upload>
              </FormItem>
            </option-block>
            <Input v-model="current.readerOutput" :rows="5" type="textarea" placeholder="解析结果"></Input>
          </Col>
          <Col span="10" style="text-align: center" v-html="readerInputImg"></Col>
        </Row>
      </TabPane>
    </Tabs>
  </div>
B
baiy 已提交
60 61
</template>
<script>
B
baiy 已提交
62 63 64 65 66 67 68 69 70 71 72 73
    import generator from 'qrcode'
    import qrcodeParser from 'qrcode-parser'
    import request from 'ajax-request'
    import cache from '../../tool/cache'
    import isUrl from 'is-url'
    import { trim } from '../../helper'

    const generateHistoryCacheName = 'qrCodeGenerateHistoryCacheName'
    const generateHistoryMaxLength = 15

    class generateHistory {
        data = []
B
baiy 已提交
74

B
baiy 已提交
75 76 77 78 79 80 81 82 83
        constructor () {
            this.data = cache.get(generateHistoryCacheName, [])
        }

        find (value) {
            for (let i = 0; i < this.data.length; i++) {
                if (this.data[i].input === value.input && this.data[i].isShort === value.isShort) {
                    return true
                }
84
            }
B
baiy 已提交
85
            return false
B
baiy 已提交
86
        }
B
baiy 已提交
87 88 89 90 91 92 93 94 95 96

        push (value) {
            if (this.find(value)) {
                return
            }
            if (this.data.length > generateHistoryMaxLength) {
                this.data.pop()
            }
            this.data.push(value)
            cache.set(generateHistoryCacheName, this.data, 86400)
B
baiy 已提交
97
        }
B
baiy 已提交
98 99 100

        getValue (index) {
            return this.data[index]
B
baiy 已提交
101
        }
B
baiy 已提交
102

B
baiy 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
        lists () {
            return this.data
        }

        length () {
            return this.data.length
        }

        clear () {
            this.data = []
            cache.remove(generateHistoryCacheName)
        }
    }

    export default {
        computed: {
            readerInputImg () {
                if (this.current.readerInput) {
                    return `<img style="width:300px" src="${this.current.readerInput}" />`
                }
                return ''
            },
B
baiy 已提交
125
        },
B
baiy 已提交
126 127 128
        created () {
            this.generateHistory = new generateHistory()
            this.current = Object.assign(this.current, this.$getToolData())
B
baiy 已提交
129
        },
B
baiy 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        methods: {
            history (index) {
                if (index === 'clear') {
                    this.generateHistory.clear()
                    return
                }
                let history = this.generateHistory.getValue(index)
                this.current.generateInput = history.input
                this.current.generateIsShort = history.isShort
                this.generate(false)
            },
            generate (insertHistory = true) {
                if (!this.current.generateInput) return
                if (this.current.generateIsShort) {
                    if (!isUrl(this.current.generateInput)) {
                        return this.$Message.error('生成短连接的内容是url')
B
baiy 已提交
146
                    }
B
baiy 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
                    request({
                        url: 'http://api.t.sina.com.cn/short_url/shorten.json',
                        data: { 'source': '2815391962', 'url_long': this.current.generateInput },
                    }, (err, res, result) => {
                        if (err) return this.$Message.error('二维码短连接生成错误:' + err)
                        result = JSON.parse(result)
                        if (!result[0]['url_short']) {
                            return this.$Message.error('短连接生成错误')
                        } else {
                            this.current.generateShortUrl = result[0]['url_short']
                            this.generateHandle(this.current.generateShortUrl)
                        }
                    })
                } else {
                    this.current.generateShortUrl = ''
                    this.generateHandle(this.current.generateInput)
                }
                if (insertHistory) {
                    this.generateHistory.push({
                        input: this.current.generateInput,
                        isShort: this.current.generateIsShort,
                    })
                }
                this.$saveToolData(this.current)
            },
            reader () {
                if (!this.current.readerInput) {
                    return
                }
                qrcodeParser(this.current.readerInput).then((c) => {
                    this.current.readerOutput = c.data
                    this.$saveToolData(this.current)
                    this.$Message.success('解析成功')
                }).catch(() => {
                    return this.$Message.error('图片解析错误')
                })
            },
            generateHandle (str) {
                generator.toDataURL(str, (error, url) => {
                    if (error) return this.$Message.error('二维码生成错误:' + error)
                    this.$Message.success('生成成功')
                    this.current.generateOutput = `<img style="width:300px" src="${url}" />`
                })
            },
            handleUpload (file) {
                let r = new FileReader()
                r.readAsDataURL(file)
                r.onloadend = () => {
                    this.current.readerInput = r.result
                    this.reader()
                }
                return false
            },
            substr (str) {
                str = trim(str.replace(/[\r\n]/g, ''))
                const strLength = 100
                return str.length > strLength ? str.substr(0, strLength) + '...' : str
            },
B
baiy 已提交
205
        },
B
baiy 已提交
206 207 208 209 210 211 212 213 214 215 216 217
        data () {
            return {
                generateHistory: {},
                current: {
                    generateInput: '',
                    generateOutput: '',
                    generateIsShort: false,
                    generateShortUrl: '',
                    readerInput: '',
                    readerOutput: '',
                    operation: 'generate',
                },
B
baiy 已提交
218
            }
B
baiy 已提交
219 220 221
        },
    }
</script>