JSON数据格式化

  1. const todayILearn = {
  2. _id: 1,
  3. content: '今天学习 JSON.stringify(),我很开心!',
  4. created_at: 'Mon Nov 25 2019 14:03:55 GMT+0800 (中国标准时间)',
  5. updated_at: 'Mon Nov 25 2019 16:03:55 GMT+0800 (中国标准时间)'
  6. }

将上面这个对象处理成下面这个对象

  1. const todayILearn = {
  2. id: 1,
  3. content: '今天学习 JSON.stringify(),我很开心!',
  4. createdAt: 'Mon Nov 25 2019 14:03:55 GMT+0800 (中国标准时间)',
  5. updatedAt: 'Mon Nov 25 2019 16:03:55 GMT+0800 (中国标准时间)'
  6. }

也就是在不改变属性的值的前提下,将对象属性修改一下。 把_id 改成 id,把 updated_at 改成 updatedAt,把 created_at 改成 createdAt

方案:序列化+ replace 美学典范

  1. const mapObj = {
  2. _id: "id",
  3. created_at: "createdAt",
  4. updated_at: "updatedAt"
  5. };
  6. JSON.parse(
  7. JSON.stringify(todayILearn).replace(
  8. /_id|created_at|updated_at/gi,
  9. matched => mapObj[matched]
  10. )
  11. )
  12. // {
  13. // id: 1,
  14. // content: '今天学习 JSON.stringify(),我很开心!',
  15. // createdAt: 'Mon Nov 25 2019 14:03:55 GMT+0800 (中国标准时间)',
  16. // updatedAt: 'Mon Nov 25 2019 16:03:55 GMT+0800 (中国标准时间)'
  17. // }

接口数据格式化

约定

要明确所有的接口格式都要如下返回:

  1. {
  2. status: 200,
  3. code: 10000,
  4. message: '',
  5. data: {
  6. ...
  7. },
  8. }

处理数据

credit.js

  1. import dayjs from 'dayjs'
  2. import { apiRequest as Axios, courseRequest } from '@@/utils/axios'
  3. import { injectRowKey } from '@@/utils/rowKey'
  4. import { getCreateTime } from '@@/utils/format'
  5. import { decodeUniqueId } from '@@/utils/crypto'
  6. const decodeName = code => {
  7. switch (code) {
  8. case 'finance_quotient_course_study_add_200':
  9. return '学习课程'
  10. case 'finance_quotient_course_notes_add_200':
  11. return '写笔记'
  12. case 'finance_quotient_course_praxis_add_200':
  13. return '做测试题'
  14. case 'finance_quotient_transfer_1_ratio':
  15. return '历史积分导入'
  16. case 'finance_quotient_course_note_share_add_400':
  17. return '分享笔记'
  18. case 'finance_quotient_exchange_subtract_1_ratio':
  19. return '积分兑换'
  20. }
  21. }
  22. const creditFormat = item => {
  23. return {
  24. name: decodeName(item.creditRuleCode),
  25. createdAt: item.createdAt || new Date().valueOf(),
  26. createdTime: getCreateTime(item.createdAt * 1000),
  27. action: item.action,
  28. amount: item.amount,
  29. tradeNumber: item.tradeNumber,
  30. isValid: item.isValid === 1, // 0-无效,1-有效
  31. }
  32. }
  33. class CreditService {
  34. async list(pageNum = 1, pageSize = 10) {
  35. const {
  36. data: { items = [], total },
  37. } = await Axios.get('/user-center/credit/transactions', {
  38. params: {
  39. pageNum,
  40. pageSize,
  41. creditTypeCode: 'fq_credit',
  42. },
  43. })
  44. const list = items.map(creditFormat)
  45. return { items: injectRowKey(list), total: total || items.length }
  46. }
  47. }
  48. export default new CreditService()

调用

import creditService from '@@/services/credit'


action = {
  handleFetch: async () => {
    const { items, total } = await creditService.list(current.pageNum, pageSize)
    this.setState({
      ...
    })
  },
}

依赖

rowKey.js

import UUID from 'uuid'

// 设置数据唯一标识
export const setRowKey = data => {
  return Object.assign({ rowKey: UUID() }, data)
}

// 数组遍历设置唯一标识
export const injectRowKey = dataSource => {
  return Array.isArray(dataSource) ? dataSource.map(setRowKey) : []
}

format.js

import dayjs from 'dayjs'

const currentYear = new Date().getFullYear()
const startDay = new Date(new Date().toLocaleDateString()).valueOf()
const endDay = startDay + 24 * 60 * 60 * 1000

const timeFormatList = [
  {
    check: time => time >= startDay && time < endDay,
    render: time => '今天 ' + dayjs(time).format('HH:mm'),
  },
  {
    check: time => dayjs(time).year() === currentYear,
    render: time => dayjs(time).format('MM-DD HH:mm'),
  },
  {
    check: () => true,
    render: time => dayjs(time).format('YYYY-MM-DD HH:mm'),
  },
]

export const getCreateTime = createdAt => {
  const format = timeFormatList.find(item => {
    if (item.check(createdAt)) {
      return item
    }
  })
  return format.render(createdAt)
}

export const timeFormat = time => {
  const thisYear = dayjs(time).year()
  if (thisYear === currentYear) {
    return dayjs(time).format('M月DD日')
  } else {
    return dayjs(time).format('YYYY年M月DD日')
  }
}

const checkList = [
  {
    check: value => value >= 1000 * 10000,
    format: value => '999万+',
  },
  {
    check: value => value >= 10000,
    format: value => {
      const result = Math.round(+value / 10000)
      if (result === 1000) {
        return '999万+'
      }
      return `${result}万`
    },
  },
  {
    check: value => value >= 0,
    format: value => value,
  },
]

// 数字格式化
export const numberFormat = number => {
  const checker = checkList.find(item => item.check(number))
  return checker.format(number)
}

// 商品价格格式化
export const priceFormat = (price = 0, precison = 2) => {
  price = Number(price) || 0
  return +price.toFixed(precison)
}

// 收益率格式化
export const profitRateFormat = (profitRate = 0) => {
  return (profitRate * 100).toFixed(2) + '%'
}

// 有符号收益率格式化
export const signedProfitRateFormat = (profitRate = 0) => {
  if (profitRate > 0) {
    return `+${(profitRate * 100).toFixed(2)}%`
  } else {
    return `${(profitRate * 100).toFixed(2)}%`
  }
}

// 百分比格式化
export const percentageFormat = (number = 0) => {
  return Math.round(number * 100) + '%'
}

crypto.js

// 课程资源自定义编号拼接
export const encodeUniqueId = (productId, chapterId, resourceId) => {
  return [productId, chapterId, resourceId].join('-')
}

// 课程资源自定义编号解析
export const decodeUniqueId = uniqueId => {
  const [productId, chapterId, resourceId] = uniqueId.split('-')
  return [productId, chapterId, resourceId].map(item => parseInt(item, 10))
}

// 课程资源自定义编号解析
export const parseUniqueId = uniqueId => {
  const [productId, chapterId, resourceId] = uniqueId.split('-').map(item => parseInt(item, 10))
  return { productId, chapterId, resourceId }
}

// 原生交互数据编码
export const encodeHybrid = data => {
  const payload = Object.assign({}, data)
  return btoa(encodeURIComponent(JSON.stringify(payload)))
}

// 原生交互数据解码
export const decodeHybrid = str => {
  try {
    return JSON.parse(decodeURIComponent(atob(str)))
  } catch (error) {
    console.warn(error)
    return {}
  }
}

参考

  1. 你不知道的 JSON.stringify() 的威力