vue專案封裝的簡易方法
阿新 • • 發佈:2022-05-30
// 日期格式化 export function parseTime(time: string, pattern: string) { if (arguments.length === 0 || !time) { return null } const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}' let date if (typeof time === 'object') { date = time } else { if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) { time// 日期格式化 export function parseTime(time: string, pattern: string) { if (arguments.length === 0 || !time) { return null } const format = pattern || '{y}-{m}-{d} {h}:{i}:{s}' let date if (typeof time === 'object') { date = time } else { if ((typeof time === 'string') && (/^[0-9]+$/.test(time))) { time = parseInt(time) } else if (typeof time === 'string') { time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), ''); } if ((typeof time === 'number') && (time.toString().length === 10)) { time = time * 1000 } date = new Date(time) } const formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { let value = formatObj[key] // Note: getDay() returns 0 on Sunday if (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] } if (result.length > 0 && value < 10) { value = '0' + value } return value || 0 }) return time_str }= parseInt(time) } else if (typeof time === 'string') { time = time.replace(new RegExp(/-/gm), '/').replace('T', ' ').replace(new RegExp(/\.[\d]{3}/gm), ''); } if ((typeof time === 'number') && (time.toString().length === 10)) { time = time * 1000 } date = new Date(time) } const formatObj= { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() } const time_str = format.replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { let value = formatObj[key] // Note: getDay() returns 0 on Sundayif (key === 'a') { return ['日', '一', '二', '三', '四', '五', '六'][value] } if (result.length > 0 && value < 10) { value = '0' + value } return value || 0 }) return time_str } /** * 去除 陣列中的空字串 */ export function trimSpace(array: any) { for (var i = 0; i < array.length; i++) { if (array[i] == " " || array[i] == "" || array[i] == null || typeof (array[i]) == "undefined") { array.splice(i, 1); i = i - 1; } } return array; } /** * 資料非空驗證 * @desc 驗證資料是否為 null undefined [] {} '' * @param data * @return {boolean} */ export function isEmpty(data: any) { if (data === null) return true if (data === undefined) return true if (Object.prototype.toString.call(data) === '[object Array]') return data.length === 0 if (Object.prototype.toString.call(data) === '[object Object]') return Object.keys(data).length === 0 if (typeof data === 'string') return data.trim() === '' return false } /** * 過濾空欄位 */ export const getNewParmes = (parme: any, ignoreArr: Array<any>) => { let parmes = parme let tempValue = { ...parme } if (parmes) { let keys = Object.keys(parmes); for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (!ignoreArr.includes(key)) { const value = parmes[key]; if (isEmpty(value)) { delete parmes[key]; } } } } tempValue = { ...parmes } if (isEmpty(tempValue)) { tempValue = {} } return tempValue; } /** * js計算中的科學計數法 */ export const toolNumber = (num_str: any) => { num_str = num_str.toString(); if (num_str.indexOf("+") != -1) { num_str = num_str.replace("+", ""); } if (num_str.indexOf("E") != -1 || num_str.indexOf("e") != -1) { var resValue = "", power: number = 0, result = null, dotIndex = 0, resArr: any = [], sym = ""; var numStr = num_str.toString(); if (numStr[0] == "-") { // 如果為負數,轉成正數處理,先去掉‘-’號,並儲存‘-’. numStr = numStr.substr(1); sym = "-"; } if (numStr.indexOf("E") != -1 || numStr.indexOf("e") != -1) { var regExp = new RegExp( "^(((\\d+.?\\d+)|(\\d+))[Ee]{1}((-(\\d+))|(\\d+)))$", "ig" ); result = regExp.exec(numStr); if (result != null) { resValue = result[2]; power = Number(result[5]); result = null; } if (!resValue && !power) { return false; } dotIndex = resValue.indexOf(".") == -1 ? 0 : resValue.indexOf("."); resValue = resValue.replace(".", ""); resArr = resValue.split(""); if (Number(power) >= 0) { var subres = resValue.substr(dotIndex); power = Number(power); //冪數大於小數點後面的數字位數時,後面加0 for (var i = 0; i <= power - subres.length; i++) { resArr.push("0"); } if (power - subres.length < 0) { resArr.splice(dotIndex + power, 0, "."); } } else { power = power.replace("-", ""); power = Number(power); //冪數大於等於 小數點的index位置, 前面加0 for (var i = 0; i < power - dotIndex; i++) { resArr.unshift("0"); } var n = power - dotIndex >= 0 ? 1 : -(power - dotIndex); resArr.splice(n, 0, "."); } } resValue = resArr.join(""); return sym + resValue; } else { return num_str; } } /** * 拼接 圖片 URL */ export const getNewUrl = (url: string, width: number | string = 100, height: number | string = 100, sku: string = 'nihao') => { let oldUrl = `${url}`; let newUrl = ''; if (!isEmpty(url)) { if (url.indexOf("img.nihaojewelry.com") !== -1) { let iconArr = url.split('.'); newUrl = `${oldUrl.replace(`.${iconArr[iconArr.length - 1]}`, `_thumb_${width}x${height}/${sku}.${iconArr[iconArr.length - 1]}`)}`; } else { newUrl = `${oldUrl}?x-oss-process=image/format,webp,image/resize,m_fill,w_${width},h_${height}`; } } return newUrl; // skuImg: "http://image.nihaostyles.com/product/2021/6/10/1402800346123866112.jpg?x-oss-process=image/resize,m_fixed,limit_0,h_120,w_120" // skuImg: "http://image.nihaostyles.com/product/2021/6/10/1402800346123866112.jpg" // 主站 圖片URL import { getNewUrl } from '/@/utils/erp'; // https://img.nihaojewelry.com/product/2022/4/13/1514075147882074112_thumb_200x200/NHMEY713729.jpg // https://img.nihaojewelry.com/product/2022/4/13/1514075147882074112.jpg // 一種型別 // https://img.nihaojewelry.com/ // 其他型別 // https://image.nihaostyles.com/ // https://hz-oss-ck.oss-cn-hangzhou.aliyuncs.com/ // = = = = = = // let newUrl = `${item.skuImg.replace(`.${iconArr[iconArr.length - 1]}`, `_thumb_${130}x${130}/${item.sku}.${iconArr[iconArr.length - 1]}`)}`; // = = = = = // http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,m_fixed,limit_0,h_4096,w_4096 // https://cc-west-usa.oss-us-west-1.aliyuncs.com/4cf4abeb-1519-4f27-adec-328bd95fc176.jpg?x-oss-process=image/format,webp,image/resize,m_fill,w_179,h_190 }
/** * 去除 陣列中的空字串 */ export function trimSpace(array: any) { for (var i = 0; i < array.length; i++) { if (array[i] == " " || array[i] == "" || array[i] == null || typeof (array[i]) == "undefined") { array.splice(i, 1); i = i - 1; } } return array; }
/** * 資料非空驗證 * @desc 驗證資料是否為 null undefined [] {} '' * @paramdata * @return{boolean} */ export function isEmpty(data: any) { if (data === null) return true if (data === undefined) return true if (Object.prototype.toString.call(data) === '[object Array]') return data.length === 0 if (Object.prototype.toString.call(data) === '[object Object]') return Object.keys(data).length === 0 if (typeof data === 'string') return data.trim() === '' return false }
/** * 過濾空欄位 */ export const getNewParmes = (parme: any, ignoreArr: Array<any>) => { let parmes = parme let tempValue = { ...parme } if (parmes) { let keys = Object.keys(parmes); for (let i = 0; i < keys.length; i++) { const key = keys[i]; if (!ignoreArr.includes(key)) { const value = parmes[key]; if (isEmpty(value)) { delete parmes[key]; } } } } tempValue = { ...parmes } if (isEmpty(tempValue)) { tempValue = {} } return tempValue; }
/** * js計算中的科學計數法 */ export const toolNumber = (num_str: any) => { num_str = num_str.toString(); if (num_str.indexOf("+") != -1) { num_str = num_str.replace("+", ""); } if (num_str.indexOf("E") != -1 || num_str.indexOf("e") != -1) { var resValue = "", power: number = 0, result = null, dotIndex = 0, resArr: any = [], sym = ""; var numStr = num_str.toString(); if (numStr[0] == "-") { // 如果為負數,轉成正數處理,先去掉‘-’號,並儲存‘-’. numStr = numStr.substr(1); sym = "-"; } if (numStr.indexOf("E") != -1 || numStr.indexOf("e") != -1) { var regExp = new RegExp( "^(((\\d+.?\\d+)|(\\d+))[Ee]{1}((-(\\d+))|(\\d+)))$", "ig" ); result = regExp.exec(numStr); if (result != null) { resValue = result[2]; power = Number(result[5]); result = null; } if (!resValue && !power) { return false; } dotIndex = resValue.indexOf(".") == -1 ? 0 : resValue.indexOf("."); resValue = resValue.replace(".", ""); resArr = resValue.split(""); if (Number(power) >= 0) { var subres = resValue.substr(dotIndex); power = Number(power); //冪數大於小數點後面的數字位數時,後面加0 for (var i = 0; i <= power - subres.length; i++) { resArr.push("0"); } if (power - subres.length < 0) { resArr.splice(dotIndex + power, 0, "."); } } else { power = power.replace("-", ""); power = Number(power); //冪數大於等於 小數點的index位置, 前面加0 for (var i = 0; i < power - dotIndex; i++) { resArr.unshift("0"); } var n = power - dotIndex >= 0 ? 1 : -(power - dotIndex); resArr.splice(n, 0, "."); } } resValue = resArr.join("");
return sym + resValue; } else { return num_str; } }
/** * 拼接 圖片 URL */ export const getNewUrl = (url: string, width: number | string = 100, height: number | string = 100, sku: string = 'nihao') => { let oldUrl = `${url}`; let newUrl = ''; if (!isEmpty(url)) { if (url.indexOf("img.nihaojewelry.com") !== -1) { let iconArr = url.split('.'); newUrl = `${oldUrl.replace(`.${iconArr[iconArr.length - 1]}`, `_thumb_${width}x${height}/${sku}.${iconArr[iconArr.length - 1]}`)}`; } else { newUrl = `${oldUrl}?x-oss-process=image/format,webp,image/resize,m_fill,w_${width},h_${height}`; } } return newUrl;
// skuImg: "http://image.nihaostyles.com/product/2021/6/10/1402800346123866112.jpg?x-oss-process=image/resize,m_fixed,limit_0,h_120,w_120" // skuImg: "http://image.nihaostyles.com/product/2021/6/10/1402800346123866112.jpg" // 主站 圖片URL import { getNewUrl } from '/@/utils/erp'; // https://img.nihaojewelry.com/product/2022/4/13/1514075147882074112_thumb_200x200/NHMEY713729.jpg // https://img.nihaojewelry.com/product/2022/4/13/1514075147882074112.jpg // 一種型別 // https://img.nihaojewelry.com/ // 其他型別 // https://image.nihaostyles.com/ // https://hz-oss-ck.oss-cn-hangzhou.aliyuncs.com/ // = = = = = = // let newUrl = `${item.skuImg.replace(`.${iconArr[iconArr.length - 1]}`, `_thumb_${130}x${130}/${item.sku}.${iconArr[iconArr.length - 1]}`)}`; // = = = = = // http://image-demo.oss-cn-hangzhou.aliyuncs.com/example.jpg?x-oss-process=image/resize,m_fixed,limit_0,h_4096,w_4096 // https://cc-west-usa.oss-us-west-1.aliyuncs.com/4cf4abeb-1519-4f27-adec-328bd95fc176.jpg?x-oss-process=image/format,webp,image/resize,m_fill,w_179,h_190
}