乐趣区

关于javascript:职责链模式在项目中的运用

  /**
   * 职责链模式
   */
  export class Chain {constructor(fn) {
      this.fn = fn
      this.successor = null
    }
  
    setNextSuccessor(successor) {return (this.successor = successor)
    }
  
    async passRequest() {const res = await this.fn.apply(this, arguments)
      if (res === 'nextSuccessor' && this.successor) {return await this.successor.passRequest.apply(this.successor, arguments)
      }
      return res
    }
  }

  /**
   * 应用职责链模式进行保留前的校验
   * @param {Object} data 须要校验的数据
   */
  async function validate(data) {
    // 校验:小计金额最大值限度
    function validateAmountMax() {const index = data.findIndex((item) => item.Amount >= 99999999)
      if (index > -1) {return new Promise((resolve) => {
          const modal = Modal.confirm({
            title: '提醒',
            content: '小计金额不能超过 99999999',
            icon: createVNode(ExclamationCircleOutlined),
            okText: '确定',
            cancelText: createVNode(),
            async onOk() {nextTick(() => modal.destroy())
              resolve(false)
            }
          })
        })
      }
      return 'nextSuccessor'
    }

    // 校验:洽购价格是否高于加权平均价
    function validateInPriceHigherThanAutoPrice() {
      // 判断洽购设置中是否启用了:“洽购价格高于加权平均价时,进行提醒”,如果启用了,// 当存在某一商品的洽购价格高于加权平均价时,弹框提醒
      if (purchaseSettings.value.InPriceHigherThanAutoPrice == 'Y') {const needTip = data.some((item) => item.Price > item.CostPrice)
        
        if (needTip) {return new Promise((resolve) => {
            const modal = Modal.confirm({
              title: '确认',
              content: '存在商品的以后价格高于加权平均价,是否持续保留?',
              icon: createVNode(ExclamationCircleOutlined),
              okText: '确定',
              cancelText: '勾销',
              async onOk() {nextTick(() => modal.destroy())
                resolve('nextSuccessor')
              },
              async onCancel() {nextTick(() => modal.destroy())
                resolve(false)
              }
            })
          })
        }
      }
      return 'nextSuccessor'
    }

    // 校验:生鲜商品容许价格高于商品档案进价
    function validateFreshAllowInPriceHigherThanItemPrice() {
      // 洽购设置中不启用:“生鲜商品容许价格高于商品档案进价”,那么当一个商品为生鲜商品,// 且价格高于档案进价时,弹框提醒
      if (purchaseSettings.value.FreshAllowInPriceHigherThanItemPrice == 'N') {const needTip = data.some((item) => item.IsFresh && item.Price > item.PurcPrice)
        if (needTip) {return new Promise((resolve) => {
            const modal = Modal.confirm({
              title: '提醒',
              content: '洽购设置设置了不容许生鲜商品价格高于商品档案进价,请从新输出价格!',
              icon: createVNode(ExclamationCircleOutlined),
              okText: '确定',
              cancelText: createVNode(),
              async onOk() {nextTick(() => modal.destroy())
                resolve(false)
              }
            })
          })
        }
      }
      return true
    }

    // 将三个校验函数包装成职责链节点
    const valAmount = new Chain(validateAmountMax)
    const valCostPrice = new Chain(validateInPriceHigherThanAutoPrice)
    const valFresh = new Chain(validateFreshAllowInPriceHigherThanItemPrice)
    // 指定节点在职责链中的程序
    valAmount.setNextSuccessor(valCostPrice).setNextSuccessor(valFresh)
    // 把申请传递给第一个节点
    return await valAmount.passRequest()}

  // 保留
  async function save(data) {if (!(await validate(data))) return false

    // ....... 调接口进行保留
  }
退出移动版