共用
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]}`)
}