From 06a8284c1c18b2af62de1600b558a32f754e84ff Mon Sep 17 00:00:00 2001 From: baiy Date: Sat, 20 Nov 2021 09:55:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=89=AA=E8=B4=B4=E6=9D=BF?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E5=92=8C=E9=83=A8=E5=88=86=E8=BE=93=E5=85=A5?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- public/tool.html | 1 - src/tool/clipboard.js | 60 +++++++++++------ src/tool/model.js | 90 ++++++++++++++++++-------- src/views/tool/ascii.vue | 2 +- src/views/tool/barcode.vue | 4 +- src/views/tool/base64.vue | 8 +-- src/views/tool/code.vue | 2 +- src/views/tool/crontab.vue | 22 +++++-- src/views/tool/decimalConvert.vue | 2 +- src/views/tool/diffs.vue | 2 +- src/views/tool/encrypt.vue | 2 +- src/views/tool/hash.vue | 2 +- src/views/tool/hexString.vue | 2 +- src/views/tool/html.vue | 2 +- src/views/tool/ip.vue | 7 +- src/views/tool/json.vue | 2 +- src/views/tool/jsonToObject.vue | 23 +++++-- src/views/tool/jwt.vue | 2 +- src/views/tool/pinyin.vue | 2 +- src/views/tool/qrCode.vue | 16 ++--- src/views/tool/randomString.vue | 2 +- src/views/tool/regex.vue | 2 +- src/views/tool/serializeConversion.vue | 2 +- src/views/tool/sign.vue | 2 +- src/views/tool/text.vue | 2 +- src/views/tool/time.vue | 2 +- src/views/tool/timestamp.vue | 31 ++++++--- src/views/tool/unicode.vue | 2 +- src/views/tool/url.vue | 2 +- src/views/tool/uuid.vue | 2 +- src/views/tool/variableConversion.vue | 2 +- src/views/tool/websocket.vue | 2 +- 32 files changed, 198 insertions(+), 108 deletions(-) diff --git a/public/tool.html b/public/tool.html index 7b081c9..6910afb 100644 --- a/public/tool.html +++ b/public/tool.html @@ -10,6 +10,5 @@
-
diff --git a/src/tool/clipboard.js b/src/tool/clipboard.js index d111d4b..f3c429c 100644 --- a/src/tool/clipboard.js +++ b/src/tool/clipboard.js @@ -1,29 +1,49 @@ -import {isUtools} from '../helper' +import {Base64} from "js-base64"; + // 剪贴板操作 -export const copy = (data,successCallback)=>{ - document.querySelector( - '#clipboard').innerHTML = '' - document.querySelector('#clipboard-text').value = data - document.querySelector('#clipboard-text').select() - if (document.execCommand('copy')) { - successCallback && successCallback() +export const copy = (data, successCallback) => { + try { + navigator.clipboard.writeText(data).then(function () { + successCallback && successCallback() + }, function (e) { + console.log('copy failed', e) + }); + } catch (e) { + console.log('copy error', e) } - document.querySelector('#clipboard').innerHTML = '' } -export const paste = ()=>{ - document.querySelector('#clipboard').innerHTML = '' - document.querySelector('#clipboard-text').select() - document.execCommand('paste') - let r = document.querySelector('#clipboard-text').value || document.querySelector('#clipboard-text').innerHTML - document.querySelector('#clipboard').innerHTML = '' - return r ? r : '' +export const paste = async () => { + return new Promise((resolve) => { + try { + navigator.clipboard.readText().then((text) => { + return resolve(text ? text : "") + }).catch((e) => { + console.error(e) + return resolve("") + }) + } catch { + resolve("") + } + }); } -export const copyImage = (imageBase64,successCallback = "")=>{ - if (isUtools && imageBase64){ - window.utools.copyImage(imageBase64) - successCallback && successCallback() +export const copyImage = (imageBase64, successCallback = "") => { + if (imageBase64) { + try { + let arr = imageBase64.split(',') + let mime = arr[0].match(/:(.*?);/)[1]; + let data = [new window.ClipboardItem({ + [mime]: new Blob([Base64.toUint8Array(arr[1])], {type: mime}) + })]; + navigator.clipboard.write(data).then(function () { + successCallback && successCallback() + }, function (e) { + console.log('copy image failed', e) + }); + } catch (e) { + console.log('copy image error', e) + } } } diff --git a/src/tool/model.js b/src/tool/model.js index e279bd8..498e45f 100644 --- a/src/tool/model.js +++ b/src/tool/model.js @@ -51,38 +51,74 @@ const debounceSaveToolDataMethod = _.debounce(function () { return history(debounceSaveToolData['tool']).push(debounceSaveToolData['data']) }, 1000) + +const appendData = async function (field = "", check = "") { + const result = (data = "") => { + if (data){ + if ( + !check + || (_.isFunction(check) && check(data)) // 函数校验 + ){ + return field ? {[field]: data} : data + } + } + return field ? {} : "" + } + return new Promise(async (resolve) => { + try { + // 使用固定输入数据 + if (fixeInputData) { + return resolve(result(fixeInputData)) + } + if (setting.autoReadCopy()) { + let paste = (await clipboard.paste()).trim() + if (paste) { + resolve(result(paste)) + } + } + resolve(result()) + } catch { + resolve(result()) + } + }); +} + export const plugin = { install: function (Vue) { - Vue.prototype.$getToolData = function (clipboardField = '',clipboardFieldRegex = "") { - let data = history(model.getCurrentTool()).current() - if (clipboardField) { - let inputData = ""; - if (fixeInputData) { // 使用固定输入数据 - inputData = fixeInputData - fixeInputData = "" - } else if (setting.autoReadCopy()) { - let paste = clipboard.paste() - if (!data[clipboardField] && paste) { - if (setting.autoReadCopyFilter()) { - paste = paste.trim() - } - inputData = paste + Vue.prototype.$initToolData = function (input = "", inputCheck = "", field = "current", isLoadHistory = true) { + let current = _.cloneDeep(this[field]) + if (isLoadHistory) { + Object.assign(current, this.$getToolData()) + } + if (!input) { + this[field] = current + return; + } + + // 初始化默认值 + if (!(input in current)){ + current[input] = ""; + } + + // 保存默认值 + let inputDefault = current[input] + current[input] = ""; + + appendData(input, inputCheck).then((append) => { + for (let key of Object.keys(append)) { + if ((key in current) && !current[key]) { + current[key] = append[key] } } - if (inputData){ - if ( - !(clipboardFieldRegex instanceof RegExp) - || - ( - clipboardFieldRegex instanceof RegExp - && clipboardFieldRegex.test(inputData) - ) - ){ - data[clipboardField] = inputData - } + if (!current[input]){ + // 使用默认值 + current[input] = inputDefault } - } - return data + this[field] = current + }) + } + Vue.prototype.$getToolData = function () { + return _.cloneDeep(history(model.getCurrentTool()).current()) } Vue.prototype.$saveToolData = function (data, ignoreDebounce = false) { if (ignoreDebounce) { diff --git a/src/views/tool/ascii.vue b/src/views/tool/ascii.vue index a8370eb..c182bf8 100644 --- a/src/views/tool/ascii.vue +++ b/src/views/tool/ascii.vue @@ -67,7 +67,7 @@ export default { }, }, created() { - this.current = Object.assign(this.current, this.$getToolData()) + this.$initToolData() }, methods: { handle() { diff --git a/src/views/tool/barcode.vue b/src/views/tool/barcode.vue index 0b70c0c..da05b4d 100644 --- a/src/views/tool/barcode.vue +++ b/src/views/tool/barcode.vue @@ -2,7 +2,7 @@
+ class="barcode" v-show="!validStr" style="cursor:pointer">

