Ver código fonte

feat(company): 添加获取当前登录用户所属公司名称的功能(当basicInfoData中不存在时使用)

xieyong 4 dias atrás
pai
commit
5af2f7bb35

+ 14 - 0
src/utils/org-util.js

@@ -78,3 +78,17 @@ export async function getCurrentUserCompany(userInfo) {
   const list = await listOrganizations();
   return findCompanyNode(list, groupId);
 }
+
+/**
+ * 当前登录用户所属公司的名称
+ * @param {Object} userInfo 当前登录用户信息
+ * @returns {Promise<String>} 公司名称, 取不到时为空串
+ */
+export async function getCurrentUserCompanyName(userInfo) {
+  try {
+    const company = await getCurrentUserCompany(userInfo);
+    return company?.name || '';
+  } catch (e) {
+    return '';
+  }
+}

+ 25 - 5
src/views/inspectionReport/components/reportTemplateWrapper.vue

@@ -144,6 +144,7 @@
   import unqualifiedProduct from '@/views/inspectionReport/components/unqualifiedProduct.vue';
   import { getList } from '@/api/unacceptedProduct/index';
   import { getCode } from '@/api/login';
+  import { getCurrentUserCompanyName } from '@/utils/org-util';
   const defForm = {
     poList: [],
     unqualifiedProductsCode: '',
@@ -480,11 +481,30 @@
         return enterprisePage({
           pageNum: 1,
           size: 200
-        }).then((res) => {
-          if (res.list?.length > 0) {
-            this.$set(this.basicInfoData, 'groupName', res.list[0].name);
-          }
-        });
+        })
+          .then((res) => {
+            if (res.list?.length > 0) {
+              this.$set(this.basicInfoData, 'groupName', res.list[0].name);
+            }
+          })
+          .catch(() => {
+            // 企业列表取不到不影响报告
+          })
+          .then(() => {
+            // 报告抬头默认取当前登录用户所属公司, 取不到时沿用企业列表第一个
+            return this.setDefaultGroupName();
+          });
+      },
+      /* 报告抬头公司: 当前登录用户所属公司 */
+      async setDefaultGroupName() {
+        const userInfo = this.$store?.state?.user?.info;
+        if (!userInfo) {
+          return;
+        }
+        const companyName = await getCurrentUserCompanyName(userInfo);
+        if (companyName) {
+          this.$set(this.basicInfoData, 'groupName', companyName);
+        }
       },
 
       setLossNumber({

+ 37 - 2
src/views/inspectionReport/template/inspection_report3.vue

@@ -18,7 +18,11 @@
           margin-bottom: 5px;
           font-weight: bold;
         "
-        >{{ basicInfoData.groupName || '湖南特瑞精密医疗器器械有限公司' }}</div
+        >{{
+          basicInfoData.groupName ||
+          currentCompanyName ||
+          '湖南特瑞精密医疗器器械有限公司'
+        }}</div
       >
       <div style="text-align: center; font-size: 18px; margin-bottom: 5px"
         >不合格品通知及评审处置表</div
@@ -489,7 +493,11 @@
           margin-bottom: 5px;
           font-weight: bold;
         "
-        >{{ basicInfoData.groupName || '湖南特瑞精密医疗器器械有限公司' }}</div
+        >{{
+          basicInfoData.groupName ||
+          currentCompanyName ||
+          '湖南特瑞精密医疗器器械有限公司'
+        }}</div
       >
       <div style="text-align: center; font-size: 18px; margin-bottom: 5px"
         >不合格品通知及评审处置表</div
@@ -1106,6 +1114,7 @@
 <script>
   // import imageUpload from '@/components/upload/imageUpload.vue';
   import { getFmImagePath } from '@/utils/file';
+  import { getCurrentUserCompanyName } from '@/utils/org-util';
 
   export default {
     name: 'QualityReport',
@@ -1121,7 +1130,11 @@
     data() {
       return {
         getFmImagePath,
+        // 当前登录用户所属公司名称(groupName 为空时的默认值)
+        currentCompanyName: '',
         basicInfoData: {
+          // 公司名称(为空时默认取当前登录用户所属公司)
+          groupName: '',
           // 表单编号、版本(两处共用)
           reportNumber: '',
           version: '',
@@ -1204,6 +1217,10 @@
         }
       };
     },
+    created() {
+      // 提前获取当前登录用户所属公司, 作为报告抬头的默认值
+      this.setDefaultCompanyName();
+    },
     methods: {
       /* 打开质检报告 */
       async init(basicInfoData) {
@@ -1214,6 +1231,24 @@
             ...basicInfoData
           });
         }
+        // groupName 为空时默认取当前登录用户所属公司
+        if (!this.basicInfoData.groupName) {
+          await this.setDefaultCompanyName();
+        }
+      },
+      /* 默认公司名称: 当前登录用户所属公司 */
+      async setDefaultCompanyName() {
+        if (!this.currentCompanyName) {
+          const userInfo = this.$store?.state?.user?.info;
+          if (!userInfo) {
+            return '';
+          }
+          this.currentCompanyName = await getCurrentUserCompanyName(userInfo);
+        }
+        if (this.currentCompanyName && !this.basicInfoData.groupName) {
+          this.$set(this.basicInfoData, 'groupName', this.currentCompanyName);
+        }
+        return this.currentCompanyName;
       },
       /* 保存编辑 */
       async getValue() {