Commit d1b349d2 by Lin

feat: 门店提成方案表单校验

1 parent 09dec86f
<template> <template>
<d2-container v-loading="$store.state.d2admin.loading.value" <d2-container v-loading="$store.state.d2admin.loading.value"
class="performance-scheme-wrapper"> class="performance-scheme-wrapper">
<el-form class="form-performance-name" <el-form ref="form"
class="form-performance-name"
label-width="170px" label-width="170px"
:model="formData" :model="formData"
:rules="rules"> :rules="rules"
:validate-on-rule-change="false">
<div class="warp-box"> <div class="warp-box">
<div class="title space-b"> <div class="title space-b">
<div class="title-wrap"> <div class="title-wrap">
...@@ -36,12 +38,13 @@ ...@@ -36,12 +38,13 @@
</div> </div>
</div> </div>
<div class="warp-content"> <div class="warp-content">
<el-form-item label="当前方案名称:" prop="name"> <el-form-item label="当前方案名称:" prop="schemeName">
<el-input v-model="schemeName" <el-input v-model="schemeName"
clearable clearable
maxlength="20" maxlength="20"
placeholder="请输入当前方案名称" placeholder="请输入当前方案名称"
style="width: 290px" /> style="width: 290px"
@blur="handleFieldBlur('schemeName')" />
</el-form-item> </el-form-item>
<ParamConfigList v-for="(parameter, index) in formData.paramConfigList" <ParamConfigList v-for="(parameter, index) in formData.paramConfigList"
...@@ -64,7 +67,8 @@ ...@@ -64,7 +67,8 @@
:picker-options="pickerOptions" :picker-options="pickerOptions"
placeholder="请选择" placeholder="请选择"
type="month" type="month"
value-format="yyyy-MM"> value-format="yyyy-MM"
@blur="handleFieldBlur('effectiveMonth')">
</el-date-picker> </el-date-picker>
</el-form-item> </el-form-item>
</div> </div>
...@@ -95,11 +99,10 @@ ...@@ -95,11 +99,10 @@
<script> <script>
import { groupBy } from "lodash-es"; import { groupBy } from "lodash-es";
import { mapGetters, mapMutations, mapState } from "vuex"; import { mapMutations, mapState } from "vuex";
import { getAllCategory } from "@/api/product.js"; import { getAllCategory } from "@/api/product.js";
import { import {
getConfigMerchantDetail, getConfigMerchantDetail,
getPerformanceConfigDetailAPI,
getTargetProductConfigListAPI, getTargetProductConfigListAPI,
postBrandMerchant, postBrandMerchant,
postSalaryPerformanceScheme, postSalaryPerformanceScheme,
...@@ -112,6 +115,7 @@ import { assessmentFactorOptions } from "../components/performanceRuleSelection/ ...@@ -112,6 +115,7 @@ import { assessmentFactorOptions } from "../components/performanceRuleSelection/
import { bindFormFields } from "../store/helper.js"; import { bindFormFields } from "../store/helper.js";
import { convertBackendToData } from "../utils/convertBackendToData.js"; import { convertBackendToData } from "../utils/convertBackendToData.js";
import { convertDataToBackend } from "../utils/index.js"; import { convertDataToBackend } from "../utils/index.js";
import { getFormRules, validateParamConfigList } from "../utils/rules.js";
// 门店提成方案 // 门店提成方案
export default { export default {
...@@ -129,6 +133,7 @@ export default { ...@@ -129,6 +133,7 @@ export default {
rules: {}, rules: {},
detailVisible: false, detailVisible: false,
detailVisibleId: undefined, detailVisibleId: undefined,
shouldValidateAll: false, // 是否应该验证所有字段
pickerOptions: { pickerOptions: {
disabledDate(time) { disabledDate(time) {
...@@ -161,6 +166,23 @@ export default { ...@@ -161,6 +166,23 @@ export default {
}, },
}, },
}, },
watch: {
formData: {
handler() {
// 当formData变化时,更新rules
this.rules = getFormRules(this.formData, this.merchantBrandRefList);
},
deep: true,
immediate: true,
},
merchantBrandRefList: {
handler() {
// 当门店列表变化时,更新rules
this.rules = getFormRules(this.formData, this.merchantBrandRefList);
},
deep: true,
},
},
async mounted() { async mounted() {
console.log("this.$route.query", this.$route.query); console.log("this.$route.query", this.$route.query);
if (this.$route.query?.id) { if (this.$route.query?.id) {
...@@ -205,7 +227,38 @@ export default { ...@@ -205,7 +227,38 @@ export default {
this.handleCancelSubmit(); this.handleCancelSubmit();
} }
}, },
async handleSubmitCheck(isConfirm = false) { // 处理单个字段失焦事件,只验证当前字段
handleFieldBlur(prop) {
if (!this.shouldValidateAll && this.$refs.form) {
this.$refs.form.validateField(prop, () => {});
}
},
// 提交审核
async handleSubmitCheck() {
// 表单校验
this.shouldValidateAll = true;
try {
await this.$refs.form.validate();
} catch (error) {
this.$message.warning("请完善表单信息");
this.shouldValidateAll = false;
return;
}
this.shouldValidateAll = false;
// 校验门店列表
if (!this.merchantBrandRefList || this.merchantBrandRefList.length === 0) {
this.$message.warning("请至少选择一个门店");
return;
}
// 校验参数配置列表
const paramConfigError = validateParamConfigList(this.formData);
if (paramConfigError) {
this.$message.warning(paramConfigError);
return;
}
const params = convertDataToBackend({ const params = convertDataToBackend({
...this.formData, ...this.formData,
}); });
...@@ -218,15 +271,9 @@ export default { ...@@ -218,15 +271,9 @@ export default {
params.merchantBrandRefList = this.merchantBrandRefList; params.merchantBrandRefList = this.merchantBrandRefList;
this.detailVisibleId = await postSalaryPerformanceScheme(params); this.detailVisibleId = await postSalaryPerformanceScheme(params);
if (this.detailVisibleId && isConfirm) { if (this.detailVisibleId) {
this.$message.success("保存成功 ");
this.detailVisible = !this.detailVisible; this.detailVisible = !this.detailVisible;
} }
// await postSalaryPerformanceScheme(convertDataToBackend({
// ...this.formData,
// merchantBrandRefList: this.merchantBrandRefList,
// }));
}, },
// 取消按钮 // 取消按钮
handleCancelSubmit() { handleCancelSubmit() {
......
  • @limin
    commented
    Owner

    你好,我是负责此次代码审查的资深软件开发工程师。

    这份代码主要涉及 Vue.js 中 Element UI 表单验证逻辑的重构。虽然引入了更细致的校验逻辑,但在数据绑定的一致性、性能优化以及框架特性的使用上存在明显问题。

    以下是本次审查的详细反馈:

    一、关键问题与优化建议(Top 3)

    1. 数据绑定与校验对象不一致(功能正确性 - 严重)

    • 问题描述:在 <el-form> 中绑定了 :model="formData",且 el-form-item 指定了 prop="schemeName"。然而,内部的输入框使用的是 v-model="schemeName"(直接绑定到组件根数据的 schemeName),而不是 formData.schemeName。
    • 影响分析:Element UI 的表单校验是基于 :model 指定的对象和 prop 指定的属性路径进行的。
      • 用户输入时更新的是 this.schemeName。
      • 表单校验时检查的是 this.formData.schemeName。
      • 这将导致校验永远针对旧值或空值进行,输入框的内容变化无法触发正确的校验状态,导致校验功能失效。
    • 优化建议:修改 v-model 绑定路径,使其与 form model 保持一致。

      <!-- 修改前 -->
      <el-input v-model="schemeName" ... />
      
      <!-- 修改后 -->
      <el-input v-model="formData.schemeName" ... />
      

    2. formData 深度监听导致性能隐患(性能与最佳实践 - 中等)

    • 问题描述:代码使用了 watch: { formData: { handler: ..., deep: true } } 来触发 getFormRules。这意味着表单中任何字段的任何一次击键都会触发一次完整的规则重新生成逻辑。
    • 影响分析:
      • 性能浪费:对于复杂的表单,频繁生成规则对象是昂贵的。
      • 潜在副作用:虽然设置了 :validate-on-rule-change="false",但频繁修改 rules 对象可能导致 Vue 内部的依赖追踪开销增大,并在某些 Element UI 版本中导致输入卡顿或焦点丢失。
    • 优化建议:
      • 如果规则是静态的,只在 mounted 生成一次。
      • 如果规则依赖特定字段(如“门店列表”变化),应仅监听该特定字段,而不是整个 formData。
      • 或者使用 computed 属性生成 rules,利用 Vue 的缓存机制。

    3. 手动接管 Blur 校验属于反模式(最佳实践 - 中等)

    • 问题描述:代码中手动添加了 @blur="handleFieldBlur('schemeName')" 并在方法中调用 validateField,同时维护了一个 shouldValidateAll 标志位。
    • 影响分析:这是对 Element UI 内置功能的“重新发明”。Element UI 的校验规则本身支持 trigger: 'blur' 配置。手动接管不仅增加了大量冗余代码(模板中的 @blur 和 script 中的处理函数),还降低了代码的可维护性。
    • 优化建议:
      • 移除模板中的 @blur 事件绑定。
      • 移除 handleFieldBlur 方法和 shouldValidateAll 变量。
      • 在 getFormRules 生成的规则对象中,确保相关规则包含 trigger: ['blur', 'change']。

    二、评分明细

    1. 功能实现的正确性与健壮性(25/40)
      • 扣分点:v-model 绑定对象与校验对象不一致是严重逻辑错误,直接导致校验失效。提交逻辑中移除了 isConfirm 判断,可能改变了原有业务逻辑(如区分保存草稿与提交)。
    2. 安全性与潜在风险(30/30)
      • 得分点:未发现明显的 XSS 或注入风险,增加了表单校验有助于数据完整性。
    3. 最佳实践与可维护性(12/20)
      • 扣分点:手动处理 blur 事件、深度监听整个表单对象生成规则,这些都属于不推荐的工程实践,增加了代码复杂度且收益不明。
    4. 性能与资源利用(3/5)
      • 扣分点:deep: true 监听大对象会带来不必要的计算开销。
    5. 提交信息质量(5/5)
      • 得分点:Commit message 清晰明了。

    三、总分

    总分:75分

Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!