// 獲取url中"?"符後的字串
function GetRequest() {
var url = location.search
var theRequest = new Object()
if (url.indexOf("?") != -1) {
var str = url.substr(1)
strs = str.split("&")
for (var i = 0; i < strs.length; i++) {
theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1])
}
}
return theRequest
}
// 時間戳轉日期格式
function format (date, type) {
if (!date) return ''
date = typeof date === 'number' ? new Date(date) : date
type = type || 'yyyy-MM-dd HH:mm:ss'
let obj = {
'y': date.getFullYear(), // 年份,注意必須用getFullYear
'M': date.getMonth() + 1, // 月份,注意是從0-11
'd': date.getDate(), // 日期
'q': Math.floor((date.getMonth() + 3) / 3), // 季度
'w': date.getDay(), // 星期,注意是0-6
'H': date.getHours(), // 24小時制
'h': date.getHours() % 12 === 0 ? 12 : date.getHours() % 12, // 12小時制
'm': date.getMinutes(), // 分鐘
's': date.getSeconds(), // 秒
'S': date.getMilliseconds() // 毫秒
}
let week = ['天', '一', '二', '三', '四', '五', '六']
for (let i in obj) {
type = type.replace(new RegExp(i + '+', 'g'), function (m) {
let val = obj[i] + ''
if (i === 'w') return (m.length > 2 ? '星期' : '周') + week[val]
for (let j = 0, len = val.length; j < m.length - len; j++) val = '0' + val
return m.length === 1 ? val : val.substring(val.length - m.length)
})
}
return type
}
// code陣列轉周幾
function getWeekList (weekStr) {
let weekArr = weekStr.split(',').sort()
let weekDayArr = []
for (let weekDay of weekArr) {
switch (weekDay) {
case '1':
weekDayArr.push('一')
break
case '2':
weekDayArr.push('二')
break
case '3':
weekDayArr.push('三')
break
case '4':
weekDayArr.push('四')
break
case '5':
weekDayArr.push('五')
break
case '6':
weekDayArr.push('六')
break
case '7':
weekDayArr.push('日')
break
}
}
return weekDayArr.length > 0 ? '每週' + weekDayArr.join('、') : '-'
}
// code轉周幾
function getWeekDay (weekCode) {
let weekDay = ''
switch (weekCode) {
case 1:
weekDay = '週一'
break
case 2:
weekDay = '週二'
break
case 3:
weekDay = '週三'
break
case 4:
weekDay = '週四'
break
case 5:
weekDay = '週五'
break
case 6:
weekDay = '週六'
break
case 7:
weekDay = '週日'
break
}
return weekDay
}
// 深拷貝
function cloneObj (obj) {
var str, newobj = obj.constructor === Array ? [] : {} // eslint-disable-line
if (typeof obj !== 'object') {
return
} else if (window.JSON) {
str = JSON.stringify(obj) // 系列化物件
newobj = JSON.parse(str) // 還原
} else {
for (var i in obj) {
newobj[i] = typeof obj[i] === 'object' ? cloneObj(obj[i]) : obj[i]
}
}
return newobj
}
// 檔案的下載
function downloadFile (url, fileName, success) {
var xhr = new XMLHttpRequest()
xhr.open('GET', url, true)
let headers = tools.getHeaders()
for (let key in headers) {
xhr.setRequestHeader(key, headers[key])
}
xhr.responseType = 'blob'
xhr.onload = function (event) {
if (xhr.response.type === 'application/json') {
success(xhr.response)
} else {
var blob = xhr.response
if (window.navigator.msSaveOrOpenBlob) {
navigator.msSaveBlob(blob, fileName)
} else {
var link = document.createElement('a')
link.download = fileName
link.href = window.URL.createObjectURL(blob)
document.body.appendChild(link)
link.click()
document.body.removeChild(link)
}
}
}
xhr.send()
}
// 引數拼接
function param (json) {
if (!json) return ''
return cleanArray(Object.keys(json).map(key => {
if (json[key] === undefined) return ''
return encodeURIComponent(key) + '=' +
encodeURIComponent(json[key])
})).join('&')
}
複製程式碼