记录业务代码优化过程

167 阅读1分钟

需求:vue页面,获取当前页面自带参数,如果并给部分参数设置默认值

原始代码没有备份只有截图 非常不优雅 不简洁 复杂度飘红 修改之后变成了橘色

微信图片_20210708180304.png

//获取当前页面
export const getUrlParams = (routeQuery) => {
  const {
    channel = this.$channel,
    agentCode = this.$agentCode,
    salorCode,
    agent,
    group,
    client = 'default',
    user = 'user',
  } = routeQuery

  let paramsJson = {
    channel,
    agentCode,
    client,
    user,
  }
  //可选链操作符
  // paramsJson.salorCode=routeQuery?.salorCode;
  // console.log(paramsJson);
  if (channel == 'AA' || channel == 'BB') {
    if (salorCode) paramsJson.salorCode = salorCode
    console.log(paramsJson)
  } else if (channel == 'CC') {
    if (agent) paramsJson.agent = agent
    if (group) paramsJson.group = group
  }
  return paramsJson
}