日期格式化
formatTime (date, str = '') {
if (date.getDate() < 10) {
return date.getMonth() < 9 ? `${date.getFullYear()}${str}0${date.getMonth() + 1}${str}0${date.getDate()}` : `${date.getFullYear()}${str}${date.getMonth() + 1}${str}0${date.getDate()}`
} else {
return date.getMonth() < 9 ? `${date.getFullYear()}${str}0${date.getMonth() + 1}${str}${date.getDate()}` : `${date.getFullYear()}${str}${date.getMonth() + 1}${str}${date.getDate()}`
}
}
格式化、删除无用请求参数
`@param {*} params 参数 {name:admin, password: ''}
@returns {object} '{name:admin}
export const paramsFormat = (params) => {
const p = {}
for (let v in params) {
let value = params[v]
if (Object.prototype.toString.call(params[value]) === '[object String]') {
value = value ? value.replace(/(^\s*)|(\s*$)/g, '') : ''
}
if (value || value === 0) {
p[v] = value
}
}
return p
}
格式化参数
@param {*} params 参数 {name:admin, password:123} @returns {object} 'name=admin&password=123'
export function formatParams (params) {
let formData = []
const undefinedKeys = []
for (const property in params) {
const encodedKey = encodeURIComponent(property)
// 参数值 去空格
const uncodedValue = trim(params[property])
const encodedValue = encodeURIComponent(uncodedValue)
if (params[property] === undefined || typeof (params[property]) === 'undefined') {
undefinedKeys.push(property)
} else {
// 去掉空格
formData.push(encodedKey + '=' + encodedValue)
}
}
formData = formData.join('&')
return { formData: formData, undefinedKeys: undefinedKeys }
}