213131

import { Button, message , modal } from 'ant-design-vue';

import { settlementStatusList,payTypeList } from '@/utils/state'

 

export function timeFix() {

const time = new Date()

const hour = time.getHours()

return hour < 9 ? '早上好' : hour <= 11 ? '上午好' : hour <= 13 ? '中午好' : hour < 20 ? '下午好' : '晚上好'

}

 

/**

* 觸發 window.resize

*/

export function triggerWindowResizeEvent() {

const event = document.createEvent('HTMLEvents')

event.initEvent('resize', true, true)

event.eventType = 'message'

window.dispatchEvent(event)

}

 

export function handleScrollHeader(callback) {

let timer = 0

 

let beforeScrollTop = window.pageYOffset

callback = callback || function () { }

window.addEventListener(

'scroll',

event => {

clearTimeout(timer)

timer = setTimeout(() => {

let direction = 'up'

const afterScrollTop = window.pageYOffset

const delta = afterScrollTop - beforeScrollTop

if (delta === 0) {

return false

}

direction = delta > 0 ? 'down' : 'up'

callback(direction)

beforeScrollTop = afterScrollTop

}, 50)

},

false

)

}

//檢測ie

export function isIE() {

const bw = window.navigator.userAgent

const compare = (s) => bw.indexOf(s) >= 0

const ie11 = (() => 'ActiveXObject' in window)()

return compare('MSIE') || ie11

}

 

export function removeLoadingAnimate(id = '', timeout = 1500) {

if (id === '') {

return

}

setTimeout(() => {

document.body.removeChild(document.getElementById(id))

}, timeout)

}

 

 

//對象轉formdata格式

export function objToFormData(config) {

let formData = new FormData();

let obj = config.data;

let arrayKey = config.arrayKey;

for (var i in obj) {

if (Array.isArray((obj[i]))) {

obj[i].map(item => {

if (!arrayKey) {

formData.append(i, item)

} else {

formData.append(i + '[]', item)

}

})

} else {

formData.append(i, obj[i])

}

}

return formData;

}

 

/獲取時間yydd

export const getDateFormat = (date) => {

var y = date.getFullYear();

var m = date.getMonth() + 1;

m = m < 10 ? ('0' + m) : m;

var d = date.getDate();

d = d < 10 ? ('0' + d) : d;

var h = date.getHours();

h = h < 10 ? ('0' + h) : h;

var minute = date.getMinutes();

var second = date.getSeconds();

minute = minute < 10 ? ('0' + minute) : minute;

second = second < 10 ? ('0' + second) : second;

return {

'ymd': y + '-' + m + '-' + d,

'ymdhm': y + '-' + m + '-' + d + ' ' + h + ':' + minute,

}

}

 

//獲取html字符串中的內容 編輯器內容

export const htmlToText = (data) => {

return data.replace(/<(style|script|iframe)[^>]*?>[\s\S]+?<\/\1\s*>/gi, '').replace(/<[^>]+?>/g, '').replace(/\s+/g, ' ').replace(/ /g, ' ').replace(/>/g, ' ');

}

//去掉對象中屬性爲空的屬性 0要特殊處理 默認爲false

export const getObjNotEmptyAttr = (afferentObj = {}) => {

let carTypeAllQuery = {};

Object.keys(afferentObj).forEach((item) => {

if (afferentObj[item] !== null && afferentObj[item] !== undefined && afferentObj[item] !== '' && afferentObj[item] !==false) {

console.log(item,'item')

carTypeAllQuery[item] = afferentObj[item]

}

})

return carTypeAllQuery

}

 

//數組去重

export const getArrDuplicate = (arr = []) => {

let getArr = new Set(arr)

return Array.from(getArr);

}

 

/刪除數組指定下標元素

export const deleteArrIndex = (arr = [], index, num) => {

// let getArr = new Set(arr)

let newArr = arr;

newArr.splice(index, num);

return newArr;

}

 

//郵箱校驗

export function isEmail(s) {

return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s)

}

 

//手機號校驗

export function isMobile(mobile) {

// var index1 = mobile.substring(0, 1);

// var index2 = mobile.substring(0, 2);

// if (index2 == '11' || index2 == '12' || index1 !== '1' || mobile.length !== 11) {

// return false;

// } else {

// return true;

// }

let getMobile = parseInt(getMobile);

if (7 <= getMobile.length <= 13) {

return true

} else {

return false

}

}

 


/數組校驗

export function isArray(arr) {

return Array.isArray(arr);

}

//截取地址 isCutoutAddress('', '/')

export function isCutoutAddress(str, mark) {

if (str.indexOf(mark) >= 0) {

let getArr = str.split(mark);

return {

getArr,

length: getArr.length

}

}

return false

}

 

//判斷爲空值 Array Object Number String

export function isEmpty(obj) {

if (!obj) {

return true;

}

//檢驗null和undefined

if (!obj && obj !== 0 && obj !== '') {

return true;

}

//檢驗數組

if (Array.prototype.isPrototypeOf(obj) && obj.length === 0) {

return true;

}

//檢驗對象

if (Object.prototype.isPrototypeOf(obj) && Object.keys(obj).length === 0) {

return true;

}

return false;

 

 

}

 


