From fff859c8582555a81a432dba0be1757557d81125 Mon Sep 17 00:00:00 2001 From: X Date: Wed, 9 Dec 2020 16:00:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/clipboard.js | 32 +++++++++++++++++++++++++++++ src/utils/data.js | 19 +++++++++++++++++ src/utils/time.js | 29 ++++++++++++++++++++++++++ src/utils/validate.js | 46 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 src/utils/clipboard.js create mode 100644 src/utils/data.js create mode 100644 src/utils/time.js create mode 100644 src/utils/validate.js diff --git a/src/utils/clipboard.js b/src/utils/clipboard.js new file mode 100644 index 0000000..e2c7a56 --- /dev/null +++ b/src/utils/clipboard.js @@ -0,0 +1,32 @@ +import Vue from 'vue' +import Clipboard from 'clipboard' + +function clipboardSuccess() { + Vue.prototype.$message({ + message: 'Copy successfully', + type: 'success', + duration: 1500 + }) +} + +function clipboardError() { + Vue.prototype.$message({ + message: 'Copy failed', + type: 'error' + }) +} + +export default function handleClipboard(text, event) { + const clipboard = new Clipboard(event.target, { + text: () => text + }) + clipboard.on('success', () => { + clipboardSuccess() + clipboard.destroy() + }) + clipboard.on('error', () => { + clipboardError() + clipboard.destroy() + }) + clipboard.onClick(event) +} diff --git a/src/utils/data.js b/src/utils/data.js new file mode 100644 index 0000000..92d4fe4 --- /dev/null +++ b/src/utils/data.js @@ -0,0 +1,19 @@ +/** + * post写法get传参 + * @param obj + * @returns {string} + */ +export function postConvertGet(obj) { + let params = '' + if (obj === undefined) { + return params + } else if (JSON.stringify(obj) === '{}') { + return params + } else { + for (const [key, value] of Object.entries(obj)) { + params += `${key}=${value}&` + } + params = `?${params.slice(0, -1)}` + return params + } +} \ No newline at end of file diff --git a/src/utils/time.js b/src/utils/time.js new file mode 100644 index 0000000..492f9eb --- /dev/null +++ b/src/utils/time.js @@ -0,0 +1,29 @@ + +/** + * 时间戳格式化 + * @param date + * @returns {string} + */ +export function timestamp2std(date) { + const addZero = num => num > 9 ? num : ('0' + num) + return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${addZero(date.getHours())}:${addZero(date.getMinutes())}:${addZero(date.getSeconds())}` +} + +/** + * 时间戳格式化 + * @param strtime + * @returns {number} + */ +export function Time2MSec(strtime) { + return (new Date(strtime)).getTime(); +} + +/** + * 时间戳格式化 + * @param t + * @returns {number} + */ +export function Time2Sec(t) { + return Date.parse(new Date(t)) / 1000; +} + diff --git a/src/utils/validate.js b/src/utils/validate.js new file mode 100644 index 0000000..3585bbf --- /dev/null +++ b/src/utils/validate.js @@ -0,0 +1,46 @@ +/*过滤数据*/ + + +export function validPhone(str) { + return /^1[3456789]\d{9}$/.test(str) +} + +/** + * @param {string} url + * @returns {Boolean} + */ +export function validURL(url) { + const reg = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/ + return reg.test(url) +} + +/** + * @param {string} email + * @returns {Boolean} + */ +export function validEmail(email) { + const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ + return reg.test(email) +} + +/** + * @param {string} str + * @returns {Boolean} + */ +export function isString(str) { + if (typeof str === 'string' || str instanceof String) { + return true + } + return false +} + +/** + * @param {Array} arg + * @returns {Boolean} + */ +export function isArray(arg) { + if (typeof Array.isArray === 'undefined') { + return Object.prototype.toString.call(arg) === '[object Array]' + } + return Array.isArray(arg) +}