Kaynağa Gözat

fix: 切换孩子模式时检查孩子数量并引导添加

User 6 ay önce
ebeveyn
işleme
6d865ec50a
1 değiştirilmiş dosya ile 190 ekleme ve 24 silme
  1. 190 24
      zxyj-frontend/pages/profile/profile.vue

+ 190 - 24
zxyj-frontend/pages/profile/profile.vue

@@ -12,6 +12,12 @@
     </view>
 
 	<view class="menu-list">
+		<!-- 修改个人资料:所有角色可见 -->
+		<view class="menu-item" @click="goToEditInfo">
+			<text>📝 修改个人资料</text>
+			<text class="arrow">›</text>
+		</view>
+		<!-- 我的孩子:仅家长可见 -->
 		<view class="menu-item" v-if="role === 'parent'" @click="goToChildren">
 			<text>👶 我的孩子</text>
 			<text class="arrow">›</text>
@@ -20,8 +26,9 @@
 			<text>📊 积分记录</text>
 			<text class="arrow">›</text>
 		</view>
-		<view class="menu-item" @click="showSetPassword">
-			<text>🔐 设置密码</text>
+		<!-- 设置/重置密码:仅家长和指导师可见,孩子不可见 -->
+		<view class="menu-item" v-if="role !== 'child'" @click="showPasswordModal = true">
+			<text>🔐 {{ hasPassword ? '重置密码' : '设置密码' }}</text>
 			<text class="arrow">›</text>
 		</view>
 		<view class="menu-item" @click="logout">
@@ -30,40 +37,156 @@
 		</view>
 	</view>
 
-    <!-- 设置密码弹窗 -->
-    <view class="modal-mask" v-if="showPasswordModal" @click="showPasswordModal = false">
-      <view class="modal" @click.stop>
-        <view class="modal-title">设置密码</view>
-        <view class="form-item">
-          <input v-model="password" type="password" placeholder="输入4位密码" maxlength="4" />
-        </view>
-        <view class="modal-btns">
-          <button @click="showPasswordModal = false">取消</button>
-          <button class="btn-primary" @click="setPassword">确定</button>
-        </view>
-      </view>
-    </view>
+	<!-- 设置/重置密码弹窗 -->
+	<view class="modal-mask" v-if="showPasswordModal" @click="showPasswordModal = false">
+		<view class="modal" @click.stop>
+			<view class="modal-title">{{ hasPassword ? '重置密码' : '设置密码' }}</view>
+			<!-- 重置密码时需要先验证旧密码 -->
+			<view class="form-item" v-if="hasPassword">
+				<input v-model="oldPassword" type="password" placeholder="输入旧密码" maxlength="4" />
+			</view>
+			<view class="form-item">
+				<input v-model="newPassword" type="password" placeholder="输入新密码" maxlength="4" />
+			</view>
+			<view class="form-item">
+				<input v-model="confirmPassword" type="password" placeholder="确认新密码" maxlength="4" />
+			</view>
+			<view class="modal-btns">
+				<button @click="showPasswordModal = false">取消</button>
+				<button class="btn-primary" @click="savePassword">确定</button>
+			</view>
+</view>
   </view>
 </template>
 
 <script>
