Sfoglia il codice sorgente

perf(商机管理): 使用防抖优化保存方法性能

liujt 8 mesi fa
parent
commit
93e6558a40

+ 75 - 62
src/views/saleManage/businessOpportunity/components/addOpportunityDialog.vue

@@ -227,6 +227,7 @@
   // import fileMain from '@/components/addDoc/index.vue';
   import processSubmitDialog from '@/BIZComponents/processSubmitDialog/processSubmitDialog.vue';
   import { contactDetail,updateRelationStatus } from '@/api/saleManage/contact';
+  import { debounce } from 'lodash';
   export default {
     props: {
       categoryTreeList: Array,
@@ -529,78 +530,90 @@
           })
         ]);
       },
-      async save(type) {
+      // 原始保存方法
+      _save(type) {
         try {
-          await this.getValidate();
-          // 表单验证通过,执行保存操作
           this.loading = true;
+          this.getValidate().then(() => {
+            // 表单验证通过,执行保存操作
+            if (!this.isUpdate) {
+              delete this.form.id;
+            }
+            let _sourceName = this.getDictValue('商机来源', this.form.sourceCode),
+              _stageName = this.getDictValue('商机阶段', this.form.stageCode);
+            this.form = Object.assign({}, this.form, {
+              responsibleId: this.userInfo.userId,
+              responsibleName: this.userInfo.name,
+              sourceName: _sourceName,
+              stageName: _stageName,
+              files: this.form.files || []
+            });
 
-          if (!this.isUpdate) {
-            delete this.form.id;
-          }
-          let _sourceName = this.getDictValue('商机来源', this.form.sourceCode),
-            _stageName = this.getDictValue('商机阶段', this.form.stageCode);
-          this.form = Object.assign({}, this.form, {
-            responsibleId: this.userInfo.userId,
-            responsibleName: this.userInfo.name,
-            sourceName: _sourceName,
-            stageName: _stageName,
-            files: this.form.files || []
-          });
-
-          if (this.$refs.inventoryTable.getTableValue().length == 0) {
-            this.$message.warning('物品清单不能为空');
-
-            return;
-          }
-          this.form.totalPrice= this.$refs.inventoryTable.getPrice()[0]
-          const commitData = {
-            opportunity: this.form,
-            competAnalysisList: this.$refs.businessAddTable.getTableValue(),
-            partyList: this.$refs.personnelAddTable.getTableValue(),
-            productList: this.$refs.inventoryTable.getTableValue()
-          };
+            if (this.$refs.inventoryTable.getTableValue().length == 0) {
+              this.$message.warning('物品清单不能为空');
+              this.loading = false;
+              return;
+            }
+            this.form.totalPrice= this.$refs.inventoryTable.getPrice()[0]
+            const commitData = {
+              opportunity: this.form,
+              competAnalysisList: this.$refs.businessAddTable.getTableValue(),
+              partyList: this.$refs.personnelAddTable.getTableValue(),
+              productList: this.$refs.inventoryTable.getTableValue()
+            };
 
-          if (this.isUpdate) {
-            UpdateInformation(commitData)
-              .then((res) => {
-                this.loading = false;
-                this.$message.success('修改成功');
-                updateRelationStatus([this.form.contactId])
+            if (this.isUpdate) {
+              UpdateInformation(commitData)
+                .then((res) => {
+                  this.loading = false;
+                  this.$message.success('修改成功');
+                  updateRelationStatus([this.form.contactId])
 
-                if (type === 'sub') {
-                  this.businessOpportunitySubmit(res);
-                  return;
-                }
-                this.cancel();
-                this.$emit('done');
-              })
-              .catch((e) => {
-                //this.loading = false;
-              });
-          } else {
-            addInformation(commitData)
-              .then((res) => {
-                this.loading = false;
-                this.$message.success('新增成功');
-                updateRelationStatus([this.form.contactId])
+                  if (type === 'sub') {
+                    this.businessOpportunitySubmit(res);
+                    return;
+                  }
+                  this.cancel();
+                  this.$emit('done');
+                })
+                .catch((e) => {
+                  this.loading = false;
+                  console.error(e);
+                });
+            } else {
+              addInformation(commitData)
+                .then((res) => {
+                  this.loading = false;
+                  this.$message.success('新增成功');
+                  updateRelationStatus([this.form.contactId])
 
-                if (type === 'sub') {
-                  this.businessOpportunitySubmit(res);
-                  return;
-                }
-                this.cancel();
-                this.$emit('done');
-              })
-              .catch((e) => {
-                //this.loading = false;
-              });
-          }
+                  if (type === 'sub') {
+                    this.businessOpportunitySubmit(res);
+                    return;
+                  }
+                  this.cancel();
+                  this.$emit('done');
+                })
+                .catch((e) => {
+                  this.loading = false;
+                  console.error(e);
+                });
+            }
+          }).catch(() => {
+            this.loading = false;
+            // 表单验证未通过,不执行保存操作
+          });
         } catch (error) {
           console.log(error);
-          // 表单验证未通过,不执行保存操作
+          this.loading = false;
+          // 异常处理
         }
       },
+      
+      // 使用 lodash 的防抖功能的保存方法
+      save: debounce(function(type) {
+        this._save(type);
+      }, 500, true),
       async businessOpportunitySubmit(res) {
         const data = await getDetail(this.businessId || res);
         this.processSubmitDialogFlag = true;