Pārlūkot izejas kodu

feat(user): 增加主管姓名解析功能,优化用户档案刷新逻辑

xieyong 1 mēnesi atpakaļ
vecāks
revīzija
b3da926629
4 mainītis faili ar 63 papildinājumiem un 6 dzēšanām
  1. 8 2
      src/api/user.js
  2. 19 3
      src/pages/apply/form.vue
  3. 30 1
      src/utils/auth.js
  4. 6 0
      src/utils/storage.js

+ 8 - 2
src/api/user.js

@@ -2,6 +2,7 @@ import {
   fetchUserDetail,
   getServerConfig,
   request,
+  resolveManagerName,
 } from "@/utils/auth";
 import { getCurrentUser, getLoginUser, saveCurrentUser } from "@/utils/storage";
 function isServerMode() {
@@ -12,7 +13,8 @@ function isServerMode() {
  * 修改档案成功后,重新拉一次当前用户的 profile 接口,
  * 把最新数据落进 storage 并返回。
  * - demo 模式:不发起请求,直接返回当前缓存的 user
- * - 服务器模式:调 /main/users/{userId}/profile(鉴权由 request() 自动处理)
+ * - 服务器模式:调 /main/users/{userId}/profile(鉴权由 request() 自动处理),
+ *   并额外解析 managerUserId → managerName 后再落 storage
  */
 export async function refetchMyProfile() {
   if (!isServerMode()) return getCurrentUser();
@@ -20,7 +22,11 @@ export async function refetchMyProfile() {
   const userId = login.userId || (getCurrentUser() || {}).id;
   if (!userId && userId !== 0) throw new Error("缺少用户ID,无法刷新档案");
   const vo = await fetchUserDetail(userId);
-  if (vo) saveCurrentUser(vo);
+  if (vo) {
+    // 把 managerUserId 对应的主管姓名回填到 vo.managerName,失败不阻塞
+    await resolveManagerName(vo);
+    saveCurrentUser(vo);
+  }
   return vo;
 }
 

+ 19 - 3
src/pages/apply/form.vue

@@ -126,8 +126,13 @@
       <view class="divider"></view>
       <view class="field"><text class="label">审批人</text>
         <view class="approver approver-auto">
-          <view class="avatar approve-avatar auto">·</view><text>系统自动匹配</text><text class="muted">(直属主管 · 由 BPM
-            引擎分配)</text>
+          <view class="avatar approve-avatar auto">{{ managerInitial }}</view>
+          <template v-if="currentUser.managerName">
+            <text>{{ currentUser.managerName }}</text>
+          </template>
+          <template v-else>
+            <text>系统自动匹配</text>
+          </template>
         </view>
       </view>
     </view>
@@ -197,6 +202,11 @@ const reasonPlaceholder = computed(() =>
       ? "请说明补卡原因(如:临时接待客户忘记打卡)"
       : "请简要说明申请原因",
 );
+// 审批人头像首字:服务器模式下展示主管姓名首字;演示模式无 managerName 兜底为 '·'
+const managerInitial = computed(() => {
+  const name = currentUser && currentUser.managerName ? String(currentUser.managerName) : "";
+  return name ? name.charAt(0) : "·";
+});
 const repairTypeLabel = computed(() =>
   form.repairType ? REPAIR_TYPE_LABEL[form.repairType] : "请选择",
 );
@@ -529,7 +539,9 @@ async function submit() {
 
   uni.showModal({
     title: "提交成功",
-    content: "申请已提交给宋慧敏审批,可在“我的申请”中查看进度。",
+    content: currentUser.managerName
+      ? `申请已提交给${currentUser.managerName}审批,可在“我的申请”中查看进度。`
+      : "申请已提交,可在“我的申请”中查看审批进度。",
     showCancel: false,
     success: () => uni.redirectTo({ url: "/pages/apply/list" }),
   });
@@ -651,6 +663,10 @@ function summary() {
   font-size: 26rpx;
 }
 
+.approver {
+  justify-content: flex-start;
+}
+
 .small-avatar,
 .approve-avatar {
   width: 52rpx;

+ 30 - 1
src/utils/auth.js

@@ -331,6 +331,30 @@ export async function fetchUserDetail(userId) {
   return profile;
 }
 
+// 拉取当前用户的主管档案并把 name 回填到 vo.managerName;
+// 失败静默:主管姓名缺失时不影响主流程(申请表单会兜底展示"系统自动匹配")
+// 复用 fetchUserDetail,与现有 resolvePostIds 串联模式保持一致
+export async function resolveManagerName(vo) {
+  if (!vo || typeof vo !== "object") return vo;
+  const raw = vo.user && typeof vo.user === "object" ? vo.user : vo;
+  const managerId = raw.managerUserId ?? vo.managerUserId;
+  if (managerId === undefined || managerId === null || managerId === "")
+    return vo;
+  try {
+    const managerVo = await fetchUserDetail(managerId);
+    if (managerVo) {
+      const name =
+        (managerVo.user && managerVo.user.name) ||
+        managerVo.name ||
+        "";
+      raw.managerName = String(name || "");
+    }
+  } catch (e) {
+    // 主管档案解析失败不阻塞登录 / 档案刷新主流程
+  }
+  return vo;
+}
+
 // 把 profile.postId("1111,2222")解析成岗位名称字符串,用 / 拼接多个岗位
 async function resolvePostIds(postIdString) {
   if (!postIdString) return "";
@@ -421,7 +445,12 @@ export async function performLogin({ account, password, rememberPassword }) {
     const merged = detailData
       ? { ...(basicData || {}), ...detailData }
       : basicData;
-    if (merged) saveCurrentUser(merged);
+    if (merged) {
+      // 解析出 managerUserId 对应的主管姓名,写回 currentUser.managerName
+      // 失败由 helper 内部吞掉,不影响登录流程
+      await resolveManagerName(merged);
+      saveCurrentUser(merged);
+    }
     if (detail.status === "rejected") {
       uni.showToast({
         title: detail.reason?.message || "员工档案加载失败",

+ 6 - 0
src/utils/storage.js

@@ -173,6 +173,12 @@ function mapCurrentUser(vo) {
     emergencyPhone: emergencyPhone,
     emergencyRelation: emergencyRelation,
     bankCardList,
+    // 直属上级(解析自 managerUserId → managerName,仅服务器模式有值)
+    managerName: vo.managerName || (vo.user && vo.user.managerName) || '',
+    managerUserId:
+      vo.managerUserId != null
+        ? vo.managerUserId
+        : (vo.user && vo.user.managerUserId) || '',
     raw: vo,
   }
 }