业务场景:点击 查询按钮 不同的查询条件有不同查询接口。当然每个接口都有自己固定的参数形式。
用if 判断确实会让代码的核心逻辑体现的不是很突出。个人认为在处理逻辑 只需要把核心逻辑拉出来即可。然后需要过滤的数据或者判断均可用方法来进行替代,这样后面比较容易维护。
*** 核心 Map (map 键值对 值value可以是任意模式的,这样可以任意拼接成想要的)***
方式 :通过get 方法得出可能性情况
let request_param = new URLSearchParams()
const query_url = new Map([
['0_0_0', {type: '0', url: '', msg: '请选择查询项..'}],
['0_0_1', {
type: '1', url: '/record/search/amount', msg: '...', params_get: (value) => {
request_param.append('start', value.startAmount)
request_param.append('end', value.endAmount)
}
}],
['0_1_0', {
type: '1', url: '/record/search/date', msg: '', params_get: (value) => {
request_param.append('start', value.startDate)
request_param.append('end', value.endDate)
}
}],
['0_1_1', {
type: '1', url: '/record/search/date-amount', params_get: ({startDate, endDate, startAmount, endAmount}) => {
request_param.append('startDate', startDate)
request_param.append('endDate', endDate)
request_param.append('startAmount', startAmount)
request_param.append('endAmount', endAmount)
}
}],
['1_0_0', {
type: '1', url: '/record/search/uid', params_get: ({uid}) => {
request_param.append('uid', uid)
}
}],
['1_1_0', {type: '0', url: '', msg: '身份证只能单独查询哦 ..'}],
['1_0_1', {type: '0', url: '', msg: '身份证只能单独查询哦 ..'}],
])
/* 判断 type 类型 */
judge_Null () {
let index, index1, index2, url, params = {}
if (this.sfz_Query === '') {
index = 0
} else {
index = 1
params.uid = this.sfz_Query
}
if (this.time_sot.length === 0) {
index1 = 0
} else {
index1 = 1
params.startDate = this.axios_packaged.formatDate(this.time_sot[0], 'yyyy-MM-dd')
params.endDate = this.axios_packaged.formatDate(this.time_sot[1], 'yyyy-MM-dd')
}
if (this.money_Query === '') {
index2 = 0
} else {
index2 = 1
params.startAmount = 0
params.endAmount = +this.money_Query
}
url = `${index}_${index1}_${index2}`
return {url: query_url.get(url), value: params}
}
let {url, value} = this.judge_Null()
if (url.type === '0') {
this.$message({
type: 'warning',
message: `${url.msg}`
})
} else {
/* 参数 覆盖 */
request_param = new URLSearchParams()
let request_url = url.url
url.params_get(value)
request_param.append('page', '1')
/* 暂时设置 5条 */
request_param.append('size', '5')
this.$axios.post(`/api${request_url}`, request_param).then((result) => {
let {code, data} = result.data
if (code === 200) {
this.process_table_data(data)
} else {
}
})
}