{{ validStr }}

@@ -146,7 +146,7 @@ import JsBarcode from 'jsbarcode' export default { created() { - this.current = Object.assign(this.current, this.$getToolData("text")) + this.$initToolData('text') }, computed: { showText() { diff --git a/src/views/tool/base64.vue b/src/views/tool/base64.vue index 1750980..77da74a 100644 --- a/src/views/tool/base64.vue +++ b/src/views/tool/base64.vue @@ -38,7 +38,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData('input')) + this.$initToolData('input') }, methods: { handle(v) { @@ -52,10 +52,10 @@ export default { } else{ this.current.output = Base64.decode(this.current.input) + this.$clipboardCopy(this.current.output) } } this.current.operation = v - this.$clipboardCopy(this.current.output) this.$saveToolData(this.current) } }, @@ -69,9 +69,9 @@ export default { }, exportFile() { let arr = this.current.input.split(','), mime = arr[0].match(/:(.*?);/)[1]; - let objectUrl = window.URL.createObjectURL(new Blob([new Blob([Base64.toUint8Array(arr[1])], {type: mime})], {type: mime})); + let objectUrl = window.URL.createObjectURL(new Blob([Base64.toUint8Array(arr[1])], {type: mime})); let aEle = document.createElement("a"); - aEle.download = `ctools-base64-decode-${moment().unix()}` + (mimeType.extension(mime) ? `.${mimeType.extension(mime)}` : ""); + aEle.download = `ctool-base64-decode-${moment().unix()}` + (mimeType.extension(mime) ? `.${mimeType.extension(mime)}` : ""); aEle.href = objectUrl; aEle.click(); aEle.remove(); diff --git a/src/views/tool/code.vue b/src/views/tool/code.vue index 9af3db6..251e094 100644 --- a/src/views/tool/code.vue +++ b/src/views/tool/code.vue @@ -53,7 +53,7 @@ export default { } }, created() { - this.current = Object.assign(this.current, this.$getToolData("content")) + this.$initToolData('content') }, methods: { handle(language) { diff --git a/src/views/tool/crontab.vue b/src/views/tool/crontab.vue index 868d7b7..0c84951 100644 --- a/src/views/tool/crontab.vue +++ b/src/views/tool/crontab.vue @@ -6,14 +6,15 @@ {{ $t('crontab_expression') }} - + -
+
@@ -35,6 +36,7 @@ import moment from "moment" import {getCurrentLocale} from "../../i18n"; import heightResize from "./components/heightResize"; import autoHeightTextarea from "./components/autoHeightTextarea"; + export default { components: { heightResize, @@ -63,7 +65,14 @@ export default { }, }, created() { - this.current = Object.assign(this.current, this.$getToolData()) + this.$initToolData('input', (data) => { + try { + cronstrue.toString(data) + } catch { + return false + } + return true + }) this.example.data = this.example.data.map((item) => { return { example: item, @@ -75,16 +84,15 @@ export default { conversion(input) { return cronstrue.toString(input, {locale: this.locale}) }, - resize(height){ + resize(height) { this.referenceHeight = height } }, data() { return { - referenceHeight:101, + referenceHeight: 101, current: { - input: "2 */5 * * 2-5", - operation: "check" + input: "2 */5 * * 2-5" }, special: { columns: [ diff --git a/src/views/tool/decimalConvert.vue b/src/views/tool/decimalConvert.vue index db84fd5..2f8e516 100644 --- a/src/views/tool/decimalConvert.vue +++ b/src/views/tool/decimalConvert.vue @@ -42,7 +42,7 @@ import Radix from './library/radix.js' const alphabet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@"; export default { created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, watch: { convert: function (val) { diff --git a/src/views/tool/diffs.vue b/src/views/tool/diffs.vue index 5b29ff3..0a92506 100644 --- a/src/views/tool/diffs.vue +++ b/src/views/tool/diffs.vue @@ -63,7 +63,7 @@ export default { } }, created() { - this.current = Object.assign(this.current, this.$getToolData()) + this.$initToolData() }, methods: { setLanguage(lang) { diff --git a/src/views/tool/encrypt.vue b/src/views/tool/encrypt.vue index 37b0091..d7b6b06 100644 --- a/src/views/tool/encrypt.vue +++ b/src/views/tool/encrypt.vue @@ -40,7 +40,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, methods: { handle(v) { diff --git a/src/views/tool/hash.vue b/src/views/tool/hash.vue index f65fbff..f7f2f7d 100644 --- a/src/views/tool/hash.vue +++ b/src/views/tool/hash.vue @@ -21,7 +21,7 @@ import crypto from "crypto-js" const sm = require('sm-crypto'); export default { created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, computed: { md5() { diff --git a/src/views/tool/hexString.vue b/src/views/tool/hexString.vue index 3090b8d..8872b9d 100644 --- a/src/views/tool/hexString.vue +++ b/src/views/tool/hexString.vue @@ -25,7 +25,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, methods: { handle(type) { diff --git a/src/views/tool/html.vue b/src/views/tool/html.vue index 6e00f65..4de5abd 100644 --- a/src/views/tool/html.vue +++ b/src/views/tool/html.vue @@ -22,7 +22,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, methods: { handle(v) { diff --git a/src/views/tool/ip.vue b/src/views/tool/ip.vue index a1d0f9c..025b49f 100644 --- a/src/views/tool/ip.vue +++ b/src/views/tool/ip.vue @@ -32,7 +32,12 @@ export default { heightResize }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input',(input)=>{ + return ( + /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/.test(input) + || input === "localhost" + ) + }) }, methods: { handle() { diff --git a/src/views/tool/json.vue b/src/views/tool/json.vue index 5659942..3be68ba 100644 --- a/src/views/tool/json.vue +++ b/src/views/tool/json.vue @@ -23,7 +23,7 @@ export default { heightResize }, created() { - this.current = Object.assign(this.current, this.$getToolData('content')) + this.$initToolData('content') }, methods: { handle(v) { diff --git a/src/views/tool/jsonToObject.vue b/src/views/tool/jsonToObject.vue index 483a211..2c33722 100644 --- a/src/views/tool/jsonToObject.vue +++ b/src/views/tool/jsonToObject.vue @@ -13,7 +13,8 @@
- + @@ -22,7 +23,8 @@ - + @@ -28,7 +34,10 @@ {{ $t('timestamp_output') }} @@ -57,10 +66,14 @@ export default { heightResize }, created() { - this.current = Object.assign(this.current, this.$getToolData('input')) - if (!this.current.input) { - this.current.input = moment().format('YYYY-MM-DD HH:mm:ss') - } + this.$initToolData('input', (data) => { + return ( + (new RegExp(/^\d+-\d+-\d+ \d+:\d+:\d+$/)).test(data) + || (new RegExp(/^\d+-\d+-\d+ \d+:\d+:\d+\.\d+$/)).test(data) + || (new RegExp(/^\d{10}$/)).test(data) + || (new RegExp(/^\d{13}$/)).test(data) + ) + }) }, mounted() { this.timer = setInterval(() => { @@ -127,7 +140,7 @@ export default { }, methods: { copy(data) { - if (data){ + if (data) { this.$clipboardCopy(data, true) } }, @@ -147,7 +160,7 @@ export default { data() { return { current: { - input: '' + input: moment().format('YYYY-MM-DD HH:mm:ss') }, timer: null, timestamp: 0, diff --git a/src/views/tool/unicode.vue b/src/views/tool/unicode.vue index e5ea41b..417b5ed 100644 --- a/src/views/tool/unicode.vue +++ b/src/views/tool/unicode.vue @@ -36,7 +36,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, methods: { handle(operation) { diff --git a/src/views/tool/url.vue b/src/views/tool/url.vue index dd4233c..f57d783 100644 --- a/src/views/tool/url.vue +++ b/src/views/tool/url.vue @@ -21,7 +21,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, methods: { handle(v) { diff --git a/src/views/tool/uuid.vue b/src/views/tool/uuid.vue index b680557..5a26b53 100644 --- a/src/views/tool/uuid.vue +++ b/src/views/tool/uuid.vue @@ -40,7 +40,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData()) + this.$initToolData() }, mounted() { if (this.current.result.length < 1){ diff --git a/src/views/tool/variableConversion.vue b/src/views/tool/variableConversion.vue index ac2c1ff..903d900 100644 --- a/src/views/tool/variableConversion.vue +++ b/src/views/tool/variableConversion.vue @@ -40,7 +40,7 @@ export default { } }, created() { - this.current = Object.assign(this.current, this.$getToolData("input")) + this.$initToolData('input') }, methods: { copy(type) { diff --git a/src/views/tool/websocket.vue b/src/views/tool/websocket.vue index 975f254..1d52c10 100644 --- a/src/views/tool/websocket.vue +++ b/src/views/tool/websocket.vue @@ -70,7 +70,7 @@ export default { autoHeightTextarea }, created() { - this.current = Object.assign(this.current, this.$getToolData()) + this.$initToolData() }, methods: { handle() { -- GitLab