// 下載excel文件; 參數name是文件名稱,res是後端返回的數據

export function downloadExcel(name, res) {

const fileName = name + '.xls' //表格名字

if ('download' in document.createElement('a')) {

// console.log('aaaaaaaaaaaaaaaaaaaa')

// 非IE下載

const blob = new Blob([res], { type: 'application/ms-excel' }) // 解析後端返回的亂碼vnd.ms-excel

const elink = document.createElement('a')

// console.log(elink,'elink值')

elink.download = fileName //定義下載名字

elink.style.display = 'none' // 決定是否隱藏

elink.href = URL.createObjectURL(blob)

document.body.appendChild(elink)

elink.click()

URL.revokeObjectURL(elink.href) // 釋放URL 對象

document.body.removeChild(elink)

}

}

 

//下載圖片

export function downloadImg(imgsrc, name){

console.log('imgsrc, name')

console.log(imgsrc, name)

let image = new Image();

// 解決跨域 Canvas 污染問題

image.setAttribute("crossOrigin", "anonymous");

image.onload = function() {

let canvas = document.createElement("canvas");

canvas.width = image.width;

canvas.height = image.height;

let context = canvas.getContext("2d");

context.drawImage(image, 0, 0, image.width, image.height);

let url = canvas.toDataURL("image/png"); //得到圖片的base64編碼數據

let a = document.createElement("a"); // 生成一個a元素

let event = new MouseEvent("click"); // 創建一個單擊事件

a.download = name || "photo"; // 設置圖片名稱

a.href = url; // 將生成的URL設置爲a.href屬性

a.dispatchEvent(event); // 觸發a的單擊事件

};

image.src = imgsrc;

 

}

 

 

//下載文件

export function downloadFile(link, name) {

console.log(link, name)

const x = new XMLHttpRequest();

x.open('GET', link, true);

x.responseType = 'blob';

x.onload = function () {

const url = window.URL.createObjectURL(x.response);

const a = document.createElement('a');

a.href = url;

a.download = name || '';

a.click();

};

x.send();

}

 

//檢測數組爲空

export function isArrNotEmpty(arr) {

if(arr&&arr.length>0){

return true

}

return false

}

 

// children 屬性爲空 設置爲undefined

export function resetChildren(data) {

const that = this

data.forEach((element) => {

if (element.children && element.children.length > 0) {

resetChildren(element.children)

} else {

element.children = undefined

}

})

return data

}

 

//獲取匹配的對象

export function getMatchObj(matchval,matchAtrr,matchArr=[]) {

return matchArr.filter((item)=>{

return matchval==item[matchAtrr]

})

}


/判斷數組裏是否有相同的值

export function isEqualVal(val,arr=[]) {

let result=arr.filter((item)=>{

return val==item

})

if(result.length>0){

return true

}

return false

}

// 字符串數組互轉數字數組 時間比較緊 不自動判斷類型 先寫死

export function arrTypeTransform(arr,type) {

if(type='Number'){

return arr.map(Number)

}else if(type='String'){

return arr.map(String)

}

}

 

// 去掉字符串空格

export function strTrim(str) {

return str.trim()

}

 

// 判斷是否是 空字符 和 null 和 undefined 和 NaN

export function isCheckVal(val) {

if(val===0||val===''||val===null||val===undefined||val===NaN){

return false

}

return true

}

 

export const debounce= (func, wait) => {

var timeout;

 

return function () {

var context = this;

var args = arguments;

 

clearTimeout(timeout)

timeout = setTimeout(function(){

func.apply(context, args)

}, wait);

}

};

 

// 節流

export const throttle = (fn, wait) => {

let canRun = true; // 通過閉包保存一個標記

return function () {

if (!canRun) return; // 在函數開頭判斷標記是否爲true,不爲true則return

canRun = false; // 立即設置爲false

setTimeout(() => { // 將外部傳入的函數的執行放在setTimeout中

fn.apply(this, arguments);

// 最後在setTimeout執行完畢後再把標記設置爲true(關鍵)表示可以執行下一次循環了。當定時器沒有執行的時候標記永遠是false,在開頭被return掉

canRun = true;

}, wait);

};

}

 

// 節流時間範圍

export const throttleTime=1000;

 

//獲取搜索條件時間

export function getSearchTime(dateArr) {

let dateStr1=new Date(dateArr[0]);

let dateStr2=new Date(dateArr[1]);

// let startDate=Date.parse(dateStr1.getFullYear()+'-'+(dateStr1.getMonth()+1)+'-'+dateStr1.getDate()+' 00:00:00')

// let endDate=Date.parse(dateStr2.getFullYear()+'-'+(dateStr2.getMonth()+1)+'-'+dateStr2.getDate()+' 00:00:00')

let startDate= dateStr1.getFullYear()+'-'+(dateStr1.getMonth()+1)+'-'+dateStr1.getDate()+' 00:00:00'

let endDate=dateStr2.getFullYear()+'-'+(dateStr2.getMonth()+1)+'-'+dateStr2.getDate()+' 23:59:59'

return {

startDate,

endDate

}

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章