优化路由switch...case

共用

const service = axios.create({
  baseURL: process.env.NODE_ENV === 'development' ? '' : '',
  timeout: 10000,
});

优化代码:

export function fetchList(urlKey: string) {
  switch (urlKey) {
    case 'iPhone-grade':
      return service({
        url: '/api/store/get_store_page?category=iPhone&condition=graded',
        method: 'get',
      })
    case 'samsung-grade':
      return service({
        url: '/api/store/get_store_page?category=Samsung&condition=graded',
        method: 'get',
      })
    case 'samsung-unGrade':
      return service({
        url: '/api/store/get_store_page?category=Samsung&condition=Ungraded',
        method: 'get',
      })
    case 'lg':
      return service({
        url: '/api/store/get_store_page?category=LG',
        method: 'get',
      })
    case 'alcatel':
      return service({
        url: '/api/store/get_store_page?category=Alcatel',
        method: 'get',
      })
    case 'iPhone-unGrade':
      return service({
        url: '/api/store/get_store_page?category=iPhone&condition=Ungraded',
        method: 'get',
      })
    case 'motorola':
      return service({
        url: '/api/store/get_store_page?category=Motorola',
        method: 'get',
      })
    case 'iPad':
      return service({
        url: '/api/store/get_store_page?category=iPad',
        method: 'get',
      })
    case 'apple-watch':
      return service({
        url: '/api/store/get_store_page?category=AppleWatch',
        method: 'get',
      })
    case 'apple-ID':
      return service({
        url: '/api/store/get_store_page?category=AppleID',
        method: 'get',
      })
    case 'android-tablet':
      return service({
        url: '/api/store/get_store_page?category=Androidtablet',
        method: 'get',
      })
    case 'android-smartwatch':
      return service({
        url: '/api/store/get_store_page?category=Androidsmartwatch',
        method: 'get',
      })
  }
}

优化代码

const listUrlParamsMap: Record <string, string> = {
  'iPhone-grade': 'category=iPhone&condition=graded',
  'samsung-grade': 'category=Samsung&condition=graded',
  'samsung-unGrade': 'category=Samsung&condition=Ungraded',
  'lg': 'category=LG',
  'alcatel': 'category=Alcatel',
  'iPhone-unGrade': 'category=iPhone&condition=Ungraded',
  'motorola': 'category=Motorola',
  'iPad': 'category=iPad',
  'apple-watch': 'category=AppleWatch',
  'apple-ID': 'category=AppleID',
  'android-tablet': 'category=Androidtablet',
  'android-smartwatch': 'category=Androidsmartwatch'
};

export function fetchList(urlKey: string) {
  return service(`/api/store/get_store_page?${listUrlParamsMap[urlKey]}`)
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值