2020-07-20 日期格式化:

日期格式化

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 }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。