Selaa lähdekoodia

fix: Critical 修复——isSwitchable 设置 + ProfileHeader 走新切换流程

iwt 2 viikkoa sitten
vanhempi
sitoutus
b43316c66c

+ 16 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/FamilyMemberService.java

@@ -276,6 +276,14 @@ public class FamilyMemberService {
             // 兼容旧前端字段
             vo.setMemberId(fm.getId());
             vo.setName(fm.getNickname());
+            // 设置 isSwitchable:仅无 openid 的不可登录成员可被切换
+            if (fm.getUserId() != null) {
+                User memberUser = userMapper.selectById(fm.getUserId());
+                String openid = memberUser != null ? memberUser.getOpenid() : null;
+                vo.setIsSwitchable(openid == null || openid.isEmpty());
+            } else {
+                vo.setIsSwitchable(true);
+            }
             result.add(vo);
         }
 
@@ -501,6 +509,14 @@ public class FamilyMemberService {
         if (member.getBirthday() != null) {
             vo.setAge(calculateAge(member.getBirthday()));
         }
+        // 设置 isSwitchable:仅无 openid 的不可登录成员可被切换
+        if (member.getUserId() != null) {
+            User memberUser = userMapper.selectById(member.getUserId());
+            String openid = memberUser != null ? memberUser.getOpenid() : null;
+            vo.setIsSwitchable(openid == null || openid.isEmpty());
+        } else {
+            vo.setIsSwitchable(true);
+        }
         return vo;
     }
 

+ 43 - 13
cfc-frontend/components/ProfileHeader.vue

@@ -19,7 +19,7 @@
 </template>
 
 <script>
-import { getChildren, verifyPassword } from '../utils/api.js'
+import { getChildren, verifyPassword, switchToFamilyMember } from '../utils/api.js'
 
 export default {
   name: 'ProfileHeader',
@@ -78,13 +78,13 @@ export default {
         this.doSwitchMode()
       }
     },
-    doSwitchMode() {
+    async doSwitchMode() {
       if (this.children.length === 0) {
         this.loadChildren()
         if (this.children.length === 0) {
           uni.showModal({
             title: '提示',
-            content: '您还没有添加孩子,请先在家庭成员中添加',
+            content: '您还没有添加成员,请先在家庭成员中添加',
             success: (res) => {
               if (res.confirm) {
                 uni.navigateTo({ url: '/pages/profile-extra/family-members' })
@@ -94,21 +94,51 @@ export default {
           return
         }
       }
-      if (this.children.length === 1) {
-        this.$store.commit('switchToChild', this.children[0].id)
-        this.role = 'child'
-        this.isSwitchedChild = true
-        uni.showToast({ title: '已切换为孩子模式', icon: 'success' })
+      // 优先显示可切换成员(不可登录成员),过滤掉可通过微信登录的成员
+      var switchable = this.children.filter(function(m) { return m.isSwitchable })
+      if (switchable.length === 0) {
+        uni.showToast({ title: '暂无可切换成员', icon: 'none' })
+        return
+      }
+      if (switchable.length === 1) {
+        var member = switchable[0]
+        uni.showModal({
+          title: '切换确认',
+          content: '切换到"' + member.nickname + '"后,退出时需要输入您的密码。是否继续?',
+          success: async (res) => {
+            if (!res.confirm) return
+            try {
+              var result = await switchToFamilyMember(member.id, member.source || 'family_member')
+              if (result.code === 200 && result.data) {
+                this.$store.commit('switchToMember', {
+                  memberId: result.data.memberId,
+                  effectiveRole: result.data.effectiveRole
+                })
+                this.isSwitchedChild = true
+                this.role = result.data.effectiveRole
+                uni.showToast({ title: '已切换', icon: 'success' })
+              } else {
+                uni.showToast({ title: result.message || '切换失败', icon: 'none' })
+              }
+            } catch (e) {
+              uni.showToast({ title: '切换失败', icon: 'none' })
+            }
+          }
+        })
         return
       }
       uni.showActionSheet({
-        itemList: this.children.map(c => c.nickname),
+        itemList: switchable.map(function(m) { return m.nickname }),
         success: (res) => {
-          const selectedChild = this.children[res.tapIndex]
-          this.$store.commit('switchToChild', selectedChild.id)
-          this.role = 'child'
+          if (res.tapIndex < 0) return
+          var member = switchable[res.tapIndex]
+          this.$store.commit('switchToMember', {
+            memberId: member.id,
+            effectiveRole: member.effectiveRole
+          })
           this.isSwitchedChild = true
-          uni.showToast({ title: `已切换为${selectedChild.nickname}模式`, icon: 'success' })
+          this.role = member.effectiveRole
+          uni.showToast({ title: '已切换', icon: 'success' })
         }
       })
     },