-import { setPassword as setPwdAPI } from '../../utils/api.js'
+import { setPassword, verifyPassword, getChildren } from '../../utils/api.js'
 
 export default {
   data() {
     return {
       nickname: '',
       role: 'parent',
+      hasPassword: false, // 是否已设置过密码
       showPasswordModal: false,
-      password: ''
+      oldPassword: '', // 旧密码(重置时用)
+      newPassword: '', // 新密码
+      confirmPassword: '', // 确认密码
+      children: [] // 孩子列表
     }
   },
   onShow() {
     this.nickname = this.$store.state.nickname
     this.role = this.$store.state.role
+    // 检查是否已设置密码(通过检查本地存储或调用API)
+    this.hasPassword = !!uni.getStorageSync('passwordSet')
+    // 加载孩子列表(用于切换模式时判断)
+    this.loadChildren()
+  },
+  methods: {
+    async loadChildren() {
+      try {
+        const res = await getChildren()
+        this.children = res.data || []
+      } catch (e) {
+        console.error('获取孩子列表失败', e)
+      }
+    },
+    switchMode() {
+      // 孩子切换到家长需要验证密码
+      if (this.role === 'child') {
+        this.verifyPasswordForSwitch(() => {
+          this.doSwitchMode()
+        })
+      } else {
+        this.doSwitchMode()
+      }
+    },
+    async doSwitchMode() {
+      const newRole = this.role === 'parent' ? 'child' : 'parent'
+      
+      // 家长切换到孩子模式时,检查是否有孩子
+      if (newRole === 'child') {
+        // 如果孩子列表为空,重新加载
+        if (this.children.length === 0) {
+          try {
+            const res = await getChildren()
+            this.children = res.data || []
+          } catch (e) {
+            uni.showToast({ title: '获取孩子信息失败', icon: 'none' })
+            return
+          }
+        }
+        
+        // 如果没有孩子,提示用户添加
+        if (this.children.length === 0) {
+          uni.showModal({
+            title: '提示',
+            content: '您还没有添加孩子,是否现在去添加?',
+            success: (res) => {
+              if (res.confirm) {
+                uni.navigateTo({ url: '/pages/profile/create-child' })
+              }
+            }
+          })
+          return
+        }
+        
+        // 如果只有一个孩子,直接切换
+        if (this.children.length === 1) {
+          this.$store.commit('switchRole', newRole)
+          this.role = newRole
+          uni.showToast({
+            title: '已切换为孩子模式',
+            icon: 'success'
+          })
+          return
+        }
+        
+        // 如果有多个孩子,让孩子选择
+        uni.showActionSheet({
+          itemList: this.children.map(c => c.nickname),
+          success: (res) => {
+            const selectedChild = this.children[res.tapIndex]
+            // 存储选中的孩子ID
+            uni.setStorageSync('currentChildId', selectedChild.id)
+            this.$store.commit('switchRole', newRole)
+            this.role = newRole
+            uni.showToast({
+              title: `已切换为${selectedChild.nickname}模式`,
+              icon: 'success'
+            })
+          }
+        })
+        return
+      }
+      
+      // 其他情况直接切换
+      this.$store.commit('switchRole', newRole)
+      this.role = newRole
+      uni.showToast({
+        title: `已切换为${newRole === 'parent' ? '家长' : '孩子'}模式`,
+        icon: 'success'
+      })
+    },
+  onShow() {
+    this.nickname = this.$store.state.nickname
+    this.role = this.$store.state.role
+    // 检查是否已设置密码(通过检查本地存储或调用API)
+    this.hasPassword = !!uni.getStorageSync('passwordSet')
   },
   methods: {
     switchMode() {
+      // 孩子切换到家长需要验证密码
+      if (this.role === 'child') {
+        this.verifyPasswordForSwitch(() => {
+          this.doSwitchMode()
+        })
+      } else {
+        this.doSwitchMode()
+      }
+    },
+    doSwitchMode() {
       const newRole = this.role === 'parent' ? 'child' : 'parent'
       this.$store.commit('switchRole', newRole)
       this.role = newRole
@@ -72,25 +195,68 @@ export default {
         icon: 'success'
       })
     },
+    // 验证密码(用于切换模式)
+    verifyPasswordForSwitch(callback) {
+      uni.showModal({
+        title: '验证密码',
+        content: '请输入家长密码',
+        editable: true,
+        success: async (res) => {
+          if (res.confirm && res.content) {
+            try {
+              const result = await verifyPassword(res.content)
+              if (result.code === 200) {
+                callback && callback()
+              } else {
+                uni.showToast({ title: '密码错误', icon: 'none' })
+              }
+            } catch (e) {
+              uni.showToast({ title: '验证失败', icon: 'none' })
+            }
+          }
+        }
+      })
+    },
+    goToEditInfo() {
+      uni.navigateTo({ url: '/pages/user-edit/user-edit' })
+    },
     goToChildren() {
       uni.navigateTo({ url: '/pages/profile/children' })
     },
     goToPointsLogs() {
       uni.navigateTo({ url: '/pages/points/points' })
     },
-    showSetPassword() {
-      this.showPasswordModal = true
-    },
-    async setPassword() {
-      if (this.password.length !== 4) {
+    async savePassword() {
+      if (this.newPassword.length !== 4) {
         uni.showToast({ title: '请输入4位密码', icon: 'none' })
         return
       }
+      if (this.newPassword !== this.confirmPassword) {
+        uni.showToast({ title: '两次密码不一致', icon: 'none' })
+        return
+      }
+      // 重置密码需要验证旧密码
+      if (this.hasPassword && this.oldPassword.length !== 4) {
+        uni.showToast({ title: '请输入旧密码', icon: 'none' })
+        return
+      }
       try {
-        await setPwdAPI(this.password)
+        if (this.hasPassword) {
+          // 验证旧密码
+          const verifyRes = await verifyPassword(this.oldPassword)
+          if (verifyRes.code !== 200) {
+            uni.showToast({ title: '旧密码错误', icon: 'none' })
+            return
+          }
+        }
+        await setPassword(this.newPassword)
+        uni.setStorageSync('passwordSet', true)
+        this.hasPassword = true
         uni.showToast({ title: '密码设置成功', icon: 'success' })
         this.showPasswordModal = false
-        this.password = ''
+        this.oldPassword = ''
+        this.newPassword = ''
+        this.confirmPassword = ''
       } catch (e) {
         console.error('设置密码失败', e)
       }